Skip to content

Commit 1dea427

Browse files
test(cts): replace --quiet and --verbose with --print-output-when
1 parent 833a2a7 commit 1dea427

2 files changed

Lines changed: 64 additions & 25 deletions

File tree

xtask/src/cts.rs

Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
//! Lines starting with `//` or `#` in the test list are treated as comments and
3131
//! ignored.
3232
33-
use anyhow::{bail, Context};
33+
use anyhow::{anyhow, bail, Context};
34+
use core::fmt;
3435
use pico_args::Arguments;
3536
use regex_lite::{Regex, RegexBuilder};
3637
use std::{ffi::OsString, sync::LazyLock};
@@ -56,6 +57,12 @@ struct TestLine {
5657
pub fails_if: Vec<String>,
5758
}
5859

60+
#[derive(Clone, Copy, Debug)]
61+
enum PrintOutputWhen {
62+
TestFails,
63+
Always,
64+
}
65+
5966
pub fn run_cts(
6067
shell: Shell,
6168
mut args: Arguments,
@@ -64,8 +71,31 @@ pub fn run_cts(
6471
let skip_checkout = args.contains("--skip-checkout");
6572
let llvm_cov = args.contains("--llvm-cov");
6673
let release = args.contains("--release");
67-
let mut quiet = args.contains("--quiet");
68-
let verbose = args.contains("--verbose");
74+
75+
let output_filter = args
76+
.opt_value_from_str::<_, String>("--print-output-when")?
77+
.map(|f| {
78+
let values = [
79+
("test-fails", PrintOutputWhen::TestFails),
80+
("always", PrintOutputWhen::Always),
81+
];
82+
let lowered = f.to_ascii_lowercase();
83+
values
84+
.iter()
85+
.find_map(|(cli_str, enum_value)| (&*lowered == *cli_str).then_some(*enum_value))
86+
.ok_or_else(|| {
87+
anyhow!(
88+
"`{f}` is not a valid `--print-output-when` value; expected one of {}",
89+
fmt::from_fn(|f| {
90+
f.debug_list()
91+
.entries(values.iter().map(|(cli, _enum)| cli))
92+
.finish()
93+
})
94+
)
95+
})
96+
})
97+
.transpose()?;
98+
6999
let running_on_backend = args.opt_value_from_str::<_, String>("--backend")?;
70100
let mut filter_pattern = args.opt_value_from_str::<_, String>("--filter")?;
71101
let mut filter_invert = false;
@@ -107,18 +137,21 @@ pub fn run_cts(
107137
})
108138
.collect::<Vec<_>>();
109139

140+
let mut default_output_filter = PrintOutputWhen::Always;
141+
110142
if tests.is_empty() && list_files.is_empty() {
111143
if passthrough_args.is_none() {
112144
log::info!("Reading default test list from {CTS_DEFAULT_TEST_LIST}");
113145
list_files.push(OsString::from(CTS_DEFAULT_TEST_LIST));
114146

115-
// Reduce output, unless `--verbose` was specified.
116-
quiet = !verbose;
147+
default_output_filter = PrintOutputWhen::TestFails;
117148
}
118149
} else if passthrough_args.is_some() {
119150
bail!("Test(s) and test list(s) are incompatible with passthrough arguments.");
120151
}
121152

153+
let output_filter = output_filter.unwrap_or(default_output_filter);
154+
122155
for file in list_files {
123156
tests.extend(shell.read_file(file)?.lines().filter_map(|line| {
124157
static TEST_LINE_REGEX: LazyLock<Regex> = LazyLock::new(|| {
@@ -311,7 +344,7 @@ pub fn run_cts(
311344
}
312345
}
313346

314-
if !quiet {
347+
if let PrintOutputWhen::Always = output_filter {
315348
log::info!("Running {}", test.selector.to_string_lossy());
316349
}
317350

@@ -330,26 +363,29 @@ pub fn run_cts(
330363
.args(["--", "./tools/run_deno", "--verbose"])
331364
.args([&test.selector]);
332365

333-
if quiet {
334-
let output = cmd.ignore_status().output().context("Failed to run CTS")?;
335-
let stdout = String::from_utf8_lossy(&output.stdout);
336-
let stderr = String::from_utf8_lossy(&output.stderr);
337-
338-
if output.status.success() {
339-
if let Some((_, summary)) = stdout.split_once("** Summary **") {
340-
println!("\n== Summary for {} ==", test.selector.to_string_lossy());
341-
println!("{}", summary.trim());
366+
match output_filter {
367+
PrintOutputWhen::TestFails => {
368+
let output = cmd.ignore_status().output().context("Failed to run CTS")?;
369+
let stdout = String::from_utf8_lossy(&output.stdout);
370+
let stderr = String::from_utf8_lossy(&output.stderr);
371+
372+
if output.status.success() {
373+
if let Some((_, summary)) = stdout.split_once("** Summary **") {
374+
println!("\n== Summary for {} ==", test.selector.to_string_lossy());
375+
println!("{}", summary.trim());
376+
} else {
377+
print!("{}", stdout);
378+
eprint!("{}", stderr);
379+
}
342380
} else {
343381
print!("{}", stdout);
344382
eprint!("{}", stderr);
383+
bail!("CTS failed");
345384
}
346-
} else {
347-
print!("{}", stdout);
348-
eprint!("{}", stderr);
349-
bail!("CTS failed");
350385
}
351-
} else {
352-
cmd.run().context("CTS failed")?;
386+
PrintOutputWhen::Always => {
387+
cmd.run().context("CTS failed")?;
388+
}
353389
}
354390
}
355391

xtask/src/main.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ Commands:
2222
cts [<options>] [<test selector...> | -f <test list file...> | -- <args...>]
2323
Check out, build, and run CTS tests
2424
25-
If no command-line arguments are specified, runs the default test list
26-
in `cts_runner/test.lst`, with quiet mode enabled.
25+
If no command-line arguments are specified, runs as if `cts
26+
cts_runner/test.lst --print-output-when=test-fails` were
27+
specified.
2728
2829
--skip-checkout Don't check out the pinned CTS version, use whatever
2930
is already checked out.
@@ -34,8 +35,10 @@ Commands:
3435
--filter <regex> Filter tests by selector using a regex pattern.
3536
Prefix with '!' to invert (exclude matching tests).
3637
Applied after all tests are collected.
37-
--quiet Only show test counts for suites without failures.
38-
--verbose Show full output when running the default test list.
38+
--print-output-when One of `test-fails` or `always`. When no `test
39+
selector`, `test list file`, or `args` are
40+
specified, this defaults to `test-fails`. Otherwise,
41+
this defaults to `always`.
3942
4043
run-wasm
4144
Build and run web examples

0 commit comments

Comments
 (0)