@@ -45,8 +45,11 @@ use crate::cross_compile::try_alternate;
45
45
use crate :: paths;
46
46
use crate :: { diff, rustc_host} ;
47
47
use anyhow:: { bail, Result } ;
48
+ use snapbox:: Data ;
49
+ use snapbox:: IntoData ;
48
50
use std:: fmt;
49
51
use std:: path:: Path ;
52
+ use std:: path:: PathBuf ;
50
53
use std:: str;
51
54
use url:: Url ;
52
55
@@ -428,46 +431,6 @@ fn substitute_macros(input: &str) -> String {
428
431
result
429
432
}
430
433
431
- /// Compares one string against another, checking that they both match.
432
- ///
433
- /// See [Patterns](index.html#patterns) for more information on pattern matching.
434
- ///
435
- /// - `description` explains where the output is from (usually "stdout" or "stderr").
436
- /// - `other_output` is other output to display in the error (usually stdout or stderr).
437
- pub ( crate ) fn match_exact (
438
- expected : & str ,
439
- actual : & str ,
440
- description : & str ,
441
- other_output : & str ,
442
- cwd : Option < & Path > ,
443
- ) -> Result < ( ) > {
444
- let expected = normalize_expected ( expected, cwd) ;
445
- let actual = normalize_actual ( actual, cwd) ;
446
- let e: Vec < _ > = expected. lines ( ) . map ( WildStr :: new) . collect ( ) ;
447
- let a: Vec < _ > = actual. lines ( ) . map ( WildStr :: new) . collect ( ) ;
448
- if e == a {
449
- return Ok ( ( ) ) ;
450
- }
451
- let diff = diff:: colored_diff ( & e, & a) ;
452
- bail ! (
453
- "{} did not match:\n \
454
- {}\n \n \
455
- other output:\n \
456
- {}\n ",
457
- description,
458
- diff,
459
- other_output,
460
- ) ;
461
- }
462
-
463
- /// Convenience wrapper around [`match_exact`] which will panic on error.
464
- #[ track_caller]
465
- pub ( crate ) fn assert_match_exact ( expected : & str , actual : & str ) {
466
- if let Err ( e) = match_exact ( expected, actual, "" , "" , None ) {
467
- crate :: panic_error ( "" , e) ;
468
- }
469
- }
470
-
471
434
/// Checks that the given string contains the given lines, ignoring the order
472
435
/// of the lines.
473
436
///
@@ -706,6 +669,127 @@ impl fmt::Debug for WildStr<'_> {
706
669
}
707
670
}
708
671
672
+ pub struct InMemoryDir {
673
+ files : Vec < ( PathBuf , Data ) > ,
674
+ }
675
+
676
+ impl InMemoryDir {
677
+ pub fn paths ( & self ) -> impl Iterator < Item = & Path > {
678
+ self . files . iter ( ) . map ( |( p, _) | p. as_path ( ) )
679
+ }
680
+
681
+ #[ track_caller]
682
+ pub fn assert_contains ( & self , expected : & Self ) {
683
+ let assert = assert_e2e ( ) ;
684
+ for ( path, expected_data) in & expected. files {
685
+ let actual_data = self
686
+ . files
687
+ . iter ( )
688
+ . find_map ( |( p, d) | ( path == p) . then ( || d. clone ( ) ) )
689
+ . unwrap_or_else ( || Data :: new ( ) ) ;
690
+ assert. eq ( actual_data, expected_data) ;
691
+ }
692
+ }
693
+ }
694
+
695
+ impl < P , D > FromIterator < ( P , D ) > for InMemoryDir
696
+ where
697
+ P : Into < std:: path:: PathBuf > ,
698
+ D : IntoData ,
699
+ {
700
+ fn from_iter < I : IntoIterator < Item = ( P , D ) > > ( files : I ) -> Self {
701
+ let files = files
702
+ . into_iter ( )
703
+ . map ( |( p, d) | ( p. into ( ) , d. into_data ( ) ) )
704
+ . collect ( ) ;
705
+ Self { files }
706
+ }
707
+ }
708
+
709
+ impl < const N : usize , P , D > From < [ ( P , D ) ; N ] > for InMemoryDir
710
+ where
711
+ P : Into < PathBuf > ,
712
+ D : IntoData ,
713
+ {
714
+ fn from ( files : [ ( P , D ) ; N ] ) -> Self {
715
+ let files = files
716
+ . into_iter ( )
717
+ . map ( |( p, d) | ( p. into ( ) , d. into_data ( ) ) )
718
+ . collect ( ) ;
719
+ Self { files }
720
+ }
721
+ }
722
+
723
+ impl < P , D > From < std:: collections:: HashMap < P , D > > for InMemoryDir
724
+ where
725
+ P : Into < PathBuf > ,
726
+ D : IntoData ,
727
+ {
728
+ fn from ( files : std:: collections:: HashMap < P , D > ) -> Self {
729
+ let files = files
730
+ . into_iter ( )
731
+ . map ( |( p, d) | ( p. into ( ) , d. into_data ( ) ) )
732
+ . collect ( ) ;
733
+ Self { files }
734
+ }
735
+ }
736
+
737
+ impl < P , D > From < std:: collections:: BTreeMap < P , D > > for InMemoryDir
738
+ where
739
+ P : Into < PathBuf > ,
740
+ D : IntoData ,
741
+ {
742
+ fn from ( files : std:: collections:: BTreeMap < P , D > ) -> Self {
743
+ let files = files
744
+ . into_iter ( )
745
+ . map ( |( p, d) | ( p. into ( ) , d. into_data ( ) ) )
746
+ . collect ( ) ;
747
+ Self { files }
748
+ }
749
+ }
750
+
751
+ impl From < ( ) > for InMemoryDir {
752
+ fn from ( _files : ( ) ) -> Self {
753
+ let files = Vec :: new ( ) ;
754
+ Self { files }
755
+ }
756
+ }
757
+
758
+ macro_rules! impl_from_tuple_for_dirsnapshot {
759
+ ( $( $var: ident $path: ident $data: ident) ,+) => {
760
+ impl <$( $path: Into <PathBuf >, $data: IntoData ) ,+> From <( $( ( $path, $data) ) ,+ , ) > for InMemoryDir {
761
+ fn from( files: ( $( ( $path, $data) ) ,+, ) ) -> Self {
762
+ let ( $( $var) ,+ , ) = files;
763
+ let files = [ $( ( $var. 0 . into( ) , $var. 1 . into_data( ) ) ) ,+] ;
764
+ files. into( )
765
+ }
766
+ }
767
+ } ;
768
+ }
769
+
770
+ macro_rules! impl_from_tuples_for_dirsnapshot {
771
+ ( $var1: ident $path1: ident $data1: ident, $( $var: ident $path: ident $data: ident) ,+) => {
772
+ impl_from_tuples_for_dirsnapshot!( __impl $var1 $path1 $data1; $( $var $path $data) ,+) ;
773
+ } ;
774
+ ( __impl $( $var: ident $path: ident $data: ident) ,+; $var1: ident $path1: ident $data1: ident $( , $var2: ident $path2: ident $data2: ident) * ) => {
775
+ impl_from_tuple_for_dirsnapshot!( $( $var $path $data) ,+) ;
776
+ impl_from_tuples_for_dirsnapshot!( __impl $( $var $path $data) ,+, $var1 $path1 $data1; $( $var2 $path2 $data2) ,* ) ;
777
+ } ;
778
+ ( __impl $( $var: ident $path: ident $data: ident) ,+; ) => {
779
+ impl_from_tuple_for_dirsnapshot!( $( $var $path $data) ,+) ;
780
+ }
781
+ }
782
+
783
+ impl_from_tuples_for_dirsnapshot ! (
784
+ s1 P1 D1 ,
785
+ s2 P2 D2 ,
786
+ s3 P3 D3 ,
787
+ s4 P4 D4 ,
788
+ s5 P5 D5 ,
789
+ s6 P6 D6 ,
790
+ s7 P7 D7
791
+ ) ;
792
+
709
793
#[ cfg( test) ]
710
794
mod test {
711
795
use snapbox:: assert_data_eq;
0 commit comments