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