4
4
use compiletest_rs as compiletest;
5
5
use compiletest_rs:: common:: Mode as TestMode ;
6
6
7
- use std:: env:: { self , set_var, var } ;
8
- use std:: ffi:: OsStr ;
7
+ use std:: env:: { self , remove_var , set_var, var_os } ;
8
+ use std:: ffi:: { OsStr , OsString } ;
9
9
use std:: fs;
10
10
use std:: io;
11
11
use std:: path:: { Path , PathBuf } ;
@@ -88,9 +88,11 @@ fn default_config() -> compiletest::Config {
88
88
config
89
89
}
90
90
91
- fn run_mode ( cfg : & mut compiletest:: Config ) {
91
+ fn run_ui ( cfg : & mut compiletest:: Config ) {
92
92
cfg. mode = TestMode :: Ui ;
93
93
cfg. src_base = Path :: new ( "tests" ) . join ( "ui" ) ;
94
+ // use tests/clippy.toml
95
+ let _g = VarGuard :: set ( "CARGO_MANIFEST_DIR" , std:: fs:: canonicalize ( "tests" ) . unwrap ( ) ) ;
94
96
compiletest:: run_tests ( cfg) ;
95
97
}
96
98
@@ -114,7 +116,7 @@ fn run_ui_toml(config: &mut compiletest::Config) {
114
116
continue ;
115
117
}
116
118
let dir_path = dir. path ( ) ;
117
- set_var ( "CARGO_MANIFEST_DIR" , & dir_path) ;
119
+ let _g = VarGuard :: set ( "CARGO_MANIFEST_DIR" , & dir_path) ;
118
120
for file in fs:: read_dir ( & dir_path) ? {
119
121
let file = file?;
120
122
let file_path = file. path ( ) ;
@@ -145,9 +147,7 @@ fn run_ui_toml(config: &mut compiletest::Config) {
145
147
146
148
let tests = compiletest:: make_tests ( config) ;
147
149
148
- let manifest_dir = var ( "CARGO_MANIFEST_DIR" ) . unwrap_or_default ( ) ;
149
150
let res = run_tests ( config, tests) ;
150
- set_var ( "CARGO_MANIFEST_DIR" , & manifest_dir) ;
151
151
match res {
152
152
Ok ( true ) => { } ,
153
153
Ok ( false ) => panic ! ( "Some tests failed" ) ,
@@ -208,7 +208,7 @@ fn run_ui_cargo(config: &mut compiletest::Config) {
208
208
Some ( "main.rs" ) => { } ,
209
209
_ => continue ,
210
210
}
211
- set_var ( "CLIPPY_CONF_DIR" , case. path ( ) ) ;
211
+ let _g = VarGuard :: set ( "CLIPPY_CONF_DIR" , case. path ( ) ) ;
212
212
let paths = compiletest:: common:: TestPaths {
213
213
file : file_path,
214
214
base : config. src_base . clone ( ) ,
@@ -236,10 +236,8 @@ fn run_ui_cargo(config: &mut compiletest::Config) {
236
236
let tests = compiletest:: make_tests ( config) ;
237
237
238
238
let current_dir = env:: current_dir ( ) . unwrap ( ) ;
239
- let conf_dir = var ( "CLIPPY_CONF_DIR" ) . unwrap_or_default ( ) ;
240
239
let res = run_tests ( config, & config. filters , tests) ;
241
240
env:: set_current_dir ( current_dir) . unwrap ( ) ;
242
- set_var ( "CLIPPY_CONF_DIR" , conf_dir) ;
243
241
244
242
match res {
245
243
Ok ( true ) => { } ,
@@ -260,8 +258,32 @@ fn prepare_env() {
260
258
fn compile_test ( ) {
261
259
prepare_env ( ) ;
262
260
let mut config = default_config ( ) ;
263
- run_mode ( & mut config) ;
261
+ run_ui ( & mut config) ;
264
262
run_ui_toml ( & mut config) ;
265
263
run_ui_cargo ( & mut config) ;
266
264
run_internal_tests ( & mut config) ;
267
265
}
266
+
267
+ /// Restores an env var on drop
268
+ #[ must_use]
269
+ struct VarGuard {
270
+ key : & ' static str ,
271
+ value : Option < OsString > ,
272
+ }
273
+
274
+ impl VarGuard {
275
+ fn set ( key : & ' static str , val : impl AsRef < OsStr > ) -> Self {
276
+ let value = var_os ( key) ;
277
+ set_var ( key, val) ;
278
+ Self { key, value }
279
+ }
280
+ }
281
+
282
+ impl Drop for VarGuard {
283
+ fn drop ( & mut self ) {
284
+ match self . value . as_deref ( ) {
285
+ None => remove_var ( self . key ) ,
286
+ Some ( value) => set_var ( self . key , value) ,
287
+ }
288
+ }
289
+ }
0 commit comments