Skip to content

Commit b057fcc

Browse files
committed
At test discovery, write to logfile in the same format as to stdout.
Example of affected command: ``` cargo test -- --logfile=/tmp/1.log --list ```
1 parent e9a4a61 commit b057fcc

File tree

2 files changed

+35
-30
lines changed

2 files changed

+35
-30
lines changed

library/test/src/console.rs

+34-29
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use std::fs::File;
44
use std::io;
55
use std::io::prelude::Write;
6+
use std::path::PathBuf;
67
use std::time::Instant;
78
use std::vec;
89

@@ -73,6 +74,33 @@ struct OutputMultiplexer {
7374
pub outputs: Vec<Box<dyn Output>>,
7475
}
7576

77+
impl OutputMultiplexer {
78+
pub fn new(lock_stdout: bool, logfile: &Option<PathBuf>) -> io::Result<Self> {
79+
let mut outputs: Vec<Box<dyn Output>> = vec![];
80+
81+
if lock_stdout {
82+
let output = match term::stdout() {
83+
None => OutputLocation::Raw(io::stdout().lock()),
84+
Some(t) => OutputLocation::Pretty(t),
85+
};
86+
outputs.push(Box::new(output))
87+
} else {
88+
let output = match term::stdout() {
89+
None => OutputLocation::Raw(io::stdout()),
90+
Some(t) => OutputLocation::Pretty(t),
91+
};
92+
outputs.push(Box::new(output))
93+
}
94+
95+
match logfile {
96+
Some(ref path) => outputs.push(Box::new(OutputLocation::Raw(File::create(path)?))),
97+
None => (),
98+
};
99+
100+
Ok(Self { outputs })
101+
}
102+
}
103+
76104
impl Output for OutputMultiplexer {
77105
fn write_pretty(&mut self, word: &str, color: term::color::Color) -> io::Result<()> {
78106
for output in &mut self.outputs {
@@ -92,33 +120,14 @@ impl Output for OutputMultiplexer {
92120
}
93121

94122
pub struct ConsoleTestDiscoveryState {
95-
log_out: OutputMultiplexer,
96123
pub tests: usize,
97124
pub benchmarks: usize,
98125
pub ignored: usize,
99126
}
100127

101128
impl ConsoleTestDiscoveryState {
102129
pub fn new(opts: &TestOpts) -> io::Result<ConsoleTestDiscoveryState> {
103-
let mut log_out = OutputMultiplexer { outputs: vec![] };
104-
match opts.logfile {
105-
Some(ref path) => {
106-
log_out.outputs.push(Box::new(OutputLocation::Raw(File::create(path)?)))
107-
}
108-
None => (),
109-
};
110-
111-
Ok(ConsoleTestDiscoveryState { log_out, tests: 0, benchmarks: 0, ignored: 0 })
112-
}
113-
114-
pub fn write_log<F, S>(&mut self, msg: F) -> io::Result<()>
115-
where
116-
S: AsRef<str>,
117-
F: FnOnce() -> S,
118-
{
119-
let msg = msg();
120-
let msg = msg.as_ref();
121-
self.log_out.write_plain(msg)
130+
Ok(ConsoleTestDiscoveryState { tests: 0, benchmarks: 0, ignored: 0 })
122131
}
123132
}
124133

@@ -219,19 +228,16 @@ impl ConsoleTestState {
219228

220229
// List the tests to console, and optionally to logfile. Filters are honored.
221230
pub fn list_tests_console(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> io::Result<()> {
222-
let mut output = match term::stdout() {
223-
None => OutputLocation::Raw(io::stdout().lock()),
224-
Some(t) => OutputLocation::Pretty(t),
225-
};
231+
let mut st = ConsoleTestDiscoveryState::new(opts)?;
226232

233+
let mut multiplexer = OutputMultiplexer::new(true, &opts.logfile)?;
227234
let mut out: Box<dyn OutputFormatter> = match opts.format {
228235
OutputFormat::Pretty | OutputFormat::Junit => {
229-
Box::new(PrettyFormatter::new(&mut output, false, 0, false, None))
236+
Box::new(PrettyFormatter::new(&mut multiplexer, false, 0, false, None))
230237
}
231-
OutputFormat::Terse => Box::new(TerseFormatter::new(&mut output, false, 0, false)),
232-
OutputFormat::Json => Box::new(JsonFormatter::new(&mut output)),
238+
OutputFormat::Terse => Box::new(TerseFormatter::new(&mut multiplexer, false, 0, false)),
239+
OutputFormat::Json => Box::new(JsonFormatter::new(&mut multiplexer)),
233240
};
234-
let mut st = ConsoleTestDiscoveryState::new(opts)?;
235241

236242
out.write_discovery_start()?;
237243
for test in filter_tests(opts, tests).into_iter() {
@@ -253,7 +259,6 @@ pub fn list_tests_console(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> io::Res
253259
st.ignored += if desc.ignore { 1 } else { 0 };
254260

255261
out.write_test_discovered(&desc, fntype)?;
256-
st.write_log(|| format!("{fntype} {}\n", desc.name))?;
257262
}
258263

259264
out.write_discovery_finish(&st)

library/test/src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -994,7 +994,7 @@ fn test_discovery_logfile_format() {
994994
// Split output at line breaks to make the comparison platform-agnostic regarding newline style.
995995
let contents_lines = contents.as_str().lines().collect::<Vec<&str>>();
996996

997-
assert_eq!(contents_lines, vec!["test whatever"]);
997+
assert_eq!(contents_lines, vec!["whatever: test", "", "1 test, 0 benchmarks"]);
998998
}
999999

10001000
#[test]

0 commit comments

Comments
 (0)