16
16
//! `[WARNING]`) to match cargo's "status" output and allows you to ignore
17
17
//! the alignment. See the source of `substitute_macros` for a complete list
18
18
//! of substitutions.
19
+ //! - `[DIRTY-MSVC]` (only when the line starts with it) would be replaced by
20
+ //! `[DIRTY]` when `cfg(target_env = "msvc")` or the line will be ignored otherwise.
21
+ //! Tests that work around [issue 7358](https://github.com/rust-lang/cargo/issues/7358)
22
+ //! can use this to avoid duplicating the `with_stderr` call like:
23
+ //! `if cfg!(target_env = "msvc") {e.with_stderr("...[DIRTY]...");} else {e.with_stderr("...");}`.
19
24
//!
20
25
//! # Normalization
21
26
//!
@@ -108,7 +113,9 @@ fn normalize_actual(actual: &str, cwd: Option<&Path>) -> String {
108
113
109
114
/// Normalizes the expected string so that it can be compared against the actual output.
110
115
fn normalize_expected ( expected : & str , cwd : Option < & Path > ) -> String {
111
- let expected = substitute_macros ( expected) ;
116
+ let expected = replace_dirty_msvc ( expected) ;
117
+ let expected = substitute_macros ( & expected) ;
118
+
112
119
if cfg ! ( windows) {
113
120
normalize_windows ( & expected, cwd)
114
121
} else {
@@ -121,6 +128,29 @@ fn normalize_expected(expected: &str, cwd: Option<&Path>) -> String {
121
128
}
122
129
}
123
130
131
+ fn replace_dirty_msvc_impl ( s : & str , is_msvc : bool ) -> String {
132
+ if is_msvc {
133
+ s. replace ( "[DIRTY-MSVC]" , "[DIRTY]" )
134
+ } else {
135
+ use itertools:: Itertools ;
136
+
137
+ let mut new = s
138
+ . lines ( )
139
+ . filter ( |it| !it. starts_with ( "[DIRTY-MSVC]" ) )
140
+ . join ( "\n " ) ;
141
+
142
+ if s. ends_with ( "\n " ) {
143
+ new. push_str ( "\n " ) ;
144
+ }
145
+
146
+ new
147
+ }
148
+ }
149
+
150
+ fn replace_dirty_msvc ( s : & str ) -> String {
151
+ replace_dirty_msvc_impl ( s, cfg ! ( target_env = "msvc" ) )
152
+ }
153
+
124
154
/// Normalizes text for both actual and expected strings on Windows.
125
155
fn normalize_windows ( text : & str , cwd : Option < & Path > ) -> String {
126
156
// Let's not deal with / vs \ (windows...)
@@ -170,6 +200,7 @@ fn substitute_macros(input: &str) -> String {
170
200
( "[DOCUMENTING]" , " Documenting" ) ,
171
201
( "[SCRAPING]" , " Scraping" ) ,
172
202
( "[FRESH]" , " Fresh" ) ,
203
+ ( "[DIRTY]" , " Dirty" ) ,
173
204
( "[UPDATING]" , " Updating" ) ,
174
205
( "[ADDING]" , " Adding" ) ,
175
206
( "[REMOVING]" , " Removing" ) ,
@@ -637,3 +668,114 @@ fn wild_str_cmp() {
637
668
assert_ne ! ( WildStr :: new( a) , WildStr :: new( b) ) ;
638
669
}
639
670
}
671
+
672
+ #[ test]
673
+ fn dirty_msvc ( ) {
674
+ let case = |expected : & str , wild : & str , msvc : bool | {
675
+ assert_eq ! ( expected, & replace_dirty_msvc_impl( wild, msvc) ) ;
676
+ } ;
677
+
678
+ // no replacements
679
+ case ( "aa" , "aa" , false ) ;
680
+ case ( "aa" , "aa" , true ) ;
681
+
682
+ // with replacements
683
+ case (
684
+ "\
685
+ [DIRTY] a",
686
+ "\
687
+ [DIRTY-MSVC] a",
688
+ true ,
689
+ ) ;
690
+ case (
691
+ "" ,
692
+ "\
693
+ [DIRTY-MSVC] a",
694
+ false ,
695
+ ) ;
696
+ case (
697
+ "\
698
+ [DIRTY] a
699
+ [COMPILING] a" ,
700
+ "\
701
+ [DIRTY-MSVC] a
702
+ [COMPILING] a" ,
703
+ true ,
704
+ ) ;
705
+ case (
706
+ "\
707
+ [COMPILING] a",
708
+ "\
709
+ [DIRTY-MSVC] a
710
+ [COMPILING] a" ,
711
+ false ,
712
+ ) ;
713
+
714
+ // test trailing newline behavior
715
+ case (
716
+ "\
717
+ A
718
+ B
719
+ " , "\
720
+ A
721
+ B
722
+ " , true ,
723
+ ) ;
724
+
725
+ case (
726
+ "\
727
+ A
728
+ B
729
+ " , "\
730
+ A
731
+ B
732
+ " , false ,
733
+ ) ;
734
+
735
+ case (
736
+ "\
737
+ A
738
+ B" , "\
739
+ A
740
+ B" , true ,
741
+ ) ;
742
+
743
+ case (
744
+ "\
745
+ A
746
+ B" , "\
747
+ A
748
+ B" , false ,
749
+ ) ;
750
+
751
+ case (
752
+ "\
753
+ [DIRTY] a
754
+ " ,
755
+ "\
756
+ [DIRTY-MSVC] a
757
+ " ,
758
+ true ,
759
+ ) ;
760
+ case (
761
+ "\n " ,
762
+ "\
763
+ [DIRTY-MSVC] a
764
+ " ,
765
+ false ,
766
+ ) ;
767
+
768
+ case (
769
+ "\
770
+ [DIRTY] a",
771
+ "\
772
+ [DIRTY-MSVC] a",
773
+ true ,
774
+ ) ;
775
+ case (
776
+ "" ,
777
+ "\
778
+ [DIRTY-MSVC] a",
779
+ false ,
780
+ ) ;
781
+ }
0 commit comments