1
- #![ feature( test) ]
1
+ #![ feature( test) ] // compiletest_rs requires this attribute
2
2
3
3
use compiletest_rs as compiletest;
4
4
use compiletest_rs:: common:: Mode as TestMode ;
@@ -11,51 +11,86 @@ use std::path::{Path, PathBuf};
11
11
12
12
mod cargo;
13
13
14
- #[ must_use]
15
- fn rustc_test_suite ( ) -> Option < PathBuf > {
16
- option_env ! ( "RUSTC_TEST_SUITE" ) . map ( PathBuf :: from)
14
+ fn host_lib ( ) -> PathBuf {
15
+ if let Some ( path) = option_env ! ( "HOST_LIBS" ) {
16
+ PathBuf :: from ( path)
17
+ } else {
18
+ cargo:: CARGO_TARGET_DIR . join ( env ! ( "PROFILE" ) )
19
+ }
17
20
}
18
21
19
- #[ must_use]
20
- fn rustc_lib_path ( ) -> PathBuf {
21
- option_env ! ( "RUSTC_LIB_PATH" ) . unwrap ( ) . into ( )
22
+ fn clippy_driver_path ( ) -> PathBuf {
23
+ if let Some ( path) = option_env ! ( "CLIPPY_DRIVER_PATH" ) {
24
+ PathBuf :: from ( path)
25
+ } else {
26
+ cargo:: TARGET_LIB . join ( "clippy-driver" )
27
+ }
28
+ }
29
+
30
+ // When we'll want to use `extern crate ..` for a dependency that is used
31
+ // both by the crate and the compiler itself, we can't simply pass -L flags
32
+ // as we'll get a duplicate matching versions. Instead, disambiguate with
33
+ // `--extern dep=path`.
34
+ // See https://github.com/rust-lang/rust-clippy/issues/4015.
35
+ fn third_party_crates ( ) -> String {
36
+ use std:: collections:: hash_map:: Entry :: * ;
37
+ use std:: collections:: HashMap ;
38
+ static CRATES : & [ & str ] = & [ "serde" , "serde_derive" , "regex" , "clippy_lints" ] ;
39
+ let dep_dir = cargo:: TARGET_LIB . join ( "deps" ) ;
40
+ let mut crates: HashMap < & str , PathBuf > = HashMap :: with_capacity ( CRATES . len ( ) ) ;
41
+ for entry in fs:: read_dir ( dep_dir) . unwrap ( ) {
42
+ let path = match entry {
43
+ Ok ( entry) => entry. path ( ) ,
44
+ _ => continue ,
45
+ } ;
46
+ if let Some ( name) = path. file_name ( ) . and_then ( OsStr :: to_str) {
47
+ for dep in CRATES {
48
+ if name. starts_with ( & format ! ( "lib{}-" , dep) ) && name. ends_with ( ".rlib" ) {
49
+ if let Vacant ( entry) = crates. entry ( dep) {
50
+ entry. insert ( path) ;
51
+ }
52
+ break ;
53
+ }
54
+ }
55
+ }
56
+ }
57
+
58
+ let v: Vec < _ > = crates
59
+ . into_iter ( )
60
+ . map ( |( dep, path) | format ! ( "--extern {}={}" , dep, path. display( ) ) )
61
+ . collect ( ) ;
62
+ v. join ( " " )
22
63
}
23
64
24
65
fn default_config ( ) -> compiletest:: Config {
25
- let build_info = cargo:: BuildInfo :: new ( ) ;
26
66
let mut config = compiletest:: Config :: default ( ) ;
27
67
28
68
if let Ok ( name) = env:: var ( "TESTNAME" ) {
29
69
config. filter = Some ( name) ;
30
70
}
31
71
32
- if rustc_test_suite ( ) . is_some ( ) {
33
- let path = rustc_lib_path ( ) ;
72
+ if let Some ( path ) = option_env ! ( "RUSTC_LIB_PATH" ) {
73
+ let path = PathBuf :: from ( path ) ;
34
74
config. run_lib_path = path. clone ( ) ;
35
75
config. compile_lib_path = path;
36
76
}
37
77
38
- let disambiguated: Vec < _ > = cargo:: BuildInfo :: third_party_crates ( )
39
- . iter ( )
40
- . map ( |( krate, path) | format ! ( "--extern {}={}" , krate, path. display( ) ) )
41
- . collect ( ) ;
42
-
43
78
config. target_rustcflags = Some ( format ! (
44
79
"-L {0} -L {1} -Dwarnings -Zui-testing {2}" ,
45
- build_info . host_lib( ) . join( "deps" ) . display( ) ,
46
- build_info . target_lib ( ) . join( "deps" ) . display( ) ,
47
- disambiguated . join ( " " )
80
+ host_lib( ) . join( "deps" ) . display( ) ,
81
+ cargo :: TARGET_LIB . join( "deps" ) . display( ) ,
82
+ third_party_crates ( ) ,
48
83
) ) ;
49
84
50
- config. build_base = if rustc_test_suite ( ) . is_some ( ) {
51
- // we don't need access to the stderr files on travis
85
+ config. build_base = if cargo :: is_rustc_test_suite ( ) {
86
+ // This make the stderr files go to clippy OUT_DIR on rustc repo build dir
52
87
let mut path = PathBuf :: from ( env ! ( "OUT_DIR" ) ) ;
53
88
path. push ( "test_build_base" ) ;
54
89
path
55
90
} else {
56
- build_info . host_lib ( ) . join ( "test_build_base" )
91
+ host_lib ( ) . join ( "test_build_base" )
57
92
} ;
58
- config. rustc_path = build_info . clippy_driver_path ( ) ;
93
+ config. rustc_path = clippy_driver_path ( ) ;
59
94
config
60
95
}
61
96
0 commit comments