Skip to content

Commit bbb45c4

Browse files
committed
Shorter output for rustc --test binaries.
A program created with `rustc --test` prints at least one line per test. This can be very verbose, especially with [data-driven tests]( https://internals.rust-lang.org/t/test-and-external-test-harnesses/3145) when hundreds or thousands of tests is not rare. This adds a `-q` or `--quiet` option that changes the output to one character instead of one line per test (except metrics and benchmarks results which have additional data to show): ``` Running target/debug/wpt-75c594dc1e6e6187 running 314 tests .............................................................................. .............................................................................. .............................................................................. .............................................................................. .. test result: ok. 314 passed; 0 failed; 0 ignored; 0 measured ``` This is a breaking change since the `test::TestOpts` struct now has one more field.
1 parent c116ae3 commit bbb45c4

File tree

3 files changed

+42
-17
lines changed

3 files changed

+42
-17
lines changed

src/compiletest/common.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -155,5 +155,8 @@ pub struct Config {
155155
pub lldb_python_dir: Option<String>,
156156

157157
// Explain what's going on
158-
pub verbose: bool
158+
pub verbose: bool,
159+
160+
// Print one character per test instead of one line
161+
pub quiet: bool,
159162
}

src/compiletest/compiletest.rs

+4
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ pub fn parse_config(args: Vec<String> ) -> Config {
7878
optopt("", "host-rustcflags", "flags to pass to rustc for host", "FLAGS"),
7979
optopt("", "target-rustcflags", "flags to pass to rustc for target", "FLAGS"),
8080
optflag("", "verbose", "run tests verbosely, showing all output"),
81+
optflag("", "quiet", "print one character per test instead of one line"),
8182
optopt("", "logfile", "file to log test execution to", "FILE"),
8283
optopt("", "target", "the target to build for", "TARGET"),
8384
optopt("", "host", "the host to build for", "HOST"),
@@ -158,6 +159,7 @@ pub fn parse_config(args: Vec<String> ) -> Config {
158159
!opt_str2(matches.opt_str("adb-test-dir")).is_empty(),
159160
lldb_python_dir: matches.opt_str("lldb-python-dir"),
160161
verbose: matches.opt_present("verbose"),
162+
quiet: matches.opt_present("quiet"),
161163
}
162164
}
163165

@@ -191,6 +193,7 @@ pub fn log_config(config: &Config) {
191193
logv(c, format!("adb_device_status: {}",
192194
config.adb_device_status));
193195
logv(c, format!("verbose: {}", config.verbose));
196+
logv(c, format!("quiet: {}", config.quiet));
194197
logv(c, format!("\n"));
195198
}
196199

@@ -257,6 +260,7 @@ pub fn test_opts(config: &Config) -> test::TestOpts {
257260
Some(ref filter) => Some(filter.clone()),
258261
},
259262
run_ignored: config.run_ignored,
263+
quiet: config.quiet,
260264
logfile: config.logfile.clone(),
261265
run_tests: true,
262266
bench_benchmarks: true,

src/libtest/lib.rs

+34-16
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl fmt::Display for TestName {
109109
}
110110
}
111111

