Skip to content
This repository was archived by the owner on May 20, 2020. It is now read-only.

Commit a1333a1

Browse files
committed
compiles if i mess with the macro use
1 parent beb1b96 commit a1333a1

File tree

2 files changed

+33
-38
lines changed

2 files changed

+33
-38
lines changed

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ pub fn test(config: &Config) -> Result<()> {
227227
let docs: Documentation = serde_json::from_reader(doc_json)?;
228228

229229
let location = config.output_path().join("tests");
230-
let tests = test::find_tests(&config, &docs);
230+
let tests = test::find_tests(&docs);
231231
test::save_tests(&config, &tests, &location)?;
232232
let binary = test::compile_tests(&config, &location)?;
233233
test::execute_tests(&binary)?;

src/test.rs

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@ struct Extern {
2727
location: PathBuf,
2828
}
2929

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>)> {
3231
let krate = docs.data.as_ref().unwrap();
3332

3433
iter::once(krate)
3534
.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)))
3736
.collect()
3837
}
3938

@@ -87,13 +86,13 @@ fn find_externs_for_crate(config: &Config) -> Result <Vec<Extern>> {
8786
}
8887

8988
/// 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> {
9190
if let Some(docs) = document.attributes.get("docs") {
9291
find_test_blocks(docs)
9392
.into_iter()
9493
.map(|block| {
9594
let crate_name = document.id.split("::").next().unwrap();
96-
preprocess(&block, crate_name, externs)
95+
preprocess(&block, crate_name)
9796
})
9897
.collect()
9998
} else {
@@ -149,7 +148,7 @@ fn find_test_blocks(docs: &str) -> Vec<String> {
149148
/// 2. Wrap the code in `fn main() {}` if there is no `main` function.
150149
///
151150
/// 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 {
153152
if let Ok(mut ast) = syn::parse_crate(test) {
154153
let has_extern_crate = ast.items.iter().any(|item| match item.node {
155154
ItemKind::ExternCrate(..) => true,
@@ -187,6 +186,19 @@ fn preprocess(test: &str, crate_name: &str, externs: &[Extern]) -> String {
187186
stmts.push(main_fn_call);
188187
}
189188

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+
190202
let a_doc_test = ItemKind::Fn(
191203
Box::new(FnDecl {
192204
inputs: vec![],
@@ -217,31 +229,6 @@ fn preprocess(test: &str, crate_name: &str, externs: &[Extern]) -> String {
217229
node: a_doc_test,
218230
});
219231

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-
245232
let mut tokens = Tokens::new();
246233
ast.to_tokens(&mut tokens);
247234
let program = tokens.to_string();
@@ -280,13 +267,14 @@ pub fn save_tests(config: &Config, tests: &Vec<(&String, Vec<String>)>, save_pat
280267
// TODO use syn here as well?
281268
let mut main = String::new();
282269

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+
//}
289276

277+
main.push_str("#[macro_use] extern crate hyper;");
290278
for m in mods {
291279
main.push_str(&format!("mod {};\n", m));
292280
}
@@ -343,6 +331,11 @@ pub fn compile_tests(config: &Config, save_path: &Path) -> Result<PathBuf> {
343331
}
344332
}
345333

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+
346339
let extern_args: Vec<_> = externs
347340
.into_iter()
348341
.flat_map(|arg| vec![String::from("--extern"), arg])
@@ -353,6 +346,8 @@ pub fn compile_tests(config: &Config, save_path: &Path) -> Result<PathBuf> {
353346
.arg("--test")
354347
.args(&["-o", TEST_NAME])
355348
.args(&["--cap-lints", "allow"])
349+
.arg("-L")
350+
.arg(search_path.to_str().unwrap())
356351
.args(extern_args)
357352
.current_dir(&save_path)
358353
.output()?;

0 commit comments

Comments
 (0)