Skip to content

Commit 25163e8

Browse files
authored
Rollup merge of #145982 - Zalathar:logv, r=jieyouxu
compiletest: Reduce the number of `println!` calls that don't have access to `TestCx` In order to stop using `#![feature(internal_output_capture)]` in compiletest, we need to be able to capture the console output of individual tests run by the executor. The approach I have planned is to have all test runners print “console” output into a trait object that is passed around as part of `TestCx`, since almost all test-runner code has easy access to that context. So `println!("foo")` will become `writeln!(self.stdout, "foo")`, and so on. In order to make that viable, we need to avoid unnecessary printing in places that don't have easy access to `TestCx`. To do so, we can either get rid of unnecessary print statements, or rearrange the code to make the context available. This PR uses both approaches. r? jieyouxu
2 parents 47f1df5 + 6340b97 commit 25163e8

File tree

8 files changed

+30
-83
lines changed

8 files changed

+30
-83
lines changed

src/tools/compiletest/src/bin/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::env;
22
use std::io::IsTerminal;
33
use std::sync::Arc;
44

5-
use compiletest::{early_config_check, log_config, parse_config, run_tests};
5+
use compiletest::{early_config_check, parse_config, run_tests};
66

77
fn main() {
88
tracing_subscriber::fmt::init();
@@ -19,6 +19,5 @@ fn main() {
1919

2020
early_config_check(&config);
2121

22-
log_config(&config);
2322
run_tests(config);
2423
}

src/tools/compiletest/src/lib.rs

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
mod tests;
1010

1111
pub mod common;
12-
pub mod compute_diff;
1312
mod debuggers;
1413
pub mod diagnostics;
1514
pub mod directives;
@@ -44,7 +43,6 @@ use crate::common::{
4443
};
4544
use crate::directives::DirectivesCache;
4645
use crate::executor::{CollectedTest, ColorConfig};
47-
use crate::util::logv;
4846

4947
/// Creates the `Config` instance for this invocation of compiletest.
5048
///
@@ -477,51 +475,6 @@ pub fn parse_config(args: Vec<String>) -> Config {
477475
}
478476
}
479477

