Skip to content

Commit c7e80be

Browse files
Improve handling of --persist-doctests with combined doctests and update associated test to check both combined and non-combined doctests
1 parent 76c9ae6 commit c7e80be

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

src/librustdoc/doctest.rs

+22-7
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ enum TestFailure {
318318
UnexpectedRunPass,
319319
}
320320

321+
#[derive(Debug)]
321322
enum DirState {
322323
Temp(tempfile::TempDir),
323324
Perm(PathBuf),
@@ -383,10 +384,26 @@ fn run_test(
383384
edition: Edition,
384385
report_unused_externs: impl Fn(UnusedExterns),
385386
no_run: bool,
387+
// Used to prevent overwriting a binary in case `--persist-doctests` is used.
388+
binary_extra: Option<&str>,
386389
) -> Result<(), TestFailure> {
387390
// Make sure we emit well-formed executable names for our target.
388-
let rust_out = add_exe_suffix("rust_out".to_owned(), &rustdoc_options.target);
389-
let output_file = outdir.path().join(rust_out);
391+
let rust_out =
392+
add_exe_suffix(format!("rust_out{}", binary_extra.unwrap_or("")), &rustdoc_options.target);
393+
let out_dir = outdir.path();
394+
let out_dir = if is_multiple_tests {
395+
// If this a "multiple tests" case, we want to put it into the parent instead.
396+
out_dir.parent().unwrap_or(out_dir)
397+
} else {
398+
out_dir
399+
};
400+
if matches!(*outdir, DirState::Perm(..)) {
401+
if let Err(err) = std::fs::create_dir_all(&out_dir) {
402+
eprintln!("Couldn't create directory for doctest executables: {err}");
403+
panic::resume_unwind(Box::new(()));
404+
}
405+
}
406+
let output_file = out_dir.join(rust_out);
390407

391408
let rustc_binary = rustdoc_options
392409
.test_builder
@@ -753,6 +770,7 @@ impl DocTest {
753770
edition,
754771
report_unused_externs,
755772
no_run,
773+
None,
756774
);
757775

758776
if let Err(err) = res {
@@ -897,11 +915,6 @@ pub(crate) fn make_test(
897915
let mut path = path.clone();
898916
path.push(&test_id);
899917

900-
if let Err(err) = std::fs::create_dir_all(&path) {
901-
eprintln!("Couldn't create directory for doctest executables: {err}");
902-
panic::resume_unwind(Box::new(()));
903-
}
904-
905918
DirState::Perm(path)
906919
} else {
907920
DirState::Temp(get_doctest_dir().expect("rustdoc needs a tempdir"))
@@ -1390,6 +1403,8 @@ test::test_main(&[{test_args}], vec![{ids}], None);
13901403
self.edition,
13911404
|_: UnusedExterns| {},
13921405
false,
1406+
// To prevent writing over an existing doctest
1407+
Some(&format!("_{}_{}", self.edition, *self.ran_edition_tests)),
13931408
);
13941409
if let Err(TestFailure::CompileError) = ret {
13951410
// We failed to compile all compatible tests as one so we push them into the

tests/run-make/doctests-keep-binaries/Makefile

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ run:
1111
mkdir -p $(TMPDIR)/doctests
1212
$(RUSTC) --crate-type rlib t.rs
1313
$(RUSTDOC) -Zunstable-options --test --persist-doctests $(TMPDIR)/doctests --extern t=$(TMPDIR)/libt.rlib t.rs
14-
$(TMPDIR)/doctests/t_rs_2_0/rust_out
15-
$(TMPDIR)/doctests/t_rs_8_0/rust_out
14+
$(TMPDIR)/doctests/t_rs_12_0/rust_out
15+
$(TMPDIR)/doctests/rust_out_2015_0
1616
rm -rf $(TMPDIR)/doctests
1717

1818
no_run:
1919
mkdir -p $(TMPDIR)/doctests
2020
$(RUSTC) --crate-type rlib t.rs
2121
$(RUSTDOC) -Zunstable-options --test --persist-doctests $(TMPDIR)/doctests --extern t=$(TMPDIR)/libt.rlib t.rs --no-run
22-
$(TMPDIR)/doctests/t_rs_2_0/rust_out
23-
$(TMPDIR)/doctests/t_rs_8_0/rust_out
22+
$(TMPDIR)/doctests/t_rs_12_0/rust_out
23+
$(TMPDIR)/doctests/rust_out_2015_0
2424
rm -rf $(TMPDIR)/doctests
2525

2626
# Behavior with --test-run-directory with relative paths.

tests/run-make/doctests-keep-binaries/t.rs

+8
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,12 @@ pub fn foople() {}
88
/// ```
99
/// t::florp();
1010
/// ```
11+
///
12+
/// ```
13+
/// #![no_std]
14+
///
15+
/// fn main() {
16+
/// let x = 12;
17+
/// }
18+
/// ```
1119
pub fn florp() {}

0 commit comments

Comments
 (0)