-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.cpp
147 lines (118 loc) · 5.83 KB
/
test.cpp
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* Testing for the subprocess library.
* Uses the Catch2 testing library (https://github.com/catchorg/Catch2)
*/
#define CATCH_CONFIG_MAIN // This tells Catch to provide a main() - only do this in one cpp file
#include "catch.hpp"
#include "subprocess.hpp"
TEST_CASE("basic echo execution", "[subprocess::execute]") {
std::list<std::string> inputs;
std::vector<std::string> outputs;
subprocess::execute("/bin/echo", {"hello"}, inputs, [&](std::string s) { outputs.push_back(s); });
REQUIRE(outputs.size() == 1);
// echo appends a newline by default
REQUIRE(outputs.front() == "hello\n");
}
TEST_CASE("no trailing output newline echo execution", "[subprocess::execute]") {
std::list<std::string> inputs;
std::vector<std::string> outputs;
subprocess::execute("/bin/echo", {"-n", "hello"}, inputs, [&](std::string s) { outputs.push_back(s); });
REQUIRE(outputs.size() == 1);
REQUIRE(outputs.front() == "hello");
}
TEST_CASE("non existent executable", "[subprocess::execute]") {
// try and run a non-existent executable, what should happen..?
std::list<std::string> inputs;
std::vector<std::string> outputs;
int retval = subprocess::execute("/bin/wangwang", {}, inputs,
[](std::string) { FAIL("this functor should never have been called"); });
// process should have failed..?
REQUIRE(retval != 0);
REQUIRE(outputs.size() == 0);
}
TEST_CASE("stdin execute simple cat", "[subprocess::execute]") {
std::list<std::string> inputs = {"henlo wurld\n", "1,2,3,4\n"};
std::vector<std::string> outputs;
int retval = subprocess::execute("/bin/cat", {}, inputs, [&](std::string s) { outputs.push_back(s); });
REQUIRE(retval == 0);
REQUIRE(outputs.size() == 2);
REQUIRE(outputs.at(0) == "henlo wurld\n");
REQUIRE(outputs.at(1) == "1,2,3,4\n");
}
TEST_CASE("stdin execute simple cat no trailing newline for last input", "[subprocess::execute]") {
// executing a command with the last one missing a newline still should work the same, as the stdin stream gets closed.
std::list<std::string> inputs = {"henlo wurld\n", "1,2,3,4"};
std::vector<std::string> outputs;
int retval = subprocess::execute("/bin/cat", {}, inputs, [&](std::string s) { outputs.push_back(s); });
REQUIRE(retval == 0);
REQUIRE(outputs.size() == 2);
REQUIRE(outputs.at(0) == "henlo wurld\n");
REQUIRE(outputs.at(1) == "1,2,3,4");
}
TEST_CASE("checkOutput simple case cat", "[subprocess::checkOutput]") {
// execute bc and pass it some equations
std::list<std::string> inputs = {"1+1\n", "2^333\n", "32-32\n"};
// this one is interesting, there's more than one line of stdout for each input (bc line breaks after a certain number of characters)
std::vector<std::string> output_expected = {"2\n", "17498005798264095394980017816940970922825355447145699491406164851279\\\n", "623993595007385788105416184430592\n", "0\n"};
int retval;
std::vector<std::string> out = subprocess::checkOutput("/usr/bin/bc", {}, inputs, retval);
REQUIRE(retval == 0);
REQUIRE(out.size() == output_expected.size());
REQUIRE(out == output_expected);
}
TEST_CASE("asynchronous is actually asynchronous", "[subprocess::async]") {
std::list<std::string> inputs;
std::vector<std::string> outputs;
std::atomic<bool> isDone(false);
std::future<int> retval = subprocess::async("/usr/bin/time", {"sleep", "3"}, inputs, [&](std::string s) { isDone.store(true); outputs.push_back(s); });
// reaching here after the async starts, means that we prrroooooobbbaabbbly (unless the user has a very, very slow computer) won't be finished
REQUIRE(isDone.load() == false);
REQUIRE(retval.get() == 0);
// time has different outputs for different OSes, pluuus they will take different times to complete. all we need is some stdout.
REQUIRE(outputs.size() > 0);
}
TEST_CASE("output iterator contains everything", "[subprocess::ProcessStream]") {
// stream output from a process
std::list<std::string> inputs = {"12232\n", "hello, world\n", "Hello, world\n", "line: Hello, world!\n"};
subprocess::ProcessStream ps("/bin/grep", {"-i", "^Hello, world$"}, inputs);
std::vector<std::string> expectedOutput = {"hello, world\n", "Hello, world\n"};
std::vector<std::string> outputs;
for (std::string out : ps) {
outputs.push_back(out);
}
REQUIRE(outputs == expectedOutput);
}
TEST_CASE("output iterator handles empty output", "[subprocess::ProcessStream]") {
std::list<std::string> inputs = {"12232\n", "hello, world\n", "Hello, world\n", "line: Hello, world!\n"};
subprocess::ProcessStream ps("/bin/grep", {"-i", "^bingo bango bongo$"}, inputs);
std::vector<std::string> expectedOutput = {};
std::vector<std::string> outputs;
for (std::string out : ps) {
FAIL("why do we have output!!! - " << out);
outputs.push_back(out);
}
REQUIRE(outputs == expectedOutput);
}
TEST_CASE("output iterator all operator overload testing", "[subprocess::ProcessStream]") {
// stream output from a process
std::list<std::string> inputs = {"12232\n", "hello, world\n", "Hello, world\n", "line: Hello, world!\n"};
subprocess::ProcessStream ps("/bin/grep", {"-i", "Hello, world"}, inputs);
std::list<std::string> expectedOutput = {"hello, world\n", "Hello, world\n", "line: Hello, world!\n"};
auto beg = ps.begin();
auto end = ps.end();
REQUIRE(beg != end);
REQUIRE(*beg == expectedOutput.front());
expectedOutput.pop_front();
REQUIRE(beg != end);
++beg;
REQUIRE(*beg == expectedOutput.front());
expectedOutput.pop_front();
REQUIRE(beg != end);
beg++;
REQUIRE(*beg == expectedOutput.front());
expectedOutput.pop_front();
beg++;
REQUIRE(beg == end);
REQUIRE(expectedOutput.size() == 0);
}
// TODO: write more test cases (this seems pretty covering, let's see how coverage looks)