aboutsummaryrefslogtreecommitdiff
path: root/tests/process-run/driver.cxx
blob: 3a6fe87ae94efcbfb3ca20f933935729604a60ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// file      : tests/process-run/driver.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2018 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <cassert>

#ifndef __cpp_lib_modules
#include <string>
#include <iostream>
#endif

// Other includes.

#ifdef __cpp_modules
#ifdef __cpp_lib_modules
import std.core;
import std.io;
#endif
import butl.path;
import butl.process;
import butl.optional; // @@ MOD Clang shouldn't be needed.
import butl.fdstream;
import butl.small_vector;
#else
#include <libbutl/path.mxx>
#include <libbutl/process.mxx>
#include <libbutl/fdstream.mxx>
#include <libbutl/small-vector.mxx>
#endif

using namespace std;
using namespace butl;

template <typename I,
          typename O,
          typename E,
          typename... A>
process_exit
run (I&& in,
     O&& out,
     E&& err,
     const process_env& env,
     A&&... args)
{
  return process_run (forward<I> (in),
                      forward<O> (out),
                      forward<E> (err),
                      env,
                      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_callback ([] (const char* c[], size_t n)
                        {
                          process::print (cout, c, n);
                          cout << endl;
                        },
                        0, 1, 2,
                        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"));

  {
    fdpipe pipe (fdopen_pipe ());
    process pr (process_start (pipe, process::pipe (-1, 1), 2, p, "-c", "-i"));
    pipe.close ();
    assert (pr.wait ());
  }

  // 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));
}