Skip to content

Commit

Permalink
feat(trycmd): Report test duration
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Feb 12, 2025
1 parent fcb5217 commit 298830f
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions crates/trycmd/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,27 +60,43 @@ impl Runner {
snapbox::debug!("Case: {:#?}", s);
match s {
Ok(status) => {
let _ = writeln!(
let _ = write!(
stderr,
"{} {} ... {}",
palette.hint("Testing"),
status.name(),
status.spawn.status.summary()
status.spawn.status.summary(),
);
if let Some(duration) = status.duration {
let _ = write!(
stderr,
" {}",
palette.hint(humantime::format_duration(duration)),
);
}
let _ = writeln!(stderr);
if !status.is_ok() {
// Assuming `status` will print the newline
let _ = write!(stderr, "{}", &status);
}
None
}
Err(status) => {
let _ = writeln!(
let _ = write!(
stderr,
"{} {} ... {}",
palette.hint("Testing"),
status.name(),
palette.error("failed"),
);
if let Some(duration) = status.duration {
let _ = write!(
stderr,
" {}",
palette.hint(humantime::format_duration(duration)),
);
}
let _ = writeln!(stderr);
// Assuming `status` will print the newline
let _ = write!(stderr, "{}", &status);
Some(status)
Expand Down Expand Up @@ -356,10 +372,13 @@ impl Case {
}

let cmd = step.to_command(cwd).map_err(|e| output.clone().error(e))?;
let timer = std::time::Instant::now();
let cmd_output = cmd
.output()
.map_err(|e| output.clone().error(e.to_string().into()))?;

let output = output.output(cmd_output);
let output = output.duration(timer.elapsed());

// For Mode::Dump's sake, allow running all
let output = self.validate_spawn(output, step.expected_status());
Expand Down Expand Up @@ -549,6 +568,7 @@ pub(crate) struct Output {
stdout: Option<Stream>,
stderr: Option<Stream>,
fs: Filesystem,
duration: Option<std::time::Duration>,
}

impl Output {
Expand All @@ -563,6 +583,7 @@ impl Output {
stdout: None,
stderr: None,
fs: Default::default(),
duration: Default::default(),
}
}

Expand All @@ -574,6 +595,7 @@ impl Output {
stdout: None,
stderr: None,
fs: Default::default(),
duration: Default::default(),
}
}

Expand All @@ -599,6 +621,11 @@ impl Output {
self
}

fn duration(mut self, duration: std::time::Duration) -> Self {
self.duration = Some(duration);
self
}

fn is_ok(&self) -> bool {
self.spawn.is_ok()
&& self.stdout.as_ref().map(|s| s.is_ok()).unwrap_or(true)
Expand Down

0 comments on commit 298830f

Please sign in to comment.