@@ -27,13 +27,12 @@ struct Extern {
27
27
location : PathBuf ,
28
28
}
29
29
30
- pub fn find_tests < ' a > ( config : & Config , docs : & ' a Documentation ) -> Vec < ( & ' a String , Vec < String > ) > {
31
- let externs = find_externs_for_crate ( config) . unwrap ( ) ;
30
+ pub fn find_tests < ' a > ( docs : & ' a Documentation ) -> Vec < ( & ' a String , Vec < String > ) > {
32
31
let krate = docs. data . as_ref ( ) . unwrap ( ) ;
33
32
34
33
iter:: once ( krate)
35
34
. chain ( docs. included . iter ( ) . flat_map ( |data| data) )
36
- . map ( |data| ( & data. id , gather_tests ( & data, & externs ) ) )
35
+ . map ( |data| ( & data. id , gather_tests ( & data) ) )
37
36
. collect ( )
38
37
}
39
38
@@ -87,13 +86,13 @@ fn find_externs_for_crate(config: &Config) -> Result <Vec<Extern>> {
87
86
}
88
87
89
88
/// Find and prepare tests in the given document.
90
- fn gather_tests ( document : & Document , externs : & [ Extern ] ) -> Vec < String > {
89
+ fn gather_tests ( document : & Document ) -> Vec < String > {
91
90
if let Some ( docs) = document. attributes . get ( "docs" ) {
92
91
find_test_blocks ( docs)
93
92
. into_iter ( )
94
93
. map ( |block| {
95
94
let crate_name = document. id . split ( "::" ) . next ( ) . unwrap ( ) ;
96
- preprocess ( & block, crate_name, externs )
95
+ preprocess ( & block, crate_name)
97
96
} )
98
97
. collect ( )
99
98
} else {
@@ -149,7 +148,7 @@ fn find_test_blocks(docs: &str) -> Vec<String> {
149
148
/// 2. Wrap the code in `fn main() {}` if there is no `main` function.
150
149
///
151
150
/// Any crate attributes are preserved at the top level.
152
- fn preprocess ( test : & str , crate_name : & str , externs : & [ Extern ] ) -> String {
151
+ fn preprocess ( test : & str , crate_name : & str ) -> String {
153
152
if let Ok ( mut ast) = syn:: parse_crate ( test) {
154
153
let has_extern_crate = ast. items . iter ( ) . any ( |item| match item. node {
155
154
ItemKind :: ExternCrate ( ..) => true ,
@@ -187,6 +186,19 @@ fn preprocess(test: &str, crate_name: &str, externs: &[Extern]) -> String {
187
186
stmts. push ( main_fn_call) ;
188
187
}
189
188
189
+ // TODO: Handle `#![doc(test(no_crate_inject))]`?
190
+ if !has_extern_crate && crate_name != "std" {
191
+ stmts. insert (
192
+ 0 ,
193
+ Stmt :: Item ( Box :: new ( Item {
194
+ ident : Ident :: new ( crate_name) ,
195
+ vis : Visibility :: Inherited ,
196
+ attrs : vec ! [ ] ,
197
+ node : ItemKind :: ExternCrate ( None ) ,
198
+ } ) ) ,
199
+ )
200
+ }
201
+
190
202
let a_doc_test = ItemKind :: Fn (
191
203
Box :: new ( FnDecl {
192
204
inputs : vec ! [ ] ,
@@ -217,31 +229,6 @@ fn preprocess(test: &str, crate_name: &str, externs: &[Extern]) -> String {
217
229
node : a_doc_test,
218
230
} ) ;
219
231
220
- // TODO: Handle `#![doc(test(no_crate_inject))]`?
221
- if !has_extern_crate && crate_name != "std" {
222
- ast. items . insert (
223
- 0 ,
224
- Item {
225
- ident : Ident :: new ( crate_name) ,
226
- vis : Visibility :: Inherited ,
227
- attrs : vec ! [ ] ,
228
- node : ItemKind :: ExternCrate ( None ) ,
229
- } ,
230
- )
231
- }
232
-
233
- //for e in externs {
234
- // ast.items.insert(
235
- // 0,
236
- // Item {
237
- // ident: Ident::new(e.name.clone()),
238
- // vis: Visibility::Inherited,
239
- // attrs: vec![],
240
- // node: ItemKind::ExternCrate(None),
241
- // },
242
- // )
243
- //}
244
-
245
232
let mut tokens = Tokens :: new ( ) ;
246
233
ast. to_tokens ( & mut tokens) ;
247
234
let program = tokens. to_string ( ) ;
@@ -280,13 +267,14 @@ pub fn save_tests(config: &Config, tests: &Vec<(&String, Vec<String>)>, save_pat
280
267
// TODO use syn here as well?
281
268
let mut main = String :: new ( ) ;
282
269
283
- let externs = find_externs_for_crate ( config) ?;
284
- for e in externs {
285
- let name = e. name . replace ( "\" " , "" ) ;
286
- let name = name. replace ( "-" , "_" ) ;
287
- main. push_str ( & format ! ( "extern crate {};\n " , name) ) ;
288
- }
270
+ // let externs = find_externs_for_crate(config)?;
271
+ // for e in externs {
272
+ // let name = e.name.replace("\"", "");
273
+ // let name = name.replace("-", "_");
274
+ // main.push_str(&format!("extern crate {};\n", name));
275
+ // }
289
276
277
+ main. push_str ( "#[macro_use] extern crate hyper;" ) ;
290
278
for m in mods {
291
279
main. push_str ( & format ! ( "mod {};\n " , m) ) ;
292
280
}
@@ -343,6 +331,11 @@ pub fn compile_tests(config: &Config, save_path: &Path) -> Result<PathBuf> {
343
331
}
344
332
}
345
333
334
+ let crate_externs = find_externs_for_crate ( config) ?;
335
+ let search_path = crate_externs. first ( ) . map ( |e| {
336
+ e. location . parent ( ) . unwrap ( ) . to_path_buf ( )
337
+ } ) . unwrap ( ) ;
338
+
346
339
let extern_args: Vec < _ > = externs
347
340
. into_iter ( )
348
341
. flat_map ( |arg| vec ! [ String :: from( "--extern" ) , arg] )
@@ -353,6 +346,8 @@ pub fn compile_tests(config: &Config, save_path: &Path) -> Result<PathBuf> {
353
346
. arg ( "--test" )
354
347
. args ( & [ "-o" , TEST_NAME ] )
355
348
. args ( & [ "--cap-lints" , "allow" ] )
349
+ . arg ( "-L" )
350
+ . arg ( search_path. to_str ( ) . unwrap ( ) )
356
351
. args ( extern_args)
357
352
. current_dir ( & save_path)
358
353
. output ( ) ?;
0 commit comments