Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beautify doctest path #130643

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions compiler/rustc_span/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,16 @@ impl FileName {
_ => None,
}
}

/// Returns the path suitable for reading from the file system on the local host,
/// if this information exists.
pub fn remapped_path_if_available(&self) -> Option<&Path> {
match self {
FileName::Real(path) => Some(path.remapped_path_if_available()),
FileName::DocTest(path, _) => Some(path),
_ => return None,
}
}
}

/// Represents a span.
Expand Down
26 changes: 23 additions & 3 deletions src/librustdoc/doctest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod rust;

use std::fs::File;
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::path::{Component, Path, PathBuf};
use std::process::{self, Command, Stdio};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
Expand Down Expand Up @@ -713,6 +713,22 @@ impl IndividualTestOptions {
}
}

fn beautify_path(path: &Path) -> PathBuf {
let mut beautiful_path = PathBuf::new();

for component in path.components().filter(|c| !matches!(c, Component::CurDir)) {
if matches!(component, Component::ParentDir) {
if beautiful_path.parent().is_none() {
return path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
}
beautiful_path.pop();
} else {
beautiful_path.push(component.as_os_str());
}
}
beautiful_path
}

/// A doctest scraped from the code, ready to be turned into a runnable test.
///
/// The pipeline goes: [`clean`] AST -> `ScrapedDoctest` -> `RunnableDoctest`.
Expand Down Expand Up @@ -743,8 +759,12 @@ impl ScrapedDocTest {
if !item_path.is_empty() {
item_path.push(' ');
}
let name =
format!("{} - {item_path}(line {line})", filename.prefer_remapped_unconditionaly());
let name = match filename.remapped_path_if_available() {
Some(path) => format!("{} - {item_path}(line {line})", beautify_path(path).display()),
None => {
format!("{} - {item_path}(line {line})", filename.prefer_remapped_unconditionaly())
}
};

Self { filename, line, langstr, text, name }
}
Expand Down
2 changes: 2 additions & 0 deletions tests/run-make/rustdoc-test-path/foo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#![doc = include_str!("./sub/bar.md")]
#![doc = include_str!("sub/../sub/bar.md")]
14 changes: 14 additions & 0 deletions tests/run-make/rustdoc-test-path/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// This test ensures that the file paths displayed in doctests are "beautified",
// meaning they don't contain ".." or "." characters.

use run_make_support::rustdoc;

fn main() {
rustdoc()
.input("foo.rs")
.arg("--test")
.run_fail()
.assert_stdout_not_contains("-- ../")
.assert_stdout_not_contains("-- ./")
.assert_stdout_contains("-- sub/bar.md ");
}
7 changes: 7 additions & 0 deletions tests/run-make/rustdoc-test-path/sub/bar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
With a code sample, that has an error:

```rust
fn main() {
let x = 234 // no semicolon here! oh no!
}
```
Loading