Skip to content

Commit 31e3f56

Browse files
committed
rustdoc: Fix --test-run-directory and relative paths.
1 parent a8a2907 commit 31e3f56

File tree

5 files changed

+62
-1
lines changed

5 files changed

+62
-1
lines changed

Diff for: src/librustdoc/doctest.rs

+16
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,9 @@ fn run_test(
463463
// Run the code!
464464
let mut cmd;
465465

466+
let output_file = make_maybe_absolute_path(output_file);
466467
if let Some(tool) = runtool {
468+
let tool = make_maybe_absolute_path(tool.into());
467469
cmd = Command::new(tool);
468470
cmd.args(runtool_args);
469471
cmd.arg(output_file);
@@ -497,6 +499,20 @@ fn run_test(
497499
Ok(())
498500
}
499501

502+
/// Converts a path intended to use as a command to absolute if it is
503+
/// relative, and not a single component.
504+
///
505+
/// This is needed to deal with relative paths interacting with
506+
/// `Command::current_dir` in a platform-specific way.
507+
fn make_maybe_absolute_path(path: PathBuf) -> PathBuf {
508+
if path.components().count() == 1 {
509+
// Look up process via PATH.
510+
path
511+
} else {
512+
std::env::current_dir().map(|c| c.join(&path)).unwrap_or_else(|_| path)
513+
}
514+
}
515+
500516
/// Transforms a test into code that can be compiled into a Rust binary, and returns the number of
501517
/// lines before the test code begins as well as if the output stream supports colors or not.
502518
pub(crate) fn make_test(

Diff for: tests/run-make/doctests-keep-binaries/Makefile

+12-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ include ../tools.mk
33

44
# Check that valid binaries are persisted by running them, regardless of whether the --run or --no-run option is used.
55

6-
all: run no_run
6+
MY_SRC_DIR := ${CURDIR}
7+
8+
all: run no_run test_run_directory
79

810
run:
911
mkdir -p $(TMPDIR)/doctests
@@ -20,3 +22,12 @@ no_run:
2022
$(TMPDIR)/doctests/t_rs_2_0/rust_out
2123
$(TMPDIR)/doctests/t_rs_8_0/rust_out
2224
rm -rf $(TMPDIR)/doctests
25+
26+
# Behavior with --test-run-directory with relative paths.
27+
test_run_directory:
28+
mkdir -p $(TMPDIR)/doctests
29+
mkdir -p $(TMPDIR)/rundir
30+
$(RUSTC) --crate-type rlib t.rs
31+
( cd $(TMPDIR); \
32+
$(RUSTDOC) -Zunstable-options --test --persist-doctests doctests --test-run-directory rundir --extern t=libt.rlib $(MY_SRC_DIR)/t.rs )
33+
rm -rf $(TMPDIR)/doctests $(TMPDIR)/rundir

Diff for: tests/run-make/doctests-runtool/Makefile

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# ignore-cross-compile
2+
include ../tools.mk
3+
4+
# Tests behavior of rustdoc --runtool
5+
6+
MY_SRC_DIR := ${CURDIR}
7+
8+
all: with_test_run_directory
9+
10+
# Behavior with --runtool with relative paths and --test-run-directory.
11+
with_test_run_directory:
12+
mkdir -p $(TMPDIR)/rundir
13+
mkdir -p $(TMPDIR)/runtool
14+
$(RUSTC) --crate-type rlib t.rs
15+
$(RUSTC) runtool.rs -o $(TMPDIR)/runtool/runtool
16+
( cd $(TMPDIR); \
17+
$(RUSTDOC) -Zunstable-options --test --test-run-directory rundir \
18+
--runtool runtool/runtool --extern t=libt.rlib $(MY_SRC_DIR)/t.rs \
19+
)
20+
rm -rf $(TMPDIR)/rundir $(TMPDIR)/runtool

Diff for: tests/run-make/doctests-runtool/runtool.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
eprintln!("{:?}", std::env::args().collect::<Vec<_>>());
3+
}

Diff for: tests/run-make/doctests-runtool/t.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// Fungle the foople.
2+
/// ```
3+
/// t::foople();
4+
/// ```
5+
pub fn foople() {}
6+
7+
/// Flomble the florp
8+
/// ```
9+
/// t::florp();
10+
/// ```
11+
pub fn florp() {}

0 commit comments

Comments
 (0)