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

Commit 53ff5dc

Browse files
committed
works unless the doctest is using macros
1 parent a1333a1 commit 53ff5dc

File tree

2 files changed

+12
-50
lines changed

2 files changed

+12
-50
lines changed

src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,9 +226,13 @@ pub fn test(config: &Config) -> Result<()> {
226226
.map_err(|e| failure::Error::from(e.context("could not find generated documentation")))?;
227227
let docs: Documentation = serde_json::from_reader(doc_json)?;
228228

229+
// TODO a better way to find crate name?
230+
let krate = docs.data.as_ref().unwrap();
231+
let crate_name = krate.id.split("::").next().unwrap();
232+
229233
let location = config.output_path().join("tests");
230234
let tests = test::find_tests(&docs);
231-
test::save_tests(&config, &tests, &location)?;
235+
test::save_tests(&tests, &location, &crate_name)?;
232236
let binary = test::compile_tests(&config, &location)?;
233237
test::execute_tests(&binary)?;
234238

src/test.rs

Lines changed: 7 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ fn find_test_blocks(docs: &str) -> Vec<String> {
150150
/// Any crate attributes are preserved at the top level.
151151
fn preprocess(test: &str, crate_name: &str) -> String {
152152
if let Ok(mut ast) = syn::parse_crate(test) {
153+
154+
// TODO if the extern crate has `#[macro_use]` we need to strip it out
153155
let has_extern_crate = ast.items.iter().any(|item| match item.node {
154156
ItemKind::ExternCrate(..) => true,
155157
_ => false,
@@ -241,7 +243,7 @@ fn preprocess(test: &str, crate_name: &str) -> String {
241243
}
242244
}
243245

244-
pub fn save_tests(config: &Config, tests: &Vec<(&String, Vec<String>)>, save_path: &Path) -> Result<()> {
246+
pub fn save_tests(tests: &Vec<(&String, Vec<String>)>, save_path: &Path, crate_name: &str) -> Result<()> {
245247
DirBuilder::new().recursive(true).create(save_path)?;
246248

247249
let mut mods = vec![];
@@ -267,14 +269,7 @@ pub fn save_tests(config: &Config, tests: &Vec<(&String, Vec<String>)>, save_pat
267269
// TODO use syn here as well?
268270
let mut main = String::new();
269271

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-
//}
276-
277-
main.push_str("#[macro_use] extern crate hyper;");
272+
main.push_str(&format!("extern crate {};", crate_name));
278273
for m in mods {
279274
main.push_str(&format!("mod {};\n", m));
280275
}
@@ -288,50 +283,13 @@ pub fn save_tests(config: &Config, tests: &Vec<(&String, Vec<String>)>, save_pat
288283
pub fn compile_tests(config: &Config, save_path: &Path) -> Result<PathBuf> {
289284
static TEST_NAME: &str = "rustdoc-test";
290285

291-
// First, determine the location of the dependency artifacts so we can pass them to rustc.
292-
let output = Command::new("cargo")
293-
.arg("build")
294-
.arg("--manifest-path")
295-
.arg(&config.manifest_path)
296-
.args(&["--message-format", "json"])
297-
.output()?;
298-
if !output.status.success() {
299-
return Err(format_err!(
300-
"cargo did not exit successfully: {}",
301-
output.status
302-
));
303-
}
304-
305-
let output = String::from_utf8(output.stdout).expect("cargo did not output valid utf-8");
286+
let crate_externs = find_externs_for_crate(config)?;
306287

307288
let mut externs = vec![];
308-
309-
for message in output.lines() {
310-
let message = serde_json::from_str::<Value>(message)?;
311-
let is_compiler_artifact = message
312-
.as_object()
313-
.unwrap()
314-
.get("reason")
315-
.and_then(Value::as_str)
316-
.map(|reason| reason == "compiler-artifact")
317-
.unwrap_or_default();
318-
319-
if is_compiler_artifact {
320-
let name = message
321-
.pointer("/target/name")
322-
.and_then(Value::as_str)
323-
.unwrap();
324-
let name = name.replace("-", "_");
325-
let rlib = message
326-
.pointer("/filenames/0")
327-
.and_then(Value::as_str)
328-
.unwrap();
329-
330-
externs.push(format!("{}={}", name, rlib));
331-
}
289+
for c in crate_externs.iter() {
290+
externs.push(format!("{}={}", &c.name, &c.location.to_string_lossy()));
332291
}
333292

334-
let crate_externs = find_externs_for_crate(config)?;
335293
let search_path = crate_externs.first().map(|e| {
336294
e.location.parent().unwrap().to_path_buf()
337295
}).unwrap();

0 commit comments

Comments
 (0)