Skip to content

Commit 4235fbe

Browse files
Correctly handle test args and move ignored doctests into the one file
1 parent cc8cea2 commit 4235fbe

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

src/librustdoc/doctest.rs

+31-19
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ fn run_test(
382382
mut lang_string: LangString,
383383
edition: Edition,
384384
report_unused_externs: impl Fn(UnusedExterns),
385+
no_run: bool,
385386
) -> Result<(), TestFailure> {
386387
// Make sure we emit well-formed executable names for our target.
387388
let rust_out = add_exe_suffix("rust_out".to_owned(), &rustdoc_options.target);
@@ -418,8 +419,7 @@ fn run_test(
418419
compiler.arg("-Z").arg("unstable-options");
419420
}
420421

421-
if lang_string.no_run && !lang_string.compile_fail && rustdoc_options.persist_doctests.is_none()
422-
{
422+
if no_run && !lang_string.compile_fail && rustdoc_options.persist_doctests.is_none() {
423423
compiler.arg("--emit=metadata");
424424
}
425425
compiler.arg("--target").arg(match rustdoc_options.target {
@@ -459,7 +459,7 @@ 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 = if is_multiple_tests {
462+
let output = if !is_multiple_tests {
463463
let status = child.wait().expect("Failed to wait");
464464
process::Output { status, stdout: Vec::new(), stderr: Vec::new() }
465465
} else {
@@ -515,7 +515,7 @@ fn run_test(
515515
}
516516
}
517517

518-
if lang_string.no_run {
518+
if no_run {
519519
return Ok(());
520520
}
521521

@@ -714,7 +714,9 @@ impl DocTest {
714714
) -> TestDescAndFn {
715715
let (code, line_offset) =
716716
self.generate_unique_doctest(self.lang_string.test_harness, opts, Some(&self.test_id));
717-
let Self { supports_color, rustdoc_test_options, lang_string, outdir, path, .. } = self;
717+
let Self {
718+
supports_color, rustdoc_test_options, lang_string, outdir, path, no_run, ..
719+
} = self;
718720
TestDescAndFn {
719721
desc: test::TestDesc {
720722
name: test::DynTestName(std::mem::replace(&mut self.name, String::new())),
@@ -728,7 +730,7 @@ impl DocTest {
728730
// compiler failures are test failures
729731
should_panic: test::ShouldPanic::No,
730732
compile_fail: lang_string.compile_fail,
731-
no_run: lang_string.no_run,
733+
no_run,
732734
test_type: test::TestType::DocTest,
733735
},
734736
testfn: test::DynTestFn(Box::new(move || {
@@ -745,6 +747,7 @@ impl DocTest {
745747
lang_string,
746748
edition,
747749
report_unused_externs,
750+
no_run,
748751
);
749752

750753
if let Err(err) = res {
@@ -808,7 +811,9 @@ impl DocTest {
808811

809812
writeln!(output, "mod {test_id} {{\n{}", self.crates).unwrap();
810813

811-
if self.main_fn_span.is_some() {
814+
if self.ignore {
815+
// We generate nothing.
816+
} else if self.main_fn_span.is_some() {
812817
output.push_str(&self.everything_else);
813818
} else {
814819
let returns_result = if self.everything_else.trim_end().ends_with("(())") {
@@ -855,10 +860,11 @@ pub const TEST: test::TestDescAndFn = test::TestDescAndFn {{
855860
ignore = self.ignore,
856861
file = self.file,
857862
line = self.line,
858-
should_panic = if self.lang_string.should_panic { "Yes" } else { "No" },
863+
should_panic = if !self.no_run && self.lang_string.should_panic { "Yes" } else { "No" },
859864
// Setting `no_run` to `true` in `TestDesc` still makes the test run, so we simply
860865
// don't give it the function to run.
861-
runner = if self.no_run { "Ok::<(), String>(())" } else { "self::main()" },
866+
runner =
867+
if self.no_run || self.ignore { "Ok::<(), String>(())" } else { "self::main()" },
862868
)
863869
.unwrap();
864870
test_id
@@ -936,10 +942,11 @@ pub(crate) fn make_test(
936942
Ok(p) => p,
937943
Err(errs) => {
938944
errs.into_iter().for_each(|err| err.cancel());
939-
return (found_main, found_extern_crate, found_macro);
945+
return (found_main, found_extern_crate, found_macro, true);
940946
}
941947
};
942948

949+
let mut has_errors = false;
943950
loop {
944951
match parser.parse_item(ForceCollect::No) {
945952
Ok(Some(item)) => {
@@ -970,6 +977,7 @@ pub(crate) fn make_test(
970977
Ok(None) => break,
971978
Err(e) => {
972979
e.cancel();
980+
has_errors = true;
973981
break;
974982
}
975983
}
@@ -979,13 +987,14 @@ pub(crate) fn make_test(
979987
parser.maybe_consume_incorrect_semicolon(&[]);
980988
}
981989

990+
has_errors = has_errors || psess.dcx.has_errors_or_delayed_bugs().is_some();
982991
// Reset errors so that they won't be reported as compiler bugs when dropping the
983992
// dcx. Any errors in the tests will be reported when the test file is compiled,
984993
// Note that we still need to cancel the errors above otherwise `Diag` will panic on
985994
// drop.
986995
psess.dcx.reset_err_count();
987996

988-
(found_main, found_extern_crate, found_macro)
997+
(found_main, found_extern_crate, found_macro, has_errors)
989998
})
990999
});
9911000

@@ -994,7 +1003,7 @@ pub(crate) fn make_test(
9941003
Ignore::None => false,
9951004
Ignore::Some(ref ignores) => ignores.iter().any(|s| target_str.contains(s)),
9961005
};
997-
let Ok((mut main_fn_span, already_has_extern_crate, found_macro)) = result else {
1006+
let Ok((mut main_fn_span, already_has_extern_crate, found_macro, has_errors)) = result else {
9981007
// If the parser panicked due to a fatal error, pass the test code through unchanged.
9991008
// The error will be reported during compilation.
10001009
return DocTest {
@@ -1050,7 +1059,7 @@ pub(crate) fn make_test(
10501059
lang_string,
10511060
line,
10521061
file,
1053-
failed_ast: false,
1062+
failed_ast: has_errors,
10541063
rustdoc_test_options,
10551064
outdir,
10561065
test_id,
@@ -1197,7 +1206,6 @@ fn partition_source(s: &str, edition: Edition) -> (String, String, String) {
11971206
(before, after.trim().to_owned(), crates)
11981207
}
11991208

1200-
#[derive(Clone)]
12011209
pub(crate) struct IndividualTestOptions {
12021210
test_builder: Option<PathBuf>,
12031211
test_builder_wrappers: Vec<PathBuf>,
@@ -1275,7 +1283,6 @@ impl DocTestKinds {
12751283
if doctest.failed_ast
12761284
|| doctest.lang_string.compile_fail
12771285
|| doctest.lang_string.test_harness
1278-
|| doctest.ignore
12791286
|| doctest.crate_attrs.contains("#![no_std]")
12801287
{
12811288
self.standalone.push(doctest.generate_test_desc_and_fn(
@@ -1300,6 +1307,7 @@ impl DocTestKinds {
13001307
test_args.push("--nocapture".to_string());
13011308
}
13021309
let Self { mut standalone, others } = self;
1310+
let mut ran_edition_tests = 0;
13031311

13041312
for (edition, mut doctests) in others {
13051313
doctests.sort_by(|a, b| a.name.cmp(&b.name));
@@ -1323,17 +1331,18 @@ impl DocTestKinds {
13231331
if !ids.is_empty() {
13241332
ids.push(',');
13251333
}
1326-
ids.push_str(&format!("&{}::TEST", doctest.generate_test_desc(pos, &mut output)));
1334+
ids.push_str(&format!("{}::TEST", doctest.generate_test_desc(pos, &mut output)));
13271335
supports_color &= doctest.supports_color;
13281336
}
1337+
let test_args =
1338+
test_args.iter().map(|arg| format!("{arg:?}.to_string(),")).collect::<String>();
13291339
write!(
13301340
output,
13311341
"\
13321342
#[rustc_main]
13331343
#[coverage(off)]
13341344
fn main() {{
1335-
test::test_main_static(&[{ids}]);
1336-
panic!(\"fuck\");
1345+
test::test_main(&[{test_args}], vec![{ids}], None);
13371346
}}",
13381347
)
13391348
.unwrap();
@@ -1347,6 +1356,7 @@ fn main() {{
13471356
LangString::empty_for_test(),
13481357
edition,
13491358
|_: UnusedExterns| {},
1359+
false,
13501360
) {
13511361
// We failed to compile all compatible tests as one so we push them into the
13521362
// "standalone" doctests.
@@ -1358,10 +1368,12 @@ fn main() {{
13581368
Arc::clone(unused_externs),
13591369
));
13601370
}
1371+
} else {
1372+
ran_edition_tests += 1;
13611373
}
13621374
}
13631375

1364-
if !standalone.is_empty() {
1376+
if ran_edition_tests == 0 || !standalone.is_empty() {
13651377
standalone.sort_by(|a, b| a.desc.name.as_slice().cmp(&b.desc.name.as_slice()));
13661378
test::test_main(&test_args, standalone, None);
13671379
}

0 commit comments

Comments
 (0)