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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use std::cell::RefCell;
use std::collections::HashMap;
use std::collections::HashSet;
use std::panic::AssertUnwindSafe;
use std::rc::Rc;
use std::sync::Arc;
use deno_core::anyhow::Context;
use deno_core::parking_lot::Mutex;
use deno_core::serde_json;
use deno_terminal::colors;
use serde::Deserialize;
use test_util::tests_path;
use test_util::PathRef;
use test_util::TestContextBuilder;
pub fn main() {
let maybe_filter = parse_cli_arg_filter();
let categories = filter(collect_tests(), maybe_filter.as_deref());
let total_tests = categories.iter().map(|c| c.tests.len()).sum::<usize>();
let mut failures = Vec::new();
let _http_guard = test_util::http_server();
// todo(dsherret): the output should be changed to be terse
// when it passes, but verbose on failure
for category in &categories {
if category.tests.is_empty() {
continue; // skip output when all the tests have been filtered out
}
eprintln!();
eprintln!(" {} {}", colors::green_bold("Running"), category.name);
eprintln!();
for test in &category.tests {
eprint!("test {} ... ", test.name);
let diagnostic_logger = Rc::new(RefCell::new(Vec::<u8>::new()));
let panic_message = Arc::new(Mutex::new(Vec::<u8>::new()));
std::panic::set_hook({
let panic_message = panic_message.clone();
Box::new(move |info| {
panic_message
.lock()
.extend(format!("{}", info).into_bytes());
})
});
let result = std::panic::catch_unwind(AssertUnwindSafe(|| {
run_test(test, diagnostic_logger.clone())
}));
let success = result.is_ok();
if !success {
let mut output = diagnostic_logger.borrow().clone();
output.push(b'\n');
output.extend(panic_message.lock().iter());
failures.push((test, output));
}
eprintln!(
"{}",
if success {
colors::green("ok")
} else {
colors::red("fail")
},
);
}
}
eprintln!();
if !failures.is_empty() {
eprintln!("spec failures:");
eprintln!();
for (failure, output) in &failures {
eprintln!("---- {} ----", failure.name);
eprintln!("{}", String::from_utf8_lossy(output));
eprintln!("Test file: {}", failure.manifest_file());
eprintln!();
}
panic!("{} failed of {}", failures.len(), total_tests);
} else {
eprintln!("{} tests passed", total_tests);
}
eprintln!();
}
fn parse_cli_arg_filter() -> Option<String> {
let args: Vec<String> = std::env::args().collect();
let maybe_filter =
args.get(1).filter(|s| !s.starts_with('-') && !s.is_empty());
maybe_filter.cloned()
}
fn run_test(test: &Test, diagnostic_logger: Rc<RefCell<Vec<u8>>>) {
let metadata = &test.metadata;
let mut builder = TestContextBuilder::new();
builder = builder.logging_capture(diagnostic_logger);
let cwd = &test.cwd;
if test.metadata.temp_dir {
builder = builder.use_temp_cwd();
} else {
builder = builder.cwd(cwd.to_string_lossy());
}
if let Some(base) = &metadata.base {
match base.as_str() {
"npm" => {
builder = builder.add_npm_env_vars();
}
"jsr" => {
builder = builder.add_jsr_env_vars().add_npm_env_vars();
}
_ => panic!("Unknown test base: {}", base),
}
}
let context = builder.build();
if test.metadata.temp_dir {
// copy all the files in the cwd to a temp directory
// excluding the metadata and assertion files
let temp_dir = context.temp_dir().path();
let assertion_paths = test.resolve_test_and_assertion_files();
cwd.copy_to_recursive_with_exclusions(temp_dir, &assertion_paths);
}
for step in &metadata.steps {
if step.clean_deno_dir {
context.deno_dir().path().remove_dir_all();
}
let command = context.new_command().envs(&step.envs);
let command = match &step.args {
VecOrString::Vec(args) => command.args_vec(args),
VecOrString::String(text) => command.args(text),
};
let command = match &step.cwd {
Some(cwd) => command.current_dir(cwd),
None => command,
};
let output = command.run();
if step.output.ends_with(".out") {
let test_output_path = cwd.join(&step.output);
output.assert_matches_file(test_output_path);
} else {
output.assert_matches_text(&step.output);
}
output.assert_exit_code(step.exit_code);
}
}
#[derive(Clone, Deserialize)]
#[serde(untagged)]
enum VecOrString {
Vec(Vec<String>),
String(String),
}
#[derive(Clone, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
struct MultiTestMetaData {
/// Whether to copy all the non-assertion files in the current
/// test directory to a temporary directory before running the
/// steps.
#[serde(default)]
pub temp_dir: bool,
/// The base environment to use for the test.
#[serde(default)]
pub base: Option<String>,
pub steps: Vec<StepMetaData>,
}
#[derive(Clone, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
struct SingleTestMetaData {
#[serde(default)]
pub base: Option<String>,
#[serde(default)]
pub temp_dir: bool,
#[serde(flatten)]
pub step: StepMetaData,
}
impl SingleTestMetaData {
pub fn into_multi(self) -> MultiTestMetaData {
MultiTestMetaData {
base: self.base,
temp_dir: self.temp_dir,
steps: vec![self.step],
}
}
}
#[derive(Clone, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
struct StepMetaData {
/// Whether to clean the deno_dir before running the step.
#[serde(default)]
pub clean_deno_dir: bool,
pub args: VecOrString,
pub cwd: Option<String>,
#[serde(default)]
pub envs: HashMap<String, String>,
pub output: String,
#[serde(default)]
pub exit_code: i32,
}
#[derive(Clone)]
struct Test {
pub name: String,
pub cwd: PathRef,
pub metadata: MultiTestMetaData,
}
impl Test {
pub fn manifest_file(&self) -> PathRef {
self.cwd.join("__test__.json")
}
}
impl Test {
pub fn resolve_test_and_assertion_files(&self) -> HashSet<PathRef> {
let mut result = HashSet::with_capacity(self.metadata.steps.len() + 1);
result.insert(self.manifest_file());
result.extend(
self
.metadata
.steps
.iter()
.map(|step| self.cwd.join(&step.output)),
);
result
}
}
struct TestCategory {
pub name: String,
pub tests: Vec<Test>,
}
fn filter(
categories: Vec<TestCategory>,
maybe_filter: Option<&str>,
) -> Vec<TestCategory> {
if categories.iter().all(|c| c.tests.is_empty()) {
panic!("no tests found");
}
match maybe_filter {
Some(filter) => categories
.into_iter()
.map(|mut c| {
c.tests.retain(|t| t.name.contains(filter));
c
})
.collect(),
None => categories,
}
}
fn collect_tests() -> Vec<TestCategory> {
let specs_dir = tests_path().join("specs");
let mut result = Vec::new();
for entry in specs_dir.read_dir() {
let entry = entry.unwrap();
let file_type = entry
.file_type()
.context(entry.path().to_string_lossy().to_string())
.unwrap();
if !file_type.is_dir() {
continue;
}
let mut category = TestCategory {
name: format!("specs::{}", entry.file_name().to_string_lossy()),
tests: Vec::new(),
};
let category_path = PathRef::new(entry.path());
for entry in category_path.read_dir() {
let entry = entry.unwrap();
let file_type = entry
.file_type()
.context(entry.path().to_string_lossy().to_string())
.unwrap();
if !file_type.is_dir() {
continue;
}
let test_dir = PathRef::new(entry.path());
let metadata_path = test_dir.join("__test__.jsonc");
let metadata_value = metadata_path.read_jsonc_value();
// checking for "steps" leads to a more targeted error message
// instead of when deserializing an untagged enum
let metadata = if metadata_value
.as_object()
.and_then(|o| o.get("steps"))
.is_some()
{
serde_json::from_value::<MultiTestMetaData>(metadata_value)
} else {
serde_json::from_value::<SingleTestMetaData>(metadata_value)
.map(|s| s.into_multi())
}
.with_context(|| format!("Failed to parse {}", metadata_path))
.unwrap();
let test_name =
format!("{}::{}", category.name, entry.file_name().to_string_lossy());
// only support characters that work with filtering with `cargo test`
if !test_name
.chars()
.all(|c| c.is_alphanumeric() || matches!(c, '_' | ':'))
{
panic!(
"Invalid test name (only supports alphanumeric and underscore): {}",
test_name
);
}
category.tests.push(Test {
name: test_name,
cwd: test_dir,
metadata,
});
}
result.push(category);
}
result
}
|