forked from degauden/Elements
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPathSearch_test.cpp
More file actions
241 lines (167 loc) · 8.85 KB
/
Copy pathPathSearch_test.cpp
File metadata and controls
241 lines (167 loc) · 8.85 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
235
236
237
238
239
240
241
/**
* @file PathSearch_test.cpp
*
* Created on: Dec 4, 2013
* Author: Pierre Dubath
*
* @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/PathSearch.h"
#include <string> // for allocator, operator+, char_traits, string, basic_string, operator==
#include <vector> // for vector
#include <boost/filesystem.hpp> // for is_directory>
#include <boost/test/unit_test.hpp> // for operator<<, BOOST_PP_IIF_1, BOOST_CHECK, ...
#include "ElementsKernel/Auxiliary.h" // for getPath
#include "ElementsKernel/Environment.h" // for Environment
#include "ElementsKernel/Exception.h" // for operator<<
#include "ElementsKernel/Temporary.h" // for TempDir
using std::string;
using std::vector;
using boost::filesystem::path;
namespace Elements {
struct PathSearch_Fixture {
Environment m_env{};
string m_env_variable_name = "ELEMENTS_CONF_PATH";
path m_root_path = Auxiliary::getPath("ElementsKernel/tests");
path m_full_path = m_root_path / "PathSearch/";
path m_full_path_1 = m_root_path / "PathSearch/dirForTestingRecursiveSearch";
path m_full_path_2 = m_root_path / "PathSearch//ElementsKernel";
string m_multiple_path = "/opt/local/bin:" + m_full_path_1.string() + ":" + m_full_path_2.string() + ":/bin";
PathSearch_Fixture() = default;
~PathSearch_Fixture() = default;
};
void createTemporaryStructure(const path& top_path) {
create_directories(top_path / "tests" / "data" / "PathSearch");
}
BOOST_AUTO_TEST_SUITE(PathSearch_test)
//-----------------------------------------------------------------------------
BOOST_FIXTURE_TEST_CASE(simple_file_search, PathSearch_Fixture) {
const string file_name{"MockFile_up.conf"};
const path expected_full_path = m_full_path / path(file_name);
path actual_full_path = pathSearch(file_name, m_full_path, SearchType::Local).at(0);
BOOST_CHECK(actual_full_path == expected_full_path);
actual_full_path = pathSearch(file_name, m_full_path, SearchType::Recursive).at(0);
BOOST_CHECK(actual_full_path == expected_full_path);
}
BOOST_FIXTURE_TEST_CASE(simple_directory_search, PathSearch_Fixture) {
const string file_name{"ElementsKernel"};
const path expected_full_path = m_full_path / path(file_name);
const path actual_full_path = pathSearch(file_name, m_full_path, SearchType::Local).at(0);
BOOST_CHECK(actual_full_path == expected_full_path);
BOOST_CHECK(pathSearch(file_name, m_full_path, SearchType::Recursive).size() == 2);
}
BOOST_FIXTURE_TEST_CASE(recursive_file_search, PathSearch_Fixture) {
const string file_name{"MockFile_down.conf"};
// should not be found on a Local search, pathSearch returns a vector of size = 0
BOOST_CHECK(pathSearch(file_name, m_full_path, SearchType::Local).empty());
const path expected_full_path = m_full_path / path("dirForTestingRecursiveSearch") / path(file_name);
// only one MockFile_down.conf in first position of the path vector
const path actual_full_path = pathSearch(file_name, m_full_path, SearchType::Recursive).at(0);
BOOST_CHECK(actual_full_path == expected_full_path);
}
BOOST_FIXTURE_TEST_CASE(recursive_search_string_interface, PathSearch_Fixture) {
const string file_name{"MockFile_down.conf"};
// should not be found on a Local search, pathSearch returns a vector of size = 0
BOOST_CHECK(pathSearch(file_name, m_full_path.string(), SearchType::Local).empty());
const path expected_full_path = m_full_path / path("dirForTestingRecursiveSearch") / path(file_name);
// only one MockFile_down.conf in first position of the path vector
const string actual_full_path = pathSearch(file_name, m_full_path.string(), SearchType::Recursive).at(0);
BOOST_CHECK(actual_full_path == expected_full_path.string());
}
BOOST_FIXTURE_TEST_CASE(duplicate_file_search, PathSearch_Fixture) {
const string file_name{"MockFile_replicate.conf"};
// should not be found on a Local search, pathSearch returns a vector of size = 0
BOOST_CHECK(pathSearch(file_name, m_full_path, SearchType::Local).empty());
// two MockFile_replicate.conf should be found on a recursive search
BOOST_CHECK(pathSearch(file_name, m_full_path.string(), SearchType::Recursive).size() == 2);
}
//----------------------------------------------------------------------------------------------------------
BOOST_FIXTURE_TEST_CASE(searchFileInEnvVariable_single_file, PathSearch_Fixture) {
Environment local;
local[m_env_variable_name] = m_multiple_path;
const string file{"MockFile_in_ElementsKernel_up.conf"};
const vector<path> actual_full_path_vector = pathSearchInEnvVariable(file, m_env_variable_name);
BOOST_CHECK(actual_full_path_vector.size() == 1);
const path expected_full_path = m_full_path / path{"ElementsKernel/MockFile_in_ElementsKernel_up.conf"};
BOOST_CHECK(expected_full_path == actual_full_path_vector.at(0));
}
BOOST_FIXTURE_TEST_CASE(searchFileInEnvVariable_multiple_file, PathSearch_Fixture) {
Environment local;
local[m_env_variable_name] = m_multiple_path;
const string file{"MockFile_replicate.conf"};
const vector<path> actual_full_path_vector = pathSearchInEnvVariable(file, m_env_variable_name);
BOOST_CHECK(actual_full_path_vector.size() == 2);
const path expected_full_path_1 = m_full_path_1.string() / path{"ElementsKernel/MockFile_replicate.conf"};
const path expected_full_path_2 = m_full_path_2.string() / path{"MockFile_replicate.conf"};
if (expected_full_path_1 != actual_full_path_vector.at(0)) {
BOOST_CHECK(expected_full_path_1 == actual_full_path_vector.at(1));
BOOST_CHECK(expected_full_path_2 == actual_full_path_vector.at(0));
} else {
BOOST_CHECK(expected_full_path_1 == actual_full_path_vector.at(0));
BOOST_CHECK(expected_full_path_2 == actual_full_path_vector.at(1));
}
}
/*
* Check whether the first file appearing in one of the path elements
* is taken first
*/
BOOST_FIXTURE_TEST_CASE(searchFileInEnvVariable_order, PathSearch_Fixture) {
// first order
string multiple_path = "/opt/local/bin:" + m_full_path_1.string() + ":" + m_full_path_2.string() + ":/bin";
Environment local;
local[m_env_variable_name] = multiple_path;
const string file_1{"MockFile_replicate.conf"};
vector<path> actual_full_path_vector = pathSearchInEnvVariable(file_1, m_env_variable_name);
path expected_full_path = m_full_path_1.string() / path{"ElementsKernel/MockFile_replicate.conf"};
BOOST_CHECK(expected_full_path == actual_full_path_vector.at(0));
// Opposite order
multiple_path = "/opt/local/bin:" + m_full_path_2.string() + ":" + m_full_path_1.string() + ":/bin";
local[m_env_variable_name] = multiple_path;
const string file_2{"MockFile_replicate.conf"};
actual_full_path_vector = pathSearchInEnvVariable(file_2, m_env_variable_name);
expected_full_path = m_full_path_2.string() / path{"MockFile_replicate.conf"};
BOOST_CHECK(expected_full_path == actual_full_path_vector.at(0));
}
BOOST_FIXTURE_TEST_CASE(searchFileInEnvVariable_file_not_found, PathSearch_Fixture) {
Environment local;
local[m_env_variable_name] = m_multiple_path;
const string file{"NonExistentFile.conf"};
const vector<path> actual_full_path_vector = pathSearchInEnvVariable(file, m_env_variable_name);
BOOST_CHECK(actual_full_path_vector.empty());
}
BOOST_FIXTURE_TEST_CASE(searchFileInEnvVariable_Env_Variable_Undefine, PathSearch_Fixture) {
Environment local;
local.unSet(m_env_variable_name);
const string file{"MockFile_replicate.conf"};
const vector<path> actual_full_path_vector = pathSearchInEnvVariable(file, m_env_variable_name);
BOOST_CHECK(actual_full_path_vector.empty());
}
BOOST_AUTO_TEST_CASE(PathConstructor_test) {
const path test_path{"toto/titi"};
BOOST_CHECK(test_path.is_relative());
BOOST_CHECK(test_path.filename() == "titi");
BOOST_CHECK(test_path.parent_path() == "toto");
const string test_str{"toto/tutu"};
const path test_path2{test_str};
BOOST_CHECK(test_path2.is_relative());
BOOST_CHECK(test_path2.string() == test_path2);
}
BOOST_AUTO_TEST_CASE(Recursion_test) {
const TempDir top_dir{"PathSearch_Recursion_test-%%%%%%%"};
const path top_dir_path = top_dir.path();
createTemporaryStructure(top_dir_path);
}
BOOST_AUTO_TEST_SUITE_END()
} // namespace Elements