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