Skip to content

Commit 46fed6e

Browse files
authored
Rollup merge of rust-lang#39859 - GuillaumeGomez:rustdoc-test-relative-path, r=alexcrichton
Set rustdoc --test files' path relative to the current directory r? @alexcrichton
2 parents e78aa5d + 958fbc5 commit 46fed6e

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/librustdoc/test.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::env;
1212
use std::ffi::OsString;
1313
use std::io::prelude::*;
1414
use std::io;
15-
use std::path::PathBuf;
15+
use std::path::{Path, PathBuf};
1616
use std::panic::{self, AssertUnwindSafe};
1717
use std::process::Command;
1818
use std::rc::Rc;
@@ -485,7 +485,15 @@ impl Collector {
485485

486486
pub fn get_filename(&self) -> String {
487487
if let Some(ref codemap) = self.codemap {
488-
codemap.span_to_filename(self.position)
488+
let filename = codemap.span_to_filename(self.position);
489+
if let Ok(cur_dir) = env::current_dir() {
490+
if let Ok(path) = Path::new(&filename).strip_prefix(&cur_dir) {
491+
if let Some(path) = path.to_str() {
492+
return path.to_owned();
493+
}
494+
}
495+
}
496+
filename
489497
} else if let Some(ref filename) = self.filename {
490498
filename.clone()
491499
} else {

src/tools/compiletest/src/runtest.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -1987,12 +1987,22 @@ actual:\n\
19871987
fn check_rustdoc_test_option(&self, res: ProcRes) {
19881988
let mut other_files = Vec::new();
19891989
let mut files: HashMap<String, Vec<usize>> = HashMap::new();
1990-
files.insert(self.testpaths.file.to_str().unwrap().to_owned(),
1990+
let cwd = env::current_dir().unwrap();
1991+
files.insert(self.testpaths.file.strip_prefix(&cwd)
1992+
.unwrap_or(&self.testpaths.file)
1993+
.to_str()
1994+
.unwrap()
1995+
.replace('\\', "/"),
19911996
self.get_lines(&self.testpaths.file, Some(&mut other_files)));
19921997
for other_file in other_files {
19931998
let mut path = self.testpaths.file.clone();
19941999
path.set_file_name(&format!("{}.rs", other_file));
1995-
files.insert(path.to_str().unwrap().to_owned(), self.get_lines(&path, None));
2000+
files.insert(path.strip_prefix(&cwd)
2001+
.unwrap_or(&path)
2002+
.to_str()
2003+
.unwrap()
2004+
.replace('\\', "/"),
2005+
self.get_lines(&path, None));
19962006
}
19972007

19982008
let mut tested = 0;
@@ -2002,7 +2012,8 @@ actual:\n\
20022012
let tmp: Vec<&str> = s.split(" - ").collect();
20032013
if tmp.len() == 2 {
20042014
let path = tmp[0].rsplit("test ").next().unwrap();
2005-
if let Some(ref mut v) = files.get_mut(path) {
2015+
if let Some(ref mut v) = files.get_mut(
2016+
&path.replace('\\', "/")) {
20062017
tested += 1;
20072018
let mut iter = tmp[1].split("(line ");
20082019
iter.next();

0 commit comments

Comments
 (0)