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