Skip to content

Commit 6c0d64c

Browse files
committed
test: cleanup temporary files
1 parent 8b3e8fb commit 6c0d64c

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

modules/task/tests/task_tests.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
#include <cstddef>
55
#include <cstdint>
66
#include <exception>
7+
#include <filesystem>
78
#include <fstream>
89
#include <memory>
910
#include <stdexcept>
11+
#include <system_error>
1012
#include <thread>
1113
#include <vector>
1214

@@ -16,6 +18,18 @@
1618

1719
using namespace ppc::task;
1820

21+
class ScopedFile {
22+
public:
23+
explicit ScopedFile(const std::string& path) : path_(path) {}
24+
~ScopedFile() {
25+
std::error_code ec;
26+
std::filesystem::remove(path_, ec);
27+
}
28+
29+
private:
30+
std::string path_;
31+
};
32+
1933
namespace ppc::test {
2034

2135
template <typename InType, typename OutType>
@@ -132,6 +146,7 @@ TEST(TaskTest, GetStringTaskType_InvalidFileThrows) {
132146

133147
TEST(TaskTest, GetStringTaskType_UnknownType_WithValidFile) {
134148
std::string path = "settings_valid.json";
149+
ScopedFile cleaner(path);
135150
std::ofstream file(path);
136151
file
137152
<< R"({"tasks": {"all": "enabled", "stl": "enabled", "omp": "enabled", "mpi": "enabled", "tbb": "enabled", "seq": "enabled"}})";
@@ -141,6 +156,7 @@ TEST(TaskTest, GetStringTaskType_UnknownType_WithValidFile) {
141156

142157
TEST(TaskTest, GetStringTaskType_ThrowsOnBadJSON) {
143158
std::string path = "bad_settings.json";
159+
ScopedFile cleaner(path);
144160
std::ofstream file(path);
145161
file << "{";
146162
file.close();
@@ -149,6 +165,7 @@ TEST(TaskTest, GetStringTaskType_ThrowsOnBadJSON) {
149165

150166
TEST(TaskTest, GetStringTaskType_EachType_WithValidFile) {
151167
std::string path = "settings_valid_all.json";
168+
ScopedFile cleaner(path);
152169
std::ofstream file(path);
153170
file
154171
<< R"({"tasks": {"all": "enabled", "stl": "enabled", "omp": "enabled", "mpi": "enabled", "tbb": "enabled", "seq": "enabled"}})";
@@ -164,6 +181,7 @@ TEST(TaskTest, GetStringTaskType_EachType_WithValidFile) {
164181

165182
TEST(TaskTest, GetStringTaskType_ReturnsUnknown_OnDefault) {
166183
std::string path = "settings_valid_unknown.json";
184+
ScopedFile cleaner(path);
167185
std::ofstream file(path);
168186
file << R"({"tasks": {"all": "enabled"}})";
169187
file.close();
@@ -174,6 +192,7 @@ TEST(TaskTest, GetStringTaskType_ReturnsUnknown_OnDefault) {
174192

175193
TEST(TaskTest, GetStringTaskType_ThrowsIfKeyMissing) {
176194
std::string path = "settings_partial.json";
195+
ScopedFile cleaner(path);
177196
std::ofstream file(path);
178197
file << R"({"tasks": {"all": "enabled"}})";
179198
file.close();

0 commit comments

Comments
 (0)