Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(trycmd): test only exit status with TRYCMD=status #376

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/trycmd/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "trycmd"
version = "0.15.8"
version = "0.15.9"
description = "Snapshot testing for a herd of CLI tests"
authors = ["Ed Page <[email protected]>"]
homepage = "https://github.com/assert-rs/trycmd"
Expand Down
24 changes: 18 additions & 6 deletions crates/trycmd/src/cases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,23 @@ fn parse_include(args: impl IntoIterator<Item = std::ffi::OsString>) -> Option<V
}

fn parse_mode(var: Option<&std::ffi::OsStr>) -> crate::Mode {
if var == Some(std::ffi::OsStr::new("overwrite")) {
crate::Mode::Overwrite
} else if var == Some(std::ffi::OsStr::new("dump")) {
crate::Mode::Dump("dump".into())
} else {
crate::Mode::Fail
use crate::Mode;
match var {
// [`OsStr`] implements [`PartialEq<str>`] so we can compare it with
// [`&str`] directly:
Some(x) if x == "overwrite" => Mode::Overwrite,
Some(x) if x == "dump" => Mode::Dump("dump".into()),
Some(x) if x == "status" => Mode::OnlyStatus,
None => Mode::Fail,
Some(x) => {
#[allow(clippy::print_stderr)]
if !x.is_empty() {
eprintln!(
"Unknown mode: TRYCMD={}, using the default mode",
x.to_string_lossy()
);
}
Mode::Fail
}
}
}
6 changes: 6 additions & 0 deletions crates/trycmd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@
//! ```
//! This will overwrite any existing `.stdout` and `.stderr` file in `tests/cmd`
//!
//! To check only the exit status of tests (ignoring outputs), you can run:
//! ```console
//! $ TRYCMD=status cargo test --test cli_tests
//! ```
//! This may be useful if the command outputs are not reproducible.
//!
//! To filter the tests to those with `name1`, `name2`, etc in their file names, you can run:
//! ```console
//! cargo test --test cli_tests -- cli_tests trycmd=name1 trycmd=name2...
Expand Down
15 changes: 11 additions & 4 deletions crates/trycmd/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ impl Case {
step.expected_status = Some(crate::schema::CommandStatus::Skipped);
}

let step_status = self.run_step(step, cwd.as_deref(), bins, &substitutions);
let step_status = self.run_step(step, cwd.as_deref(), mode, bins, &substitutions);
if fs_context.is_mutable() && step_status.is_err() && *mode == Mode::Fail {
prior_step_failed = true;
}
Expand Down Expand Up @@ -261,7 +261,7 @@ impl Case {
}
}
}
Mode::Fail => {}
Mode::Fail | Mode::OnlyStatus => {}
}

if sequence.fs.sandbox() {
Expand Down Expand Up @@ -305,6 +305,7 @@ impl Case {
&self,
step: &mut crate::schema::Step,
cwd: Option<&std::path::Path>,
mode: &Mode,
bins: &crate::BinRegistry,
substitutions: &snapbox::Redactions,
) -> Result<Output, Output> {
Expand Down Expand Up @@ -362,7 +363,10 @@ impl Case {

// For Mode::Dump's sake, allow running all
let output = self.validate_spawn(output, step.expected_status());
let output = self.validate_streams(output, step, substitutions);
let output = match mode {
Mode::OnlyStatus => output,
_ => self.validate_streams(output, step, substitutions),
};
Comment on lines +366 to +369
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only non-trivial logic is here: we skip validate_streams when we have Mode::OnlyStatus.


if output.is_ok() {
Ok(output)
Expand Down Expand Up @@ -991,6 +995,8 @@ impl std::fmt::Display for FileStatus {
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum Mode {
Fail,
/// Only validate the return statuses of the commands
OnlyStatus,
Overwrite,
Dump(std::path::PathBuf),
}
Expand All @@ -999,6 +1005,7 @@ impl Mode {
pub(crate) fn initialize(&self) -> Result<(), std::io::Error> {
match self {
Self::Fail => {}
Self::OnlyStatus => {}
Self::Overwrite => {}
Self::Dump(root) => {
std::fs::create_dir_all(root)?;
Expand Down Expand Up @@ -1029,7 +1036,7 @@ fn fs_context(
}
Ok(context)
}
Mode::Fail | Mode::Overwrite => {
Mode::Fail | Mode::Overwrite | Mode::OnlyStatus => {
let mut context = snapbox::dir::DirRoot::mutable_temp()?;
if let Some(cwd) = cwd {
context = context.with_template(cwd)?;
Expand Down