stc
Loading...
Searching...
No Matches
TestDirectory.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <filesystem>
4
5namespace stc::testutil {
6
20 std::filesystem::path folder;
21 bool dummyMode = false;
22
23 [[nodiscard("Discarding this object will immediately delete the folder")]]
25 const std::filesystem::path& p,
26 bool create = false
27 ) : folder(p) {
30
31 if (create) {
32 std::filesystem::create_directories(p);
33 }
34 }
35
36 [[nodiscard("Discarding this object will immediately delete the folder")]]
38 const std::filesystem::path& p,
39 bool create,
40 bool dummyMode
41 ) : folder(p), dummyMode(dummyMode) {
44
45 if (create) {
46 std::filesystem::create_directories(p);
47 }
48 }
49
53
54 void checkCompatiblePath(const std::filesystem::path& p) {
55 if (std::filesystem::exists(p) && (
56 !std::filesystem::is_directory(p)
57 || std::filesystem::is_symlink(p)
58 )) {
59 throw std::runtime_error(
60 "The path you supplied to TestDirectory exists, but is not a directory or is a symlink. "
61 "This utility is only compatible with standard directories. "
62 "If you were trying to take ownership of a file, use TestFile instead."
63 );
64 } else if (
65 p.string().starts_with("/usr")
66 || p.string().starts_with("/bin")
67 || p.string().starts_with("/share")
68 || p.string().starts_with("/dev")
69 || p.string().starts_with("/etc")
70 || p.string().starts_with("/proc")
71 || p.string().starts_with("/boot")
72 || std::filesystem::weakly_canonical(p) == std::filesystem::canonical("/")
73 ) {
74 throw std::runtime_error(
75 "You fucked up"
76 );
77 }
78 }
79
80
81 void deleteFolder() {
82 if (dummyMode) {
83 return;
84 }
85 if (std::filesystem::exists(this->folder)) {
86 std::filesystem::remove_all(this->folder);
87 }
88 }
89};
90
91}
Definition CaptureStream.hpp:6
Definition TestDirectory.hpp:19
bool dummyMode
Definition TestDirectory.hpp:21
void deleteFolder()
Definition TestDirectory.hpp:81
TestDirectory(const std::filesystem::path &p, bool create, bool dummyMode)
Definition TestDirectory.hpp:37
TestDirectory(const std::filesystem::path &p, bool create=false)
Definition TestDirectory.hpp:24
~TestDirectory()
Definition TestDirectory.hpp:50
std::filesystem::path folder
Definition TestDirectory.hpp:20
void checkCompatiblePath(const std::filesystem::path &p)
Definition TestDirectory.hpp:54