Skip to content

Commit

Permalink
Rollup merge of #81264 - Swatinem:doctest-run-directory, r=jyn514
Browse files Browse the repository at this point in the history
Add unstable option to control doctest run directory

This option will allow splitting the compile-time from the run-time
directory of doctest invocations and is one step to solve
rust-lang/cargo#8993 (comment)

r? `@jyn514`
  • Loading branch information
jonas-schievink authored Jan 24, 2021
2 parents 22dc82f + 9b1d27d commit 504d6de
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/librustdoc/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ crate struct Options {
crate should_test: bool,
/// List of arguments to pass to the test harness, if running tests.
crate test_args: Vec<String>,
/// The working directory in which to run tests.
crate test_run_directory: Option<PathBuf>,
/// Optional path to persist the doctest executables to, defaults to a
/// temporary directory if not set.
crate persist_doctests: Option<PathBuf>,
Expand Down Expand Up @@ -175,6 +177,7 @@ impl fmt::Debug for Options {
.field("lint_cap", &self.lint_cap)
.field("should_test", &self.should_test)
.field("test_args", &self.test_args)
.field("test_run_directory", &self.test_run_directory)
.field("persist_doctests", &self.persist_doctests)
.field("default_passes", &self.default_passes)
.field("manual_passes", &self.manual_passes)
Expand Down Expand Up @@ -572,6 +575,7 @@ impl Options {
let enable_index_page = matches.opt_present("enable-index-page") || index_page.is_some();
let static_root_path = matches.opt_str("static-root-path");
let generate_search_filter = !matches.opt_present("disable-per-crate-search");
let test_run_directory = matches.opt_str("test-run-directory").map(PathBuf::from);
let persist_doctests = matches.opt_str("persist-doctests").map(PathBuf::from);
let test_builder = matches.opt_str("test-builder").map(PathBuf::from);
let codegen_options_strs = matches.opt_strs("C");
Expand Down Expand Up @@ -613,6 +617,7 @@ impl Options {
display_warnings,
show_coverage,
crate_version,
test_run_directory,
persist_doctests,
runtool,
runtool_args,
Expand Down
3 changes: 3 additions & 0 deletions src/librustdoc/doctest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,9 @@ fn run_test(
} else {
cmd = Command::new(output_file);
}
if let Some(run_directory) = options.test_run_directory {
cmd.current_dir(run_directory);
}

match cmd.output() {
Err(e) => return Err(TestFailure::ExecutionError(e)),
Expand Down
8 changes: 8 additions & 0 deletions src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@ fn opts() -> Vec<RustcOptGroup> {
stable("test-args", |o| {
o.optmulti("", "test-args", "arguments to pass to the test runner", "ARGS")
}),
unstable("test-run-directory", |o| {
o.optopt(
"",
"test-run-directory",
"The working directory in which to run tests",
"PATH",
)
}),
stable("target", |o| o.optopt("", "target", "target triple to document", "TRIPLE")),
stable("markdown-css", |o| {
o.optmulti(
Expand Down
6 changes: 6 additions & 0 deletions src/test/rustdoc-ui/run-directory.correct.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

running 1 test
test $DIR/run-directory.rs - foo (line 10) ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME

6 changes: 6 additions & 0 deletions src/test/rustdoc-ui/run-directory.incorrect.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

running 1 test
test $DIR/run-directory.rs - foo (line 19) ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME

23 changes: 23 additions & 0 deletions src/test/rustdoc-ui/run-directory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// this test asserts that the cwd of doctest invocations is set correctly.

// revisions: correct incorrect
// check-pass
// [correct]compile-flags:--test --test-run-directory={{src-base}}
// [incorrect]compile-flags:--test --test-run-directory={{src-base}}/coverage
// normalize-stdout-test: "src/test/rustdoc-ui" -> "$$DIR"
// normalize-stdout-test "finished in \d+\.\d+s" -> "finished in $$TIME"

/// ```
/// assert_eq!(
/// std::fs::read_to_string("run-directory.rs").unwrap(),
/// include_str!("run-directory.rs"),
/// );
/// ```
#[cfg(correct)]
pub fn foo() {}

/// ```
/// assert!(std::fs::read_to_string("run-directory.rs").is_err());
/// ```
#[cfg(incorrect)]
pub fn foo() {}

0 comments on commit 504d6de

Please sign in to comment.