From 4a416fd328d685b0946e5e6f40e5ea18e9c1adf0 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Thu, 9 Feb 2017 16:35:03 +0300 Subject: Add support for &dir/*[*][/] test path cleanup syntax --- build2/test/script/runner.cxx | 57 +++++++++++++- tests/test/config-test/testscript | 9 +-- tests/test/script/runner/cleanup.test | 139 +++++++++++++++++++++++++--------- 3 files changed, 160 insertions(+), 45 deletions(-) diff --git a/build2/test/script/runner.cxx b/build2/test/script/runner.cxx index ffb95e8..bc3c1ce 100644 --- a/build2/test/script/runner.cxx +++ b/build2/test/script/runner.cxx @@ -672,12 +672,14 @@ namespace build2 continue; const path& p (c.path); + const path& l (p.leaf ()); + const string& ls (l.string ()); // Remove the directory recursively if not current. Fail otherwise. // Recursive removal of non-existing directory is not an error for // 'maybe' cleanup type. // - if (p.leaf ().string () == "***") + if (ls == "***") { // Cast to uint16_t to avoid ambiguity with libbutl::rmdir_r(). // @@ -697,6 +699,56 @@ namespace build2 : " doesn't match a directory"); } + // Remove files or directories using wildcard. Removal of sub-entries + // of non-existing directory is not an error for 'maybe' cleanup + // type. + // + // Note that only the leaf part of the cleanup wildcard is a pattern + // in terms of libbutl::path_search(). + // + if (ls == "*" || ls == "**") + { + dir_path d (p.directory ()); + + if (t == cleanup_type::always && !dir_exists (d)) + fail (ll) << "registered for cleanup wildcard " << p + << " doesn't match a directory"; + + if (l.to_directory ()) + { + auto rm = [&p, &d, &ll] (path&& de) -> bool + { + dir_path sd (path_cast (d / de)); + + // We can get not_exist here due to racing conditions, but + // that's ok if somebody did our job. + // + rmdir_status r (rmdir (sd, 2)); + + if (r != rmdir_status::not_empty) + return true; + + fail (ll) << "registered for cleanup directory " << sd + << " is not empty" << + info << "wildcard: '" << p << "'" << endf; + }; + + path_search (l, rm, d); + } + else + { + auto rm = [&d] (path&& p) -> bool + { + rmfile (d / p, 2); // That's ok if not exists. + return true; + }; + + path_search (l, rm, d); + } + + continue; + } + // Remove the directory if exists and empty. Fail otherwise. Removal // of non-existing directory is not an error for 'maybe' cleanup // type. @@ -770,7 +822,8 @@ namespace build2 const path& p (cl.path); path np (normalize (p, sp, ll)); - bool wc (np.leaf ().string () == "***"); + const string& ls (np.leaf ().string ()); + bool wc (ls == "*" || ls == "**" || ls == "***"); const path& cp (wc ? np.directory () : np); const dir_path& wd (sp.root->wd_path); diff --git a/tests/test/config-test/testscript b/tests/test/config-test/testscript index 0851da7..2c262b7 100644 --- a/tests/test/config-test/testscript +++ b/tests/test/config-test/testscript @@ -7,8 +7,7 @@ test.options = --jobs 1 --quiet test.arguments = 'test(../proj/@./)' # Test out-of-src (for parallel). -#test.cleanups = &**/ # Cleanup out directory structure. -test.cleanups = &!./ #@@ TMP +test.cleanups = &**/ # Cleanup out directory structure. +mkdir proj +mkdir proj/build @@ -192,9 +191,3 @@ EOO $* config.test=tests/@{basics/baz/bar} >>EOO tests/script/basics/baz/bar EOO - -# @@ TMP HACK -# --rm -r all/ alias-pass/ alias-test/ alias-pass-test/ alias-pass-id-only/ \ - target-simple/ target-script/ id/ target-id/ target-ids/ id-group/ \ - id-in-group/ diff --git a/tests/test/script/runner/cleanup.test b/tests/test/script/runner/cleanup.test index 7b3539b..14e931e 100644 --- a/tests/test/script/runner/cleanup.test +++ b/tests/test/script/runner/cleanup.test @@ -144,47 +144,116 @@ b += --no-column : wildcard : { - : always + : self : - $c <'$* -d a/b -f a/b/c &a/***' && $b + { + : always + : + $c <'$* -d a/b -f a/b/c &a/***' && $b - : maybe - : - $c <'$* &?a/***' && $b + : maybe + : + $c <'$* &?a/***' && $b - : not-exists - : - : Test cleanup of a wildcard not matching any directory. - : - $c <'$* &a/***' && $b 2>>/EOE != 0 - testscript:1: error: registered for cleanup wildcard test/1/a/*** doesn't match a directory - EOE + : not-exists + : + : Test cleanup of a wildcard not matching any directory. + : + $c <'$* &a/***' && $b 2>>/EOE != 0 + testscript:1: error: registered for cleanup wildcard test/1/a/*** doesn't match a directory + EOE - : out-wd - : - : Test cleanup of a wildcard out of the testscript working directory. - : - $c <'$* &../../a/***' && $b 2>>/EOE != 0 - testscript:1: error: wildcard cleanup ../../a/*** is out of working directory test/ - EOE + : out-wd + : + : Test cleanup of a wildcard out of the testscript working directory. + : + $c <'$* &../../a/***' && $b 2>>/EOE != 0 + testscript:1: error: wildcard cleanup ../../a/*** is out of working directory test/ + EOE - : in-wd - : - : Test cleanup registration of a wildcard matching the directory that being - : outside the test working directory is inside the testscript working - : directory. - : - $c <'$* &../a/***' && $b 2>>/EOE != 0 - testscript:1: error: registered for cleanup wildcard test/a/*** doesn't match a directory - EOE + : in-wd + : + : Test cleanup registration of a wildcard matching the directory that being + : outside the test working directory is inside the testscript working + : directory. + : + $c <'$* &../a/***' && $b 2>>/EOE != 0 + testscript:1: error: registered for cleanup wildcard test/a/*** doesn't match a directory + EOE - : not-dir - : - : Test cleanup of a file as a wildcard. - : - $c <'$* -f a &a/***' && $b 2>>/~%EOE% != 0 - %error: unable to remove directory test/1/a/: .+% - EOE + : not-dir + : + : Test cleanup of a file as a wildcard. + : + $c <'$* -f a &a/***' && $b 2>>/~%EOE% != 0 + %error: unable to remove directory test/1/a/: .+% + EOE + } + + : dir + : + { + : always + : + { + : immediate + : + $c <'$* -d a/b &a/ &a/*/' && $b + + : recursive + : + $c <'$* -d a/b/c &a/ &a/**/' && $b + } + + : maybe + : + $c <'$* &?a/**/' && $b + + : not-exists + : + : Test cleanup of a wildcard that doesn't match any directory. + : + $c <'$* &a/**/' && $b 2>>/EOE != 0 + testscript:1: error: registered for cleanup wildcard test/1/a/**/ doesn't match a directory + EOE + + : not-dir + : + : Test cleanup of a file as a directory wildcard. + : + $c <'$* -f a &a/**/' && $b 2>>/EOE != 0 + testscript:1: error: registered for cleanup wildcard test/1/a/**/ doesn't match a directory + EOE + + : not-empty + : + : Test cleanup of a non-empty directory as a wildcard. + : + $c <'$* -d a/b/c -f a/b/d &a/**/' && $b 2>>/EOE != 0 + testscript:1: error: registered for cleanup directory test/1/a/b/ is not empty + info: wildcard: 'test/1/a/**/' + EOE + } + + : file + : + { + : always + : + { + : immediate + : + $c <'$* -d a -f a/c &a/ &a/*' && $b + + : recursive + : + $c <'$* -d a/b -f a/c -f a/b/e &a/ &a/b/ &a/**' && $b + } + + : maybe + : + $c <'$* &?a/**' && $b + } } : order -- cgit v1.1