From 4564a26c0b88d684c12c396d7ef5b0e66f686964 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Wed, 13 Oct 2021 20:05:27 +0300 Subject: Add --cwd|-t option to env pseudo-builtin --- tests/test/script/runner/driver.cxx | 132 +++++++++++++++++--------------- tests/test/script/runner/env.testscript | 34 ++++++++ 2 files changed, 106 insertions(+), 60 deletions(-) (limited to 'tests') diff --git a/tests/test/script/runner/driver.cxx b/tests/test/script/runner/driver.cxx index 9c05e27..b84f167 100644 --- a/tests/test/script/runner/driver.cxx +++ b/tests/test/script/runner/driver.cxx @@ -17,6 +17,7 @@ #include #include +#include #include #include #include @@ -45,8 +46,8 @@ main (int argc, char* argv[]) // Usage: driver [-i ] (-o )* (-e )* (-f )* // (-d )* (-v )* [(-t (a|m|s|z)) | (-s )] // - // Execute actions specified by -i, -o, -e, -f, -d, -v, and -l options in - // the order as they appear on the command line. After that terminate + // Execute actions specified by -i, -o, -e, -f, -d, -w, -v, and -l options + // in the order as they appear on the command line. After that terminate // abnormally if -t option is provided, otherwise exit normally with the // status specified by -s option (0 by default). // @@ -67,6 +68,9 @@ main (int argc, char* argv[]) // Create a directory with the path specified. Create parent directories // if required. // + // -w + // Print CWD to stdout. + // // -v // If the specified variable is set then print its value to stdout and // the string '' otherwise. @@ -95,10 +99,7 @@ main (int argc, char* argv[]) for (int i (1); i < argc; ++i) { - string o (argv[i++]); - assert (i < argc); - - string v (argv[i]); + string o (argv[i]); auto toi = [] (const string& s) -> int { @@ -118,69 +119,80 @@ main (int argc, char* argv[]) return r; }; - if (o == "-i") + if (o == "-w") { - assert (ifd == 3); // Make sure is not set yet. - - ifd = toi (v); - assert (ifd >= 0 && ifd < 3); + cout << dir_path::current_directory () << endl; + } + else // Handle options other than flags. + { + ++i; + assert (i < argc); + string v (argv[i]); - if (ifd == 0) - cin.ignore (numeric_limits::max ()); - else if (cin.peek () != istream::traits_type::eof ()) + if (o == "-i") { - ostream& o (ifd == 1 ? cout : cerr); - o << cin.rdbuf (); - o.flush (); + assert (ifd == 3); // Make sure is not set yet. + + ifd = toi (v); + assert (ifd >= 0 && ifd < 3); + + if (ifd == 0) + cin.ignore (numeric_limits::max ()); + else if (cin.peek () != istream::traits_type::eof ()) + { + ostream& o (ifd == 1 ? cout : cerr); + o << cin.rdbuf (); + o.flush (); + } } - } - else if (o == "-o") - { - cout << v << endl; - } - else if (o == "-e") - { - cerr << v << endl; - } - else if (o == "-f") - { - ofdstream os (v); - os.close (); - } - else if (o == "-d") - { - try_mkdir_p (dir_path (v)); - } - else if (o == "-v") - { - optional var (getenv (v)); - cout << (var ? *var : "") << endl; - } - else if (o == "-l") - { - size_t t (toi (v)); + else if (o == "-o") + { + cout << v << endl; + } + else if (o == "-e") + { + cerr << v << endl; + } + else if (o == "-f") + { + ofdstream os (v); + os.close (); + } + else if (o == "-d") + { + try_mkdir_p (dir_path (v)); + } + else if (o == "-v") + { + optional var (getenv (v)); + cout << (var ? *var : "") << endl; + } + else if (o == "-l") + { + size_t t (toi (v)); - // MinGW GCC 4.9 doesn't implement this_thread so use Win32 Sleep(). - // + // MinGW GCC 4.9 doesn't implement this_thread so use Win32 Sleep(). + // #ifndef _WIN32 - this_thread::sleep_for (chrono::seconds (t)); + this_thread::sleep_for (chrono::seconds (t)); #else - Sleep (static_cast (t * 1000)); + Sleep (static_cast (t * 1000)); #endif + } + else if (o == "-t") + { + assert (aterm == '\0' && !status); // Make sure exit method is not set. + assert (v.size () == 1 && v.find_first_of ("amsz") != string::npos); + aterm = v[0]; + } + else if (o == "-s") + { + assert (!status && aterm == '\0'); // Make sure exit method is not set. + status = toi (v); + } + else + assert (false); } - else if (o == "-t") - { - assert (aterm == '\0' && !status); // Make sure exit method is not set. - assert (v.size () == 1 && v.find_first_of ("amsz") != string::npos); - aterm = v[0]; - } - else if (o == "-s") - { - assert (!status && aterm == '\0'); // Make sure exit method is not set. - status = toi (v); - } - else - assert (false); } switch (aterm) diff --git a/tests/test/script/runner/env.testscript b/tests/test/script/runner/env.testscript index ef90c3b..512139a 100644 --- a/tests/test/script/runner/env.testscript +++ b/tests/test/script/runner/env.testscript @@ -3,6 +3,40 @@ .include ../common.testscript +: cwd +: +{ + : not-exist + : + $c <'env -c a -- $* -w' && $b 2>>/~%EOE% != 0 + %testscript:1:1: error: specified working directory .+/a/ does not exist% + info: test id: 1 + EOE + + : process + : + $c </~%.+/a% + EOI + + : builtin + : + $c <