Skip to content

Commit 722baed

Browse files
authored
Rollup merge of #39597 - GuillaumeGomez:correct_rustdoc_test_file, r=alexcrichton
Display correct filename with --test option Fixes #39592. With the current files: ```rust pub mod foo; /// This is a Foo; /// /// ``` /// println!("baaaaaar"); /// ``` pub struct Foo; /// This is a Bar; /// /// ``` /// println!("fooooo"); /// ``` pub struct Bar; ``` ```rust // note the whitespaces /// ``` /// println!("foo"); /// ``` pub fn foo() {} ``` It displays: ``` ./build/x86_64-apple-darwin/stage1/bin/rustdoc --test test.rs running 3 tests test test.rs - line 13 ... ok test test.rs - line 5 ... ok test foo.rs - line 2 ... ok test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured ``` ``` ` `` println!("lol"); ` `` asdjnfasd asd ``` It displays: ``` ./build/x86_64-apple-darwin/stage1/bin/rustdoc --test foo.md running 1 test test <input> - line 3 ... ok test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured ``` r? @alexcrichton
2 parents 7658921 + d2f8abf commit 722baed

File tree

6 files changed

+108
-47
lines changed

6 files changed

+108
-47
lines changed

src/librustdoc/html/markdown.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use std::fmt::{self, Write};
3535
use std::slice;
3636
use std::str;
3737
use syntax::feature_gate::UnstableFeatures;
38+
use syntax::codemap::Span;
3839

3940
use html::render::derive_id;
4041
use html::toc::TocBuilder;
@@ -424,7 +425,7 @@ pub fn render(w: &mut fmt::Formatter,
424425
}
425426
}
426427

427-
pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector, start_line: usize) {
428+
pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector, position: Span) {
428429
extern fn block(_ob: *mut hoedown_buffer,
429430
text: *const hoedown_buffer,
430431
lang: *const hoedown_buffer,
@@ -449,11 +450,12 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector, start_line:
449450
});
450451
let text = lines.collect::<Vec<&str>>().join("\n");
451452
let line = tests.get_line() + line;
453+
let filename = tests.get_filename();
452454
tests.add_test(text.to_owned(),
453455
block_info.should_panic, block_info.no_run,
454456
block_info.ignore, block_info.test_harness,
455457
block_info.compile_fail, block_info.error_codes,
456-
line);
458+
line, filename);
457459
}
458460
}
459461

@@ -474,7 +476,7 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector, start_line:
474476
}
475477
}
476478

477-
tests.set_line(start_line);
479+
tests.set_position(position);
478480
unsafe {
479481
let ob = hoedown_buffer_new(DEF_OUNIT);
480482
let renderer = hoedown_html_renderer_new(0, 0);

src/librustdoc/markdown.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use getopts;
1818
use testing;
1919
use rustc::session::search_paths::SearchPaths;
2020
use rustc::session::config::Externs;
21+
use syntax::codemap::DUMMY_SP;
2122

2223
use externalfiles::{ExternalHtml, LoadStringError, load_string};
2324

@@ -154,9 +155,8 @@ pub fn test(input: &str, cfgs: Vec<String>, libs: SearchPaths, externs: Externs,
154155
let mut opts = TestOptions::default();
155156
opts.no_crate_inject = true;
156157
let mut collector = Collector::new(input.to_string(), cfgs, libs, externs,
157-
true, opts, maybe_sysroot, "input".to_string(),
158-
None);
159-
find_testable_code(&input_str, &mut collector, 0);
158+
true, opts, maybe_sysroot, None);
159+
find_testable_code(&input_str, &mut collector, DUMMY_SP);
160160
test_args.insert(0, "rustdoctest".to_string());
161161
testing::test_main(&test_args, collector.tests);
162162
0

src/librustdoc/test.rs

+21-16
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use rustc_trans::back::link;
3737
use syntax::ast;
3838
use syntax::codemap::CodeMap;
3939
use syntax::feature_gate::UnstableFeatures;
40-
use syntax_pos::{BytePos, DUMMY_SP, Pos};
40+
use syntax_pos::{BytePos, DUMMY_SP, Pos, Span};
4141
use errors;
4242
use errors::emitter::ColorConfig;
4343

@@ -97,15 +97,13 @@ pub fn run(input: &str,
9797
link::find_crate_name(None, &hir_forest.krate().attrs, &input)
9898
});
9999
let opts = scrape_test_config(hir_forest.krate());
100-
let filename = input_path.to_str().unwrap_or("").to_owned();
101100
let mut collector = Collector::new(crate_name,
102101
cfgs,
103102
libs,
104103
externs,
105104
false,
106105
opts,
107106
maybe_sysroot,
108-
filename,
109107
Some(codemap));
110108

