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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
use std::{
ffi::OsStr,
fmt::{self, Display, Formatter},
path::{Path, PathBuf},
process::Output,
};
use anyhow::{Context, Error};
use crate::{
assertions::{
Assertion, ExitSuccessfully, ExitUnsuccessfully, MatchStdioStream,
},
Outcome, TestContext,
};
pub fn load(test_root: &Path) -> Result<Vec<Test>, Error> {
let runefiles =
walkdir::WalkDir::new(test_root)
.into_iter()
.filter_map(|e| match e {
Ok(entry)
if entry.path().file_name()
== Some(OsStr::new("Runefile.yml")) =>
{
Some(entry.into_path())
},
_ => None,
});
let mut tests = Vec::new();
for runefile in runefiles {
let directory =
runefile.parent().expect("We are at least 2 levels deep");
let test = Test::for_directory(directory).with_context(|| {
format!("Unable to load tests from \"{}\"", directory.display())
})?;
log::debug!("Found \"{}\"", test.name);
tests.push(test);
}
Ok(tests)
}
#[derive(Debug, Clone, PartialEq)]
pub struct FullName {
pub category: Category,
pub exit_condition: ExitCondition,
pub name: String,
}
impl Display for FullName {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}-{}/{}", self.category, self.exit_condition, self.name)?;
Ok(())
}
}
#[derive(Debug)]
pub struct Test {
pub directory: PathBuf,
pub expected_output: Vec<MatchStdioStream>,
pub name: FullName,
}
impl Test {
pub fn for_directory(directory: impl AsRef<Path>) -> Result<Self, Error> {
let directory = directory.as_ref();
let directory = directory
.canonicalize()
.context("Unable to get the full path")?;
let name = directory
.file_name()
.context("Unable to determine the directory's name")?
.to_string_lossy()
.into_owned();
let parent = directory
.parent()
.and_then(|parent| parent.file_name())
.and_then(|parent| parent.to_str())
.context("Unable to determine the parent directory")?;
let (category, exit_successfully) = match parent {
"compile-pass" => (Category::Compile, true),
"compile-fail" => (Category::Compile, false),
"run-pass" => (Category::Run, true),
"run-fail" => (Category::Run, false),
_ => anyhow::bail!(
"Unable to determine the family, expected one of \
compile-pass, compile-fail, run-pass, or run-fail, but found \
\"{}\"",
parent
),
};
let expected_output = load_stderr_files(&directory)
.context("Unable to load the *stderr files")?;
let exit_condition = if exit_successfully {
ExitCondition::Success(ExitSuccessfully)
} else {
ExitCondition::Fail(ExitUnsuccessfully)
};
let name = FullName {
name,
category,
exit_condition,
};
Ok(Test {
name,
directory,
expected_output,
})
}
pub fn is_ignored(&self) -> bool { self.name.name.starts_with('_') }
fn get_rune_output(&self, ctx: &TestContext) -> Result<Output, Error> {
match self.name.category {
Category::Run => {
crate::run::rune_output(&self.name, &self.directory, ctx)
},
Category::Compile => {
crate::compile::rune_output(&self.name, &self.directory, ctx)
},
}
}
pub fn run(&self, ctx: &TestContext) -> Outcome {
if self.is_ignored() {
return Outcome::Skipped;
}
let output = match self.get_rune_output(ctx) {
Ok(output) => output,
Err(e) => {
return Outcome::Bug(
e.context("Unable to run the `rune` command"),
)
},
};
let mut errors = Vec::new();
for assertion in self.assertions() {
if let Err(e) = assertion.check_for_errors(&output) {
errors.push(e);
}
}
if errors.is_empty() {
Outcome::Pass
} else {
Outcome::Fail { errors, output }
}
}
fn exit_condition(&self) -> &dyn Assertion {
match &self.name.exit_condition {
ExitCondition::Success(s) => s,
ExitCondition::Fail(f) => f,
}
}
fn assertions(&self) -> impl Iterator<Item = &dyn Assertion> + '_ {
let expected_output =
self.expected_output.iter().map(|a| a as &dyn Assertion);
std::iter::once(self.exit_condition()).chain(expected_output)
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum ExitCondition {
Success(ExitSuccessfully),
Fail(ExitUnsuccessfully),
}
impl Assertion for ExitCondition {
fn check_for_errors(&self, output: &Output) -> Result<(), Error> {
match self {
ExitCondition::Success(e) => e.check_for_errors(output),
ExitCondition::Fail(e) => e.check_for_errors(output),
}
}
}
impl Display for ExitCondition {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
ExitCondition::Success(_) => write!(f, "pass"),
ExitCondition::Fail(_) => write!(f, "fail"),
}
}
}
fn load_stderr_files(directory: &Path) -> Result<Vec<MatchStdioStream>, Error> {
let mut stderr = Vec::new();
for entry in directory.read_dir()? {
let entry = entry?;
let path = entry.path();
if let Some(assertion) = MatchStdioStream::for_file(&path)
.with_context(|| format!("Unable to load \"{}\"", path.display()))?
{
stderr.push(assertion);
}
}
Ok(stderr)
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Category {
Run,
Compile,
}
impl Display for Category {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Category::Run => write!(f, "run"),
Category::Compile => write!(f, "compile"),
}
}
}