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
25#include <xrpl/basics/contract.h>
26
27#include <boost/filesystem.hpp>
28
29#include <fstream>
30
31namespace ripple {
32namespace test {
33namespace detail {
34
39{
40protected:
41 using path = boost::filesystem::path;
42
43private:
45 bool rmSubDir_{false};
46
47protected:
49
50 auto
51 rmDir(path const& toRm)
52 {
53 if (is_directory(toRm) && is_empty(toRm))
54 remove(toRm);
55 else
56 test_.log << "Expected " << toRm.string()
57 << " to be an empty existing directory." << std::endl;
58 }
59
60public:
61 DirGuard(beast::unit_test::suite& test, path subDir, bool useCounter = true)
62 : subDir_(std::move(subDir)), test_(test)
63 {
64 using namespace boost::filesystem;
65
66 static auto subDirCounter = 0;
67 if (useCounter)
68 subDir_ += std::to_string(++subDirCounter);
69 if (!exists(subDir_))
70 {
71 create_directory(subDir_);
72 rmSubDir_ = true;
73 }
74 else if (is_directory(subDir_))
75 rmSubDir_ = false;
76 else
77 {
78 // Cannot run the test. Someone created a file where we want to
79 // put our directory
80 Throw<std::runtime_error>(
81 "Cannot create directory: " + subDir_.string());
82 }
83 }
84
86 {
87 try
88 {
89 using namespace boost::filesystem;
90
91 if (rmSubDir_)
93 }
94 catch (std::exception& e)
95 {
96 // if we throw here, just let it die.
97 test_.log << "Error in ~DirGuard: " << e.what() << std::endl;
98 };
99 }
100
101 path const&
102 subdir() const
103 {
104 return subDir_;
105 }
106};
107
111class FileDirGuard : public DirGuard
112{
113protected:
114 path const file_;
115 bool created_ = false;
116
117public:
120 path subDir,
121 path file,
122 std::string const& contents,
123 bool useCounter = true,
124 bool create = true)
125 : DirGuard(test, subDir, useCounter)
126 , file_(file.is_absolute() ? file : subdir() / file)
127 {
128 if (!exists(file_))
129 {
130 if (create)
131 {
132 std::ofstream o(file_.string());
133 o << contents;
134 created_ = true;
135 }
136 }
137 else
138 {
139 Throw<std::runtime_error>(
140 "Refusing to overwrite existing file: " + file_.string());
141 }
142 }
143
145 {
146 try
147 {
148 using namespace boost::filesystem;
149 if (exists(file_))
150 {
151 remove(file_);
152 }
153 else
154 {
155 if (created_)
156 test_.log << "Expected " << file_.string()
157 << " to be an existing file." << std::endl;
158 }
159 }
160 catch (std::exception& e)
161 {
162 // if we throw here, just let it die.
163 test_.log << "Error in ~FileGuard: " << e.what() << std::endl;
164 };
165 }
166
167 path const&
168 file() const
169 {
170 return file_;
171 }
172
173 bool
175 {
176 return boost::filesystem::exists(file_);
177 }
178};
179
180} // namespace detail
181} // namespace test
182} // namespace ripple
183
184#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.
path const & subdir() const
DirGuard(beast::unit_test::suite &test, path subDir, bool useCounter=true)
auto rmDir(path const &toRm)
beast::unit_test::suite & test_
boost::filesystem::path path
Write a file in a directory and remove when done.
FileDirGuard(beast::unit_test::suite &test, path subDir, path file, std::string const &contents, bool useCounter=true, bool create=true)
Add a path.
Definition paths.h:58
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:25
STL namespace.
T to_string(T... args)
T what(T... args)