rippled
FileDirGuard.h
1 //------------------------------------------------------------------------------
2 /*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2018 Ripple Labs Inc.
5 
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9 
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR 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 <ripple/basics/contract.h>
24 #include <boost/filesystem.hpp>
25 #include <test/jtx/TestSuite.h>
26 
27 namespace ripple {
28 namespace test {
29 namespace detail {
30 
34 class DirGuard
35 {
36 protected:
37  using path = boost::filesystem::path;
38 
39 private:
41  bool rmSubDir_{false};
42 
43 protected:
44  beast::unit_test::suite& test_;
45 
46  auto
47  rmDir(path const& toRm)
48  {
49  if (is_directory(toRm) && is_empty(toRm))
50  remove(toRm);
51  else
52  test_.log << "Expected " << toRm.string()
53  << " to be an empty existing directory." << std::endl;
54  }
55 
56 public:
57  DirGuard(beast::unit_test::suite& test, path subDir, bool useCounter = true)
58  : subDir_(std::move(subDir)), test_(test)
59  {
60  using namespace boost::filesystem;
61 
62  static auto subDirCounter = 0;
63  if (useCounter)
64  subDir_ += std::to_string(++subDirCounter);
65  if (!exists(subDir_))
66  {
67  create_directory(subDir_);
68  rmSubDir_ = true;
69  }
70  else if (is_directory(subDir_))
71  rmSubDir_ = false;
72  else
73  {
74  // Cannot run the test. Someone created a file where we want to
75  // put our directory
76  Throw<std::runtime_error>(
77  "Cannot create directory: " + subDir_.string());
78  }
79  }
80 
82  {
83  try
84  {
85  using namespace boost::filesystem;
86 
87  if (rmSubDir_)
88  rmDir(subDir_);
89  else
90  test_.log << "Skipping rm dir: " << subDir_.string()
91  << std::endl;
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 
110 class FileDirGuard : public DirGuard
111 {
112 protected:
113  path const file_;
114  bool created_ = false;
115 
116 public:
118  beast::unit_test::suite& test,
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
173  fileExists() const
174  {
175  return boost::filesystem::exists(file_);
176  }
177 };
178 
179 } // namespace detail
180 } // namespace test
181 } // namespace ripple
182 
183 #endif // TEST_UNIT_TEST_DIRGUARD_H
std::string
STL class.
std::exception
STL class.
ripple::test::detail::DirGuard
Create a directory and remove it when it's done.
Definition: FileDirGuard.h:34
ripple::test::detail::DirGuard::~DirGuard
~DirGuard()
Definition: FileDirGuard.h:81
ripple::test::detail::DirGuard::subdir
path const & subdir() const
Definition: FileDirGuard.h:101
ripple::test::detail::FileDirGuard::file
path const & file() const
Definition: FileDirGuard.h:167
std::ofstream
STL class.
std::to_string
T to_string(T... args)
ripple::test::jtx::path
Add a path.
Definition: paths.h:55
ripple::test::detail::DirGuard::test_
beast::unit_test::suite & test_
Definition: FileDirGuard.h:44
ripple::test::detail::FileDirGuard::~FileDirGuard
~FileDirGuard()
Definition: FileDirGuard.h:143
ripple::test::detail::DirGuard::subDir_
path subDir_
Definition: FileDirGuard.h:40
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::test::detail::FileDirGuard::FileDirGuard
FileDirGuard(beast::unit_test::suite &test, path subDir, path file, std::string const &contents, bool useCounter=true, bool create=true)
Definition: FileDirGuard.h:117
std::endl
T endl(T... args)
std
STL namespace.
ripple::test::detail::DirGuard::rmSubDir_
bool rmSubDir_
Definition: FileDirGuard.h:41
ripple::test::detail::FileDirGuard::fileExists
bool fileExists() const
Definition: FileDirGuard.h:173
ripple::test::detail::FileDirGuard
Write a file in a directory and remove when done.
Definition: FileDirGuard.h:110
ripple::test::detail::DirGuard::rmDir
auto rmDir(path const &toRm)
Definition: FileDirGuard.h:47
ripple::test::detail::FileDirGuard::file_
const path file_
Definition: FileDirGuard.h:113
ripple::test::detail::DirGuard::path
boost::filesystem::path path
Definition: FileDirGuard.h:37
std::exception::what
T what(T... args)
ripple::test::detail::DirGuard::DirGuard
DirGuard(beast::unit_test::suite &test, path subDir, bool useCounter=true)
Definition: FileDirGuard.h:57