Repository navigation
Expand file tree
/
Copy pathsubprocess_tests.cpp
More file actions
70 lines (54 loc) · 2.18 KB
/
Copy pathsubprocess_tests.cpp
File metadata and controls
70 lines (54 loc) · 2.18 KB
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
#include <gtest/gtest.h>
#include "subprocess.h"
using namespace subprocess;
TEST(SubprocessTest, IsTrueTrue) { ASSERT_TRUE(!run(true_)); }
TEST(SubprocessTest, IsFalseFalse) { ASSERT_FALSE(!run(false_)); }
TEST(SubprocessTest, IsFalseAndTrueFalse) {
ASSERT_FALSE(!run(false_ && true_));
}
TEST(SubprocessTest, IsTrueAndTrueTrue) { ASSERT_TRUE(!run(true_ && true_)); }
TEST(SubprocessTest, IsTrueOrFalseTrue) { ASSERT_TRUE(!run(true_ || false_)); }
TEST(SubprocessTest, IsFalseOrFalseFalse) {
ASSERT_FALSE(!run(false_ || false_));
}
TEST(SubprocessTest, EchoRead) {
Environment env{};
string test1 = "Does";
string test2 = "echo";
string test3 = "work";
ASSERT_TRUE(!env.run(echo(test1) | read("test1")));
ASSERT_STREQ(test1.c_str(), ${"test1"}.get(env).c_str());
ASSERT_TRUE(!env.run(read("test2") << test2));
ASSERT_STREQ(test2.c_str(), ${"test2"}.get(env).c_str());
ASSERT_TRUE(!env.run(echo(${"test1"}, ${"test2"}, test3, "?") | read("out")));
ASSERT_STREQ((test1 + " " + test2 + " " + test3 + " ?").c_str(),
${"out"}.get(env).c_str());
}
TEST(SubprocessTest, ShortCircuitAnd) {
Environment env{};
ASSERT_FALSE(!env.run(false_ && echo("test") | read("test")));
ASSERT_THROW(${"test"}.get(env), std::out_of_range);
}
TEST(SubprocessTest, ShortCircuitOr) {
Environment env{};
ASSERT_TRUE(!env.run(true_ || echo("test") | read("test")));
ASSERT_THROW(${"test"}.get(env), std::out_of_range);
}
TEST(SubprocessTest, FileWriteRead) {
Environment env{};
string test1 = "test1";
string test2 = "test2";
ASSERT_TRUE(!env.run(exec("mktemp") | read("tmpfile")));
ASSERT_TRUE(!env.run(echo(test1) > ${"tmpfile"}));
ASSERT_TRUE(!env.run(echo(test2) >> ${"tmpfile"}));
ASSERT_TRUE(!env.run(read("out") < ${"tmpfile"}));
ASSERT_TRUE(!env.run(exec("rm", ${"tmpfile"})));
ASSERT_STREQ((test1 + "\n" + test2).c_str(), ${"out"}.get(env).c_str());
}
TEST(SubprocessTest, LongPipe) {
// The parentheses aren't needed, they're just so gcc shuts up with
// -Wparentheses
ASSERT_TRUE(!run(exec("ls", "/etc") | exec("rev") |
exec("cut", "-d.", "-f1") | exec("rev") | exec("sort") |
(exec("uniq", "-c") > dev::null)));
}