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