rippled
Loading...
Searching...
No Matches
FileDirGuard.h
1//------------------------------------------------------------------------------
2/*
3This file is part of rippled: https://github.com/ripple/rippled
4Copyright (c) 2018 Ripple Labs Inc.
5
6Permission to use, copy, modify, and/or distribute this software for any
7purpose with or without fee is hereby granted, provided that the above
8copyright notice and this permission notice appear in all copies.
9
10THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#ifndef TEST_UNIT_TEST_DIRGUARD_H
21#define TEST_UNIT_TEST_DIRGUARD_H
22
23#include <test/jtx/TestSuite.h>
24#include <xrpl/basics/contract.h>
25
26#include <boost/filesystem.hpp>
27
28namespace ripple {
29namespace test {
30namespace detail {
31
36{
37protected:
38 using path = boost::filesystem::path;
39
40private:
42 bool rmSubDir_{false};
43
44protected:
46
47 auto
48 rmDir(path const& toRm)
49 {
50 if (is_directory(toRm) && is_empty(toRm))
51 remove(toRm);
52 else
53 test_.log << "Expected " << toRm.string()
54 << " to be an empty existing directory." << std::endl;
55 }
56
57public:
58 DirGuard(beast::unit_test::suite& test, path subDir, bool useCounter = true)
59 : subDir_(std::move(subDir)), test_(test)
60 {
61 using namespace boost::filesystem;
62
63 static auto subDirCounter = 0;
64 if (useCounter)
65 subDir_ += std::to_string(++subDirCounter);
66 if (!exists(subDir_))
67 {
68 create_directory(subDir_);
69 rmSubDir_ = true;
70 }
71 else if (is_directory(subDir_))
72 rmSubDir_ = false;
73 else
74 {
75 // Cannot run the test. Someone created a file where we want to
76 // put our directory
77 Throw<std::runtime_error>(
78 "Cannot create directory: " + subDir_.string());
79 }
80 }
81
83 {
84 try
85 {
86 using namespace boost::filesystem;
87
88 if (rmSubDir_)
90 }
91 catch (std::exception& e)
92 {
93 // if we throw here, just let it die.
94 test_.log << "Error in ~DirGuard: " << e.what() << std::endl;
95 };
96 }
97
98 path const&
99 subdir() const
100 {
101 return subDir_;
102 }
103};
104
108class FileDirGuard : public DirGuard
109{
110protected:
111 path const file_;
112 bool created_ = false;
113
114public:
117 path subDir,
118 path file,
119 std::string const& contents,
120 bool useCounter = true,
121 bool create = true)
122 : DirGuard(test, subDir, useCounter)
123 , file_(file.is_absolute() ? file : subdir() / file)
124 {
125 if (!exists(file_))
126 {
127 if (create)
128 {
129 std::ofstream o(file_.string());
130 o << contents;
131 created_ = true;
132 }
133 }
134 else
135 {
136 Throw<std::runtime_error>(
137 "Refusing to overwrite existing file: " + file_.string());
138 }
139 }
140
142 {
143 try
144 {
145 using namespace boost::filesystem;
146 if (exists(file_))
147 {
148 remove(file_);
149 }
150 else
151 {
152 if (created_)
153 test_.log << "Expected " << file_.string()
154 << " to be an existing file." << std::endl;
155 }
156 }
157 catch (std::exception& e)
158 {
159 // if we throw here, just let it die.
160 test_.log << "Error in ~FileGuard: " << e.what() << std::endl;
161 };
162 }
163
164 path const&
165 file() const
166 {
167 return file_;
168 }
169
170 bool
172 {
173 return boost::filesystem::exists(file_);
174 }
175};
176
177} // namespace detail
178} // namespace test
179} // namespace ripple
180
181#endif // TEST_UNIT_TEST_DIRGUARD_H
A testsuite class.
Definition: suite.h:55
log_os< char > log
Logging output stream.
Definition: suite.h:152
Create a directory and remove it when it's done.
Definition: FileDirGuard.h:36
path const & subdir() const
Definition: FileDirGuard.h:99
DirGuard(beast::unit_test::suite &test, path subDir, bool useCounter=true)
Definition: FileDirGuard.h:58
auto rmDir(path const &toRm)
Definition: FileDirGuard.h:48
beast::unit_test::suite & test_
Definition: FileDirGuard.h:45
boost::filesystem::path path
Definition: FileDirGuard.h:38
Write a file in a directory and remove when done.
Definition: FileDirGuard.h:109
FileDirGuard(beast::unit_test::suite &test, path subDir, path file, std::string const &contents, bool useCounter=true, bool create=true)
Definition: FileDirGuard.h:115
Add a path.
Definition: paths.h:57
T endl(T... args)
Json::Value create(AccountID const &account, AccountID const &to, STAmount const &amount, NetClock::duration const &settleDelay, PublicKey const &pk, std::optional< NetClock::time_point > const &cancelAfter, std::optional< std::uint32_t > const &dstTag)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
STL namespace.
T to_string(T... args)
T what(T... args)