Skip to content

Commit d4708f4

Browse files
Correctly handle exit status
1 parent 743d5a6 commit d4708f4

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

src/librustdoc/doctest.rs

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,12 @@ fn run_test(
459459
let stdin = child.stdin.as_mut().expect("Failed to open stdin");
460460
stdin.write_all(test.as_bytes()).expect("could write out test sources");
461461
}
462-
let output = child.wait_with_output().expect("Failed to read stdout");
462+
let output = if is_multiple_tests {
463+
let status = child.wait().expect("Failed to wait");
464+
process::Output { status, stdout: Vec::new(), stderr: Vec::new() }
465+
} else {
466+
child.wait_with_output().expect("Failed to read stdout")
467+
};
463468

464469
struct Bomb<'a>(&'a str);
465470
impl Drop for Bomb<'_> {
@@ -540,17 +545,17 @@ fn run_test(
540545
cmd.output()
541546
};
542547
match result {
543-
Err(e) => return Err(TestFailure::ExecutionError(e)),
548+
Err(e) => Err(TestFailure::ExecutionError(e)),
544549
Ok(out) => {
545550
if lang_string.should_panic && out.status.success() {
546-
return Err(TestFailure::UnexpectedRunPass);
551+
Err(TestFailure::UnexpectedRunPass)
547552
} else if !lang_string.should_panic && !out.status.success() {
548-
return Err(TestFailure::ExecutionFailure(out));
553+
Err(TestFailure::ExecutionFailure(out))
554+
} else {
555+
Ok(())
549556
}
550557
}
551558
}
552-
553-
Ok(())
554559
}
555560

556561
/// Converts a path intended to use as a command to absolute if it is
@@ -1276,11 +1281,14 @@ impl DocTestKinds {
12761281
doctest: DocTest,
12771282
opts: &GlobalTestOptions,
12781283
edition: Edition,
1284+
rustdoc_options: &RustdocOptions,
12791285
unused_externs: &Arc<Mutex<Vec<UnusedExterns>>>,
12801286
) {
12811287
if doctest.failed_ast
12821288
|| doctest.lang_string.compile_fail
12831289
|| doctest.lang_string.test_harness
1290+
|| rustdoc_options.nocapture
1291+
|| rustdoc_options.test_args.iter().any(|arg| arg == "--show-output")
12841292
|| doctest.crate_attrs.contains("#![no_std]")
12851293
{
12861294
self.standalone.push(doctest.generate_test_desc_and_fn(
@@ -1306,6 +1314,7 @@ impl DocTestKinds {
13061314
}
13071315
let Self { mut standalone, others } = self;
13081316
let mut ran_edition_tests = 0;
1317+
let mut nb_errors = 0;
13091318

13101319
for (edition, mut doctests) in others {
13111320
doctests.sort_by(|a, b| a.name.cmp(&b.name));
@@ -1348,7 +1357,7 @@ fn main() {{
13481357
}}",
13491358
)
13501359
.unwrap();
1351-
if let Err(TestFailure::CompileError) = run_test(
1360+
let ret = run_test(
13521361
output,
13531362
supports_color,
13541363
None,
@@ -1359,7 +1368,8 @@ fn main() {{
13591368
edition,
13601369
|_: UnusedExterns| {},
13611370
false,
1362-
) {
1371+
);
1372+
if let Err(TestFailure::CompileError) = ret {
13631373
// We failed to compile all compatible tests as one so we push them into the
13641374
// "standalone" doctests.
13651375
debug!("Failed to compile compatible doctests for edition {edition} all at once");
@@ -1372,13 +1382,20 @@ fn main() {{
13721382
}
13731383
} else {
13741384
ran_edition_tests += 1;
1385+
if ret.is_err() {
1386+
nb_errors += 1;
1387+
}
13751388
}
13761389
}
13771390

13781391
if ran_edition_tests == 0 || !standalone.is_empty() {
13791392
standalone.sort_by(|a, b| a.desc.name.as_slice().cmp(&b.desc.name.as_slice()));
13801393
test::test_main(&test_args, standalone, None);
13811394
}
1395+
if nb_errors != 0 {
1396+
// libtest::ERROR_EXIT_CODE is not public but it's the same value.
1397+
std::process::exit(101);
1398+
}
13821399
}
13831400
}
13841401

@@ -1539,7 +1556,13 @@ impl Tester for Collector {
15391556
path,
15401557
no_run,
15411558
);
1542-
self.tests.add_doctest(doctest, &opts, edition, &self.unused_extern_reports);
1559+
self.tests.add_doctest(
1560+
doctest,
1561+
&opts,
1562+
edition,
1563+
&self.rustdoc_options,
1564+
&self.unused_extern_reports,
1565+
);
15431566
}
15441567

15451568
fn get_line(&self) -> usize {

0 commit comments

Comments
 (0)