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."
36 << std::endl;
37 }
38
39public:
40 DirGuard(beast::unit_test::suite& test, path subDir, bool useCounter = true)
41 : subDir_(std::move(subDir)), test_(test)
42 {
43 using namespace boost::filesystem;
44
45 static auto subDirCounter = 0;
46 if (useCounter)
47 subDir_ += std::to_string(++subDirCounter);
48 if (!exists(subDir_))
49 {
50 create_directory(subDir_);
51 rmSubDir_ = true;
52 }
53 else if (is_directory(subDir_))
54 rmSubDir_ = false;
55 else
56 {
57 // Cannot run the test. Someone created a file where we want to
58 // put our directory
59 Throw<std::runtime_error>("Cannot create directory: " + subDir_.string());
60 }
61 }
62
64 {
65 try
66 {
67 using namespace boost::filesystem;
68
69 if (rmSubDir_)
71 }
72 catch (std::exception& e)
73 {
74 // if we throw here, just let it die.
75 test_.log << "Error in ~DirGuard: " << e.what() << std::endl;
76 };
77 }
78
79 path const&
80 subdir() const
81 {
82 return subDir_;
83 }
84};
85
89class FileDirGuard : public DirGuard
90{
91protected:
92 path const file_;
93 bool created_ = false;
94
95public:
98 path subDir,
99 path file,
100 std::string const& contents,
101 bool useCounter = true,
102 bool create = true)
103 : DirGuard(test, subDir, useCounter), file_(file.is_absolute() ? file : subdir() / file)
104 {
105 if (!exists(file_))
106 {
107 if (create)
108 {
109 std::ofstream o(file_.string());
110 o << contents;
111 created_ = true;
112 }
113 }
114 else
115 {
116 Throw<std::runtime_error>("Refusing to overwrite existing file: " + file_.string());
117 }
118 }
119
121 {
122 try
123 {
124 using namespace boost::filesystem;
125 if (exists(file_))
126 {
127 remove(file_);
128 }
129 else
130 {
131 if (created_)
132 test_.log << "Expected " << file_.string() << " to be an existing file."
133 << std::endl;
134 }
135 }
136 catch (std::exception& e)
137 {
138 // if we throw here, just let it die.
139 test_.log << "Error in ~FileGuard: " << e.what() << std::endl;
140 };
141 }
142
143 path const&
144 file() const
145 {
146 return file_;
147 }
148
149 bool
151 {
152 return boost::filesystem::exists(file_);
153 }
154};
155
156} // namespace detail
157} // namespace xrpl
A testsuite class.
Definition suite.h:51
log_os< char > log
Logging output stream.
Definition suite.h:147
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)