Skip to content

Commit 695f845

Browse files
Copilotmortenvpjaxels20
authored
Fix: Only create mismatch directory when files are actually written (#5)
* Initial plan * Fix: Only create mismatch directory when files are actually written Co-authored-by: mortenvp <1279584+mortenvp@users.noreply.github.com> * ran --lock-versions * clang-format * update newst.rst --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: mortenvp <1279584+mortenvp@users.noreply.github.com> Co-authored-by: Jeppe Axelsen <jeppebergaxelsen@gmail.com>
1 parent 246b07b commit 695f845

5 files changed

Lines changed: 164 additions & 9 deletions

File tree

NEWS.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ every change, see the Git log.
66

77
Latest
88
------
9-
* tbd
9+
* Patch: Stopped the creation of mismatch dir when no files will be written to it.
1010

1111
1.0.1
1212
-----

lock_version_resolve.json

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
{
2+
"abacus": {
3+
"commit_id": "964e2035583b77d8bdb4eb30f1e95db0ba2577a4",
4+
"resolver_info": "8.0.0",
5+
"sha1": "81811cbb1887f87ef86cd7b1022280663b55c8a9"
6+
},
7+
"abseil": {
8+
"commit_id": "d6a9562e974b409cbc8d1e44b40f713a80ff7d6c",
9+
"resolver_info": "1.0.2",
10+
"sha1": "fbbadb116d16047bce4ad8e1726804a01eb8e98a"
11+
},
12+
"abseil-source": {
13+
"commit_id": "bc257a88f7c1939f24e0379f14a3589e926c950c",
14+
"resolver_info": "20250512.0",
15+
"sha1": "8e016b57dd5361fe7f66ae47f3df42ab634321c5"
16+
},
17+
"bourne": {
18+
"commit_id": "cf4fed3ea15525a6608722db56f10ff4314c2354",
19+
"resolver_info": "11.0.0",
20+
"sha1": "f705097630cc09ecd747eaf3b5eef8966cfa4fba"
21+
},
22+
"endian": {
23+
"commit_id": "b3702d8d134a06b5829119934718a75f4e171699",
24+
"resolver_info": "14.0.0",
25+
"sha1": "ebfaeeec96922b484b061fd860c46ee1264eca21"
26+
},
27+
"expected": {
28+
"commit_id": "e84368a61a223dd0fb3ad60d6e6791a3ffc199f7",
29+
"resolver_info": "2.0.0",
30+
"sha1": "2760f759e9baeaace1f61449d61c8197d1ffb77d"
31+
},
32+
"expected-source": {
33+
"commit_id": "292eff8bd8ee230a7df1d6a1c00c4ea0eb2f0362",
34+
"resolver_info": "v1.1.0",
35+
"sha1": "5078750ed20c6d6323d27ed96fb2ddebc43bb247"
36+
},
37+
"fmt": {
38+
"commit_id": "5d5eb0f166979fa523704114620a40b3e9e6bb08",
39+
"resolver_info": "5.0.0",
40+
"sha1": "160978c7e6571cc06d6c8a590e5e0c4f3c826fba"
41+
},
42+
"fmt-source": {
43+
"commit_id": "0c9fce2ffefecfdce794e1859584e25877b7b592",
44+
"resolver_info": "11.0.2",
45+
"sha1": "f39ff778b3283f543cdfc9b1662ec101c453efce"
46+
},
47+
"gtest": {
48+
"commit_id": "8e1f80ae8618fdba4389f1b56c811ea7241569d1",
49+
"resolver_info": "6.0.2",
50+
"sha1": "44c35f8798a738692c902fe2b67dc0cb76a1df53"
51+
},
52+
"gtest-source": {
53+
"commit_id": "52eb8108c5bdec04579160ae17225d66034bd723",
54+
"resolver_info": "v1.17.0",
55+
"sha1": "8600eafa3daedc694d3115677da0dda3af6a8633"
56+
},
57+
"libassert-source": {
58+
"commit_id": "60667829264ae01241257df7f1c7d6dba668eb9d",
59+
"resolver_info": "v2.1.5",
60+
"sha1": "650d881b8cb6cc1adf354d47d6353db6ec4fd6a5"
61+
},
62+
"platform": {
63+
"commit_id": "7740600dfa6c8800819737dd85fb89c16c1c0c01",
64+
"resolver_info": "6.0.0",
65+
"sha1": "7a014ba5a95cf7f0193213e95d3d2d8fcfa313b7"
66+
},
67+
"poke": {
68+
"commit_id": "97669413ea61728f6c0971558e66ae7d383ba85a",
69+
"resolver_info": "16.0.2",
70+
"sha1": "779b0b9b6ef137a2e7672d88c582136ee28c0610"
71+
},
72+
"protobuf": {
73+
"commit_id": "a32b1956282c0e89a8e24cecfcb1e46606071750",
74+
"resolver_info": "3.0.0",
75+
"sha1": "4423b2b25fe48344a027237e34317898651b99cf"
76+
},
77+
"protobuf-source": {
78+
"commit_id": "3d4adad5c4c4e6a6f9f038769b8c90716065b0e4",
79+
"resolver_info": "v31.0",
80+
"sha1": "93bde6d21d12c24893dc159f0a51bbb717118abb"
81+
},
82+
"source_location": {
83+
"commit_id": "c2dbc1e904f3900e881c98e0569d23e044a48246",
84+
"resolver_info": "2.0.0",
85+
"sha1": "1a08be8297afc59e088f710950b95ec073f032df"
86+
},
87+
"source_location-source": {
88+
"commit_id": "e3849453a302b52305a146eaa3809bd797679233",
89+
"resolver_info": "e384945",
90+
"sha1": "67edfd49ad879868b7d6a48b4e72d7f0cd969b46"
91+
},
92+
"toolchains": {
93+
"commit_id": "d771e98c84304735f43db13d50edffd8e7d5a9d6",
94+
"resolver_info": "1.0.5",
95+
"sha1": "443cb23b0570dffd39283d8679ec393ed809302f"
96+
},
97+
"verify": {
98+
"commit_id": "7f7c4b0d66d6d0036aa84ccab16fae2b1c8c207b",
99+
"resolver_info": "6.0.2",
100+
"sha1": "fed379602b6ea7bd611fc249daffa11b9e593800"
101+
},
102+
"zlib-source": {
103+
"commit_id": "51b7f2abdade71cd9bb0e7a373ef2610ec6f9daf",
104+
"resolver_info": "51b7f2abdade71cd9bb0e7a373ef2610ec6f9daf",
105+
"sha1": "33283130debb8bf95a98929ff6cadd5bd9cb6a92"
106+
}
107+
}

src/datarecorder/datarecorder.hpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ class datarecorder
266266
}
267267
}
268268

