diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index 442a2f40742..3df476907d9 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -448,7 +448,8 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) { tests.add_test(text.to_owned(), block_info.should_panic, block_info.no_run, block_info.ignore, block_info.test_harness, - block_info.compile_fail, block_info.error_codes); + block_info.compile_fail, block_info.error_codes, + block_info.original); } } @@ -488,6 +489,7 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) { #[derive(Eq, PartialEq, Clone, Debug)] struct LangString { + original: String, should_panic: bool, no_run: bool, ignore: bool, @@ -500,6 +502,7 @@ struct LangString { impl LangString { fn all_false() -> LangString { LangString { + original: String::new(), should_panic: false, no_run: false, ignore: false, @@ -521,6 +524,7 @@ fn parse(string: &str) -> LangString { allow_error_code_check = true; } + data.original = string.to_owned(); let tokens = string.split(|c: char| !(c == '_' || c == '-' || c.is_alphanumeric()) ); @@ -647,6 +651,7 @@ fn t(s: &str, test_harness: test_harness, compile_fail: compile_fail, error_codes: error_codes, + original: s.to_owned(), }) } diff --git a/src/librustdoc/test.rs b/src/librustdoc/test.rs index ab0ac02fd88..00327007dd0 100644 --- a/src/librustdoc/test.rs +++ b/src/librustdoc/test.rs @@ -161,13 +161,15 @@ fn runtest(test: &str, cratename: &str, cfgs: Vec, libs: SearchPaths, externs: Externs, should_panic: bool, no_run: bool, as_test_harness: bool, compile_fail: bool, mut error_codes: Vec, opts: &TestOptions, - maybe_sysroot: Option) { + maybe_sysroot: Option, + original: &str) { // the test harness wants its own `main` & top level functions, so // never wrap the test in `fn main() { ... }` - let test = maketest(test, Some(cratename), as_test_harness, opts); + let new_test = maketest(test, Some(cratename), as_test_harness, opts); + let test = format!("```{}\n{}\n```\n", original, test); let input = config::Input::Str { name: driver::anon_src(), - input: test.to_owned(), + input: new_test.to_owned(), }; let outputs = OutputTypes::new(&[(OutputType::Exe, None)]); @@ -249,20 +251,22 @@ fn drop(&mut self) { if count > 0 && !compile_fail { sess.fatal("aborting due to previous error(s)") } else if count == 0 && compile_fail { - panic!("test compiled while it wasn't supposed to") + panic!("test compiled while it wasn't supposed to:\n\n{}\n", test) } if count > 0 && error_codes.len() > 0 { let out = String::from_utf8(data.lock().unwrap().to_vec()).unwrap(); error_codes.retain(|err| !out.contains(err)); } } - Ok(()) if compile_fail => panic!("test compiled while it wasn't supposed to"), + Ok(()) if compile_fail => { + panic!("test compiled while it wasn't supposed to:\n\n{}\n", test) + } _ => {} } } Err(_) => { if !compile_fail { - panic!("couldn't compile the test"); + panic!("couldn't compile the test:\n\n{}\n", test); } if error_codes.len() > 0 { let out = String::from_utf8(data.lock().unwrap().to_vec()).unwrap(); @@ -272,7 +276,7 @@ fn drop(&mut self) { } if error_codes.len() > 0 { - panic!("Some expected error codes were not found: {:?}", error_codes); + panic!("Some expected error codes were not found: {:?}\n\n{}\n", error_codes, test); } if no_run { return } @@ -294,17 +298,18 @@ fn drop(&mut self) { cmd.env(var, &newpath); match cmd.output() { - Err(e) => panic!("couldn't run the test: {}{}", e, + Err(e) => panic!("couldn't run the test: {}{}\n\n{}\n", e, if e.kind() == io::ErrorKind::PermissionDenied { " - maybe your tempdir is mounted with noexec?" - } else { "" }), + } else { "" }, test), Ok(out) => { if should_panic && out.status.success() { - panic!("test executable succeeded when it should have failed"); + panic!("test executable succeeded when it should have failed\n\n{}\n", test); } else if !should_panic && !out.status.success() { - panic!("test executable failed:\n{}\n{}", + panic!("test executable failed:\n{}\n{}\n\n{}\n", str::from_utf8(&out.stdout).unwrap_or(""), - str::from_utf8(&out.stderr).unwrap_or("")); + str::from_utf8(&out.stderr).unwrap_or(""), + test); } } } @@ -406,7 +411,8 @@ pub fn new(cratename: String, cfgs: Vec, libs: SearchPaths, externs: Ext pub fn add_test(&mut self, test: String, should_panic: bool, no_run: bool, should_ignore: bool, - as_test_harness: bool, compile_fail: bool, error_codes: Vec) { + as_test_harness: bool, compile_fail: bool, error_codes: Vec, + original: String) { let name = if self.use_headers { let s = self.current_header.as_ref().map(|s| &**s).unwrap_or(""); format!("{}_{}", s, self.cnt) @@ -446,7 +452,8 @@ pub fn add_test(&mut self, test: String, compile_fail, error_codes, &opts, - maybe_sysroot) + maybe_sysroot, + &original) }) } { Ok(()) => (),