about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorEric Huss <eric@huss.org>2023-06-17 16:06:27 -0700
committerEric Huss <eric@huss.org>2023-06-17 17:20:02 -0700
commit31e3f56e26e97cc319ae2cf7d66ad12fdb005ebb (patch)
treeb3376b88625834ec0e79e2664070ee7fdebc8c16 /src
parenta8a29070f07e47a244c8199ad582f93172019743 (diff)
rustdoc: Fix --test-run-directory and relative paths.
Diffstat (limited to 'src')
-rw-r--r--src/librustdoc/doctest.rs16
1 files changed, 16 insertions, 0 deletions
diff --git a/src/librustdoc/doctest.rs b/src/librustdoc/doctest.rs
index f6631b66f5b..b409515c5ea 100644
--- a/src/librustdoc/doctest.rs
+++ b/src/librustdoc/doctest.rs
@@ -463,7 +463,9 @@ fn run_test(
     // Run the code!
     let mut cmd;
 
+    let output_file = make_maybe_absolute_path(output_file);
     if let Some(tool) = runtool {
+        let tool = make_maybe_absolute_path(tool.into());
         cmd = Command::new(tool);
         cmd.args(runtool_args);
         cmd.arg(output_file);
@@ -497,6 +499,20 @@ fn run_test(
     Ok(())
 }
 
+/// Converts a path intended to use as a command to absolute if it is
+/// relative, and not a single component.
+///
+/// This is needed to deal with relative paths interacting with
+/// `Command::current_dir` in a platform-specific way.
+fn make_maybe_absolute_path(path: PathBuf) -> PathBuf {
+    if path.components().count() == 1 {
+        // Look up process via PATH.
+        path
+    } else {
+        std::env::current_dir().map(|c| c.join(&path)).unwrap_or_else(|_| path)
+    }
+}
+
 /// Transforms a test into code that can be compiled into a Rust binary, and returns the number of
 /// lines before the test code begins as well as if the output stream supports colors or not.
 pub(crate) fn make_test(