@@ -76,6 +76,8 @@ pub fn run(input: &str,
76
76
"rustdoc-test" , None )
77
77
. expect ( "phase_2_configure_and_expand aborted in rustdoc!" ) ;
78
78
79
+ let inject_crate = should_inject_crate ( & krate) ;
80
+
79
81
let ctx = core:: DocContext {
80
82
krate : & krate,
81
83
maybe_typed : core:: NotTyped ( sess) ,
@@ -100,7 +102,8 @@ pub fn run(input: &str,
100
102
let mut collector = Collector :: new ( krate. name . to_string ( ) ,
101
103
libs,
102
104
externs,
103
- false ) ;
105
+ false ,
106
+ inject_crate) ;
104
107
collector. fold_crate ( krate) ;
105
108
106
109
test_args. insert ( 0 , "rustdoctest" . to_string ( ) ) ;
@@ -110,13 +113,42 @@ pub fn run(input: &str,
110
113
0
111
114
}
112
115
116
+ // Look for #![doc(test(no_crate_inject))], used by crates in the std facade
117
+ fn should_inject_crate ( krate : & :: syntax:: ast:: Crate ) -> bool {
118
+ use syntax:: attr:: AttrMetaMethods ;
119
+
120
+ let mut inject_crate = true ;
121
+
122
+ for attr in & krate. attrs {
123
+ if attr. check_name ( "doc" ) {
124
+ for list in attr. meta_item_list ( ) . into_iter ( ) {
125
+ for attr in list {
126
+ if attr. check_name ( "test" ) {
127
+ for list in attr. meta_item_list ( ) . into_iter ( ) {
128
+ for attr in list {
129
+ if attr. check_name ( "no_crate_inject" ) {
130
+ inject_crate = false ;
131
+ }
132
+ }
133
+ }
134
+ }
135
+ }
136
+ }
137
+ }
138
+ }
139
+
140
+ return inject_crate;
141
+ }
142
+
113
143
#[ allow( deprecated) ]
114
144
fn runtest ( test : & str , cratename : & str , libs : SearchPaths ,
115
145
externs : core:: Externs ,
116
- should_panic : bool , no_run : bool , as_test_harness : bool ) {
146
+ should_panic : bool , no_run : bool , as_test_harness : bool ,
147
+ inject_crate : bool ) {
117
148
// the test harness wants its own `main` & top level functions, so
118
149
// never wrap the test in `fn main() { ... }`
119
- let test = maketest ( test, Some ( cratename) , true , as_test_harness) ;
150
+ let test = maketest ( test, Some ( cratename) , true , as_test_harness,
151
+ inject_crate) ;
120
152
let input = config:: Input :: Str ( test. to_string ( ) ) ;
121
153
122
154
let sessopts = config:: Options {
@@ -218,7 +250,8 @@ fn runtest(test: &str, cratename: &str, libs: SearchPaths,
218
250
}
219
251
}
220
252
221
- pub fn maketest ( s : & str , cratename : Option < & str > , lints : bool , dont_insert_main : bool ) -> String {
253
+ pub fn maketest ( s : & str , cratename : Option < & str > , lints : bool ,
254
+ dont_insert_main : bool , inject_crate : bool ) -> String {
222
255
let ( crate_attrs, everything_else) = partition_source ( s) ;
223
256
224
257
let mut prog = String :: new ( ) ;
@@ -235,7 +268,7 @@ pub fn maketest(s: &str, cratename: Option<&str>, lints: bool, dont_insert_main:
235
268
236
269
// Don't inject `extern crate std` because it's already injected by the
237
270
// compiler.
238
- if !s. contains ( "extern crate" ) && cratename != Some ( "std" ) {
271
+ if !s. contains ( "extern crate" ) && cratename != Some ( "std" ) && inject_crate {
239
272
match cratename {
240
273
Some ( cratename) => {
241
274
if s. contains ( cratename) {
@@ -267,7 +300,7 @@ fn partition_source(s: &str) -> (String, String) {
267
300
let mut after = String :: new ( ) ;
268
301
269
302
for line in s. lines ( ) {
270
- let trimline = StrExt :: trim ( line ) ;
303
+ let trimline = line . trim ( ) ;
271
304
let header = trimline. is_whitespace ( ) ||
272
305
trimline. starts_with ( "#![feature" ) ;
273
306
if !header || after_header {
@@ -292,11 +325,12 @@ pub struct Collector {
292
325
use_headers : bool ,
293
326
current_header : Option < String > ,
294
327
cratename : String ,
328
+ inject_crate : bool
295
329
}
296
330
297
331
impl Collector {
298
332
pub fn new ( cratename : String , libs : SearchPaths , externs : core:: Externs ,
299
- use_headers : bool ) -> Collector {
333
+ use_headers : bool , inject_crate : bool ) -> Collector {
300
334
Collector {
301
335
tests : Vec :: new ( ) ,
302
336
names : Vec :: new ( ) ,
@@ -306,11 +340,13 @@ impl Collector {
306
340
use_headers : use_headers,
307
341
current_header : None ,
308
342
cratename : cratename,
343
+ inject_crate : inject_crate
309
344
}
310
345
}
311
346
312
347
pub fn add_test ( & mut self , test : String ,
313
- should_panic : bool , no_run : bool , should_ignore : bool , as_test_harness : bool ) {
348
+ should_panic : bool , no_run : bool , should_ignore : bool ,
349
+ as_test_harness : bool ) {
314
350
let name = if self . use_headers {
315
351
let s = self . current_header . as_ref ( ) . map ( |s| & * * s) . unwrap_or ( "" ) ;
316
352
format ! ( "{}_{}" , s, self . cnt)
@@ -321,6 +357,7 @@ impl Collector {
321
357
let libs = self . libs . clone ( ) ;
322
358
let externs = self . externs . clone ( ) ;
323
359
let cratename = self . cratename . to_string ( ) ;
360
+ let inject_crate = self . inject_crate ;
324
361
debug ! ( "Creating test {}: {}" , name, test) ;
325
362
self . tests . push ( testing:: TestDescAndFn {
326
363
desc : testing:: TestDesc {
@@ -335,7 +372,8 @@ impl Collector {
335
372
externs,
336
373
should_panic,
337
374
no_run,
338
- as_test_harness) ;
375
+ as_test_harness,
376
+ inject_crate) ;
339
377
} ) )
340
378
} ) ;
341
379
}
0 commit comments