Skip to content

Commit

Permalink
Improve handling of --persist-doctests with combined doctests and u…
Browse files Browse the repository at this point in the history
…pdate associated test to check both combined and non-combined doctests
  • Loading branch information
GuillaumeGomez committed Apr 17, 2024
1 parent 76c9ae6 commit c7e80be
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
29 changes: 22 additions & 7 deletions src/librustdoc/doctest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ enum TestFailure {
UnexpectedRunPass,
}

#[derive(Debug)]
enum DirState {
Temp(tempfile::TempDir),
Perm(PathBuf),
Expand Down Expand Up @@ -383,10 +384,26 @@ fn run_test(
edition: Edition,
report_unused_externs: impl Fn(UnusedExterns),
no_run: bool,
// Used to prevent overwriting a binary in case `--persist-doctests` is used.
binary_extra: Option<&str>,
) -> Result<(), TestFailure> {
// Make sure we emit well-formed executable names for our target.
let rust_out = add_exe_suffix("rust_out".to_owned(), &rustdoc_options.target);
let output_file = outdir.path().join(rust_out);
let rust_out =
add_exe_suffix(format!("rust_out{}", binary_extra.unwrap_or("")), &rustdoc_options.target);
let out_dir = outdir.path();
let out_dir = if is_multiple_tests {
// If this a "multiple tests" case, we want to put it into the parent instead.
out_dir.parent().unwrap_or(out_dir)
} else {
out_dir
};
if matches!(*outdir, DirState::Perm(..)) {
if let Err(err) = std::fs::create_dir_all(&out_dir) {
eprintln!("Couldn't create directory for doctest executables: {err}");
panic::resume_unwind(Box::new(()));
}
}
let output_file = out_dir.join(rust_out);

let rustc_binary = rustdoc_options
.test_builder
Expand Down Expand Up @@ -753,6 +770,7 @@ impl DocTest {
edition,
report_unused_externs,
no_run,
None,
);

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

if let Err(err) = std::fs::create_dir_all(&path) {
eprintln!("Couldn't create directory for doctest executables: {err}");
panic::resume_unwind(Box::new(()));
}

DirState::Perm(path)
} else {
DirState::Temp(get_doctest_dir().expect("rustdoc needs a tempdir"))
Expand Down Expand Up @@ -1390,6 +1403,8 @@ test::test_main(&[{test_args}], vec![{ids}], None);
self.edition,
|_: UnusedExterns| {},
false,
// To prevent writing over an existing doctest
Some(&format!("_{}_{}", self.edition, *self.ran_edition_tests)),
);
if let Err(TestFailure::CompileError) = ret {
// We failed to compile all compatible tests as one so we push them into the
Expand Down
8 changes: 4 additions & 4 deletions tests/run-make/doctests-keep-binaries/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ run:
mkdir -p $(TMPDIR)/doctests
$(RUSTC) --crate-type rlib t.rs
$(RUSTDOC) -Zunstable-options --test --persist-doctests $(TMPDIR)/doctests --extern t=$(TMPDIR)/libt.rlib t.rs
$(TMPDIR)/doctests/t_rs_2_0/rust_out
$(TMPDIR)/doctests/t_rs_8_0/rust_out
$(TMPDIR)/doctests/t_rs_12_0/rust_out
$(TMPDIR)/doctests/rust_out_2015_0
rm -rf $(TMPDIR)/doctests

no_run:
mkdir -p $(TMPDIR)/doctests
$(RUSTC) --crate-type rlib t.rs
$(RUSTDOC) -Zunstable-options --test --persist-doctests $(TMPDIR)/doctests --extern t=$(TMPDIR)/libt.rlib t.rs --no-run
$(TMPDIR)/doctests/t_rs_2_0/rust_out
$(TMPDIR)/doctests/t_rs_8_0/rust_out
$(TMPDIR)/doctests/t_rs_12_0/rust_out
$(TMPDIR)/doctests/rust_out_2015_0
rm -rf $(TMPDIR)/doctests

# Behavior with --test-run-directory with relative paths.
Expand Down
8 changes: 8 additions & 0 deletions tests/run-make/doctests-keep-binaries/t.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@ pub fn foople() {}
/// ```
/// t::florp();
/// ```
///
/// ```
/// #![no_std]
///
/// fn main() {
/// let x = 12;
/// }
/// ```
pub fn florp() {}

0 comments on commit c7e80be

Please sign in to comment.