xrpld
Loading...
Searching...
No Matches
FileDirGuard.h
1#pragma once
2
3#include <xrpl/basics/contract.h>
4#include <xrpl/beast/unit_test/suite.h>
5
6#include <exception>
7#include <filesystem>
8#include <fstream>
9#include <ostream>
10#include <stdexcept>
11#include <string>
12#include <utility>
13
14namespace xrpl::detail {
15
20{
21protected:
23
24private:
26 bool rmSubDir_{false};
27
28protected:
30
31 auto
32 rmDir(path const& toRm)
33 {
34 if (is_directory(toRm) && is_empty(toRm))
35 {
36 remove(toRm);
37 }
38 else
39 {
40 test_.log << "Expected " << toRm.string() << " to be an empty existing directory."
41 << std::endl;
42 }
43 }
44
45public:
46 DirGuard(beast::unit_test::Suite& test, path subDir, bool useCounter = true)
47 : subDir_(std::move(subDir)), test_(test)
48 {
49 using namespace std::filesystem;
50
51 static auto kSubDirCounter = 0;
52 if (useCounter)
53 subDir_ += std::to_string(++kSubDirCounter);
54 if (!exists(subDir_))
55 {
57 rmSubDir_ = true;
58 }
59 else if (is_directory(subDir_))
60 {
61 rmSubDir_ = false;
62 }
63 else
64 {
65 // Cannot run the test. Someone created a file where we want to
66 // put our directory
67 Throw<std::runtime_error>("Cannot create directory: " + subDir_.string());
68 }
69 }
70
72 {
73 try
74 {
75 using namespace std::filesystem;
76
77 if (rmSubDir_)
79 }
80 catch (std::exception& e)
81 {
82 // if we throw here, just let it die.
83 test_.log << "Error in ~DirGuard: " << e.what() << std::endl;
84 };
85 }
86
87 [[nodiscard]] path const&
88 subdir() const
89 {
90 return subDir_;
91 }
92};
93
97class FileDirGuard : public DirGuard
98{
99protected:
100 path const file_;
101 bool created_ = false;
102
103public:
106 path subDir,
107 path file,
108 std::string const& contents,
109 bool useCounter = true,
110 bool create = true)
111 : DirGuard(test, subDir, useCounter), file_(file.is_absolute() ? file : subdir() / file)
112 {
113 if (!exists(file_))
114 {
115 if (create)
116 {
117 std::ofstream o(file_.string());
118 o << contents;
119 created_ = true;
120 }
121 }
122 else
123 {
124 Throw<std::runtime_error>("Refusing to overwrite existing file: " + file_.string());
125 }
126 }
127
129 {
130 try
131 {
132 using namespace std::filesystem;
133 if (exists(file_))
134 {
135 remove(file_);
136 }
137 else
138 {
139 if (created_)
140 {
141 test_.log << "Expected " << file_.string() << " to be an existing file."
142 << std::endl;
143 }
144 }
145 }
146 catch (std::exception& e)
147 {
148 // if we throw here, just let it die.
149 test_.log << "Error in ~FileGuard: " << e.what() << std::endl;
150 };
151 }
152
153 [[nodiscard]] path const&
154 file() const
155 {
156 return file_;
157 }
158
159 [[nodiscard]] bool
161 {
163 }
164};
165
166} // namespace xrpl::detail
A testsuite class.
Definition suite.h:52
beast::unit_test::Suite & test_
DirGuard(beast::unit_test::Suite &test, path subDir, bool useCounter=true)
path const & subdir() const
std::filesystem::path path
auto rmDir(path const &toRm)
path const & file() const
FileDirGuard(beast::unit_test::Suite &test, path subDir, path file, std::string const &contents, bool useCounter=true, bool create=true)
T create_directory(T... args)
T endl(T... args)
T exists(T... args)
T is_directory(T... args)
STL namespace.
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition contract.h:52
T remove(T... args)
T to_string(T... args)
T what(T... args)