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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#[derive(Debug, Default)]
pub struct TestCases {
runner: std::cell::RefCell<crate::RunnerSpec>,
bins: std::cell::RefCell<crate::BinRegistry>,
has_run: std::cell::Cell<bool>,
}
impl TestCases {
pub fn new() -> Self {
let s = Self::default();
s.runner
.borrow_mut()
.include(parse_include(std::env::args_os()));
s
}
pub fn case(&self, glob: impl AsRef<std::path::Path>) -> &Self {
self.runner.borrow_mut().case(glob.as_ref(), None);
self
}
pub fn pass(&self, glob: impl AsRef<std::path::Path>) -> &Self {
self.runner
.borrow_mut()
.case(glob.as_ref(), Some(crate::schema::CommandStatus::Success));
self
}
pub fn fail(&self, glob: impl AsRef<std::path::Path>) -> &Self {
self.runner
.borrow_mut()
.case(glob.as_ref(), Some(crate::schema::CommandStatus::Failed));
self
}
pub fn interrupted(&self, glob: impl AsRef<std::path::Path>) -> &Self {
self.runner.borrow_mut().case(
glob.as_ref(),
Some(crate::schema::CommandStatus::Interrupted),
);
self
}
pub fn skip(&self, glob: impl AsRef<std::path::Path>) -> &Self {
self.runner
.borrow_mut()
.case(glob.as_ref(), Some(crate::schema::CommandStatus::Skipped));
self
}
pub fn default_bin_path(&self, path: impl AsRef<std::path::Path>) -> &Self {
let bin = Some(crate::schema::Bin::Path(path.as_ref().into()));
self.runner.borrow_mut().default_bin(bin);
self
}
pub fn default_bin_name(&self, name: impl AsRef<str>) -> &Self {
let bin = Some(crate::schema::Bin::Name(name.as_ref().into()));
self.runner.borrow_mut().default_bin(bin);
self
}
pub fn timeout(&self, time: std::time::Duration) -> &Self {
self.runner.borrow_mut().timeout(Some(time));
self
}
pub fn env(&self, key: impl Into<String>, value: impl Into<String>) -> &Self {
self.runner.borrow_mut().env(key, value);
self
}
pub fn register_bin(
&self,
name: impl Into<String>,
path: impl Into<crate::schema::Bin>,
) -> &Self {
self.bins
.borrow_mut()
.register_bin(name.into(), path.into());
self
}
pub fn register_bins<N: Into<String>, B: Into<crate::schema::Bin>>(
&self,
bins: impl IntoIterator<Item = (N, B)>,
) -> &Self {
self.bins
.borrow_mut()
.register_bins(bins.into_iter().map(|(n, b)| (n.into(), b.into())));
self
}
pub fn run(&self) {
self.has_run.set(true);
let mode = parse_mode(std::env::var_os("TRYCMD").as_deref());
mode.initialize().unwrap();
let runner = self.runner.borrow_mut().prepare();
runner.run(&mode, &self.bins.borrow());
}
}
impl std::panic::RefUnwindSafe for TestCases {}
#[doc(hidden)]
impl Drop for TestCases {
fn drop(&mut self) {
if !self.has_run.get() && !std::thread::panicking() {
self.run();
}
}
}
#[allow(clippy::needless_collect)]
fn parse_include(args: impl IntoIterator<Item = std::ffi::OsString>) -> Option<Vec<String>> {
let filters = args
.into_iter()
.flat_map(std::ffi::OsString::into_string)
.filter_map(|arg| {
const PREFIX: &str = "trycmd=";
if let Some(remainder) = arg.strip_prefix(PREFIX) {
if remainder.is_empty() {
None
} else {
Some(remainder.to_owned())
}
} else {
None
}
})
.collect::<Vec<String>>();
if filters.is_empty() {
None
} else {
Some(filters)
}
}
fn parse_mode(var: Option<&std::ffi::OsStr>) -> crate::Mode {
if var == Some(std::ffi::OsStr::new("overwrite")) {
crate::Mode::Overwrite
} else if var == Some(std::ffi::OsStr::new("dump")) {
crate::Mode::Dump("dump".into())
} else {
crate::Mode::Fail
}
}