From 330820504e2f231b9e0255142147e2b7939bce0b Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Mon, 20 Mar 2017 18:26:51 +0300 Subject: Add support for --no-cleanup testscript builtin option --- build2/test/script/builtin.cxx | 85 +++++++++++++++++++++++++++++++----------- doc/testscript.cli | 26 +++++++------ 2 files changed, 78 insertions(+), 33 deletions(-) diff --git a/build2/test/script/builtin.cxx b/build2/test/script/builtin.cxx index 336fa72..5b011e3 100644 --- a/build2/test/script/builtin.cxx +++ b/build2/test/script/builtin.cxx @@ -273,6 +273,7 @@ namespace build2 static void cpfile (scope& sp, const path& from, const path& to, + bool cleanup, const function& fail) { try @@ -282,7 +283,7 @@ namespace build2 cpfile (from, to, cpflags::overwrite_permissions | cpflags::overwrite_content); - if (!exists) + if (!exists && cleanup) sp.clean ({cleanup_type::always, to}, true); } catch (const system_error& e) @@ -300,6 +301,7 @@ namespace build2 static void cpdir (scope& sp, const dir_path& from, const dir_path& to, + bool cleanup, const function& fail) { try @@ -307,7 +309,8 @@ namespace build2 if (try_mkdir (to) == mkdir_status::already_exists) throw system_error (EEXIST, system_category ()); - sp.clean ({cleanup_type::always, to}, true); + if (cleanup) + sp.clean ({cleanup_type::always, to}, true); for (const auto& de: dir_iterator (from)) // Can throw. { @@ -318,9 +321,10 @@ namespace build2 cpdir (sp, path_cast (move (f)), path_cast (move (t)), + cleanup, fail); else - cpfile (sp, f, t, fail); + cpfile (sp, f, t, cleanup, fail); } } catch (const system_error& e) @@ -330,10 +334,10 @@ namespace build2 } } - // cp - // cp -R|-r - // cp ... / - // cp -R|-r ... / + // cp [--no-cleanup] + // cp [--no-cleanup] -R|-r + // cp [--no-cleanup] ... / + // cp [--no-cleanup] -R|-r ... / // // Note: can be executed synchronously. // @@ -362,13 +366,18 @@ namespace build2 // Process options. // bool recursive (false); + bool cleanup (true); for (; i != e; ++i) { - if (*i == "-R" || *i == "-r") + const string& o (*i); + + if (o == "-R" || o == "-r") recursive = true; + else if (o == "--no-cleanup") + cleanup = false; else { - if (*i == "--") + if (o == "--") ++i; break; @@ -410,12 +419,13 @@ namespace build2 if (!recursive) // Synopsis 1: make a file copy at the specified path. // - cpfile (sp, src, dst, fail); + cpfile (sp, src, dst, cleanup, fail); else // Synopsis 2: make a directory copy at the specified path. // cpdir (sp, path_cast (src), path_cast (dst), + cleanup, fail); } else @@ -432,12 +442,13 @@ namespace build2 cpdir (sp, path_cast (src), path_cast (dst / src.leaf ()), + cleanup, fail); else // Synopsis 3: copy a file into the specified directory. Also, // here we cover synopsis 4 for the source path being a file. // - cpfile (sp, src, dst / src.leaf (), fail); + cpfile (sp, src, dst / src.leaf (), cleanup, fail); } } @@ -533,19 +544,21 @@ namespace build2 // directories for cleanup. The directory path must be absolute. // static void - mkdir_p (scope& sp, const dir_path& p) + mkdir_p (scope& sp, const dir_path& p, bool cleanup) { if (!dir_exists (p)) { if (!p.root ()) - mkdir_p (sp, p.directory ()); + mkdir_p (sp, p.directory (), cleanup); try_mkdir (p); // Returns success or throws. - sp.clean ({cleanup_type::always, p}, true); + + if (cleanup) + sp.clean ({cleanup_type::always, p}, true); } } - // mkdir [-p] ... + // mkdir [--no-cleanup] [-p] ... // // Note that POSIX doesn't specify if after a directory creation failure // the command should proceed with the rest of the arguments. The current @@ -578,10 +591,15 @@ namespace build2 // Process options. // bool parent (false); + bool cleanup (true); for (; i != e; ++i) { - if (*i == "-p") + const string& o (*i); + + if (o == "-p") parent = true; + else if (o == "--no-cleanup") + cleanup = false; else { if (*i == "--") @@ -603,9 +621,12 @@ namespace build2 try { if (parent) - mkdir_p (sp, p); + mkdir_p (sp, p, cleanup); else if (try_mkdir (p) == mkdir_status::success) - sp.clean ({cleanup_type::always, p}, true); + { + if (cleanup) + sp.clean ({cleanup_type::always, p}, true); + } else // == mkdir_status::already_exists throw system_error (EEXIST, system_category ()); } @@ -1210,7 +1231,7 @@ namespace build2 return 2; } - // touch ... + // touch [--no-cleanup] ... // // Note that POSIX doesn't specify the behavior for touching an entry // other than file. @@ -1243,9 +1264,30 @@ namespace build2 if (args.empty ()) error () << "missing file"; + auto i (args.begin ()); + auto e (args.end ()); + + // Process options. + // + bool cleanup (true); + for (; i != e; ++i) + { + const string& o (*i); + + if (o == "--no-cleanup") + cleanup = false; + else + { + if (o == "--") + ++i; + + break; + } + } + // Create files. // - for (auto i (args.begin ()); i != args.end (); ++i) + for (; i != e; ++i) { path p (parse_path (*i, sp.wd_path)); @@ -1278,7 +1320,8 @@ namespace build2 error () << "cannot create file '" << p << "': " << e; } - sp.clean ({cleanup_type::always, p}, true); + if (cleanup) + sp.clean ({cleanup_type::always, p}, true); } else error () << "'" << p << "' exists and is not a file"; diff --git a/doc/testscript.cli b/doc/testscript.cli index 078f674..bbe1a9c 100644 --- a/doc/testscript.cli +++ b/doc/testscript.cli @@ -2220,10 +2220,10 @@ Read files in order and write their contents to \c{stdout}. Read from \h#builtins-cp|\c{cp}| \ -cp -cp -R|-r -cp ... / -cp -R|-r ... / +cp [--no-cleanup] +cp [--no-cleanup] -R|-r +cp [--no-cleanup] ... / +cp [--no-cleanup] -R|-r ... / \ Copy files and/or directories. The first two forms make a copy of a single @@ -2283,8 +2283,9 @@ files/directories does not exist or if the \i{dst-dir} filesystem entry does not exist or is not a directory. For a \i{src-path} directory \c{cp} also fails if the \i{dst-dir/src-name} filesystem entry already exists. -Newly created files and directories that are inside the script working -directory are automatically registered for cleanup. +Unless the --no-cleanup option is specified, newly created files and +directories that are inside the script working directory are automatically +registered for cleanup. \h#builtins-diff|\c{diff}| @@ -2323,7 +2324,7 @@ Do nothing and terminate normally with the 1 exit code (indicating failure). \h#builtins-mkdir|\c{mkdir}| \ -mkdir [-p] ... +mkdir [--no-cleanup] [-p] ... \ Create directories. Unless the \c{-p} option is specified, all the leading @@ -2336,8 +2337,9 @@ directories must exist and the directory itself must not exist. Create missing leading directories and ignore directories that already exist.|| -Newly created directories (including the leading ones) that are inside the -script working directory are automatically registered for cleanup. +Unless the --no-cleanup option is specified, newly created directories +(including the leading ones) that are inside the script working directory are +automatically registered for cleanup. \h#builtins-rm|\c{rm}| @@ -2547,15 +2549,15 @@ Note that tests dereference symbolic links. \h#builtins-touch|\c{touch}| \ -touch ... +touch [--no-cleanup] ... \ Change file access and modification times to the current time. Create files that do not exist. Fail if a filesystem entry other than the file exists for the specified name. -Newly created files that are inside the script working directory are -automatically registered for cleanup. +Unless the --no-cleanup option is specified, newly created files that are +inside the script working directory are automatically registered for cleanup. \h#builtins-true|\c{true}| -- cgit v1.1