Skip to content

Commit 1d7e7f8

Browse files
authored
Ensure test cleanup via RAII (#523)
- add a `ScopedFile` RAII helper for test files - remove any temporary files in `task_tests.cpp`
1 parent bb5c761 commit 1d7e7f8

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

modules/task/tests/task_tests.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
#include <cstddef>
55
#include <cstdint>
66
#include <exception>
7+
#include <filesystem>
78
#include <fstream>
89
#include <memory>
910
#include <stdexcept>
11+
#include <string>
12+
#include <system_error>
1013
#include <thread>
14+
#include <utility>
1115
#include <vector>
1216

1317
#include "runners/include/runners.hpp"
@@ -16,6 +20,18 @@
1620

1721
using namespace ppc::task;
1822

23+
class ScopedFile {
24+
public:
25+
explicit ScopedFile(std::string path) : path_(std::move(path)) {}
26+
~ScopedFile() {
27+
std::error_code ec;
28+
std::filesystem::remove(path_, ec);
29+
}
30+
31+
private:
32+
std::string path_;
33+
};
34+
1935
namespace ppc::test {
2036

2137
template <typename InType, typename OutType>
@@ -132,6 +148,7 @@ TEST(TaskTest, GetStringTaskType_InvalidFileThrows) {
132148

133149
TEST(TaskTest, GetStringTaskType_UnknownType_WithValidFile) {
134150
std::string path = "settings_valid.json";
151+
ScopedFile cleaner(path);
135152
std::ofstream file(path);
136153
file
137154
<< R"({"tasks": {"all": "enabled", "stl": "enabled", "omp": "enabled", "mpi": "enabled", "tbb": "enabled", "seq": "enabled"}})";
@@ -141,6 +158,7 @@ TEST(TaskTest, GetStringTaskType_UnknownType_WithValidFile) {
141158

142159
TEST(TaskTest, GetStringTaskType_ThrowsOnBadJSON) {
143160
std::string path = "bad_settings.json";
161+
ScopedFile cleaner(path);
144162
std::ofstream file(path);
145163
file << "{";
146164
file.close();
@@ -149,6 +167,7 @@ TEST(TaskTest, GetStringTaskType_ThrowsOnBadJSON) {
149167

150168
TEST(TaskTest, GetStringTaskType_EachType_WithValidFile) {
151169
std::string path = "settings_valid_all.json";
170+
ScopedFile cleaner(path);
152171
std::ofstream file(path);
153172
file
154173
<< R"({"tasks": {"all": "enabled", "stl": "enabled", "omp": "enabled", "mpi": "enabled", "tbb": "enabled", "seq": "enabled"}})";
@@ -164,6 +183,7 @@ TEST(TaskTest, GetStringTaskType_EachType_WithValidFile) {
164183

165184
TEST(TaskTest, GetStringTaskType_ReturnsUnknown_OnDefault) {
166185
std::string path = "settings_valid_unknown.json";
186+
ScopedFile cleaner(path);
167187
std::ofstream file(path);
168188
file << R"({"tasks": {"all": "enabled"}})";
169189
file.close();
@@ -174,6 +194,7 @@ TEST(TaskTest, GetStringTaskType_ReturnsUnknown_OnDefault) {
174194

175195
TEST(TaskTest, GetStringTaskType_ThrowsIfKeyMissing) {
176196
std::string path = "settings_partial.json";
197+
ScopedFile cleaner(path);
177198
std::ofstream file(path);
178199
file << R"({"tasks": {"all": "enabled"}})";
179200
file.close();

0 commit comments

Comments
 (0)