Skip to content

Commit ff2ddeb

Browse files
committed
feat(test): Allow snapshotting of Execs
1 parent 0761937 commit ff2ddeb

File tree

1 file changed

+43
-0
lines changed
  • crates/cargo-test-support/src

1 file changed

+43
-0
lines changed

crates/cargo-test-support/src/lib.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use std::time::{self, Duration};
2525

2626
use anyhow::{bail, Result};
2727
use cargo_util::{is_ci, ProcessBuilder, ProcessError};
28+
use snapbox::IntoData as _;
2829
use url::Url;
2930

3031
use self::paths::CargoPathExt;
@@ -534,6 +535,8 @@ pub struct Execs {
534535
expect_stdin: Option<String>,
535536
expect_stderr: Option<String>,
536537
expect_exit_code: Option<i32>,
538+
expect_stdout_data: Option<snapbox::Data>,
539+
expect_stderr_data: Option<snapbox::Data>,
537540
expect_stdout_contains: Vec<String>,
538541
expect_stderr_contains: Vec<String>,
539542
expect_stdout_contains_n: Vec<(String, usize)>,
@@ -545,6 +548,7 @@ pub struct Execs {
545548
expect_json: Option<String>,
546549
expect_json_contains_unordered: Option<String>,
547550
stream_output: bool,
551+
assert: snapbox::Assert,
548552
}
549553

550554
impl Execs {
@@ -567,6 +571,22 @@ impl Execs {
567571
self
568572
}
569573

574+
/// Verifies that stdout is equal to the given lines.
575+
///
576+
/// See [`compare::assert_e2e`] for assertion details.
577+
pub fn with_stdout_data(&mut self, expected: impl snapbox::IntoData) -> &mut Self {
578+
self.expect_stdout_data = Some(expected.into_data());
579+
self
580+
}
581+
582+
/// Verifies that stderr is equal to the given lines.
583+
///
584+
/// See [`compare::assert_e2e`] for assertion details.
585+
pub fn with_stderr_data(&mut self, expected: impl snapbox::IntoData) -> &mut Self {
586+
self.expect_stderr_data = Some(expected.into_data());
587+
self
588+
}
589+
570590
/// Writes the given lines to stdin.
571591
pub fn with_stdin<S: ToString>(&mut self, expected: S) -> &mut Self {
572592
self.expect_stdin = Some(expected.to_string());
@@ -914,6 +934,8 @@ impl Execs {
914934
&& self.expect_stdout.is_none()
915935
&& self.expect_stdin.is_none()
916936
&& self.expect_stderr.is_none()
937+
&& self.expect_stdout_data.is_none()
938+
&& self.expect_stderr_data.is_none()
917939
&& self.expect_stdout_contains.is_empty()
918940
&& self.expect_stderr_contains.is_empty()
919941
&& self.expect_stdout_contains_n.is_empty()
@@ -1011,6 +1033,24 @@ impl Execs {
10111033
if let Some(expect_stderr) = &self.expect_stderr {
10121034
compare::match_exact(expect_stderr, stderr, "stderr", stdout, cwd)?;
10131035
}
1036+
if let Some(expect_stdout_data) = &self.expect_stdout_data {
1037+
if let Err(err) = self.assert.try_eq(
1038+
Some(&"stdout"),
1039+
stdout.into_data(),
1040+
expect_stdout_data.clone(),
1041+
) {
1042+
panic!("{err}")
1043+
}
1044+
}
1045+
if let Some(expect_stderr_data) = &self.expect_stderr_data {
1046+
if let Err(err) = self.assert.try_eq(
1047+
Some(&"stderr"),
1048+
stderr.into_data(),
1049+
expect_stderr_data.clone(),
1050+
) {
1051+
panic!("{err}")
1052+
}
1053+
}
10141054
for expect in self.expect_stdout_contains.iter() {
10151055
compare::match_contains(expect, stdout, cwd)?;
10161056
}
@@ -1063,6 +1103,8 @@ pub fn execs() -> Execs {
10631103
expect_stderr: None,
10641104
expect_stdin: None,
10651105
expect_exit_code: Some(0),
1106+
expect_stdout_data: None,
1107+
expect_stderr_data: None,
10661108
expect_stdout_contains: Vec::new(),
10671109
expect_stderr_contains: Vec::new(),
10681110
expect_stdout_contains_n: Vec::new(),
@@ -1074,6 +1116,7 @@ pub fn execs() -> Execs {
10741116
expect_json: None,
10751117
expect_json_contains_unordered: None,
10761118
stream_output: false,
1119+
assert: compare::assert_e2e(),
10771120
}
10781121
}
10791122

0 commit comments

Comments
 (0)