Skip to content

Commit c5972fa

Browse files
committed
Add TryFrom<Output> for SanitizedOutput
`SanitizedOutput`s have `String` `stdout` and `stderr` fields. This makes `SanitizedOutput`s easier to display than `Output`s. This commit adds a `TryFrom<Output>` implementation for `SanitizedOutput`. In this way, a test can run a command to produce an `Output`, and from that `Output` produce a `SanitizedOutput`. Extracted from rust-lang#4175
1 parent fe3fd88 commit c5972fa

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/test/mock/clitools.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -763,15 +763,12 @@ impl Config {
763763
self.run_subprocess(name, args.clone(), env)
764764
};
765765
let duration = Instant::now() - start;
766-
let output = SanitizedOutput {
767-
ok: matches!(out.status, Some(0)),
768-
stdout: String::from_utf8(out.stdout).unwrap(),
769-
stderr: String::from_utf8(out.stderr).unwrap(),
770-
};
766+
let status = out.status;
767+
let output: SanitizedOutput = out.try_into().unwrap();
771768

772769
println!("ran: {} {:?}", name, args);
773770
println!("inprocess: {inprocess}");
774-
println!("status: {:?}", out.status);
771+
println!("status: {:?}", status);
775772
println!("duration: {:.3}s", duration.as_secs_f32());
776773
println!("stdout:\n====\n{}\n====\n", output.stdout);
777774
println!("stderr:\n====\n{}\n====\n", output.stderr);
@@ -920,6 +917,18 @@ pub struct SanitizedOutput {
920917
pub stderr: String,
921918
}
922919

920+
impl TryFrom<Output> for SanitizedOutput {
921+
type Error = std::string::FromUtf8Error;
922+
fn try_from(out: Output) -> Result<Self, Self::Error> {
923+
let sanitized_output = Self {
924+
ok: matches!(out.status, Some(0)),
925+
stdout: String::from_utf8(out.stdout)?,
926+
stderr: String::from_utf8(out.stderr)?,
927+
};
928+
Ok(sanitized_output)
929+
}
930+
}
931+
923932
pub fn cmd<I, A>(config: &Config, name: &str, args: I) -> Command
924933
where
925934
I: IntoIterator<Item = A>,

0 commit comments

Comments
 (0)