174TEST_CASE(
"Getting a list of files",
"[ioutils::fileUtils]" )
176 std::string basedir =
"/tmp/fileUtils_test/dir";
177 createfiles( basedir );
179 SECTION(
"a directory with files of various type and names" )
181 SECTION(
"directory only" )
183 std::vector<std::string> fnames;
187 REQUIRE( fnames.size() == 10 );
190 SECTION(
"single extension with ." )
192 std::vector<std::string> fnames;
196 REQUIRE( fnames.size() == 5 );
197 REQUIRE( fnames[0] == basedir +
"/elif_txt_3.txt" );
198 REQUIRE( fnames[1] == basedir +
"/elif_txt_5.txt" );
199 REQUIRE( fnames[2] == basedir +
"/file_txt_1.txt" );
200 REQUIRE( fnames[3] == basedir +
"/file_xtx_2.txt" );
201 REQUIRE( fnames[4] == basedir +
"/file_xtx_4.txt" );
204 SECTION(
"single extension without ." )
206 std::vector<std::string> fnames;
210 REQUIRE( fnames.size() == 5 );
211 REQUIRE( fnames[0] == basedir +
"/elif_txt_3.txt" );
212 REQUIRE( fnames[1] == basedir +
"/elif_txt_5.txt" );
213 REQUIRE( fnames[2] == basedir +
"/file_txt_1.txt" );
214 REQUIRE( fnames[3] == basedir +
"/file_xtx_2.txt" );
215 REQUIRE( fnames[4] == basedir +
"/file_xtx_4.txt" );
218 SECTION(
"different extension with ." )
220 std::vector<std::string> fnames;
224 REQUIRE( fnames.size() == 5 );
225 REQUIRE( fnames[0] == basedir +
"/elif_txt_2.xxx" );
226 REQUIRE( fnames[1] == basedir +
"/elif_txt_4.xxx" );
227 REQUIRE( fnames[2] == basedir +
"/elif_xtx_1.xxx" );
228 REQUIRE( fnames[3] == basedir +
"/file_xtx_3.xxx" );
229 REQUIRE( fnames[4] == basedir +
"/file_xtx_5.xxx" );
232 SECTION(
"a prefix, no extension" )
234 std::vector<std::string> fnames;
238 REQUIRE( fnames.size() == 5 );
239 REQUIRE( fnames[0] == basedir +
"/file_txt_1.txt" );
240 REQUIRE( fnames[1] == basedir +
"/file_xtx_2.txt" );
241 REQUIRE( fnames[2] == basedir +
"/file_xtx_3.xxx" );
242 REQUIRE( fnames[3] == basedir +
"/file_xtx_4.txt" );
243 REQUIRE( fnames[4] == basedir +
"/file_xtx_5.xxx" );
246 SECTION(
"a prefix, with extension with ." )
248 std::vector<std::string> fnames;
252 REQUIRE( fnames.size() == 3 );
253 REQUIRE( fnames[0] == basedir +
"/file_txt_1.txt" );
254 REQUIRE( fnames[1] == basedir +
"/file_xtx_2.txt" );
255 REQUIRE( fnames[2] == basedir +
"/file_xtx_4.txt" );
258 SECTION(
"a substr alone" )
260 std::vector<std::string> fnames;
264 REQUIRE( fnames.size() == 5 );
265 REQUIRE( fnames[0] == basedir +
"/elif_xtx_1.xxx" );
266 REQUIRE( fnames[1] == basedir +
"/file_xtx_2.txt" );
267 REQUIRE( fnames[2] == basedir +
"/file_xtx_3.xxx" );
268 REQUIRE( fnames[3] == basedir +
"/file_xtx_4.txt" );
269 REQUIRE( fnames[4] == basedir +
"/file_xtx_5.xxx" );
272 SECTION(
"a substr which is actually the prefix" )
274 std::vector<std::string> fnames;
278 REQUIRE( fnames.size() == 0 );
281 SECTION(
"a prefix and a substr which is actually the prefix" )
283 std::vector<std::string> fnames;
287 REQUIRE( fnames.size() == 0 );
290 SECTION(
"a prefix, and a substr, no extension" )
292 std::vector<std::string> fnames;
296 REQUIRE( fnames.size() == 4 );
297 REQUIRE( fnames[0] == basedir +
"/file_xtx_2.txt" );
298 REQUIRE( fnames[1] == basedir +
"/file_xtx_3.xxx" );
299 REQUIRE( fnames[2] == basedir +
"/file_xtx_4.txt" );
300 REQUIRE( fnames[3] == basedir +
"/file_xtx_5.xxx" );
303 SECTION(
"a prefix, a substr, and an extension with ." )
305 std::vector<std::string> fnames;
309 REQUIRE( fnames.size() == 2 );
310 REQUIRE( fnames[0] == basedir +
"/file_xtx_2.txt" );
311 REQUIRE( fnames[1] == basedir +
"/file_xtx_4.txt" );
315 SECTION(
"a directory which does not exist" )
317 std::vector<std::string> fnames;
323 SECTION(
"a directory which is a file" )
325 std::vector<std::string> fnames;
331 SECTION(
"a directory which does not exist, verbose = vv" )
333 std::vector<std::string> fnames;
334 mx::error_t errc = mx::ioutils::getFileNames<mx::verbose::vv>( fnames, basedir +
"nf",
"file",
"xtx",
".txt" );
339 SECTION(
"a directory which is a file, verbose = vv" )
341 std::vector<std::string> fnames;
343 mx::ioutils::getFileNames<mx::verbose::vv>( fnames, basedir +
"/file_xtx_2.txt",
"file",
"xtx",
".txt" );
348 SECTION(
"a directory which does not exist, verbose = v" )
350 std::vector<std::string> fnames;
351 mx::error_t errc = mx::ioutils::getFileNames<mx::verbose::v>( fnames, basedir +
"nf",
"file",
"xtx",
".txt" );
356 SECTION(
"a directory which is a file, verbose = v" )
358 std::vector<std::string> fnames;
360 mx::ioutils::getFileNames<mx::verbose::v>( fnames, basedir +
"/file_xtx_2.txt",
"file",
"xtx",
".txt" );
365 SECTION(
"a directory which does not exist, verbose = o" )
367 std::vector<std::string> fnames;
368 mx::error_t errc = mx::ioutils::getFileNames<mx::verbose::o>( fnames, basedir +
"nf",
"file",
"xtx",
".txt" );
373 SECTION(
"a directory which is a file, verbose = o" )
375 std::vector<std::string> fnames;
377 mx::ioutils::getFileNames<mx::verbose::o>( fnames, basedir +
"/file_xtx_2.txt",
"file",
"xtx",
".txt" );
error_t getFileNames(std::vector< std::string > &fileNames, const std::string &directory, const std::string &prefix, const std::string &substr, const std::string &extension)
Get a list of file names from the specified directory, specifying a prefix, a substring to match,...