112-
#[derive(Clone, Copy)]
112+
#[derive(Clone, Copy, PartialEq, Eq)]
113113
enum NamePadding {
114114
PadNone,
115115
PadOnRight,
@@ -301,6 +301,7 @@ pub struct TestOpts {
301301
pub logfile: Option<PathBuf>,
302302
pub nocapture: bool,
303303
pub color: ColorConfig,
304+
pub quiet: bool,
304305
}
305306

306307
impl TestOpts {
@@ -314,6 +315,7 @@ impl TestOpts {
314315
logfile: None,
315316
nocapture: false,
316317
color: AutoColor,
318+
quiet: false,
317319
}
318320
}
319321
}
@@ -331,6 +333,7 @@ fn optgroups() -> Vec<getopts::OptGroup> {
331333
of stdout", "PATH"),
332334
getopts::optflag("", "nocapture", "don't capture stdout/stderr of each \
333335
task, allow printing directly"),
336+
getopts::optflag("q", "quiet", "Display one character per test instead of one line"),
334337
getopts::optopt("", "color", "Configure coloring of output:
335338
auto = colorize if stdout is a tty and tests are run on serially (default);
336339
always = always colorize output;
@@ -388,6 +391,7 @@ pub fn parse_opts(args: &[String]) -> Option<OptRes> {
388391
};
389392

390393
let run_ignored = matches.opt_present("ignored");
394+
let quiet = matches.opt_present("quiet");
391395

392396
let logfile = matches.opt_str("logfile");
393397
let logfile = logfile.map(|s| PathBuf::from(&s));
@@ -420,6 +424,7 @@ pub fn parse_opts(args: &[String]) -> Option<OptRes> {
420424
logfile: logfile,
421425
nocapture: nocapture,
422426
color: color,
427+
quiet: quiet,
423428
};
424429

425430
Some(Ok(test_opts))
@@ -451,6 +456,7 @@ struct ConsoleTestState<T> {
451456
log_out: Option<File>,
452457
out: OutputLocation<T>,
453458
use_color: bool,
459+
quiet: bool,
454460
total: usize,
455461
passed: usize,
456462
failed: usize,
@@ -476,6 +482,7 @@ impl<T: Write> ConsoleTestState<T> {
476482
out: out,
477483
log_out: log_out,
478484
use_color: use_color(opts),
485+
quiet: opts.quiet,
479486
total: 0,
480487
passed: 0,
481488
failed: 0,
@@ -488,15 +495,15 @@ impl<T: Write> ConsoleTestState<T> {
488495
}
489496

490497
pub fn write_ok(&mut self) -> io::Result<()> {
491-
self.write_pretty("ok", term::color::GREEN)
498+
self.write_short_result("ok", ".", term::color::GREEN)
492499
}
493500

494501
pub fn write_failed(&mut self) -> io::Result<()> {
495-
self.write_pretty("FAILED", term::color::RED)
502+
self.write_short_result("FAILED", "F", term::color::RED)
496503
}
497504

498505
pub fn write_ignored(&mut self) -> io::Result<()> {
499-
self.write_pretty("ignored", term::color::YELLOW)
506+
self.write_short_result("ignored", "i", term::color::YELLOW)
500507
}
501508

502509
pub fn write_metric(&mut self) -> io::Result<()> {
@@ -507,6 +514,16 @@ impl<T: Write> ConsoleTestState<T> {
507514
self.write_pretty("bench", term::color::CYAN)
508515
}
509516

517+
pub fn write_short_result(&mut self, verbose: &str, quiet: &str, color: term::color::Color)
518+
-> io::Result<()> {
519+
if self.quiet {
520+
self.write_pretty(quiet, color)
521+
} else {
522+
try!(self.write_pretty(verbose, color));
523+
self.write_plain("\n")
524+
}
525+
}
526+
510527
pub fn write_pretty(&mut self, word: &str, color: term::color::Color) -> io::Result<()> {
511528
match self.out {
512529
Pretty(ref mut term) => {
@@ -550,28 +567,28 @@ impl<T: Write> ConsoleTestState<T> {
550567
}
551568

552569
pub fn write_test_start(&mut self, test: &TestDesc, align: NamePadding) -> io::Result<()> {
553-
let name = test.padded_name(self.max_name_len, align);
554-
self.write_plain(&format!("test {} ... ", name))
570+
if self.quiet && align != PadOnRight {
571+
Ok(())
572+
} else {
573+
let name = test.padded_name(self.max_name_len, align);
574+
self.write_plain(&format!("test {} ... ", name))
575+
}
555576
}
556577

557578
pub fn write_result(&mut self, result: &TestResult) -> io::Result<()> {
558-
try!(match *result {
579+
match *result {
559580
TrOk => self.write_ok(),
560581
TrFailed => self.write_failed(),
561582
TrIgnored => self.write_ignored(),
562583
TrMetrics(ref mm) => {
563584
try!(self.write_metric());
564-
self.write_plain(&format!(": {}", mm.fmt_metrics()))
585+
self.write_plain(&format!(": {}\n", mm.fmt_metrics()))
565586
}
566587
TrBench(ref bs) => {
567588
try!(self.write_bench());
568-
569-
try!(self.write_plain(&format!(": {}", fmt_bench_samples(bs))));
570-
571-
Ok(())
589+
self.write_plain(&format!(": {}\n", fmt_bench_samples(bs)))
572590
}
573-
});
574-
self.write_plain("\n")
591+
}
575592
}
576593

577594
pub fn write_log(&mut self, test: &TestDesc, result: &TestResult) -> io::Result<()> {
@@ -629,9 +646,9 @@ impl<T: Write> ConsoleTestState<T> {
629646
try!(self.write_plain("\ntest result: "));
630647
if success {
631648
// There's no parallelism at this point so it's safe to use color
632-
try!(self.write_ok());
649+
try!(self.write_pretty("ok", term::color::GREEN));
633650
} else {
634-
try!(self.write_failed());
651+
try!(self.write_pretty("FAILED", term::color::RED));
635652
}
636653
let s = format!(". {} passed; {} failed; {} ignored; {} measured\n\n",
637654
self.passed,
@@ -758,6 +775,7 @@ fn should_sort_failures_before_printing_them() {
758775
log_out: None,
759776
out: Raw(Vec::new()),
760777
use_color: false,
778+
quiet: false,
761779
total: 0,
762780
passed: 0,
763781
failed: 0,

0 commit comments

Comments
 (0)