forked from oxidecomputer/humility
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli_tests.rs
159 lines (133 loc) · 4.43 KB
/
cli_tests.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
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
148
149
150
151
152
153
154
155
156
157
158
159
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
use anyhow::Result;
use std::fmt::Write;
use std::fs::{self, File};
use std::path::Path;
struct Test {
name: &'static str,
cmd: &'static str,
arg: Option<&'static str>,
}
impl Test {
fn basic(name: &'static str) -> Self {
Test { name, cmd: name, arg: None }
}
fn witharg(
name: &'static str,
cmd: &'static str,
arg: &'static str,
) -> Self {
Test { name, cmd, arg: Some(arg) }
}
}
fn make_tests() -> Result<()> {
let postmortem = [
Test::witharg("extract", "extract", "app.toml"),
Test::witharg("extract-list", "extract", "--list"),
Test::basic("manifest"),
Test::basic("spd"),
Test::basic("map"),
Test::basic("pmp"),
Test::basic("registers"),
Test::witharg("registers-s", "registers", "-s"),
Test::basic("ringbuf"),
Test::witharg("ringbuf-arg", "ringbuf", "i2c"),
Test::witharg("readvar-list", "readvar", "-l"),
Test::witharg("readvar-ticks", "readvar", "TICKS"),
Test::basic("stackmargin"),
Test::basic("tasks"),
Test::witharg("tasks-slvr", "tasks", "-slvr"),
Test::basic("log"),
];
let mut cores = vec![];
let dir = Path::new("./tests/cmd/cores");
for entry in fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();
if let Some(f) = path.file_name() {
if let Some(s) = f.to_str() {
let prefix = "hubris.core.";
if let Some(name) = s.strip_prefix(&prefix) {
cores.push((name.to_string(), s.to_string()));
}
}
}
}
for test in &postmortem {
let dirpath = format!("./tests/cmd/{}", test.name);
let dir = Path::new(&dirpath);
fs::create_dir_all(dir)?;
for (core, filename) in &cores {
let base = format!("{}.{}", test.name, core);
let toml = format!("{}.toml", base);
let stdout = format!("{}.stdout", base);
let stderr = format!("{}.stderr", base);
let succeeds = format!("{}.stdout.succeeds", base);
let fails = format!("{}.stderr.fails", base);
let testcase = dir.join(toml);
let succeeds = dir.join(succeeds);
let fails = dir.join(fails);
if !testcase.exists() || succeeds.exists() || fails.exists() {
let mut testfile = File::create(testcase)?;
let mut contents = String::new();
let testcmd = if let Some(arg) = test.arg {
format!("{} {}", test.cmd, arg)
} else {
format!("{}", test.cmd)
};
writeln!(
&mut contents,
r##"
#
# This test case has been automatically created, but can be edited and
# should be checked in. Should it ever be regenerated, simply delete
# it and re-run "cargo test"
#
fs.base = "../cores"
bin.name = "humility"
args = "-d {} {}"
"##,
filename, testcmd,
)?;
let stderr = dir.join(stderr);
let stdout = dir.join(stdout);
let fails = if succeeds.exists() {
fs::remove_file(succeeds)?;
false
} else if fails.exists() {
fs::remove_file(fails)?;
true
} else {
core.ends_with(".fails")
};
if fails {
writeln!(&mut contents, "status.code = 1")?;
}
File::create(stderr)?;
File::create(stdout)?;
use std::io::Write;
testfile.write_all(contents.as_bytes())?;
}
}
}
Ok(())
}
#[test]
fn cli_tests() {
if let Err(err) = make_tests() {
panic!("make_tests() failed: {:?}", err);
}
match std::env::var_os("TRYCMD_TEST") {
Some(case) => {
trycmd::TestCases::new().case(case);
}
None => {
trycmd::TestCases::new()
.case("tests/cmd/*.trycmd")
.case("tests/cmd/*.toml")
.case("tests/cmd/*/*.toml");
}
}
}