-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
test_command.rs
84 lines (73 loc) Β· 2.03 KB
/
test_command.rs
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
use super::*;
pub(crate) struct TestCommand {
args: Vec<String>,
expected_status: i32,
expected_stderr: Expected,
expected_stdout: Expected,
tempdir: TempDir,
}
impl TestCommand {
pub(crate) fn new() -> Self {
Self {
tempdir: TempDir::new().unwrap(),
args: Vec::new(),
expected_status: 0,
expected_stderr: Expected::Ignore,
expected_stdout: Expected::String(String::new()),
}
}
pub(crate) fn command(self, args: &str) -> Self {
Self {
args: args.split_whitespace().map(str::to_owned).collect(),
..self
}
}
pub(crate) fn expected_stdout(self, expected_stdout: impl AsRef<str>) -> Self {
Self {
expected_stdout: Expected::String(expected_stdout.as_ref().unindent()),
..self
}
}
pub(crate) fn stdout_regex(self, expected_stdout: impl AsRef<str>) -> Self {
Self {
expected_stdout: Expected::regex(&expected_stdout.as_ref().unindent()),
..self
}
}
pub(crate) fn expected_stderr(self, expected_stderr: impl AsRef<str>) -> Self {
Self {
expected_stderr: Expected::String(expected_stderr.as_ref().unindent()),
..self
}
}
pub(crate) fn expected_status(self, expected_status: i32) -> Self {
Self {
expected_status,
..self
}
}
pub(crate) fn run(self) {
let output = Command::new(executable_path("ord"))
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(if !matches!(self.expected_stderr, Expected::Ignore) {
Stdio::piped()
} else {
Stdio::inherit()
})
.current_dir(&self.tempdir)
.args(self.args.clone())
.output()
.unwrap();
let stdout = str::from_utf8(&output.stdout).unwrap();
let stderr = str::from_utf8(&output.stderr).unwrap();
if output.status.code() != Some(self.expected_status) {
panic!(
"Test failed: {}\nstdout:\n{}\nstderr:\n{}",
output.status, stdout, stderr
);
}
self.expected_stderr.assert_match(&stderr);
self.expected_stdout.assert_match(stdout);
}
}