From 550b5257aba507bcce98f6832b8905769a14955d Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 10 Apr 2017 13:19:19 +0200 Subject: Add process_run()/process_start() higher-level API on top of class process --- tests/process-run/buildfile | 7 ++++ tests/process-run/driver.cxx | 91 ++++++++++++++++++++++++++++++++++++++++++++ tests/process-run/testscript | 14 +++++++ 3 files changed, 112 insertions(+) create mode 100644 tests/process-run/buildfile create mode 100644 tests/process-run/driver.cxx create mode 100644 tests/process-run/testscript (limited to 'tests/process-run') diff --git a/tests/process-run/buildfile b/tests/process-run/buildfile new file mode 100644 index 0000000..5c55af9 --- /dev/null +++ b/tests/process-run/buildfile @@ -0,0 +1,7 @@ +# file : tests/process-run/buildfile +# copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +# license : MIT; see accompanying LICENSE file + +exe{driver}: cxx{driver} ../../butl/lib{butl} test{testscript} + +include ../../butl/ 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 + +#include +#include +#include + +using namespace std; +using namespace butl; + +template +process_exit +run (I&& in, + O&& out, + E&& err, + const P& p, + A&&... args) +{ + return process_run (forward (in), + forward (out), + forward (err), + dir_path (), + p, + forward (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)); +} diff --git a/tests/process-run/testscript b/tests/process-run/testscript new file mode 100644 index 0000000..49abac4 --- /dev/null +++ b/tests/process-run/testscript @@ -0,0 +1,14 @@ +# file : tests/process-run/testscript +# copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +# license : MIT; see accompanying LICENSE file + +$* -p >>"EOO" 2>>EOE +$0 -c +abc +abc +abc +abc +123 +EOO +abc +EOE -- cgit v1.1