-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTestFile.cpp
More file actions
95 lines (76 loc) · 2.57 KB
/
TestFile.cpp
File metadata and controls
95 lines (76 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "tests/TestFile.h"
#include "tests/TestParser.h"
static uint64_t nextId = 0;
namespace {
std::string stripFileExtension(const std::string& str) {
std::size_t lastIdx = str.find_last_of(".");
return str.substr(0, lastIdx);
}
} // anonymous namespace
namespace tester {
// Initialize the static id to zero
std::atomic<uint64_t> TestFile::nextId(0);
// An atoimc
uint64_t TestFile::generateId() {
return nextId.fetch_add(1, std::memory_order_relaxed);
}
TestFile::TestFile(const fs::path& path, const fs::path& artifactDir)
: id(generateId()), testPath(path) {
try {
// Create .test-artifacts if it doesn't exist
if (!fs::exists(artifactDir)) {
fs::create_directories(artifactDir);
}
// create .test-artifacts/testfiles if it doesn't exist
fs::path testArtifactsDir = artifactDir / "testfiles";
if (!fs::exists(testArtifactsDir)) {
fs::create_directories(testArtifactsDir);
}
std::string testName = path.stem();
fs::path basePath = testArtifactsDir / fs::path(testName + '-' + std::to_string(id));
setInsPath(fs::path(basePath.string() + ".ins"));
setOutPath(fs::path(basePath.string() + ".out"));
// std::cout << "Creating file: " << testName << std::endl;
// std::cout << "INS: " << getInsPath() << std::endl;
// std::cout << "OUT: " << getOutPath() << std::endl;
} catch (const fs::filesystem_error& e) {
throw std::runtime_error("Filesystem error: " + std::string(e.what()));
}
}
TestFile::~TestFile() {
// std::cout << "Calling Destructor...\n";
try {
if (fs::exists(insPath)) {
// Remove temporary input stream file
fs::remove(insPath);
}
if (fs::exists(outPath)) {
// Remove the tenmporary testfile directory and the expected out
fs::remove(outPath);
}
} catch (const std::exception& e) {
std::cerr << "Caught exception in destructor: "<< e.what() << std::endl;
}
}
std::string TestFile::getParseErrorMsg() const {
switch (getParseError()) {
case ParseError::NoError:
return "No error";
break;
case ParseError::DirectiveConflict:
return "Two or more testfile directives supplied that can not "
"coexist in one file.";
break;
case ParseError::FileError:
return "A filepath provided in the testfile was unable to be "
"located or opened.";
break;
case ParseError::RuntimeError:
return "An unexpected runtime error occured while parsing the "
"testifle.";
break;
default:
return "No matching Parse Error";
}
}
} // namespace tester