@@ -38,6 +38,12 @@ use html::markdown;
38
38
use passes;
39
39
use visit_ast:: RustdocVisitor ;
40
40
41
+ #[ derive( Clone , Default ) ]
42
+ pub struct TestOptions {
43
+ pub no_crate_inject : bool ,
44
+ pub attrs : Vec < String > ,
45
+ }
46
+
41
47
pub fn run ( input : & str ,
42
48
cfgs : Vec < String > ,
43
49
libs : SearchPaths ,
@@ -75,7 +81,7 @@ pub fn run(input: &str,
75
81
"rustdoc-test" , None )
76
82
. expect ( "phase_2_configure_and_expand aborted in rustdoc!" ) ;
77
83
78
- let inject_crate = should_inject_crate ( & krate) ;
84
+ let opts = scrape_test_config ( & krate) ;
79
85
80
86
let ctx = core:: DocContext {
81
87
krate : & krate,
@@ -102,7 +108,7 @@ pub fn run(input: &str,
102
108
libs,
103
109
externs,
104
110
false ,
105
- inject_crate ) ;
111
+ opts ) ;
106
112
collector. fold_crate ( krate) ;
107
113
108
114
test_args. insert ( 0 , "rustdoctest" . to_string ( ) ) ;
@@ -113,41 +119,44 @@ pub fn run(input: &str,
113
119
}
114
120
115
121
// Look for #![doc(test(no_crate_inject))], used by crates in the std facade
116
- fn should_inject_crate ( krate : & :: syntax:: ast:: Crate ) -> bool {
122
+ fn scrape_test_config ( krate : & :: syntax:: ast:: Crate ) -> TestOptions {
117
123
use syntax:: attr:: AttrMetaMethods ;
124
+ use syntax:: print:: pprust;
118
125
119
- let mut inject_crate = true ;
120
-
121
- for attr in & krate. attrs {
122
- if attr. check_name ( "doc" ) {
123
- for list in attr. meta_item_list ( ) . into_iter ( ) {
124
- for attr in list {
125
- if attr. check_name ( "test" ) {
126
- for list in attr. meta_item_list ( ) . into_iter ( ) {
127
- for attr in list {
128
- if attr. check_name ( "no_crate_inject" ) {
129
- inject_crate = false ;
130
- }
131
- }
132
- }
133
- }
126
+ let mut opts = TestOptions {
127
+ no_crate_inject : true ,
128
+ attrs : Vec :: new ( ) ,
129
+ } ;
130
+
131
+ let attrs = krate. attrs . iter ( ) . filter ( |a| a. check_name ( "doc" ) )
132
+ . filter_map ( |a| a. meta_item_list ( ) )
133
+ . flat_map ( |l| l. iter ( ) )
134
+ . filter ( |a| a. check_name ( "test" ) )
135
+ . filter_map ( |a| a. meta_item_list ( ) )
136
+ . flat_map ( |l| l. iter ( ) ) ;
137
+ for attr in attrs {
138
+ if attr. check_name ( "no_crate_inject" ) {
139
+ opts. no_crate_inject = true ;
140
+ }
141
+ if attr. check_name ( "attr" ) {
142
+ if let Some ( l) = attr. meta_item_list ( ) {
143
+ for item in l {
144
+ opts. attrs . push ( pprust:: meta_item_to_string ( item) ) ;
134
145
}
135
146
}
136
147
}
137
148
}
138
149
139
- return inject_crate ;
150
+ return opts ;
140
151
}
141
152
142
- #[ allow( deprecated) ]
143
153
fn runtest ( test : & str , cratename : & str , libs : SearchPaths ,
144
154
externs : core:: Externs ,
145
155
should_panic : bool , no_run : bool , as_test_harness : bool ,
146
- inject_crate : bool ) {
156
+ opts : & TestOptions ) {
147
157
// the test harness wants its own `main` & top level functions, so
148
158
// never wrap the test in `fn main() { ... }`
149
- let test = maketest ( test, Some ( cratename) , true , as_test_harness,
150
- inject_crate) ;
159
+ let test = maketest ( test, Some ( cratename) , as_test_harness, opts) ;
151
160
let input = config:: Input :: Str ( test. to_string ( ) ) ;
152
161
153
162
let sessopts = config:: Options {
@@ -250,8 +259,8 @@ fn runtest(test: &str, cratename: &str, libs: SearchPaths,
250
259
}
251
260
}
252
261
253
- pub fn maketest ( s : & str , cratename : Option < & str > , lints : bool ,
254
- dont_insert_main : bool , inject_crate : bool ) -> String {
262
+ pub fn maketest ( s : & str , cratename : Option < & str > , dont_insert_main : bool ,
263
+ opts : & TestOptions ) -> String {
255
264
let ( crate_attrs, everything_else) = partition_source ( s) ;
256
265
257
266
let mut prog = String :: new ( ) ;
@@ -260,20 +269,18 @@ pub fn maketest(s: &str, cratename: Option<&str>, lints: bool,
260
269
// are intended to be crate attributes.
261
270
prog. push_str ( & crate_attrs) ;
262
271
263
- if lints {
264
- prog. push_str ( r"
265
- #![allow(unused_variables, unused_assignments, unused_mut, unused_attributes, dead_code)]
266
- " ) ;
272
+ // Next, any attributes for other aspects such as lints.
273
+ for attr in & opts. attrs {
274
+ prog. push_str ( & format ! ( "#![{}]\n " , attr) ) ;
267
275
}
268
276
269
277
// Don't inject `extern crate std` because it's already injected by the
270
278
// compiler.
271
- if !s. contains ( "extern crate" ) && inject_crate {
279
+ if !s. contains ( "extern crate" ) && !opts . no_crate_inject {
272
280
match cratename {
273
281
Some ( cratename) => {
274
282
if s. contains ( cratename) {
275
- prog. push_str ( & format ! ( "extern crate {};\n " ,
276
- cratename) ) ;
283
+ prog. push_str ( & format ! ( "extern crate {};\n " , cratename) ) ;
277
284
}
278
285
}
279
286
None => { }
@@ -325,12 +332,12 @@ pub struct Collector {
325
332
use_headers : bool ,
326
333
current_header : Option < String > ,
327
334
cratename : String ,
328
- inject_crate : bool
335
+ opts : TestOptions ,
329
336
}
330
337
331
338
impl Collector {
332
339
pub fn new ( cratename : String , libs : SearchPaths , externs : core:: Externs ,
333
- use_headers : bool , inject_crate : bool ) -> Collector {
340
+ use_headers : bool , opts : TestOptions ) -> Collector {
334
341
Collector {
335
342
tests : Vec :: new ( ) ,
336
343
names : Vec :: new ( ) ,
@@ -340,7 +347,7 @@ impl Collector {
340
347
use_headers : use_headers,
341
348
current_header : None ,
342
349
cratename : cratename,
343
- inject_crate : inject_crate
350
+ opts : opts ,
344
351
}
345
352
}
346
353
@@ -357,13 +364,14 @@ impl Collector {
357
364
let libs = self . libs . clone ( ) ;
358
365
let externs = self . externs . clone ( ) ;
359
366
let cratename = self . cratename . to_string ( ) ;
360
- let inject_crate = self . inject_crate ;
367
+ let opts = self . opts . clone ( ) ;
361
368
debug ! ( "Creating test {}: {}" , name, test) ;
362
369
self . tests . push ( testing:: TestDescAndFn {
363
370
desc : testing:: TestDesc {
364
371
name : testing:: DynTestName ( name) ,
365
372
ignore : should_ignore,
366
- should_panic : testing:: ShouldPanic :: No , // compiler failures are test failures
373
+ // compiler failures are test failures
374
+ should_panic : testing:: ShouldPanic :: No ,
367
375
} ,
368
376
testfn : testing:: DynTestFn ( Box :: new ( move || {
369
377
runtest ( & test,
@@ -373,7 +381,7 @@ impl Collector {
373
381
should_panic,
374
382
no_run,
375
383
as_test_harness,
376
- inject_crate ) ;
384
+ & opts ) ;
377
385
} ) )
378
386
} ) ;
379
387
}
0 commit comments