Skip to content

Commit 230234f

Browse files
Add information in case of markdown block code test failure
1 parent 57ecd7a commit 230234f

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

src/librustdoc/html/markdown.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,8 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) {
448448
tests.add_test(text.to_owned(),
449449
block_info.should_panic, block_info.no_run,
450450
block_info.ignore, block_info.test_harness,
451-
block_info.compile_fail, block_info.error_codes);
451+
block_info.compile_fail, block_info.error_codes,
452+
block_info.original);
452453
}
453454
}
454455

@@ -488,6 +489,7 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) {
488489

489490
#[derive(Eq, PartialEq, Clone, Debug)]
490491
struct LangString {
492+
original: String,
491493
should_panic: bool,
492494
no_run: bool,
493495
ignore: bool,
@@ -500,6 +502,7 @@ struct LangString {
500502
impl LangString {
501503
fn all_false() -> LangString {
502504
LangString {
505+
original: String::new(),
503506
should_panic: false,
504507
no_run: false,
505508
ignore: false,
@@ -521,6 +524,7 @@ impl LangString {
521524
allow_error_code_check = true;
522525
}
523526

527+
data.original = string.to_owned();
524528
let tokens = string.split(|c: char|
525529
!(c == '_' || c == '-' || c.is_alphanumeric())
526530
);
@@ -647,6 +651,7 @@ mod tests {
647651
test_harness: test_harness,
648652
compile_fail: compile_fail,
649653
error_codes: error_codes,
654+
original: s.to_owned(),
650655
})
651656
}
652657

src/librustdoc/test.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,15 @@ fn runtest(test: &str, cratename: &str, cfgs: Vec<String>, libs: SearchPaths,
161161
externs: Externs,
162162
should_panic: bool, no_run: bool, as_test_harness: bool,
163163
compile_fail: bool, mut error_codes: Vec<String>, opts: &TestOptions,
164-
maybe_sysroot: Option<PathBuf>) {
164+
maybe_sysroot: Option<PathBuf>,
165+
original: &str) {
165166
// the test harness wants its own `main` & top level functions, so
166167
// never wrap the test in `fn main() { ... }`
167-
let test = maketest(test, Some(cratename), as_test_harness, opts);
168+
let new_test = maketest(test, Some(cratename), as_test_harness, opts);
169+
let test = format!("```{}\n{}\n```\n", original, test);
168170
let input = config::Input::Str {
169171
name: driver::anon_src(),
170-
input: test.to_owned(),
172+
input: new_test.to_owned(),
171173
};
172174
let outputs = OutputTypes::new(&[(OutputType::Exe, None)]);
173175

@@ -249,20 +251,22 @@ fn runtest(test: &str, cratename: &str, cfgs: Vec<String>, libs: SearchPaths,
249251
if count > 0 && !compile_fail {
250252
sess.fatal("aborting due to previous error(s)")
251253
} else if count == 0 && compile_fail {
252-
panic!("test compiled while it wasn't supposed to")
254+
panic!("test compiled while it wasn't supposed to:\n\n{}\n", test)
253255
}
254256
if count > 0 && error_codes.len() > 0 {
255257
let out = String::from_utf8(data.lock().unwrap().to_vec()).unwrap();
256258
error_codes.retain(|err| !out.contains(err));
257259
}
258260
}
259-
Ok(()) if compile_fail => panic!("test compiled while it wasn't supposed to"),
261+
Ok(()) if compile_fail => {
262+
panic!("test compiled while it wasn't supposed to:\n\n{}\n", test)
263+
}
260264
_ => {}
261265
}
262266
}
263267
Err(_) => {
264268
if !compile_fail {
265-
panic!("couldn't compile the test");
269+
panic!("couldn't compile the test:\n\n{}\n", test);
266270
}
267271
if error_codes.len() > 0 {
268272
let out = String::from_utf8(data.lock().unwrap().to_vec()).unwrap();
@@ -272,7 +276,7 @@ fn runtest(test: &str, cratename: &str, cfgs: Vec<String>, libs: SearchPaths,
272276
}
273277

274278
if error_codes.len() > 0 {
275-
panic!("Some expected error codes were not found: {:?}", error_codes);
279+
panic!("Some expected error codes were not found: {:?}\n\n{}\n", error_codes, test);
276280
}
277281

278282
if no_run { return }
@@ -294,17 +298,18 @@ fn runtest(test: &str, cratename: &str, cfgs: Vec<String>, libs: SearchPaths,
294298
cmd.env(var, &newpath);
295299

296300
match cmd.output() {
297-
Err(e) => panic!("couldn't run the test: {}{}", e,
301+
Err(e) => panic!("couldn't run the test: {}{}\n\n{}\n", e,
298302
if e.kind() == io::ErrorKind::PermissionDenied {
299303
" - maybe your tempdir is mounted with noexec?"
300-
} else { "" }),
304+
} else { "" }, test),
301305
Ok(out) => {
302306
if should_panic && out.status.success() {
303-
panic!("test executable succeeded when it should have failed");
307+
panic!("test executable succeeded when it should have failed\n\n{}\n", test);
304308
} else if !should_panic && !out.status.success() {
305-
panic!("test executable failed:\n{}\n{}",
309+
panic!("test executable failed:\n{}\n{}\n\n{}\n",
306310
str::from_utf8(&out.stdout).unwrap_or(""),
307-
str::from_utf8(&out.stderr).unwrap_or(""));
311+
str::from_utf8(&out.stderr).unwrap_or(""),
312+
test);
308313
}
309314
}
310315
}
@@ -406,7 +411,8 @@ impl Collector {
406411

407412
pub fn add_test(&mut self, test: String,
408413
should_panic: bool, no_run: bool, should_ignore: bool,
409-
as_test_harness: bool, compile_fail: bool, error_codes: Vec<String>) {
414+
as_test_harness: bool, compile_fail: bool, error_codes: Vec<String>,
415+
original: String) {
410416
let name = if self.use_headers {
411417
let s = self.current_header.as_ref().map(|s| &**s).unwrap_or("");
412418
format!("{}_{}", s, self.cnt)
@@ -446,7 +452,8 @@ impl Collector {
446452
compile_fail,
447453
error_codes,
448454
&opts,
449-
maybe_sysroot)
455+
maybe_sysroot,
456+
&original)
450457
})
451458
} {
452459
Ok(()) => (),

0 commit comments

Comments
 (0)