rippled
Loading...
Searching...
No Matches
FileDirGuard.h
1#ifndef TEST_UNIT_TEST_DIRGUARD_H
2#define TEST_UNIT_TEST_DIRGUARD_H
3
4#include <test/jtx/TestSuite.h>
5
6#include <xrpl/basics/contract.h>
7
8#include <boost/filesystem.hpp>
9
10#include <fstream>
11
12namespace ripple {
13namespace detail {
14
19{
20protected:
21 using path = boost::filesystem::path;
22
23private:
25 bool rmSubDir_{false};
26
27protected:
29
30 auto
31 rmDir(path const& toRm)
32 {
33 if (is_directory(toRm) && is_empty(toRm))
34 remove(toRm);
35 else
36 test_.log << "Expected " << toRm.string()
37 << " to be an empty existing directory." << std::endl;
38 }
39
40public:
41 DirGuard(beast::unit_test::suite& test, path subDir, bool useCounter = true)
42 : subDir_(std::move(subDir)), test_(test)
43 {
44 using namespace boost::filesystem;
45
46 static auto subDirCounter = 0;
47 if (useCounter)
48 subDir_ += std::to_string(++subDirCounter);
49 if (!exists(subDir_))
50 {
51 create_directory(subDir_);
52 rmSubDir_ = true;
53 }
54 else if (is_directory(subDir_))
55 rmSubDir_ = false;
56 else
57 {
58 // Cannot run the test. Someone created a file where we want to
59 // put our directory
60 Throw<std::runtime_error>(
61 "Cannot create directory: " + subDir_.string());
62 }
63 }
64
66 {
67 try
68 {
69 using namespace boost::filesystem;
70
71 if (rmSubDir_)
73 }
74 catch (std::exception& e)
75 {
76 // if we throw here, just let it die.
77 test_.log << "Error in ~DirGuard: " << e.what() << std::endl;
78 };
79 }
80
81 path const&
82 subdir() const
83 {
84 return subDir_;
85 }
86};
87
91class FileDirGuard : public DirGuard
92{
93protected:
94 path const file_;
95 bool created_ = false;
96
97public:
100 path subDir,
101 path file,
102 std::string const& contents,
103 bool useCounter = true,
104 bool create = true)
105 : DirGuard(test, subDir, useCounter)
106 , file_(file.is_absolute() ? file : subdir() / file)
107 {
108 if (!exists(file_))
109 {
110 if (create)
111 {
112 std::ofstream o(file_.string());
113 o << contents;
114 created_ = true;
115 }
116 }
117 else
118 {
119 Throw<std::runtime_error>(
120 "Refusing to overwrite existing file: " + file_.string());
121 }
122 }
123
125 {
126 try
127 {
128 using namespace boost::filesystem;
129 if (exists(file_))
130 {
131 remove(file_);
132 }
133 else
134 {
135 if (created_)
136 test_.log << "Expected " << file_.string()
137 << " to be an existing file." << std::endl;
138 }
139 }
140 catch (std::exception& e)
141 {
142 // if we throw here, just let it die.
143 test_.log << "Error in ~FileGuard: " << e.what() << std::endl;
144 };
145 }
146
147 path const&
148 file() const
149 {
150 return file_;
151 }
152
153 bool
155 {
156 return boost::filesystem::exists(file_);
157 }
158};
159
160} // namespace detail
161} // namespace ripple
162
163#endif // TEST_UNIT_TEST_DIRGUARD_H
A testsuite class.
Definition suite.h:52
log_os< char > log
Logging output stream.
Definition suite.h:149
Create a directory and remove it when it's done.
auto rmDir(path const &toRm)
DirGuard(beast::unit_test::suite &test, path subDir, bool useCounter=true)
boost::filesystem::path path
path const & subdir() const
beast::unit_test::suite & test_
Write a file in a directory and remove when done.
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)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
STL namespace.
T to_string(T... args)
T what(T... args)