@@ -25,6 +25,7 @@ use std::time::{self, Duration};
25
25
26
26
use anyhow:: { bail, Result } ;
27
27
use cargo_util:: { is_ci, ProcessBuilder , ProcessError } ;
28
+ use snapbox:: IntoData as _;
28
29
use url:: Url ;
29
30
30
31
use self :: paths:: CargoPathExt ;
@@ -534,6 +535,8 @@ pub struct Execs {
534
535
expect_stdin : Option < String > ,
535
536
expect_stderr : Option < String > ,
536
537
expect_exit_code : Option < i32 > ,
538
+ expect_stdout_data : Option < snapbox:: Data > ,
539
+ expect_stderr_data : Option < snapbox:: Data > ,
537
540
expect_stdout_contains : Vec < String > ,
538
541
expect_stderr_contains : Vec < String > ,
539
542
expect_stdout_contains_n : Vec < ( String , usize ) > ,
@@ -545,6 +548,7 @@ pub struct Execs {
545
548
expect_json : Option < String > ,
546
549
expect_json_contains_unordered : Option < String > ,
547
550
stream_output : bool ,
551
+ assert : snapbox:: Assert ,
548
552
}
549
553
550
554
impl Execs {
@@ -555,18 +559,36 @@ impl Execs {
555
559
556
560
/// Verifies that stdout is equal to the given lines.
557
561
/// See [`compare`] for supported patterns.
562
+ #[ deprecated( note = "replaced with `Execs::with_stdout_data(expected)`" ) ]
558
563
pub fn with_stdout < S : ToString > ( & mut self , expected : S ) -> & mut Self {
559
564
self . expect_stdout = Some ( expected. to_string ( ) ) ;
560
565
self
561
566
}
562
567
563
568
/// Verifies that stderr is equal to the given lines.
564
569
/// See [`compare`] for supported patterns.
570
+ #[ deprecated( note = "replaced with `Execs::with_stderr_data(expected)`" ) ]
565
571
pub fn with_stderr < S : ToString > ( & mut self , expected : S ) -> & mut Self {
566
572
self . expect_stderr = Some ( expected. to_string ( ) ) ;
567
573
self
568
574
}
569
575
576
+ /// Verifies that stdout is equal to the given lines.
577
+ ///
578
+ /// See [`compare::assert_e2e`] for assertion details.
579
+ pub fn with_stdout_data ( & mut self , expected : impl snapbox:: IntoData ) -> & mut Self {
580
+ self . expect_stdout_data = Some ( expected. into_data ( ) ) ;
581
+ self
582
+ }
583
+
584
+ /// Verifies that stderr is equal to the given lines.
585
+ ///
586
+ /// See [`compare::assert_e2e`] for assertion details.
587
+ pub fn with_stderr_data ( & mut self , expected : impl snapbox:: IntoData ) -> & mut Self {
588
+ self . expect_stderr_data = Some ( expected. into_data ( ) ) ;
589
+ self
590
+ }
591
+
570
592
/// Writes the given lines to stdin.
571
593
pub fn with_stdin < S : ToString > ( & mut self , expected : S ) -> & mut Self {
572
594
self . expect_stdin = Some ( expected. to_string ( ) ) ;
@@ -593,6 +615,7 @@ impl Execs {
593
615
/// its output.
594
616
///
595
617
/// See [`compare`] for supported patterns.
618
+ #[ deprecated( note = "replaced with `Execs::with_stdout_data(expected)`" ) ]
596
619
pub fn with_stdout_contains < S : ToString > ( & mut self , expected : S ) -> & mut Self {
597
620
self . expect_stdout_contains . push ( expected. to_string ( ) ) ;
598
621
self
@@ -602,6 +625,7 @@ impl Execs {
602
625
/// its output.
603
626
///
604
627
/// See [`compare`] for supported patterns.
628
+ #[ deprecated( note = "replaced with `Execs::with_stderr_data(expected)`" ) ]
605
629
pub fn with_stderr_contains < S : ToString > ( & mut self , expected : S ) -> & mut Self {
606
630
self . expect_stderr_contains . push ( expected. to_string ( ) ) ;
607
631
self
@@ -611,6 +635,7 @@ impl Execs {
611
635
/// its output, and should be repeated `number` times.
612
636
///
613
637
/// See [`compare`] for supported patterns.
638
+ #[ deprecated( note = "replaced with `Execs::with_stdout_data(expected)`" ) ]
614
639
pub fn with_stdout_contains_n < S : ToString > ( & mut self , expected : S , number : usize ) -> & mut Self {
615
640
self . expect_stdout_contains_n
616
641
. push ( ( expected. to_string ( ) , number) ) ;
@@ -622,6 +647,7 @@ impl Execs {
622
647
/// See [`compare`] for supported patterns.
623
648
///
624
649
/// See note on [`Self::with_stderr_does_not_contain`].
650
+ #[ deprecated]
625
651
pub fn with_stdout_does_not_contain < S : ToString > ( & mut self , expected : S ) -> & mut Self {
626
652
self . expect_stdout_not_contains . push ( expected. to_string ( ) ) ;
627
653
self
@@ -636,6 +662,7 @@ impl Execs {
636
662
/// your test will pass without verifying the correct behavior. If
637
663
/// possible, write the test first so that it fails, and then implement
638
664
/// your fix/feature to make it pass.
665
+ #[ deprecated]
639
666
pub fn with_stderr_does_not_contain < S : ToString > ( & mut self , expected : S ) -> & mut Self {
640
667
self . expect_stderr_not_contains . push ( expected. to_string ( ) ) ;
641
668
self
@@ -645,6 +672,7 @@ impl Execs {
645
672
/// ignoring the order of the lines.
646
673
///
647
674
/// See [`Execs::with_stderr_unordered`] for more details.
675
+ #[ deprecated( note = "replaced with `Execs::with_stdout_data(expected.unordered())`" ) ]
648
676
pub fn with_stdout_unordered < S : ToString > ( & mut self , expected : S ) -> & mut Self {
649
677
self . expect_stdout_unordered . push ( expected. to_string ( ) ) ;
650
678
self
@@ -671,6 +699,7 @@ impl Execs {
671
699
///
672
700
/// This will randomly fail if the other crate name is `bar`, and the
673
701
/// order changes.
702
+ #[ deprecated( note = "replaced with `Execs::with_stderr_data(expected.unordered())`" ) ]
674
703
pub fn with_stderr_unordered < S : ToString > ( & mut self , expected : S ) -> & mut Self {
675
704
self . expect_stderr_unordered . push ( expected. to_string ( ) ) ;
676
705
self
@@ -698,6 +727,7 @@ impl Execs {
698
727
///
699
728
/// Be careful writing the `without` fragments, see note in
700
729
/// `with_stderr_does_not_contain`.
730
+ #[ deprecated]
701
731
pub fn with_stderr_line_without < S : ToString > (
702
732
& mut self ,
703
733
with : & [ S ] ,
@@ -730,6 +760,7 @@ impl Execs {
730
760
/// - The order of arrays is ignored.
731
761
/// - Strings support patterns described in [`compare`].
732
762
/// - Use `"{...}"` to match any object.
763
+ #[ deprecated( note = "replaced with `Execs::with_stdout_data(expected.json_lines())`" ) ]
733
764
pub fn with_json ( & mut self , expected : & str ) -> & mut Self {
734
765
self . expect_json = Some ( expected. to_string ( ) ) ;
735
766
self
@@ -744,6 +775,7 @@ impl Execs {
744
775
/// what you are doing.
745
776
///
746
777
/// See `with_json` for more detail.
778
+ #[ deprecated]
747
779
pub fn with_json_contains_unordered ( & mut self , expected : & str ) -> & mut Self {
748
780
match & mut self . expect_json_contains_unordered {
749
781
None => self . expect_json_contains_unordered = Some ( expected. to_string ( ) ) ,
@@ -908,11 +940,14 @@ impl Execs {
908
940
}
909
941
}
910
942
943
+ #[ track_caller]
911
944
fn verify_checks_output ( & self , stdout : & [ u8 ] , stderr : & [ u8 ] ) {
912
945
if self . expect_exit_code . unwrap_or ( 0 ) != 0
913
946
&& self . expect_stdout . is_none ( )
914
947
&& self . expect_stdin . is_none ( )
915
948
&& self . expect_stderr . is_none ( )
949
+ && self . expect_stdout_data . is_none ( )
950
+ && self . expect_stderr_data . is_none ( )
916
951
&& self . expect_stdout_contains . is_empty ( )
917
952
&& self . expect_stderr_contains . is_empty ( )
918
953
&& self . expect_stdout_contains_n . is_empty ( )
@@ -934,6 +969,7 @@ impl Execs {
934
969
}
935
970
}
936
971
972
+ #[ track_caller]
937
973
fn match_process ( & self , process : & ProcessBuilder ) -> Result < RawOutput > {
938
974
println ! ( "running {}" , process) ;
939
975
let res = if self . stream_output {
@@ -984,6 +1020,7 @@ impl Execs {
984
1020
}
985
1021
}
986
1022
1023
+ #[ track_caller]
987
1024
fn match_output ( & self , code : Option < i32 > , stdout : & [ u8 ] , stderr : & [ u8 ] ) -> Result < ( ) > {
988
1025
self . verify_checks_output ( stdout, stderr) ;
989
1026
let stdout = std:: str:: from_utf8 ( stdout) . expect ( "stdout is not utf8" ) ;
@@ -1008,6 +1045,24 @@ impl Execs {
1008
1045
if let Some ( expect_stderr) = & self . expect_stderr {
1009
1046
compare:: match_exact ( expect_stderr, stderr, "stderr" , stdout, cwd) ?;
1010
1047
}
1048
+ if let Some ( expect_stdout_data) = & self . expect_stdout_data {
1049
+ if let Err ( err) = self . assert . try_eq (
1050
+ Some ( & "stdout" ) ,
1051
+ stdout. into_data ( ) ,
1052
+ expect_stdout_data. clone ( ) ,
1053
+ ) {
1054
+ panic ! ( "{err}" )
1055
+ }
1056
+ }
1057
+ if let Some ( expect_stderr_data) = & self . expect_stderr_data {
1058
+ if let Err ( err) = self . assert . try_eq (
1059
+ Some ( & "stderr" ) ,
1060
+ stderr. into_data ( ) ,
1061
+ expect_stderr_data. clone ( ) ,
1062
+ ) {
1063
+ panic ! ( "{err}" )
1064
+ }
1065
+ }
1011
1066
for expect in self . expect_stdout_contains . iter ( ) {
1012
1067
compare:: match_contains ( expect, stdout, cwd) ?;
1013
1068
}
@@ -1060,6 +1115,8 @@ pub fn execs() -> Execs {
1060
1115
expect_stderr : None ,
1061
1116
expect_stdin : None ,
1062
1117
expect_exit_code : Some ( 0 ) ,
1118
+ expect_stdout_data : None ,
1119
+ expect_stderr_data : None ,
1063
1120
expect_stdout_contains : Vec :: new ( ) ,
1064
1121
expect_stderr_contains : Vec :: new ( ) ,
1065
1122
expect_stdout_contains_n : Vec :: new ( ) ,
@@ -1071,6 +1128,7 @@ pub fn execs() -> Execs {
1071
1128
expect_json : None ,
1072
1129
expect_json_contains_unordered : None ,
1073
1130
stream_output : false ,
1131
+ assert : compare:: assert_e2e ( ) ,
1074
1132
}
1075
1133
}
1076
1134
0 commit comments