Skip to content

Commit d20216c

Browse files
committed
docs(test): Document Execs assertions based on port effort
A lot of this was pulled from #14039
1 parent 84ac190 commit d20216c

File tree

1 file changed

+154
-8
lines changed
  • crates/cargo-test-support/src

1 file changed

+154
-8
lines changed

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

Lines changed: 154 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,57 @@ impl Execs {
667667
/// Verifies that stdout is equal to the given lines.
668668
///
669669
/// See [`compare::assert_e2e`] for assertion details.
670+
///
671+
/// <div class="warning">
672+
///
673+
/// Prefer passing in [`str!`] for `expected` to get snapshot updating.
674+
///
675+
/// If `format!` is needed for content that changes from run to run that you don't care about,
676+
/// consider whether you could have [`compare::assert_e2e`] redact the content.
677+
/// If nothing else, a wildcard (`[..]`, `...`) may be useful.
678+
///
679+
/// However, `""` may be preferred for intentionally empty output so people don't accidentally
680+
/// bless a change.
681+
///
682+
/// </div>
683+
///
684+
/// # Examples
685+
///
686+
/// ```no_run
687+
/// use cargo_test_support::prelude::*;
688+
/// use cargo_test_support::str;
689+
/// use cargo_test_support::execs;
690+
///
691+
/// execs().with_stdout_data(str![r#"
692+
/// Hello world!
693+
/// "#]);
694+
/// ```
695+
///
696+
/// Non-deterministic compiler output
697+
/// ```no_run
698+
/// use cargo_test_support::prelude::*;
699+
/// use cargo_test_support::str;
700+
/// use cargo_test_support::execs;
701+
///
702+
/// execs().with_stdout_data(str![r#"
703+
/// [COMPILING] foo
704+
/// [COMPILING] bar
705+
/// "#].unordered());
706+
/// ```
707+
///
708+
/// jsonlines
709+
/// ```no_run
710+
/// use cargo_test_support::prelude::*;
711+
/// use cargo_test_support::str;
712+
/// use cargo_test_support::execs;
713+
///
714+
/// execs().with_stdout_data(str![r#"
715+
/// [
716+
/// {},
717+
/// {}
718+
/// ]
719+
/// "#].is_json().against_jsonlines());
720+
/// ```
670721
pub fn with_stdout_data(&mut self, expected: impl snapbox::IntoData) -> &mut Self {
671722
self.expect_stdout_data = Some(expected.into_data());
672723
self
@@ -675,6 +726,57 @@ impl Execs {
675726
/// Verifies that stderr is equal to the given lines.
676727
///
677728
/// See [`compare::assert_e2e`] for assertion details.
729+
///
730+
/// <div class="warning">
731+
///
732+
/// Prefer passing in [`str!`] for `expected` to get snapshot updating.
733+
///
734+
/// If `format!` is needed for content that changes from run to run that you don't care about,
735+
/// consider whether you could have [`compare::assert_e2e`] redact the content.
736+
/// If nothing else, a wildcard (`[..]`, `...`) may be useful.
737+
///
738+
/// However, `""` may be preferred for intentionally empty output so people don't accidentally
739+
/// bless a change.
740+
///
741+
/// </div>
742+
///
743+
/// # Examples
744+
///
745+
/// ```no_run
746+
/// use cargo_test_support::prelude::*;
747+
/// use cargo_test_support::str;
748+
/// use cargo_test_support::execs;
749+
///
750+
/// execs().with_stderr_data(str![r#"
751+
/// Hello world!
752+
/// "#]);
753+
/// ```
754+
///
755+
/// Non-deterministic compiler output
756+
/// ```no_run
757+
/// use cargo_test_support::prelude::*;
758+
/// use cargo_test_support::str;
759+
/// use cargo_test_support::execs;
760+
///
761+
/// execs().with_stderr_data(str![r#"
762+
/// [COMPILING] foo
763+
/// [COMPILING] bar
764+
/// "#].unordered());
765+
/// ```
766+
///
767+
/// jsonlines
768+
/// ```no_run
769+
/// use cargo_test_support::prelude::*;
770+
/// use cargo_test_support::str;
771+
/// use cargo_test_support::execs;
772+
///
773+
/// execs().with_stderr_data(str![r#"
774+
/// [
775+
/// {},
776+
/// {}
777+
/// ]
778+
/// "#].is_json().against_jsonlines());
779+
/// ```
678780
pub fn with_stderr_data(&mut self, expected: impl snapbox::IntoData) -> &mut Self {
679781
self.expect_stderr_data = Some(expected.into_data());
680782
self
@@ -706,6 +808,14 @@ impl Execs {
706808
/// its output.
707809
///
708810
/// See [`compare`] for supported patterns.
811+
///
812+
/// <div class="warning">
813+
///
814+
/// Prefer [`Execs::with_stdout_data`] where possible.
815+
/// - `expected` cannot be snapshotted
816+
/// - `expected` can end up being ambiguous, causing the assertion to succeed when it should fail
817+
///
818+
/// </div>
709819
#[deprecated(note = "replaced with `Execs::with_stdout_data(expected)`")]
710820
pub fn with_stdout_contains<S: ToString>(&mut self, expected: S) -> &mut Self {
711821
self.expect_stdout_contains.push(expected.to_string());
@@ -716,6 +826,14 @@ impl Execs {
716826
/// its output.
717827
///
718828
/// See [`compare`] for supported patterns.
829+
///
830+
/// <div class="warning">
831+
///
832+
/// Prefer [`Execs::with_stderr_data`] where possible.
833+
/// - `expected` cannot be snapshotted
834+
/// - `expected` can end up being ambiguous, causing the assertion to succeed when it should fail
835+
///
836+
/// </div>
719837
#[deprecated(note = "replaced with `Execs::with_stderr_data(expected)`")]
720838
pub fn with_stderr_contains<S: ToString>(&mut self, expected: S) -> &mut Self {
721839
self.expect_stderr_contains.push(expected.to_string());
@@ -727,6 +845,18 @@ impl Execs {
727845
/// See [`compare`] for supported patterns.
728846
///
729847
/// See note on [`Self::with_stderr_does_not_contain`].
848+
///
849+
/// <div class="warning">
850+
///
851+
/// Prefer [`Execs::with_stdout_data`] where possible.
852+
/// - `expected` cannot be snapshotted
853+
/// - The absence of `expected` can either mean success or that the string being looked for
854+
/// changed.
855+
///
856+
/// To mitigate this, consider matching this up with
857+
/// [`Execs::with_stdout_contains`].
858+
///
859+
/// </div>
730860
#[deprecated]
731861
pub fn with_stdout_does_not_contain<S: ToString>(&mut self, expected: S) -> &mut Self {
732862
self.expect_stdout_not_contains.push(expected.to_string());
@@ -737,11 +867,18 @@ impl Execs {
737867
///
738868
/// See [`compare`] for supported patterns.
739869
///
740-
/// Care should be taken when using this method because there is a
741-
/// limitless number of possible things that *won't* appear. A typo means
742-
/// your test will pass without verifying the correct behavior. If
743-
/// possible, write the test first so that it fails, and then implement
744-
/// your fix/feature to make it pass.
870+
/// <div class="warning">
871+
///
872+
/// Prefer [`Execs::with_stdout_data`] where possible.
873+
/// - `expected` cannot be snapshotted
874+
/// - The absence of `expected` can either mean success or that the string being looked for
875+
/// changed.
876+
///
877+
/// To mitigate this, consider either matching this up with
878+
/// [`Execs::with_stdout_contains`] or replace it
879+
/// with [`Execs::with_stderr_line_without`].
880+
///
881+
/// </div>
745882
#[deprecated]
746883
pub fn with_stderr_does_not_contain<S: ToString>(&mut self, expected: S) -> &mut Self {
747884
self.expect_stderr_not_contains.push(expected.to_string());
@@ -751,7 +888,18 @@ impl Execs {
751888
/// Verify that a particular line appears in stderr with and without the
752889
/// given substrings. Exactly one line must match.
753890
///
754-
/// The substrings are matched as `contains`. Example:
891+
/// The substrings are matched as `contains`.
892+
///
893+
/// <div class="warning">
894+
///
895+
/// Prefer [`Execs::with_stdout_data`] where possible.
896+
/// - `with` cannot be snapshotted
897+
/// - The absence of `without`` can either mean success or that the string being looked for
898+
/// changed.
899+
///
900+
/// </div>
901+
///
902+
/// # Example
755903
///
756904
/// ```no_run
757905
/// use cargo_test_support::execs;
@@ -768,8 +916,6 @@ impl Execs {
768916
/// This will check that a build line includes `-C opt-level=3` but does
769917
/// not contain `-C debuginfo` or `-C incremental`.
770918
///
771-
/// Be careful writing the `without` fragments, see note in
772-
/// `with_stderr_does_not_contain`.
773919
#[deprecated]
774920
pub fn with_stderr_line_without<S: ToString>(
775921
&mut self,

0 commit comments

Comments
 (0)