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 <test/jtx/TestSuite.h>
25 #include <boost/filesystem.hpp>
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 rmDir (path const& toRm)
47  {
48  if (is_directory (toRm) && is_empty (toRm))
49  remove (toRm);
50  else
51  test_.log << "Expected " << toRm.string ()
52  << " to be an empty existing directory." << std::endl;
53  }
54 
55 public:
56  DirGuard (beast::unit_test::suite& test, path subDir,
57  bool useCounter = true)
58  : subDir_ (std::move (subDir))
59  , 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_)
89  rmDir (subDir_);
90  else
91  test_.log << "Skipping rm dir: "
92  << subDir_.string () << std::endl;
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& 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:
117  FileDirGuard(beast::unit_test::suite& test,
118  path subDir, path file, std::string const& contents,
119  bool useCounter = true, bool create = true)
120  : DirGuard(test, subDir, useCounter)
121  , file_(file.is_absolute() ? file : subdir() / file)
122  {
123  if (!exists (file_))
124  {
125  if (create)
126  {
127  std::ofstream o (file_.string ());
128  o << contents;
129  created_ = true;
130  }
131  }
132  else
133  {
134  Throw<std::runtime_error> (
135  "Refusing to overwrite existing file: " +
136  file_.string ());
137  }
138  }
139 
141  {
142  try
143  {
144  using namespace boost::filesystem;
145  if (exists (file_))
146  {
147  remove (file_);
148  }
149  else
150  {
151  if (created_)
152  test_.log << "Expected " << file_.string ()
153  << " to be an existing file." << std::endl;
154  }
155  }
156  catch (std::exception& e)
157  {
158  // if we throw here, just let it die.
159  test_.log << "Error in ~FileGuard: "
160  << e.what () << std::endl;
161  };
162  }
163 
164  path const& file() const
165  {
166  return file_;
167  }
168 
169  bool fileExists () const
170  {
171  return boost::filesystem::exists (file_);
172  }
173 };
174 
175 }
176 }
177 }
178 
179 #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:82
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:164
std::ofstream
STL class.
std::to_string
T to_string(T... args)
ripple::test::detail::DirGuard::test_
beast::unit_test::suite & test_
Definition: FileDirGuard.h:44
ripple::test::detail::FileDirGuard::~FileDirGuard
~FileDirGuard()
Definition: FileDirGuard.h:140
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:169
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:46
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:56