diff options
Diffstat (limited to 'tests/process-run/driver.cxx')
-rw-r--r-- | tests/process-run/driver.cxx | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/tests/process-run/driver.cxx b/tests/process-run/driver.cxx new file mode 100644 index 0000000..15d5f61 --- /dev/null +++ b/tests/process-run/driver.cxx @@ -0,0 +1,91 @@ +// file : tests/process-run/driver.cxx -*- C++ -*- +// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#include <iostream> + +#include <butl/path> +#include <butl/process> +#include <butl/fdstream> + +using namespace std; +using namespace butl; + +template <typename I, + typename O, + typename E, + typename P, + typename... A> +process_exit +run (I&& in, + O&& out, + E&& err, + const P& p, + A&&... args) +{ + return process_run (forward<I> (in), + forward<O> (out), + forward<E> (err), + dir_path (), + p, + forward<A> (args)...); +} + +int +main (int argc, const char* argv[]) +{ + if (argc < 2) // No argument test. + return 0; + + string a (argv[1]); + + if (a == "-c") + { + // -i read from stdin + // -o write argument to stdout + // -e write argument to stderr + // -x exit with argument + // + for (int i (2); i != argc; ++i) + { + a = argv[i]; + + if (a == "-i") cin >> a; + else if (a == "-o") cout << argv[++i] << endl; + else if (a == "-e") cerr << argv[++i] << endl; + else if (a == "-x") return atoi (argv[++i]); + } + + return 0; + } + else + assert (a == "-p"); + + const string p (argv[0]); + + assert (run (0, 1, 2, p)); + assert (run (0, 1, 2, p, "-c")); + + process_run ([] (const char* c[], size_t n) + { + process::print (cout, c, n); + cout << endl; + }, + 0, 1, 2, + dir_path (), + p, + "-c"); + + // Stream conversion and redirection. + // + assert (run (fdnull (), 1, 2, p, "-c", "-i")); + assert (run (fdnull (), 2, 2, p, "-c", "-o", "abc")); + assert (run (fdnull (), 1, 1, p, "-c", "-e", "abc")); + + // Argument conversion. + // + assert (run (0, 1, 2, p, "-c", "-o", "abc")); + assert (run (0, 1, 2, p, "-c", "-o", string ("abc"))); + assert (run (0, 1, 2, p, "-c", "-o", path ("abc"))); + assert (run (0, 1, 2, p, "-c", "-o", 123)); +} |