forked from degauden/Elements
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTemporary_test.cpp
More file actions
234 lines (182 loc) · 6.7 KB
/
Copy pathTemporary_test.cpp
File metadata and controls
234 lines (182 loc) · 6.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/**
* @file Temporary_test.cpp
*
* @date Dec 4, 2013
* @author Hubert Degaudenzi
*
* @copyright 2012-2020 Euclid Science Ground Segment
*
* This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General
* Public License as published by the Free Software Foundation; either version 3.0 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "ElementsKernel/Temporary.h"
#include <fstream> // for ofstream
#include <iostream> // for basic_ostream, operator<<, endl, cout
#include <string> // for operator==, char_traits, allocator, string, basic_string
#include <boost/filesystem.hpp> // for create_directory, exists>
#include <boost/test/unit_test.hpp>
#include "ElementsKernel/Environment.h" // for Environment
#include "ElementsKernel/Path.h" // for Item
#include "ElementsKernel/System.h" // for getEnv, setEnv, unSetEnv
using std::string;
using boost::filesystem::create_directory;
using boost::filesystem::exists;
namespace Elements {
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//
// Begin of the Boost tests
//
//-----------------------------------------------------------------------------
const string WORKDIR_VAR{"TMP_WORKSPACE"};
struct Temporary_Fixture {
TempDir m_top_dir{"Temporary_test-%%%%%%%"};
TempEnv m_env;
Temporary_Fixture() {
// setup
m_env[WORKDIR_VAR] = (m_top_dir.path() / "work").string();
}
~Temporary_Fixture() = default;
};
BOOST_AUTO_TEST_SUITE(Temporary_test)
//-----------------------------------------------------------------------------
BOOST_FIXTURE_TEST_CASE(AutoDestruct_test, Temporary_Fixture) {
// handle on created path names
Path::Item test_path;
Path::Item test_file_path;
{
// block creation for local variables
TempDir one;
BOOST_CHECK_EQUAL(one.motif(), DEFAULT_TMP_MOTIF);
test_path = one.path();
BOOST_CHECK(exists(test_path));
TempFile two;
test_file_path = two.path();
BOOST_CHECK(exists(test_file_path));
}
// the items must have been destroyed after the
// closing of the block
if (m_env.get("KEEPTEMPDIR", "0") == "0") {
BOOST_CHECK(!exists(test_path));
BOOST_CHECK(!exists(test_file_path));
} else {
BOOST_CHECK(exists(test_path));
BOOST_CHECK(exists(test_file_path));
}
Path::Item test2_path;
Path::Item test2_file_path;
{
using std::endl;
TempDir three;
test2_path = three.path();
test2_file_path = test2_path / "toto.txt";
BOOST_CHECK(!exists(test2_file_path));
std::ofstream ofs(test2_file_path);
ofs << "test text" << endl;
ofs.close();
BOOST_CHECK(exists(test2_file_path));
BOOST_CHECK(exists(test2_path));
}
if (m_env.get("KEEPTEMPDIR", "0") == "0") {
BOOST_CHECK(!exists(test2_path));
BOOST_CHECK(!exists(test2_file_path));
} else {
BOOST_CHECK(exists(test2_path));
BOOST_CHECK(exists(test2_file_path));
}
}
BOOST_FIXTURE_TEST_CASE(TempEnv_test, Temporary_Fixture) {
using System::getEnv;
using System::setEnv;
using System::unSetEnv;
// test if the global temporary directory exists.
BOOST_CHECK(exists(m_top_dir.path()));
const Path::Item test_tmpdir = m_top_dir.path() / "tmpdir";
create_directory(test_tmpdir);
setEnv("TMPDIR", test_tmpdir.c_str(), true);
const string tmp_env_val = getEnv("TMPDIR");
// test that the variable is actually set in the environment
// of the process
BOOST_CHECK(tmp_env_val == test_tmpdir.string());
// create a new temporary directory that should be rooted at the
// value of the TMPDIR directory.
const TempDir new_one;
// test that the new tmp directory has been created in the right
// directory (in $TMPDIR)
BOOST_CHECK(new_one.path().parent_path() == test_tmpdir);
// remove the environment variable
unSetEnv("TMPDIR");
// check that it is gone
BOOST_CHECK(getEnv("TMPDIR").empty());
BOOST_CHECK(exists(test_tmpdir));
}
BOOST_FIXTURE_TEST_CASE(TempEnv2_test, Temporary_Fixture) {
using System::getEnv;
BOOST_CHECK(m_env[WORKDIR_VAR].value() == (m_top_dir.path() / "work").string());
// test if the global temporary directory exists.
BOOST_CHECK(exists(m_top_dir.path()));
const Path::Item test_tmpdir = m_top_dir.path() / "tmpdir2";
create_directory(test_tmpdir);
{
TempEnv local;
local["TMPDIR"] = test_tmpdir.c_str();
BOOST_CHECK(local[WORKDIR_VAR].value() == (m_top_dir.path() / "work").string());
BOOST_CHECK(m_env[WORKDIR_VAR].value() == (m_top_dir.path() / "work").string());
local[WORKDIR_VAR] = "that_work";
BOOST_CHECK(local[WORKDIR_VAR].value() == "that_work");
BOOST_CHECK(m_env[WORKDIR_VAR].value() == "that_work");
const string tmp_env_val = getEnv("TMPDIR");
// test that the variable is actually set in the environment
// of the process
BOOST_CHECK(tmp_env_val == test_tmpdir.string());
BOOST_CHECK(local["TMPDIR"].value() == test_tmpdir.string());
}
BOOST_CHECK(m_env[WORKDIR_VAR].value() == (m_top_dir.path() / "work").string());
BOOST_CHECK(getEnv("TMPDIR").empty());
BOOST_CHECK(exists(test_tmpdir));
}
BOOST_AUTO_TEST_CASE(KeepTmpDir_test) {
using boost::filesystem::remove_all;
Environment current;
current["KEEPTEMPDIR"] = "1";
Path::Item that_path;
{
const TempDir that;
that_path = that.path();
BOOST_CHECK(exists(that_path));
}
BOOST_CHECK(exists(that_path));
remove_all(that_path);
BOOST_CHECK(not exists(that_path));
}
BOOST_AUTO_TEST_CASE(Fake_test) {
using boost::filesystem::temp_directory_path;
const string motif1;
const string motif2 = "toto-%%%";
const auto path1 = temp_directory_path() / uniquePath(motif1);
const auto path1p = temp_directory_path() / uniquePath();
const auto path2 = temp_directory_path() / uniquePath(motif2);
using std::cout;
using std::endl;
cout << "path1:" << path1 << endl;
cout << "path1p:" << path1p << endl;
cout << "path2:" << path2 << endl;
TempPath p1;
TempPath p2(motif1);
}
BOOST_AUTO_TEST_SUITE_END()
//-----------------------------------------------------------------------------
//
// End of the Boost tests
//
//-----------------------------------------------------------------------------
} // namespace Elements