480-
pub fn log_config(config: &Config) {
481-
let c = config;
482-
logv(c, "configuration:".to_string());
483-
logv(c, format!("compile_lib_path: {}", config.compile_lib_path));
484-
logv(c, format!("run_lib_path: {}", config.run_lib_path));
485-
logv(c, format!("rustc_path: {}", config.rustc_path));
486-
logv(c, format!("cargo_path: {:?}", config.cargo_path));
487-
logv(c, format!("rustdoc_path: {:?}", config.rustdoc_path));
488-
489-
logv(c, format!("src_root: {}", config.src_root));
490-
logv(c, format!("src_test_suite_root: {}", config.src_test_suite_root));
491-
492-
logv(c, format!("build_root: {}", config.build_root));
493-
logv(c, format!("build_test_suite_root: {}", config.build_test_suite_root));
494-
495-
logv(c, format!("sysroot_base: {}", config.sysroot_base));
496-
497-
logv(c, format!("stage: {}", config.stage));
498-
logv(c, format!("stage_id: {}", config.stage_id));
499-
logv(c, format!("mode: {}", config.mode));
500-
logv(c, format!("run_ignored: {}", config.run_ignored));
501-
logv(c, format!("filters: {:?}", config.filters));
502-
logv(c, format!("skip: {:?}", config.skip));
503-
logv(c, format!("filter_exact: {}", config.filter_exact));
504-
logv(
505-
c,
506-
format!("force_pass_mode: {}", opt_str(&config.force_pass_mode.map(|m| format!("{}", m))),),
507-
);
508-
logv(c, format!("runner: {}", opt_str(&config.runner)));
509-
logv(c, format!("host-rustcflags: {:?}", config.host_rustcflags));
510-
logv(c, format!("target-rustcflags: {:?}", config.target_rustcflags));
511-
logv(c, format!("target: {}", config.target));
512-
logv(c, format!("host: {}", config.host));
513-
logv(c, format!("android-cross-path: {}", config.android_cross_path));
514-
logv(c, format!("adb_path: {}", config.adb_path));
515-
logv(c, format!("adb_test_dir: {}", config.adb_test_dir));
516-
logv(c, format!("adb_device_status: {}", config.adb_device_status));
517-
logv(c, format!("ar: {}", config.ar));
518-
logv(c, format!("target-linker: {:?}", config.target_linker));
519-
logv(c, format!("host-linker: {:?}", config.host_linker));
520-
logv(c, format!("verbose: {}", config.verbose));
521-
logv(c, format!("minicore_path: {}", config.minicore_path));
522-
logv(c, "\n".to_string());
523-
}
524-
525478
pub fn opt_str(maybestr: &Option<String>) -> &str {
526479
match *maybestr {
527480
None => "(none)",
@@ -538,6 +491,8 @@ pub fn opt_str2(maybestr: Option<String>) -> String {
538491

539492
/// Called by `main` after the config has been parsed.
540493
pub fn run_tests(config: Arc<Config>) {
494+
debug!(?config, "run_tests");
495+
541496
// If we want to collect rustfix coverage information,
542497
// we first make sure that the coverage file does not exist.
543498
// It will be created later on.

src/tools/compiletest/src/runtest.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::io::prelude::*;
77
use std::io::{self, BufReader};
88
use std::process::{Child, Command, ExitStatus, Output, Stdio};
99
use std::sync::Arc;
10-
use std::{env, iter, str};
10+
use std::{env, fmt, iter, str};
1111

1212
use build_helper::fs::remove_and_create_dir_all;
1313
use camino::{Utf8Path, Utf8PathBuf};
@@ -21,15 +21,13 @@ use crate::common::{
2121
UI_WINDOWS_SVG, expected_output_path, incremental_dir, output_base_dir, output_base_name,
2222
output_testname_unique,
2323
};
24-
use crate::compute_diff::{DiffLine, make_diff, write_diff, write_filtered_diff};
2524
use crate::directives::TestProps;
2625
use crate::errors::{Error, ErrorKind, load_errors};
2726
use crate::read2::{Truncated, read2_abbreviated};
28-
use crate::util::{Utf8PathBufExt, add_dylib_path, logv, static_regex};
27+
use crate::runtest::compute_diff::{DiffLine, make_diff, write_diff, write_filtered_diff};
28+
use crate::util::{Utf8PathBufExt, add_dylib_path, static_regex};
2929
use crate::{ColorConfig, help, json, stamp_file_path, warning};
3030

31-
mod debugger;
32-
3331
// Helper modules that implement test running logic for each test suite.
3432
// tidy-alphabetical-start
3533
mod assembly;
@@ -48,6 +46,8 @@ mod rustdoc_json;
4846
mod ui;
4947
// tidy-alphabetical-end
5048

49+
mod compute_diff;
50+
mod debugger;
5151
#[cfg(test)]
5252
mod tests;
5353

@@ -1459,7 +1459,7 @@ impl<'test> TestCx<'test> {
14591459
) -> ProcRes {
14601460
let cmdline = {
14611461
let cmdline = self.make_cmdline(&command, lib_path);
1462-
logv(self.config, format!("executing {}", cmdline));
1462+
self.logv(format_args!("executing {cmdline}"));
14631463
cmdline
14641464
};
14651465

@@ -2006,6 +2006,18 @@ impl<'test> TestCx<'test> {
20062006
output_base_name(self.config, self.testpaths, self.safe_revision())
20072007
}
20082008

2009+
/// Prints a message to (captured) stdout if `config.verbose` is true.
2010+
/// The message is also logged to `tracing::debug!` regardles of verbosity.
2011+
///
2012+
/// Use `format_args!` as the argument to perform formatting if required.
2013+
fn logv(&self, message: impl fmt::Display) {
2014+
debug!("{message}");
2015+
if self.config.verbose {
2016+
// Note: `./x test ... --verbose --no-capture` is needed to see this print.
2017+
println!("{message}");
2018+
}
2019+
}
2020+
20092021
/// Prefix to print before error messages. Normally just `error`, but also
20102022
/// includes the revision name for tests that use revisions.
20112023
#[must_use]
@@ -2666,20 +2678,17 @@ impl<'test> TestCx<'test> {
26662678
//
26672679
// It's not possible to detect paths in the error messages generally, but this is a
26682680
// decent enough heuristic.
2669-
static_regex!(
2670-
r#"(?x)
2681+
let re = static_regex!(
2682+
r#"(?x)
26712683
(?:
26722684
# Match paths that don't include spaces.
26732685
(?:\\[\pL\pN\.\-_']+)+\.\pL+
26742686
|
26752687
# If the path starts with a well-known root, then allow spaces and no file extension.
26762688
\$(?:DIR|SRC_DIR|TEST_BUILD_DIR|BUILD_DIR|LIB_DIR)(?:\\[\pL\pN\.\-_'\ ]+)+
26772689
)"#
2678-
)
2679-
.replace_all(&output, |caps: &Captures<'_>| {
2680-
println!("{}", &caps[0]);
2681-
caps[0].replace(r"\", "/")
2682-
})
2690+
);
2691+
re.replace_all(&output, |caps: &Captures<'_>| caps[0].replace(r"\", "/"))
26832692
.replace("\r\n", "\n")
26842693
}
26852694

src/tools/compiletest/src/runtest/debuginfo.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use super::debugger::DebuggerCommands;
1010
use super::{Debugger, Emit, ProcRes, TestCx, Truncated, WillExecute};
1111
use crate::common::Config;
1212
use crate::debuggers::{extract_gdb_version, is_android_gdb_target};
13-
use crate::util::logv;
1413

1514
impl TestCx<'_> {
1615
pub(super) fn run_debuginfo_test(&self) {
@@ -234,7 +233,7 @@ impl TestCx<'_> {
234233
gdb.args(debugger_opts);
235234
// FIXME(jieyouxu): don't pass an empty Path
236235
let cmdline = self.make_cmdline(&gdb, Utf8Path::new(""));
237-
logv(self.config, format!("executing {}", cmdline));
236+
self.logv(format_args!("executing {cmdline}"));
238237
cmdline
239238
};
240239

src/tools/compiletest/src/runtest/mir_opt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use miropt_test_tools::{MiroptTest, MiroptTestFile, files_for_miropt_test};
66
use tracing::debug;
77

88
use super::{Emit, TestCx, WillExecute};
9-
use crate::compute_diff::write_diff;
9+
use crate::runtest::compute_diff::write_diff;
1010

1111
impl TestCx<'_> {
1212
pub(super) fn run_mir_opt_test(&self) {

src/tools/compiletest/src/runtest/pretty.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
use std::fs;
22

33
use super::{ProcRes, ReadFrom, TestCx};
4-
use crate::util::logv;
54

65
impl TestCx<'_> {
76
pub(super) fn run_pretty_test(&self) {
87
if self.props.pp_exact.is_some() {
9-
logv(self.config, "testing for exact pretty-printing".to_owned());
8+
self.logv("testing for exact pretty-printing");
109
} else {
11-
logv(self.config, "testing for converging pretty-printing".to_owned());
10+
self.logv("testing for converging pretty-printing");
1211
}
1312

1413
let rounds = match self.props.pp_exact {
@@ -21,10 +20,7 @@ impl TestCx<'_> {
2120

2221
let mut round = 0;
2322
while round < rounds {
24-
logv(
25-
self.config,
26-
format!("pretty-printing round {} revision {:?}", round, self.revision),
27-
);
23+
self.logv(format_args!("pretty-printing round {round} revision {:?}", self.revision));
2824
let read_from =
2925
if round == 0 { ReadFrom::Path } else { ReadFrom::Stdin(srcs[round].to_owned()) };
3026

src/tools/compiletest/src/util.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ use std::env;
22
use std::process::Command;
33

44
use camino::{Utf8Path, Utf8PathBuf};
5-
use tracing::*;
6-
7-
use crate::common::Config;
85

96
#[cfg(test)]
107
mod tests;
@@ -26,14 +23,6 @@ fn path_div() -> &'static str {
2623
";"
2724
}
2825

29-
pub fn logv(config: &Config, s: String) {
30-
debug!("{}", s);
31-
if config.verbose {
32-
// Note: `./x test ... --verbose --no-capture` is needed to see this print.
33-
println!("{}", s);
34-
}
35-
}
36-
3726
pub trait Utf8PathBufExt {
3827
/// Append an extension to the path, even if it already has one.
3928
fn with_extra_extension(&self, extension: &str) -> Utf8PathBuf;

0 commit comments

Comments
 (0)