Repository navigation
Expand file tree
/
Copy pathtemp_file_test.h
More file actions
112 lines (96 loc) · 3.06 KB
/
Copy pathtemp_file_test.h
File metadata and controls
112 lines (96 loc) · 3.06 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#pragma once
#include <catch2/catch.hpp>
#include <fmt/core.h>
#include <filesystem>
#include <fstream>
#include <unordered_set>
#ifndef _WIN32
#include <stdlib.h> // for mkdtemp on linux
#include <unistd.h> // for mkdtemp on macOS
#endif
namespace fs = std::filesystem;
struct TempFileTest {
public:
fs::path current_path;
fs::path tmp_path;
std::string tmp_path_str;
std::unordered_set<std::string> all_files_created;
std::unordered_set<std::string> all_dirs_created;
template<typename F>
void create_files(std::string_view file_def, F&& file_visitor) {
std::ofstream fout;
auto starts_with = [](std::string_view a, std::string_view b) {
return (a.substr(0, b.size()) == b);
};
while (!file_def.empty()) {
std::string_view line = file_def.substr(0, file_def.find_first_of("\n\r"));
//if (line.starts_with(">")) { // todo: this works in C++20 but this still needs to build with C++17
if(starts_with(line, ">")) {
if (fout) fout.close();
std::string_view file = line.substr(2); // todo: allow arbitrary whitespace after >
fout.open(tmp_path / file);
file_visitor((std::string)file);
all_files_created.insert((std::string)file);
} else if (fout.is_open()) {
fout << line << "\n";
}
if (file_def.size() == line.size())
break;
file_def.remove_prefix(line.size() + 1); // UB if file_def doesn't have at least size + 1 elems
};
}
void create_files(std::string_view file_def) {
create_files(file_def, [](const std::string&){});
}
fs::path create_dir(fs::path name) {
auto path = tmp_path / name;
fs::create_directories(path);
// we only need to add the first component to the set of dirs created
// because that whole subtree will be removed in the destructor
for (auto& component : name) {
all_dirs_created.insert(component.string());
break;
}
return path;
}
void touch(std::string_view file_name) {
auto path = tmp_path / file_name;
if (fs::exists(path)) {
fs::last_write_time(path, fs::file_time_type::clock::now());
} else {
std::ofstream { path };
all_files_created.insert((std::string)file_name);
}
}
void remove(std::string_view file_name) {
auto path = tmp_path / file_name;
fs::remove(path);
all_files_created.erase((std::string)file_name);
}
public:
TempFileTest() {
current_path = fs::current_path();
// note: std::tmpnam has a deprecation warning
#ifdef _WIN32
char tmp_dir_name[256];
REQUIRE(tmpnam_s(tmp_dir_name) == 0);
tmp_path = fs::temp_directory_path() / "cppm" / fs::path { tmp_dir_name }.filename();
REQUIRE(fs::create_directories(tmp_path));
#else
char tmp_dir_name[256] = "/tmp/cppm_XXXXXX";
REQUIRE(nullptr != mkdtemp(tmp_dir_name));
tmp_path = fs::path { tmp_dir_name };
#endif
tmp_path = std::filesystem::canonical(tmp_path);
tmp_path_str = tmp_path.string();
}
~TempFileTest() {
// note: can't remove tmp_path if something chdir'd into it
fs::current_path(current_path);
for (auto& file : all_files_created)
fs::remove(tmp_path / file);
for (auto& dir : all_dirs_created)
fs::remove_all(tmp_path / dir);
fs::remove(tmp_path);
}
};