]> git.lizzy.rs Git - rust.git/commitdiff
Add option to control doctest run directory
authorArpad Borsos <arpad.borsos@googlemail.com>
Fri, 22 Jan 2021 09:54:53 +0000 (10:54 +0100)
committerArpad Borsos <arpad.borsos@googlemail.com>
Sat, 23 Jan 2021 17:52:51 +0000 (18:52 +0100)
This option will allow splitting the compile-time from the run-time
directory of doctest invocations and is one step to solve
https://github.com/rust-lang/cargo/issues/8993#issuecomment-760088944

src/librustdoc/config.rs
src/librustdoc/doctest.rs
src/librustdoc/lib.rs
src/test/rustdoc-ui/run-directory.correct.stdout [new file with mode: 0644]
src/test/rustdoc-ui/run-directory.incorrect.stdout [new file with mode: 0644]
src/test/rustdoc-ui/run-directory.rs [new file with mode: 0644]

index e43ea965c04237d8b6f9cc9d23d8dbe7251e5968..508bed072e61de47695cd7b64fa9f6a853d3498f 100644 (file)
@@ -103,6 +103,8 @@ fn try_from(value: &str) -> Result<Self, Self::Error> {
     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>,
@@ -175,6 +177,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
             .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)
@@ -572,6 +575,7 @@ fn println_condition(condition: Condition) {
         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");
@@ -613,6 +617,7 @@ fn println_condition(condition: Condition) {
             display_warnings,
             show_coverage,
             crate_version,
+            test_run_directory,
             persist_doctests,
             runtool,
             runtool_args,
index c87ab833f7a5e443d430b0e210556934e6a4273c..09cbd94e4ac3f10ba5871b52cb75189e25f1c654 100644 (file)
@@ -365,6 +365,9 @@ fn drop(&mut self) {
     } 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)),
index 719aca612f50d61338c16b1e5eec565fbb743494..d823f789013ffbd14d81d133b398e1ead6ee9dc0 100644 (file)
@@ -167,6 +167,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(
diff --git a/src/test/rustdoc-ui/run-directory.correct.stdout b/src/test/rustdoc-ui/run-directory.correct.stdout
new file mode 100644 (file)
index 0000000..e9b2754
--- /dev/null
@@ -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
+
diff --git a/src/test/rustdoc-ui/run-directory.incorrect.stdout b/src/test/rustdoc-ui/run-directory.incorrect.stdout
new file mode 100644 (file)
index 0000000..97a5dbc
--- /dev/null
@@ -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
+
diff --git a/src/test/rustdoc-ui/run-directory.rs b/src/test/rustdoc-ui/run-directory.rs
new file mode 100644 (file)
index 0000000..78431c0
--- /dev/null
@@ -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() {}