Skip to content

Commit 85ae890

Browse files
committed
Ensure tmp files get created and deleted
1 parent aa2829e commit 85ae890

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

src/tests/TestFile.cpp

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,48 @@ uint64_t TestFile::nextId = 0;
1717
TestFile::TestFile(const fs::path& path, const fs::path& tmpPath)
1818
: testPath(path) {
1919

20-
setInsPath(tmpPath / std::to_string(nextId) / "test.ins");
21-
setOutPath(tmpPath / std::to_string(nextId) / "test.ins");
22-
23-
std::cout << "Use tmp path: " << tmpPath << std::endl;
24-
std::cout << "Out path: " << outPath << std::endl;
25-
std::cout << "In path: " << insPath << std::endl;
20+
fs::path testDir = tmpPath / std::to_string(nextId);
21+
setInsPath(testDir / "test.ins");
22+
setOutPath(testDir / "test.out");
2623

24+
try {
25+
// Create tmp directory if it doesn't exist
26+
std::cout << "Attempting to create directory: " << testDir << std::endl;
27+
if (!fs::exists(testDir)) {
28+
if (!fs::create_directories(testDir)) {
29+
throw std::runtime_error("Failed to create directory: " + testDir.string());
30+
}
31+
}
32+
// Create the temporary input and ouput files
33+
std::ofstream createInsFile(insPath);
34+
std::ofstream createOutFile(outPath);
35+
if (!createInsFile) {
36+
throw std::runtime_error("Failed to create input file: " + insPath.string());
37+
}
38+
if (!createOutFile) {
39+
throw std::runtime_error("Failed to create output file: " + outPath.string());
40+
}
41+
createInsFile.close();
42+
createOutFile.close();
43+
44+
} catch (const fs::filesystem_error& e) {
45+
throw std::runtime_error("Filesystem error: " + std::string(e.what()));
46+
} catch (const std::exception& e) {
47+
throw std::runtime_error("Error in TestFile constructor: " + std::string(e.what()));
48+
}
2749
nextId++;
2850
}
2951

3052
TestFile::~TestFile() {
3153
if (fs::exists(insPath)) {
54+
// Remove temporary input stream file
3255
fs::remove(insPath);
3356
}
3457
if (fs::exists(outPath)) {
58+
// Remove the tenmporary testfile directory and the expected out
59+
fs::path testfileDir = outPath.parent_path();
3560
fs::remove(outPath);
61+
fs::remove(testfileDir);
3662
}
3763
}
3864

0 commit comments

Comments
 (0)