111109
{
@@ -391,15 +389,14 @@ pub struct Collector {
391389
cratename: String,
392390
opts: TestOptions,
393391
maybe_sysroot: Option<PathBuf>,
394-
filename: String,
395-
start_line: usize,
392+
position: Span,
396393
codemap: Option<Rc<CodeMap>>,
397394
}
398395

399396
impl Collector {
400397
pub fn new(cratename: String, cfgs: Vec<String>, libs: SearchPaths, externs: Externs,
401398
use_headers: bool, opts: TestOptions, maybe_sysroot: Option<PathBuf>,
402-
filename: String, codemap: Option<Rc<CodeMap>>) -> Collector {
399+
codemap: Option<Rc<CodeMap>>) -> Collector {
403400
Collector {
404401
tests: Vec::new(),
405402
names: Vec::new(),
@@ -412,17 +409,16 @@ impl Collector {
412409
cratename: cratename,
413410
opts: opts,
414411
maybe_sysroot: maybe_sysroot,
415-
filename: filename,
416-
start_line: 0,
412+
position: DUMMY_SP,
417413
codemap: codemap,
418414
}
419415
}
420416

421417
pub fn add_test(&mut self, test: String,
422418
should_panic: bool, no_run: bool, should_ignore: bool,
423419
as_test_harness: bool, compile_fail: bool, error_codes: Vec<String>,
424-
line: usize) {
425-
let name = format!("{} - line {}", self.filename, line);
420+
line: usize, filename: String) {
421+
let name = format!("{} - line {}", filename, line);
426422
self.cnt += 1;
427423
let cfgs = self.cfgs.clone();
428424
let libs = self.libs.clone();
@@ -467,16 +463,25 @@ impl Collector {
467463
}
468464

469465
pub fn get_line(&self) -> usize {
470-
if let Some(ref codemap) = self.codemap{
471-
let line = codemap.lookup_char_pos(BytePos(self.start_line as u32)).line;
466+
if let Some(ref codemap) = self.codemap {
467+
let line = self.position.lo.to_usize();
468+
let line = codemap.lookup_char_pos(BytePos(line as u32)).line;
472469
if line > 0 { line - 1 } else { line }
473470
} else {
474-
self.start_line
471+
0
475472
}
476473
}
477474

478-
pub fn set_line(&mut self, start_line: usize) {
479-
self.start_line = start_line;
475+
pub fn set_position(&mut self, position: Span) {
476+
self.position = position;
477+
}
478+
479+
pub fn get_filename(&self) -> String {
480+
if let Some(ref codemap) = self.codemap {
481+
codemap.span_to_filename(self.position)
482+
} else {
483+
"<input>".to_owned()
484+
}
480485
}
481486

482487
pub fn register_header(&mut self, name: &str, level: u32) {
@@ -520,7 +525,7 @@ impl<'a, 'hir> HirCollector<'a, 'hir> {
520525
if let Some(doc) = attrs.doc_value() {
521526
self.collector.cnt = 0;
522527
markdown::find_testable_code(doc, self.collector,
523-
attrs.span.unwrap_or(DUMMY_SP).lo.to_usize());
528+
attrs.span.unwrap_or(DUMMY_SP));
524529
}
525530

526531
nested(self);
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: --test
12+
// check-test-line-numbers-match
13+
14+
/// This looks like another awesome test!
15+
///
16+
/// ```
17+
/// println!("foo?");
18+
/// ```
19+
pub fn foooo() {}

src/test/rustdoc/test_option_check/test.rs

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
// compile-flags: --test
1212
// check-test-line-numbers-match
1313

14+
pub mod bar;
15+
1416
/// This is a Foo;
1517
///
1618
/// ```

src/tools/compiletest/src/runtest.rs

+58-25
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use std::io::{self, BufReader};
3030
use std::path::{Path, PathBuf};
3131
use std::process::{Command, Output, ExitStatus};
3232
use std::str;
33+
use std::collections::HashMap;
3334

3435
use extract_gdb_version;
3536

@@ -1942,17 +1943,28 @@ actual:\n\
19421943
}
19431944
}
19441945

1945-
fn check_rustdoc_test_option(&self, res: ProcRes) {
1946-
let mut file = fs::File::open(&self.testpaths.file)
1946+
fn get_lines<P: AsRef<Path>>(&self, path: &P,
1947+
mut other_files: Option<&mut Vec<String>>) -> Vec<usize> {
1948+
let mut file = fs::File::open(path)
19471949
.expect("markdown_test_output_check_entry File::open failed");
19481950
let mut content = String::new();
19491951
file.read_to_string(&mut content)
19501952
.expect("markdown_test_output_check_entry read_to_string failed");
19511953
let mut ignore = false;
1952-
let mut v: Vec<usize> =
1953-
content.lines()
1954-
.enumerate()
1955-
.filter_map(|(line_nb, line)| {
1954+
content.lines()
1955+
.enumerate()
1956+
.filter_map(|(line_nb, line)| {
1957+
if (line.trim_left().starts_with("pub mod ") ||
1958+
line.trim_left().starts_with("mod ")) &&
1959+
line.ends_with(";") {
1960+
if let Some(ref mut other_files) = other_files {
1961+
other_files.push(line.rsplit("mod ")
1962+
.next()
1963+
.unwrap()
1964+
.replace(";", ""));
1965+
}
1966+
None
1967+
} else {
19561968
let sline = line.split("///").last().unwrap_or("");
19571969
let line = sline.trim_left();
19581970
if line.starts_with("```") {
@@ -1966,36 +1978,57 @@ actual:\n\
19661978
} else {
19671979
None
19681980
}
1969-
})
1970-
.collect();
1981+
}
1982+
})
1983+
.collect()
1984+
}
1985+
1986+
fn check_rustdoc_test_option(&self, res: ProcRes) {
1987+
let mut other_files = Vec::new();
1988+
let mut files: HashMap<String, Vec<usize>> = HashMap::new();
1989+
files.insert(self.testpaths.file.to_str().unwrap().to_owned(),
1990+
self.get_lines(&self.testpaths.file, Some(&mut other_files)));
1991+
for other_file in other_files {
1992+
let mut path = self.testpaths.file.clone();
1993+
path.set_file_name(&format!("{}.rs", other_file));
1994+
files.insert(path.to_str().unwrap().to_owned(), self.get_lines(&path, None));
1995+
}
19711996

19721997
let mut tested = 0;
19731998
for _ in res.stdout.split("\n")
19741999
.filter(|s| s.starts_with("test "))
19752000
.inspect(|s| {
19762001
let tmp: Vec<&str> = s.split(" - line ").collect();
19772002
if tmp.len() == 2 {
1978-
tested += 1;
1979-
let line = tmp[1].split(" ...")
1980-
.next()
1981-
.unwrap_or("0")
1982-
.parse()
1983-
.unwrap_or(0);
1984-
if let Ok(pos) = v.binary_search(&line) {
1985-
v.remove(pos);
1986-
} else {
1987-
self.fatal_proc_rec(
1988-
&format!("Not found doc test: \"{}\" in {:?}", s, v),
1989-
&res);
2003+
let path = tmp[0].rsplit("test ").next().unwrap();
2004+
if let Some(ref mut v) = files.get_mut(path) {
2005+
tested += 1;
2006+
let line = tmp[1].split(" ...")
2007+
.next()
2008+
.unwrap_or("0")
2009+
.parse()
2010+
.unwrap_or(0);
2011+
if let Ok(pos) = v.binary_search(&line) {
2012+
v.remove(pos);
2013+
} else {
2014+
self.fatal_proc_rec(
2015+
&format!("Not found doc test: \"{}\" in \"{}\":{:?}",
2016+
s, path, v),
2017+
&res);
2018+
}
19902019
}
19912020
}
19922021
}) {}
19932022
if tested == 0 {
1994-
self.fatal_proc_rec("No test has been found", &res);
1995-
} else if v.len() != 0 {
1996-
self.fatal_proc_rec(&format!("Not found test at line{} {:?}",
1997-
if v.len() > 1 { "s" } else { "" }, v),
1998-
&res);
2023+
self.fatal_proc_rec(&format!("No test has been found... {:?}", files), &res);
2024+
} else {
2025+
for (entry, v) in &files {
2026+
if v.len() != 0 {
2027+
self.fatal_proc_rec(&format!("Not found test at line{} \"{}\":{:?}",
2028+
if v.len() > 1 { "s" } else { "" }, entry, v),
2029+
&res);
2030+
}
2031+
}
19992032
}
20002033
}
20012034

0 commit comments

Comments
 (0)