Skip to content

Commit cc80307

Browse files
Create new flag to test rustdoc --test
1 parent 66aa7bd commit cc80307

File tree

6 files changed

+55
-53
lines changed

6 files changed

+55
-53
lines changed

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
url = https://github.com/rust-lang/compiler-rt.git
88
[submodule "src/rt/hoedown"]
99
path = src/rt/hoedown
10-
url = https://github.com/GuillaumeGomez/hoedown.git
10+
url = https://github.com/rust-lang/hoedown.git
1111
[submodule "src/jemalloc"]
1212
path = src/jemalloc
1313
url = https://github.com/rust-lang/jemalloc.git

src/librustdoc/test.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,8 @@ impl Collector {
467467

468468
pub fn get_line(&self) -> usize {
469469
if let Some(ref codemap) = self.codemap{
470-
codemap.lookup_char_pos(BytePos(self.start_line as u32)).line - 1
470+
let line = codemap.lookup_char_pos(BytePos(self.start_line as u32)).line;
471+
if line > 0 { line - 1 } else { line }
471472
} else {
472473
self.start_line
473474
}

src/test/rustdoc/test_option_check/test.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// compile-flags:--test
12-
// check-stdout
11+
// compile-flags: --test
12+
// check-test-line-numbers-match
1313

1414
/// This is a Foo;
1515
///
1616
/// ```
1717
/// println!("baaaaaar");
1818
/// ```
19-
#[unstable]
2019
pub struct Foo;
2120

2221
/// This is a Bar;

src/tools/compiletest/src/header.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ pub struct TestProps {
224224
pub incremental_dir: Option<PathBuf>,
225225
// Specifies that a cfail test must actually compile without errors.
226226
pub must_compile_successfully: bool,
227+
// rustdoc will test the output of the `--test` option
228+
pub check_test_line_numbers_match: bool,
227229
}
228230

229231
impl TestProps {
@@ -248,6 +250,7 @@ impl TestProps {
248250
forbid_output: vec![],
249251
incremental_dir: None,
250252
must_compile_successfully: false,
253+
check_test_line_numbers_match: false,
251254
}
252255
}
253256

@@ -347,6 +350,10 @@ impl TestProps {
347350
if !self.must_compile_successfully {
348351
self.must_compile_successfully = parse_must_compile_successfully(ln);
349352
}
353+
354+
if !self.check_test_line_numbers_match {
355+
self.check_test_line_numbers_match = parse_check_test_line_numbers_match(ln);
356+
}
350357
});
351358

352359
for key in vec!["RUST_TEST_NOCAPTURE", "RUST_TEST_THREADS"] {
@@ -458,6 +465,10 @@ fn parse_must_compile_successfully(line: &str) -> bool {
458465
parse_name_directive(line, "must-compile-successfully")
459466
}
460467

468+
fn parse_check_test_line_numbers_match(line: &str) -> bool {
469+
parse_name_directive(line, "check-test-line-numbers-match")
470+
}
471+
461472
fn parse_env(line: &str, name: &str) -> Option<(String, String)> {
462473
parse_name_value_directive(line, name).map(|nv| {
463474
// nv is either FOO or FOO=BAR

src/tools/compiletest/src/runtest.rs

Lines changed: 38 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1879,20 +1879,19 @@ actual:\n\
18791879
fn run_rustdoc_test(&self) {
18801880
assert!(self.revision.is_none(), "revisions not relevant here");
18811881

1882-
if self.props.compile_flags.contains(&"--test".to_owned()) &&
1883-
self.props.check_stdout == true {
1884-
self.check_rustdoc_test_option();
1885-
} else {
1886-
let out_dir = self.output_base_name();
1887-
let _ = fs::remove_dir_all(&out_dir);
1888-
self.create_dir_racy(&out_dir);
1882+
let out_dir = self.output_base_name();
1883+
let _ = fs::remove_dir_all(&out_dir);
1884+
self.create_dir_racy(&out_dir);
18891885

1890-
let proc_res = self.document(&out_dir);
1891-
if !proc_res.status.success() {
1892-
self.fatal_proc_rec("rustdoc failed!", &proc_res);
1893-
}
1894-
let root = self.find_rust_src_root().unwrap();
1886+
let proc_res = self.document(&out_dir);
1887+
if !proc_res.status.success() {
1888+
self.fatal_proc_rec("rustdoc failed!", &proc_res);
1889+
}
18951890

1891+
if self.props.check_test_line_numbers_match == true {
1892+
self.check_rustdoc_test_option(proc_res);
1893+
} else {
1894+
let root = self.find_rust_src_root().unwrap();
18961895
let res = self.cmd2procres(Command::new(&self.config.docck_python)
18971896
.arg(root.join("src/etc/htmldocck.py"))
18981897
.arg(out_dir)
@@ -1903,21 +1902,20 @@ actual:\n\
19031902
}
19041903
}
19051904

1906-
fn check_rustdoc_test_option(&self) {
1905+
fn check_rustdoc_test_option(&self, res: ProcRes) {
19071906
let mut file = fs::File::open(&self.testpaths.file)
19081907
.expect("markdown_test_output_check_entry File::open failed");
19091908
let mut content = String::new();
19101909
file.read_to_string(&mut content)
19111910
.expect("markdown_test_output_check_entry read_to_string failed");
19121911
let mut ignore = false;
19131912
let mut v: Vec<usize> =
1914-
content.split("\n")
1913+
content.lines()
19151914
.enumerate()
19161915
.filter_map(|(line_nb, line)| {
19171916
let sline = line.split("///").last().unwrap_or("");
19181917
let line = sline.trim_left();
1919-
if line.starts_with("```") &&
1920-
!line.contains("ignore") {
1918+
if line.starts_with("```") {
19211919
if ignore {
19221920
ignore = false;
19231921
None
@@ -1931,37 +1929,30 @@ actual:\n\
19311929
})
19321930
.collect();
19331931

1934-
let args = ProcArgs {
1935-
prog: self.config.rustdoc_path.to_str().unwrap().to_owned(),
1936-
args: vec!["--test".to_owned(), self.testpaths.file.to_str().unwrap().to_owned()],
1937-
};
1938-
let env = self.props.exec_env.clone();
1939-
let res = self.compose_and_run(args,
1940-
env,
1941-
self.config.run_lib_path.to_str().unwrap(),
1942-
None,
1943-
None);
1944-
1945-
res.stdout.split("\n")
1946-
.filter(|s| s.starts_with("test "))
1947-
.inspect(|s| {
1948-
let tmp: Vec<&str> = s.split(" - line ").collect();
1949-
if tmp.len() == 2 {
1950-
let line = usize::from_str_radix(tmp[1].split(" ...")
1951-
.next()
1952-
.unwrap_or("0"), 10)
1953-
.unwrap_or(0);
1954-
if let Ok(pos) = v.binary_search(&line) {
1955-
v.remove(pos);
1956-
} else {
1957-
self.fatal_proc_rec(&format!("Not found doc test: \"{}\" in {:?}",
1958-
s, v),
1959-
&res);
1960-
}
1961-
}
1962-
})
1963-
.all(|_| true);
1964-
if v.len() != 0 {
1932+
let mut tested = 0;
1933+
for _ in res.stdout.split("\n")
1934+
.filter(|s| s.starts_with("test "))
1935+
.inspect(|s| {
1936+
let tmp: Vec<&str> = s.split(" - line ").collect();
1937+
if tmp.len() == 2 {
1938+
tested += 1;
1939+
let line = tmp[1].split(" ...")
1940+
.next()
1941+
.unwrap_or("0")
1942+
.parse()
1943+
.unwrap_or(0);
1944+
if let Ok(pos) = v.binary_search(&line) {
1945+
v.remove(pos);
1946+
} else {
1947+
self.fatal_proc_rec(
1948+
&format!("Not found doc test: \"{}\" in {:?}", s, v),
1949+
&res);
1950+
}
1951+
}
1952+
}) {}
1953+
if tested == 0 {
1954+
self.fatal_proc_rec("No test has been found", &res);
1955+
} else if v.len() != 0 {
19651956
self.fatal_proc_rec(&format!("Not found test at line{} {:?}",
19661957
if v.len() > 1 { "s" } else { "" }, v),
19671958
&res);

0 commit comments

Comments
 (0)