269-
auto determine_mismatch_dir() -> std::filesystem::path
269+
auto determine_mismatch_path() -> std::filesystem::path
270270
{
271271
VERIFY(m_recording_dir, "Recording dir must not be empty");
272272

@@ -282,17 +282,22 @@ class datarecorder
282282
mismatch_dir = tmp_dir / ("cppmismatch-" + std::to_string(i));
283283
}
284284

285-
// Create the directory
286-
std::error_code ec;
287-
bool created = std::filesystem::create_directory(mismatch_dir, ec);
288-
289-
VERIFY(created, "Could not create directory", ec);
290-
285+
// Return the path without creating the directory
291286
return mismatch_dir;
292287
}
293288

294289
void write_data(const std::filesystem::path& path, const std::string& data)
295290
{
291+
// Create parent directories if they don't exist
292+
std::filesystem::path parent_dir = path.parent_path();
293+
if (!parent_dir.empty() && !std::filesystem::exists(parent_dir))
294+
{
295+
std::error_code ec;
296+
bool created = std::filesystem::create_directories(parent_dir, ec);
297+
VERIFY(created || std::filesystem::exists(parent_dir),
298+
"Could not create parent directories", ec, parent_dir);
299+
}
300+
296301
std::ofstream file(path, std::ios::out | std::ios::trunc);
297302
VERIFY(file.is_open(), "Could not open file for writing", errno, path);
298303

@@ -326,7 +331,7 @@ class datarecorder
326331
if (data != recording_data)
327332
{
328333
// If it exists we check for a mismatch
329-
std::filesystem::path mismatch_dir = determine_mismatch_dir();
334+
std::filesystem::path mismatch_dir = determine_mismatch_path();
330335

331336
m_monitor.log(poke::log_level::debug,
332337
poke::log::str{"message", "Mismatch found"});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test data for directory creation

test/src/test_datarecorder.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,45 @@ TEST(datarecorder, record_string)
2424
auto mismatch_result = recorder.record(data);
2525
EXPECT_FALSE(mismatch_result);
2626
}
27+
28+
TEST(datarecorder, mismatch_directory_only_created_when_needed)
29+
{
30+
// Test that mismatch directories are not created unnecessarily
31+
datarecorder::datarecorder recorder;
32+
recorder.set_recording_dir("test/recordings");
33+
34+
// Record initial data
35+
std::string data = "test data for directory creation";
36+
auto result = recorder.record(data);
37+
EXPECT_TRUE(result);
38+
39+
// Count existing mismatch directories before test
40+
std::filesystem::path tmp_dir = std::filesystem::temp_directory_path();
41+
int initial_count = 0;
42+
for (const auto& entry : std::filesystem::directory_iterator(tmp_dir))
43+
{
44+
if (entry.is_directory() &&
45+
entry.path().filename().string().find("cppmismatch-") == 0)
46+
{
47+
initial_count++;
48+
}
49+
}
50+
51+
// Record the same data again (no mismatch, no directory should be created)
52+
result = recorder.record(data);
53+
EXPECT_TRUE(result);
54+
55+
// Count directories after test
56+
int final_count = 0;
57+
for (const auto& entry : std::filesystem::directory_iterator(tmp_dir))
58+
{
59+
if (entry.is_directory() &&
60+
entry.path().filename().string().find("cppmismatch-") == 0)
61+
{
62+
final_count++;
63+
}
64+
}
65+
66+
// No new directories should have been created
67+
EXPECT_EQ(initial_count, final_count);
68+
}

0 commit comments

Comments
 (0)