xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
test
unit_test
FileDirGuard.h
1
#pragma once
2
3
#include <xrpl/basics/contract.h>
4
#include <xrpl/beast/unit_test/suite.h>
5
6
#include <
exception
>
7
#include <
filesystem
>
8
#include <
fstream
>
9
#include <
ostream
>
10
#include <
stdexcept
>
11
#include <
string
>
12
#include <
utility
>
13
14
namespace
xrpl::detail
{
15
19
class
DirGuard
20
{
21
protected
:
22
using
Path
=
std::filesystem::path
;
23
24
private
:
25
Path
subDir_
;
26
bool
rmSubDir_
{
false
};
27
28
protected
:
29
beast::unit_test::Suite
&
test_
;
30
31
auto
32
rmDir
(
Path
const
& toRm)
33
{
34
if
(is_directory(toRm) && is_empty(toRm))
35
{
36
remove(toRm);
37
}
38
else
39
{
40
test_
.log <<
"Expected "
<< toRm.
string
() <<
" to be an empty existing directory."
41
<<
std::endl
;
42
}
43
}
44
45
public
:
46
DirGuard
(
beast::unit_test::Suite
&
test
,
Path
subDir,
bool
useCounter =
true
)
47
:
subDir_
(
std
::move(subDir)),
test_
(
test
)
48
{
49
using namespace
std::filesystem
;
50
51
static
auto
kSubDirCounter = 0;
52
if
(useCounter)
53
subDir_
+=
std::to_string
(++kSubDirCounter);
54
if
(!
exists
(
subDir_
))
55
{
56
create_directory
(
subDir_
);
57
rmSubDir_
=
true
;
58
}
59
else
if
(
is_directory
(
subDir_
))
60
{
61
rmSubDir_
=
false
;
62
}
63
else
64
{
65
// Cannot run the test. Someone created a file where we want to
66
// put our directory
67
Throw<std::runtime_error>
(
"Cannot create directory: "
+
subDir_
.string());
68
}
69
}
70
71
~DirGuard
()
72
{
73
try
74
{
75
using namespace
std::filesystem
;
76
77
if
(
rmSubDir_
)
78
rmDir
(
subDir_
);
79
}
80
catch
(
std::exception
& e)
81
{
82
// if we throw here, just let it die.
83
test_
.log <<
"Error in ~DirGuard: "
<< e.
what
() <<
std::endl
;
84
};
85
}
86
87
[[nodiscard]]
Path
const
&
88
subdir
()
const
89
{
90
return
subDir_
;
91
}
92
};
93
97
class
FileDirGuard
:
public
DirGuard
98
{
99
protected
:
100
Path
const
file_
;
101
bool
created_
=
false
;
102
103
public
:
104
FileDirGuard
(
105
beast::unit_test::Suite
&
test
,
106
Path
subDir,
107
Path
file
,
108
std::string
const
& contents,
109
bool
useCounter =
true
,
110
bool
create =
true
)
111
:
DirGuard
(
test
, subDir, useCounter),
file_
(
file
.is_absolute() ?
file
:
subdir
() /
file
)
112
{
113
if
(!
exists
(
file_
))
114
{
115
if
(create)
116
{
117
std::ofstream
o(
file_
.string());
118
o << contents;
119
created_
=
true
;
120
}
121
}
122
else
123
{
124
Throw<std::runtime_error>
(
"Refusing to overwrite existing file: "
+
file_
.string());
125
}
126
}
127
128
~FileDirGuard
()
129
{
130
try
131
{
132
using namespace
std::filesystem
;
133
if
(
exists
(
file_
))
134
{
135
remove
(
file_
);
136
}
137
else
138
{
139
if
(
created_
)
140
{
141
test_
.log <<
"Expected "
<<
file_
.string() <<
" to be an existing file."
142
<<
std::endl
;
143
}
144
}
145
}
146
catch
(
std::exception
& e)
147
{
148
// if we throw here, just let it die.
149
test_
.log <<
"Error in ~FileGuard: "
<< e.
what
() <<
std::endl
;
150
};
151
}
152
153
[[nodiscard]]
Path
const
&
154
file
()
const
155
{
156
return
file_
;
157
}
158
159
[[nodiscard]]
bool
160
fileExists
()
const
161
{
162
return
std::filesystem::exists
(
file_
);
163
}
164
};
165
166
}
// namespace xrpl::detail
std::ofstream
std::string
beast::unit_test::Suite
A testsuite class.
Definition
suite.h:52
xrpl::detail::DirGuard::rmDir
auto rmDir(Path const &toRm)
Definition
FileDirGuard.h:32
xrpl::detail::DirGuard::rmSubDir_
bool rmSubDir_
Definition
FileDirGuard.h:26
xrpl::detail::DirGuard::subDir_
Path subDir_
Definition
FileDirGuard.h:25
xrpl::detail::DirGuard::test_
beast::unit_test::Suite & test_
Definition
FileDirGuard.h:29
xrpl::detail::DirGuard::subdir
Path const & subdir() const
Definition
FileDirGuard.h:88
xrpl::detail::DirGuard::Path
std::filesystem::path Path
Definition
FileDirGuard.h:22
xrpl::detail::DirGuard::~DirGuard
~DirGuard()
Definition
FileDirGuard.h:71
xrpl::detail::DirGuard::DirGuard
DirGuard(beast::unit_test::Suite &test, Path subDir, bool useCounter=true)
Definition
FileDirGuard.h:46
xrpl::detail::FileDirGuard::file
Path const & file() const
Definition
FileDirGuard.h:154
xrpl::detail::FileDirGuard::created_
bool created_
Definition
FileDirGuard.h:101
xrpl::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:104
xrpl::detail::FileDirGuard::fileExists
bool fileExists() const
Definition
FileDirGuard.h:160
xrpl::detail::FileDirGuard::~FileDirGuard
~FileDirGuard()
Definition
FileDirGuard.h:128
xrpl::detail::FileDirGuard::file_
Path const file_
Definition
FileDirGuard.h:100
std::filesystem::create_directory
T create_directory(T... args)
std::endl
T endl(T... args)
exception
std::filesystem::exists
T exists(T... args)
filesystem
fstream
std::filesystem::is_directory
T is_directory(T... args)
std::filesystem
std
STL namespace.
xrpl::detail
Definition
base_uint.h:37
xrpl::test
Definition
STLedgerEntry.h:21
xrpl::Throw
XRPL_NO_SANITIZE_ADDRESS void Throw(Args &&... args)
Definition
contract.h:52
ostream
std::filesystem::path
std::filesystem::remove
T remove(T... args)
stdexcept
string
std::to_string
T to_string(T... args)
utility
std::exception::what
T what(T... args)
Generated by
1.17.0