44#![ warn( rust_2018_idioms, unused_lifetimes) ]
55#![ allow( unused_extern_crates) ]
66
7- use compiletest:: { status_emitter, Args , CommandBuilder , OutputConflictHandling } ;
8- use ui_test as compiletest;
9- use ui_test:: Mode as TestMode ;
7+ use ui_test:: { status_emitter, Args , CommandBuilder , Config , Match , Mode , OutputConflictHandling } ;
108
119use std:: collections:: BTreeMap ;
12- use std:: env:: { self , remove_var , set_var, var_os} ;
10+ use std:: env:: { self , set_var, var_os} ;
1311use std:: ffi:: { OsStr , OsString } ;
1412use std:: fs;
1513use std:: path:: { Path , PathBuf } ;
@@ -29,6 +27,8 @@ extern crate quote;
2927extern crate syn;
3028extern crate tokio;
3129
30+ mod test_utils;
31+
3232/// All crates used in UI tests are listed here
3333static TEST_DEPENDENCIES : & [ & str ] = & [
3434 "clippy_lints" ,
@@ -104,8 +104,6 @@ static EXTERN_FLAGS: LazyLock<Vec<String>> = LazyLock::new(|| {
104104 . collect ( )
105105} ) ;
106106
107- mod test_utils;
108-
109107// whether to run internal tests or not
110108const RUN_INTERNAL_TESTS : bool = cfg ! ( feature = "internal" ) ;
111109
@@ -115,7 +113,7 @@ fn canonicalize(path: impl AsRef<Path>) -> PathBuf {
115113 fs:: canonicalize ( path) . unwrap_or_else ( |err| panic ! ( "{} cannot be canonicalized: {err}" , path. display( ) ) )
116114}
117115
118- fn base_config ( test_dir : & str ) -> ( compiletest :: Config , Args ) {
116+ fn base_config ( test_dir : & str ) -> ( Config , Args ) {
119117 let bless = var_os ( "RUSTC_BLESS" ) . is_some_and ( |v| v != "0" ) || env:: args ( ) . any ( |arg| arg == "--bless" ) ;
120118
121119 let args = Args {
@@ -131,18 +129,18 @@ fn base_config(test_dir: &str) -> (compiletest::Config, Args) {
131129 skip : Vec :: new ( ) ,
132130 } ;
133131
134- let mut config = compiletest :: Config {
135- mode : TestMode :: Yolo { rustfix : true } ,
136- stderr_filters : vec ! [ ] ,
132+ let mut config = Config {
133+ mode : Mode :: Yolo { rustfix : true } ,
134+ stderr_filters : vec ! [ ( Match :: PathBackslash , b"/" ) ] ,
137135 stdout_filters : vec ! [ ] ,
138136 output_conflict_handling : if bless {
139137 OutputConflictHandling :: Bless
140138 } else {
141139 OutputConflictHandling :: Error ( "cargo uibless" . into ( ) )
142140 } ,
143141 target : None ,
144- out_dir : canonicalize ( std :: env :: var_os ( "CARGO_TARGET_DIR" ) . unwrap_or_else ( || "target" . into ( ) ) ) . join ( "ui_test" ) ,
145- ..compiletest :: Config :: rustc ( Path :: new ( "tests" ) . join ( test_dir) )
142+ out_dir : canonicalize ( var_os ( "CARGO_TARGET_DIR" ) . unwrap_or_else ( || "target" . into ( ) ) ) . join ( "ui_test" ) ,
143+ ..Config :: rustc ( Path :: new ( "tests" ) . join ( test_dir) )
146144 } ;
147145 let current_exe_path = env:: current_exe ( ) . unwrap ( ) ;
148146 let deps_path = current_exe_path. parent ( ) . unwrap ( ) ;
@@ -167,10 +165,6 @@ fn base_config(test_dir: &str) -> (compiletest::Config, Args) {
167165 config. program . args . push ( dep. into ( ) ) ;
168166 }
169167
170- // Normalize away slashes in windows paths.
171- config. stderr_filter ( r"\\" , "/" ) ;
172-
173- //config.build_base = profile_path.join("test").join(test_dir);
174168 config. program . program = profile_path. join ( if cfg ! ( windows) {
175169 "clippy-driver.exe"
176170 } else {
@@ -180,18 +174,19 @@ fn base_config(test_dir: &str) -> (compiletest::Config, Args) {
180174}
181175
182176fn run_ui ( ) {
183- let ( config, args) = base_config ( "ui" ) ;
184- // use tests/clippy.toml
185- let _g = VarGuard :: set ( "CARGO_MANIFEST_DIR" , canonicalize ( "tests" ) ) ;
186- let _threads = VarGuard :: set ( "RUST_TEST_THREADS" , args. threads . to_string ( ) ) ;
177+ let ( mut config, args) = base_config ( "ui" ) ;
178+ config
179+ . program
180+ . envs
181+ . push ( ( "CLIPPY_CONF_DIR" . into ( ) , Some ( canonicalize ( "tests" ) . into ( ) ) ) ) ;
187182
188183 let quiet = args. quiet ;
189184
190- compiletest :: run_tests_generic (
185+ ui_test :: run_tests_generic (
191186 vec ! [ config] ,
192187 args,
193- compiletest :: default_file_filter,
194- compiletest :: default_per_file_config,
188+ ui_test :: default_file_filter,
189+ ui_test :: default_per_file_config,
195190 if quiet {
196191 status_emitter:: Text :: quiet ( )
197192 } else {
@@ -212,11 +207,11 @@ fn run_internal_tests() {
212207 }
213208 let quiet = args. quiet ;
214209
215- compiletest :: run_tests_generic (
210+ ui_test :: run_tests_generic (
216211 vec ! [ config] ,
217212 args,
218- compiletest :: default_file_filter,
219- compiletest :: default_per_file_config,
213+ ui_test :: default_file_filter,
214+ ui_test :: default_per_file_config,
220215 if quiet {
221216 status_emitter:: Text :: quiet ( )
222217 } else {
@@ -229,24 +224,27 @@ fn run_internal_tests() {
229224fn run_ui_toml ( ) {
230225 let ( mut config, args) = base_config ( "ui-toml" ) ;
231226
232- config. stderr_filter (
233- & regex:: escape (
234- & canonicalize ( "tests" )
235- . parent ( )
236- . unwrap ( )
237- . display ( )
238- . to_string ( )
239- . replace ( '\\' , "/" ) ,
227+ config. stderr_filters = vec ! [
228+ (
229+ Match :: Exact (
230+ canonicalize( "tests" )
231+ . parent( )
232+ . unwrap( )
233+ . to_string_lossy( )
234+ . as_bytes( )
235+ . to_vec( ) ,
236+ ) ,
237+ b"$DIR" ,
240238 ) ,
241- "$$DIR" ,
242- ) ;
239+ ( Match :: Exact ( b" \\ " . to_vec ( ) ) , b"/" ) ,
240+ ] ;
243241
244242 let quiet = args. quiet ;
245243
246244 ui_test:: run_tests_generic (
247245 vec ! [ config] ,
248246 args,
249- compiletest :: default_file_filter,
247+ ui_test :: default_file_filter,
250248 |config, path, _file_contents| {
251249 config
252250 . program
@@ -285,17 +283,20 @@ fn run_ui_cargo() {
285283 } ) ;
286284 config. edition = None ;
287285
288- config. stderr_filter (
289- & regex:: escape (
290- & canonicalize ( "tests" )
291- . parent ( )
292- . unwrap ( )
293- . display ( )
294- . to_string ( )
295- . replace ( '\\' , "/" ) ,
286+ config. stderr_filters = vec ! [
287+ (
288+ Match :: Exact (
289+ canonicalize( "tests" )
290+ . parent( )
291+ . unwrap( )
292+ . to_string_lossy( )
293+ . as_bytes( )
294+ . to_vec( ) ,
295+ ) ,
296+ b"$DIR" ,
296297 ) ,
297- "$$DIR" ,
298- ) ;
298+ ( Match :: Exact ( b" \\ " . to_vec ( ) ) , b"/" ) ,
299+ ] ;
299300
300301 let quiet = args. quiet ;
301302
@@ -410,27 +411,3 @@ fn ui_cargo_toml_metadata() {
410411 ) ;
411412 }
412413}
413-
414- /// Restores an env var on drop
415- #[ must_use]
416- struct VarGuard {
417- key : & ' static str ,
418- value : Option < OsString > ,
419- }
420-
421- impl VarGuard {
422- fn set ( key : & ' static str , val : impl AsRef < OsStr > ) -> Self {
423- let value = var_os ( key) ;
424- set_var ( key, val) ;
425- Self { key, value }
426- }
427- }
428-
429- impl Drop for VarGuard {
430- fn drop ( & mut self ) {
431- match self . value . as_deref ( ) {
432- None => remove_var ( self . key ) ,
433- Some ( value) => set_var ( self . key , value) ,
434- }
435- }
436- }
0 commit comments