From b946e380d4e414cec85082ebe67c8ffed6579277 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Wed, 16 May 2018 22:20:49 +0300 Subject: Add ignore_dangling parameter to dir_iterator() ctor --- tests/dir-iterator/driver.cxx | 54 ++++++++++++++++++++++++++++++++----------- tests/dir-iterator/testscript | 19 +++++++++++++-- 2 files changed, 57 insertions(+), 16 deletions(-) (limited to 'tests/dir-iterator') diff --git a/tests/dir-iterator/driver.cxx b/tests/dir-iterator/driver.cxx index 1adef6f..bad9e58 100644 --- a/tests/dir-iterator/driver.cxx +++ b/tests/dir-iterator/driver.cxx @@ -41,45 +41,71 @@ operator<< (ostream& os, entry_type e) return os << entry_type_string[static_cast (e)]; } -// @@ Should we make the test silent unless -v arg passed. In silen mode could -// compare the output with a set of predefined dir entries. +// Usage: argv[0] [-v] [-i] +// +// Iterates over a directory filesystem sub-entries, obtains their types and +// target types for symlinks. +// +// -v +// Print the filesystem entries types and names to STDOUT. +// +// -i +// Ignore dangling symlinks, rather than fail trying to obtain the target +// type. // int main (int argc, const char* argv[]) { - if (!(argc == 2 || (argc == 3 && argv[1] == string ("-v")))) + assert (argc > 0); + + bool verbose (false); + bool ignore_dangling (false); + + int i (1); + for (; i != argc; ++i) + { + string v (argv[i]); + + if (v == "-v") + verbose = true; + else if (v == "-i") + ignore_dangling = true; + else + break; + } + + if (i != argc - 1) { - cerr << "usage: " << argv[0] << " [-v] " << endl; + cerr << "usage: " << argv[0] << " [-v] [-i] " << endl; return 1; } - bool v (argc == 3); - const char* d (argv[argc - 1]); + const char* d (argv[i]); try { - for (const dir_entry& de: dir_iterator (dir_path (d))) + for (const dir_entry& de: dir_iterator (dir_path (d), ignore_dangling)) { entry_type lt (de.ltype ()); - entry_type t (lt == entry_type::symlink ? de.ltype () : lt); + entry_type t (lt == entry_type::symlink ? de.type () : lt); const path& p (de.path ()); - if (v) + if (verbose) { - cerr << lt << " "; + cout << lt << " "; if (lt == entry_type::symlink) - cerr << t; + cout << t; else - cerr << " "; + cout << " "; - cerr << " " << p << endl; + cout << " " << p << endl; } } } catch (const exception& e) { - cerr << argv[1] << ": " << e << endl; + cerr << e << endl; return 1; } } diff --git a/tests/dir-iterator/testscript b/tests/dir-iterator/testscript index 75a1ca6..f592c78 100644 --- a/tests/dir-iterator/testscript +++ b/tests/dir-iterator/testscript @@ -8,9 +8,24 @@ test.options = -v : mkdir a; touch a/b; -$* a 2>"reg b" +$* a >"reg b" : dir : mkdir -p a/b; -$* a 2>"dir b" +$* a >"dir b" + +: dangling-link +: +if ($cxx.target.class != 'windows') +{ + +mkdir a + +touch --no-cleanup a/b + +ln -s a/b a/l + +rm a/b + + +touch a/c + + $* ../a >! 2>! != 0 : keep + $* -i ../a >'reg c' : skip +} -- cgit v1.1