1
1
#![ feature( test) ]
2
2
3
3
use compiletest_rs as compiletest;
4
- extern crate tester as test ;
4
+ use compiletest_rs :: common :: Mode as TestMode ;
5
5
6
- use std:: env:: { set_var , var } ;
6
+ use std:: env:: { self , set_var } ;
7
7
use std:: ffi:: OsStr ;
8
8
use std:: fs;
9
9
use std:: io;
10
10
use std:: path:: { Path , PathBuf } ;
11
11
12
- #[ must_use]
13
- fn clippy_driver_path ( ) -> PathBuf {
14
- if let Some ( path) = option_env ! ( "CLIPPY_DRIVER_PATH" ) {
15
- PathBuf :: from ( path)
16
- } else {
17
- PathBuf :: from ( concat ! ( "target/" , env!( "PROFILE" ) , "/clippy-driver" ) )
18
- }
19
- }
20
-
21
- #[ must_use]
22
- fn host_libs ( ) -> PathBuf {
23
- if let Some ( path) = option_env ! ( "HOST_LIBS" ) {
24
- PathBuf :: from ( path)
25
- } else {
26
- Path :: new ( "target" ) . join ( env ! ( "PROFILE" ) )
27
- }
28
- }
29
-
30
- #[ must_use]
31
- fn target_libs ( ) -> Option < PathBuf > {
32
- option_env ! ( "TARGET_LIBS" ) . map ( PathBuf :: from)
33
- }
12
+ mod cargo;
34
13
35
14
#[ must_use]
36
15
fn rustc_test_suite ( ) -> Option < PathBuf > {
@@ -42,73 +21,52 @@ fn rustc_lib_path() -> PathBuf {
42
21
option_env ! ( "RUSTC_LIB_PATH" ) . unwrap ( ) . into ( )
43
22
}
44
23
45
- fn config ( mode : & str , dir : PathBuf ) -> compiletest:: Config {
24
+ fn default_config ( ) -> compiletest:: Config {
25
+ let build_info = cargo:: BuildInfo :: new ( ) ;
46
26
let mut config = compiletest:: Config :: default ( ) ;
47
27
48
- let cfg_mode = mode. parse ( ) . expect ( "Invalid mode" ) ;
49
- if let Ok ( name) = var :: < & str > ( "TESTNAME" ) {
50
- config. filter = Some ( name)
28
+ if let Ok ( name) = env:: var ( "TESTNAME" ) {
29
+ config. filter = Some ( name) ;
51
30
}
52
31
53
32
if rustc_test_suite ( ) . is_some ( ) {
54
- config. run_lib_path = rustc_lib_path ( ) ;
55
- config. compile_lib_path = rustc_lib_path ( ) ;
33
+ let path = rustc_lib_path ( ) ;
34
+ config. run_lib_path = path. clone ( ) ;
35
+ config. compile_lib_path = path;
56
36
}
57
37
58
- // When we'll want to use `extern crate ..` for a dependency that is used
59
- // both by the crate and the compiler itself, we can't simply pass -L flags
60
- // as we'll get a duplicate matching versions. Instead, disambiguate with
61
- // `--extern dep=path`.
62
- // See https://github.com/rust-lang/rust-clippy/issues/4015.
63
- let needs_disambiguation = [ "serde" , "regex" , "clippy_lints" ] ;
64
- // This assumes that deps are compiled (they are for Cargo integration tests).
65
- let deps = fs:: read_dir ( target_libs ( ) . unwrap_or_else ( host_libs) . join ( "deps" ) ) . unwrap ( ) ;
66
- let disambiguated = deps
67
- . filter_map ( |dep| {
68
- let path = dep. ok ( ) ?. path ( ) ;
69
- let name = path. file_name ( ) ?. to_string_lossy ( ) ;
70
- // NOTE: This only handles a single dep
71
- // https://github.com/laumann/compiletest-rs/issues/101
72
- needs_disambiguation. iter ( ) . find_map ( |dep| {
73
- if name. starts_with ( & format ! ( "lib{}-" , dep) ) && name. ends_with ( ".rlib" ) {
74
- Some ( format ! ( "--extern {}={}" , dep, path. display( ) ) )
75
- } else {
76
- None
77
- }
78
- } )
79
- } )
80
- . collect :: < Vec < _ > > ( ) ;
38
+ let disambiguated: Vec < _ > = cargo:: BuildInfo :: third_party_crates ( )
39
+ . iter ( )
40
+ . map ( |( krate, path) | format ! ( "--extern {}={}" , krate, path. display( ) ) )
41
+ . collect ( ) ;
81
42
82
43
config. target_rustcflags = Some ( format ! (
83
- "-L {0} -L {0}/deps { 1} -Dwarnings -Zui-testing {2}" ,
84
- host_libs ( ) . display( ) ,
85
- target_libs ( ) . map_or_else ( String :: new , |path| format! ( "-L {0} -L {0}/ deps", path . display( ) ) ) ,
44
+ "-L {0} -L {1} -Dwarnings -Zui-testing {2}" ,
45
+ build_info . host_lib ( ) . join ( "deps" ) . display( ) ,
46
+ build_info . target_lib ( ) . join ( " deps") . display( ) ,
86
47
disambiguated. join( " " )
87
48
) ) ;
88
49
89
- config. mode = cfg_mode;
90
50
config. build_base = if rustc_test_suite ( ) . is_some ( ) {
91
51
// we don't need access to the stderr files on travis
92
52
let mut path = PathBuf :: from ( env ! ( "OUT_DIR" ) ) ;
93
53
path. push ( "test_build_base" ) ;
94
54
path
95
55
} else {
96
- let mut path = std:: env:: current_dir ( ) . unwrap ( ) ;
97
- path. push ( "target/debug/test_build_base" ) ;
98
- path
56
+ build_info. host_lib ( ) . join ( "test_build_base" )
99
57
} ;
100
- config. src_base = dir;
101
- config. rustc_path = clippy_driver_path ( ) ;
58
+ config. rustc_path = build_info. clippy_driver_path ( ) ;
102
59
config
103
60
}
104
61
105
- fn run_mode ( mode : & str , dir : PathBuf ) {
106
- let cfg = config ( mode, dir) ;
62
+ fn run_mode ( cfg : & mut compiletest:: Config ) {
63
+ cfg. mode = TestMode :: Ui ;
64
+ cfg. src_base = Path :: new ( "tests" ) . join ( "ui" ) ;
107
65
compiletest:: run_tests ( & cfg) ;
108
66
}
109
67
110
68
#[ allow( clippy:: identity_conversion) ]
111
- fn run_ui_toml_tests ( config : & compiletest:: Config , mut tests : Vec < test :: TestDescAndFn > ) -> Result < bool , io:: Error > {
69
+ fn run_ui_toml_tests ( config : & compiletest:: Config , mut tests : Vec < tester :: TestDescAndFn > ) -> Result < bool , io:: Error > {
112
70
let mut result = true ;
113
71
let opts = compiletest:: test_opts ( config) ;
114
72
for dir in fs:: read_dir ( & config. src_base ) ? {
@@ -137,15 +95,16 @@ fn run_ui_toml_tests(config: &compiletest::Config, mut tests: Vec<test::TestDesc
137
95
. iter ( )
138
96
. position ( |test| test. desc . name == test_name)
139
97
. expect ( "The test should be in there" ) ;
140
- result &= test :: run_tests_console ( & opts, vec ! [ tests. swap_remove( index) ] ) ?;
98
+ result &= tester :: run_tests_console ( & opts, vec ! [ tests. swap_remove( index) ] ) ?;
141
99
}
142
100
}
143
101
Ok ( result)
144
102
}
145
103
146
- fn run_ui_toml ( ) {
147
- let path = PathBuf :: from ( "tests/ui-toml" ) . canonicalize ( ) . unwrap ( ) ;
148
- let config = config ( "ui" , path) ;
104
+ fn run_ui_toml ( config : & mut compiletest:: Config ) {
105
+ config. mode = TestMode :: Ui ;
106
+ config. src_base = Path :: new ( "tests" ) . join ( "ui-toml" ) . canonicalize ( ) . unwrap ( ) ;
107
+
149
108
let tests = compiletest:: make_tests ( & config) ;
150
109
151
110
let res = run_ui_toml_tests ( & config, tests) ;
@@ -167,6 +126,7 @@ fn prepare_env() {
167
126
#[ test]
168
127
fn compile_test ( ) {
169
128
prepare_env ( ) ;
170
- run_mode ( "ui" , "tests/ui" . into ( ) ) ;
171
- run_ui_toml ( ) ;
129
+ let mut config = default_config ( ) ;
130
+ run_mode ( & mut config) ;
131
+ run_ui_toml ( & mut config) ;
172
132
}
0 commit comments