Skip to content

Commit 794434e

Browse files
committed
initial implementation of rustdoc nested aux-build
1 parent 56c698c commit 794434e

File tree

31 files changed

+369
-34
lines changed

31 files changed

+369
-34
lines changed

src/tools/compiletest/src/command-list.rs

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const KNOWN_DIRECTIVE_NAMES: &[&str] = &[
99
"aux-codegen-backend",
1010
"aux-crate",
1111
"build-aux-docs",
12+
"unique-doc-out-dir",
1213
"build-fail",
1314
"build-pass",
1415
"check-fail",
@@ -18,6 +19,7 @@ const KNOWN_DIRECTIVE_NAMES: &[&str] = &[
1819
"check-test-line-numbers-match",
1920
"compare-output-lines-by-subset",
2021
"compile-flags",
22+
"doc-flags",
2123
"dont-check-compiler-stderr",
2224
"dont-check-compiler-stdout",
2325
"dont-check-failure-status",

src/tools/compiletest/src/header.rs

+13
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ pub struct TestProps {
9595
pub compile_flags: Vec<String>,
9696
// Extra flags to pass when the compiled code is run (such as --bench)
9797
pub run_flags: Vec<String>,
98+
/// Extra flags to pass to rustdoc but not the compiler.
99+
pub doc_flags: Vec<String>,
98100
// If present, the name of a file that this test should match when
99101
// pretty-printed
100102
pub pp_exact: Option<PathBuf>,
@@ -122,6 +124,9 @@ pub struct TestProps {
122124
pub unset_exec_env: Vec<String>,
123125
// Build documentation for all specified aux-builds as well
124126
pub build_aux_docs: bool,
127+
/// Build the documentation for each crate in a unique output directory.
128+
/// Uses <root output directory>/docs/<test name>/doc
129+
pub unique_doc_out_dir: bool,
125130
// Flag to force a crate to be built with the host architecture
126131
pub force_host: bool,
127132
// Check stdout for error-pattern output as well as stderr
@@ -220,8 +225,10 @@ mod directives {
220225
pub const REGEX_ERROR_PATTERN: &'static str = "regex-error-pattern";
221226
pub const COMPILE_FLAGS: &'static str = "compile-flags";
222227
pub const RUN_FLAGS: &'static str = "run-flags";
228+
pub const DOC_FLAGS: &'static str = "doc-flags";
223229
pub const SHOULD_ICE: &'static str = "should-ice";
224230
pub const BUILD_AUX_DOCS: &'static str = "build-aux-docs";
231+
pub const UNIQUE_DOC_OUT_DIR: &'static str = "unique-doc-out-dir";
225232
pub const FORCE_HOST: &'static str = "force-host";
226233
pub const CHECK_STDOUT: &'static str = "check-stdout";
227234
pub const CHECK_RUN_RESULTS: &'static str = "check-run-results";
@@ -267,6 +274,7 @@ impl TestProps {
267274
regex_error_patterns: vec![],
268275
compile_flags: vec![],
269276
run_flags: vec![],
277+
doc_flags: vec![],
270278
pp_exact: None,
271279
aux_builds: vec![],
272280
aux_bins: vec![],
@@ -281,6 +289,7 @@ impl TestProps {
281289
exec_env: vec![],
282290
unset_exec_env: vec![],
283291
build_aux_docs: false,
292+
unique_doc_out_dir: false,
284293
force_host: false,
285294
check_stdout: false,
286295
check_run_results: false,
@@ -377,6 +386,8 @@ impl TestProps {
377386
|r| r,
378387
);
379388

389+
config.push_name_value_directive(ln, DOC_FLAGS, &mut self.doc_flags, |r| r);
390+
380391
fn split_flags(flags: &str) -> Vec<String> {
381392
// Individual flags can be single-quoted to preserve spaces; see
382393
// <https://github.com/rust-lang/rust/pull/115948/commits/957c5db6>.
@@ -414,6 +425,8 @@ impl TestProps {
414425

415426
config.set_name_directive(ln, SHOULD_ICE, &mut self.should_ice);
416427
config.set_name_directive(ln, BUILD_AUX_DOCS, &mut self.build_aux_docs);
428+
config.set_name_directive(ln, UNIQUE_DOC_OUT_DIR, &mut self.unique_doc_out_dir);
429+
417430
config.set_name_directive(ln, FORCE_HOST, &mut self.force_host);
418431
config.set_name_directive(ln, CHECK_STDOUT, &mut self.check_stdout);
419432
config.set_name_directive(ln, CHECK_RUN_RESULTS, &mut self.check_run_results);

src/tools/compiletest/src/runtest.rs

+71-33
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
// ignore-tidy-filelength
22

3+
use crate::compute_diff::{write_diff, write_filtered_diff};
4+
use crate::errors::{self, Error, ErrorKind};
5+
use crate::header::TestProps;
6+
use crate::json;
7+
use crate::read2::{read2_abbreviated, Truncated};
8+
use crate::util::{add_dylib_path, copy_dir_all, dylib_env_var, logv, static_regex, PathBufExt};
9+
use crate::ColorConfig;
10+
use colored::Colorize;
11+
use miropt_test_tools::{files_for_miropt_test, MiroptTest, MiroptTestFile};
12+
use regex::{Captures, Regex};
13+
use rustfix::{apply_suggestions, get_suggestions_from_json, Filter};
14+
use std::borrow::Cow;
315
use std::collections::{HashMap, HashSet};
416
use std::ffi::{OsStr, OsString};
517
use std::fs::{self, create_dir_all, File, OpenOptions};
@@ -723,7 +735,7 @@ impl<'test> TestCx<'test> {
723735
self.maybe_add_external_args(&mut rustc, &self.config.target_rustcflags);
724736
rustc.args(&self.props.compile_flags);
725737

726-
self.compose_and_run_compiler(rustc, Some(src))
738+
self.compose_and_run_compiler(rustc, Some(src), self.testpaths)
727739
}
728740

729741
fn run_debuginfo_test(&self) {
@@ -1579,13 +1591,15 @@ impl<'test> TestCx<'test> {
15791591
passes,
15801592
);
15811593

1582-
self.compose_and_run_compiler(rustc, None)
1594+
self.compose_and_run_compiler(rustc, None, self.testpaths)
15831595
}
15841596

1585-
fn document(&self, out_dir: &Path) -> ProcRes {
1597+
/// `root_out_dir` and `root_testpaths` refer to the parameters of the actual test being run.
1598+
/// Auxiliaries, no matter how deep, have the same root_out_dir and root_testpaths.
1599+
fn document(&self, root_out_dir: &Path, root_testpaths: &TestPaths) -> ProcRes {
15861600
if self.props.build_aux_docs {
15871601
for rel_ab in &self.props.aux_builds {
1588-
let aux_testpaths = self.compute_aux_test_paths(&self.testpaths, rel_ab);
1602+
let aux_testpaths = self.compute_aux_test_paths(root_testpaths, rel_ab);
15891603
let aux_props =
15901604
self.props.from_aux_file(&aux_testpaths.file, self.revision, self.config);
15911605
let aux_cx = TestCx {
@@ -1596,7 +1610,7 @@ impl<'test> TestCx<'test> {
15961610
};
15971611
// Create the directory for the stdout/stderr files.
15981612
create_dir_all(aux_cx.output_base_dir()).unwrap();
1599-
let auxres = aux_cx.document(out_dir);
1613+
let auxres = aux_cx.document(&root_out_dir, root_testpaths);
16001614
if !auxres.status.success() {
16011615
return auxres;
16021616
}
@@ -1606,21 +1620,47 @@ impl<'test> TestCx<'test> {
16061620
let aux_dir = self.aux_output_dir_name();
16071621

16081622
let rustdoc_path = self.config.rustdoc_path.as_ref().expect("--rustdoc-path not passed");
1609-
let mut rustdoc = Command::new(rustdoc_path);
16101623

1624+
// actual --out-dir given to the auxiliary or test, as opposed to the root out dir for the entire
1625+
// test
1626+
let out_dir: Cow<'_, Path> = if self.props.unique_doc_out_dir {
1627+
let file_name = self
1628+
.testpaths
1629+
.file
1630+
.file_name()
1631+
.expect("file name should not be empty")
1632+
.to_str()
1633+
.expect("file name utf8")
1634+
.trim_end_matches(".rs");
1635+
let out_dir = PathBuf::from_iter([
1636+
root_out_dir,
1637+
Path::new("docs"),
1638+
Path::new(file_name),
1639+
Path::new("doc"),
1640+
]);
1641+
create_dir_all(&out_dir).unwrap();
1642+
Cow::Owned(out_dir)
1643+
} else {
1644+
Cow::Borrowed(root_out_dir)
1645+
};
1646+
1647+
let mut rustdoc = Command::new(rustdoc_path);
1648+
let current_dir = output_base_dir(self.config, root_testpaths, self.safe_revision());
1649+
rustdoc.current_dir(current_dir);
16111650
rustdoc
16121651
.arg("-L")
16131652
.arg(self.config.run_lib_path.to_str().unwrap())
16141653
.arg("-L")
16151654
.arg(aux_dir)
16161655
.arg("-o")
1617-
.arg(out_dir)
1656+
.arg(out_dir.as_ref())
16181657
.arg("--deny")
16191658
.arg("warnings")
16201659
.arg(&self.testpaths.file)
16211660
.arg("-A")
16221661
.arg("internal_features")
1623-
.args(&self.props.compile_flags);
1662+
.args(&self.props.compile_flags)
1663+
.args(&self.props.doc_flags);
16241664

16251665
if self.config.mode == RustdocJson {
16261666
rustdoc.arg("--output-format").arg("json").arg("-Zunstable-options");
@@ -1630,7 +1670,7 @@ impl<'test> TestCx<'test> {
16301670
rustdoc.arg(format!("-Clinker={}", linker));
16311671
}
16321672

1633-
self.compose_and_run_compiler(rustdoc, None)
1673+
self.compose_and_run_compiler(rustdoc, None, root_testpaths)
16341674
}
16351675

16361676
fn exec_compiled_test(&self) -> ProcRes {
@@ -1828,9 +1868,16 @@ impl<'test> TestCx<'test> {
18281868
}
18291869
}
18301870

1831-
fn compose_and_run_compiler(&self, mut rustc: Command, input: Option<String>) -> ProcRes {
1871+
/// `root_testpaths` refers to the path of the original test.
1872+
/// the auxiliary and the test with an aux-build have the same `root_testpaths`.
1873+
fn compose_and_run_compiler(
1874+
&self,
1875+
mut rustc: Command,
1876+
input: Option<String>,
1877+
root_testpaths: &TestPaths,
1878+
) -> ProcRes {
18321879
let aux_dir = self.aux_output_dir();
1833-
self.build_all_auxiliary(&self.testpaths, &aux_dir, &mut rustc);
1880+
self.build_all_auxiliary(root_testpaths, &aux_dir, &mut rustc);
18341881

18351882
rustc.envs(self.props.rustc_env.clone());
18361883
self.props.unset_rustc_env.iter().fold(&mut rustc, Command::env_remove);
@@ -2545,7 +2592,7 @@ impl<'test> TestCx<'test> {
25452592
Vec::new(),
25462593
);
25472594

2548-
let proc_res = self.compose_and_run_compiler(rustc, None);
2595+
let proc_res = self.compose_and_run_compiler(rustc, None, self.testpaths);
25492596
let output_path = self.get_filecheck_file("ll");
25502597
(proc_res, output_path)
25512598
}
@@ -2581,7 +2628,7 @@ impl<'test> TestCx<'test> {
25812628
Vec::new(),
25822629
);
25832630

2584-
let proc_res = self.compose_and_run_compiler(rustc, None);
2631+
let proc_res = self.compose_and_run_compiler(rustc, None, self.testpaths);
25852632
let output_path = self.get_filecheck_file("s");
25862633
(proc_res, output_path)
25872634
}
@@ -2664,7 +2711,7 @@ impl<'test> TestCx<'test> {
26642711
let out_dir = self.output_base_dir();
26652712
remove_and_create_dir_all(&out_dir);
26662713

2667-
let proc_res = self.document(&out_dir);
2714+
let proc_res = self.document(&out_dir, &self.testpaths);
26682715
if !proc_res.status.success() {
26692716
self.fatal_proc_rec("rustdoc failed!", &proc_res);
26702717
}
@@ -2723,7 +2770,7 @@ impl<'test> TestCx<'test> {
27232770
let aux_dir = new_rustdoc.aux_output_dir();
27242771
new_rustdoc.build_all_auxiliary(&new_rustdoc.testpaths, &aux_dir, &mut rustc);
27252772

2726-
let proc_res = new_rustdoc.document(&compare_dir);
2773+
let proc_res = new_rustdoc.document(&compare_dir, &new_rustdoc.testpaths);
27272774
if !proc_res.status.success() {
27282775
eprintln!("failed to run nightly rustdoc");
27292776
return;
@@ -2846,7 +2893,7 @@ impl<'test> TestCx<'test> {
28462893
let out_dir = self.output_base_dir();
28472894
remove_and_create_dir_all(&out_dir);
28482895

2849-
let proc_res = self.document(&out_dir);
2896+
let proc_res = self.document(&out_dir, &self.testpaths);
28502897
if !proc_res.status.success() {
28512898
self.fatal_proc_rec("rustdoc failed!", &proc_res);
28522899
}
@@ -2922,24 +2969,15 @@ impl<'test> TestCx<'test> {
29222969
fn check_rustdoc_test_option(&self, res: ProcRes) {
29232970
let mut other_files = Vec::new();
29242971
let mut files: HashMap<String, Vec<usize>> = HashMap::new();
2925-
let cwd = env::current_dir().unwrap();
2926-
files.insert(
2927-
self.testpaths
2928-
.file
2929-
.strip_prefix(&cwd)
2930-
.unwrap_or(&self.testpaths.file)
2931-
.to_str()
2932-
.unwrap()
2933-
.replace('\\', "/"),
2934-
self.get_lines(&self.testpaths.file, Some(&mut other_files)),
2935-
);
2972+
let normalized = fs::canonicalize(&self.testpaths.file).expect("failed to canonicalize");
2973+
let normalized = normalized.to_str().unwrap().replace('\\', "/");
2974+
files.insert(normalized, self.get_lines(&self.testpaths.file, Some(&mut other_files)));
29362975
for other_file in other_files {
29372976
let mut path = self.testpaths.file.clone();
29382977
path.set_file_name(&format!("{}.rs", other_file));
2939-
files.insert(
2940-
path.strip_prefix(&cwd).unwrap_or(&path).to_str().unwrap().replace('\\', "/"),
2941-
self.get_lines(&path, None),
2942-
);
2978+
let path = fs::canonicalize(path).expect("failed to canonicalize");
2979+
let normalized = path.to_str().unwrap().replace('\\', "/");
2980+
files.insert(normalized, self.get_lines(&path, None));
29432981
}
29442982

29452983
let mut tested = 0;
@@ -3778,7 +3816,7 @@ impl<'test> TestCx<'test> {
37783816
if let Some(nodejs) = &self.config.nodejs {
37793817
let out_dir = self.output_base_dir();
37803818

3781-
self.document(&out_dir);
3819+
self.document(&out_dir, &self.testpaths);
37823820

37833821
let root = self.config.find_rust_src_root().unwrap();
37843822
let file_stem =
@@ -4094,7 +4132,7 @@ impl<'test> TestCx<'test> {
40944132
rustc.arg(crate_name);
40954133
}
40964134

4097-
let res = self.compose_and_run_compiler(rustc, None);
4135+
let res = self.compose_and_run_compiler(rustc, None, self.testpaths);
40984136
if !res.status.success() {
40994137
self.fatal_proc_rec("failed to compile fixed code", &res);
41004138
}

src/tools/compiletest/src/runtest/coverage.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ impl<'test> TestCx<'test> {
191191

192192
rustdoc_cmd.arg(&self.testpaths.file);
193193

194-
let proc_res = self.compose_and_run_compiler(rustdoc_cmd, None);
194+
let proc_res = self.compose_and_run_compiler(rustdoc_cmd, None, self.testpaths);
195195
if !proc_res.status.success() {
196196
self.fatal_proc_rec("rustdoc --test failed!", &proc_res)
197197
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//@ build-aux-docs
2+
3+
4+
pub struct Quebec;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//@ aux-build:q.rs
2+
//@ build-aux-docs
3+
4+
5+
extern crate q;
6+
pub trait Tango {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//@ aux-build:t.rs
2+
//@ build-aux-docs
3+
4+
//@ has t/trait.Tango.html
5+
//@ hasraw search-index.js 'Quebec'
6+
//@ hasraw trait.impl/t/trait.Tango.js 'struct.Sierra.html'
7+
//@ hasraw s/struct.Sierra.html 'Tango'
8+
//@ hasraw search-index.js 'Sierra'
9+
//@ hasraw search-index.js 'Tango'
10+
//@ has q/struct.Quebec.html
11+
//@ has s/struct.Sierra.html
12+
13+
// We document multiple crates into the same output directory, which merges the cross-crate information. Everything is available.
14+
15+
extern crate t;
16+
pub struct Sierra;
17+
impl t::Tango for Sierra {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//@ build-aux-docs
2+
//@ doc-flags:--enable-index-page
3+
//@ doc-flags:-Zunstable-options
4+
5+
6+
pub struct Quebec;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//@ aux-build:q.rs
2+
//@ build-aux-docs
3+
//@ doc-flags:--enable-index-page
4+
//@ doc-flags:-Zunstable-options
5+
6+
7+
extern crate q;
8+
pub trait Tango {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//@ aux-build:t.rs
2+
//@ build-aux-docs
3+
//@ doc-flags:--enable-index-page
4+
//@ doc-flags:-Zunstable-options
5+
6+
//@ has t/trait.Tango.html
7+
//@ hasraw search-index.js 'Quebec'
8+
//@ has index.html '//ul[@class="all-items"]//a[@href="q/index.html"]' 'q'
9+
//@ hasraw s/struct.Sierra.html 'Tango'
10+
//@ hasraw trait.impl/t/trait.Tango.js 'struct.Sierra.html'
11+
//@ has index.html '//ul[@class="all-items"]//a[@href="t/index.html"]' 't'
12+
//@ has index.html '//h1' 'List of all crates'
13+
//@ has index.html '//ul[@class="all-items"]//a[@href="s/index.html"]' 's'
14+
//@ hasraw search-index.js 'Sierra'
15+
//@ hasraw search-index.js 'Tango'
16+
//@ has index.html
17+
//@ has q/struct.Quebec.html
18+
//@ has s/struct.Sierra.html
19+
20+
// We document multiple crates into the same output directory, which merges the cross-crate information. Everything is available.
21+
22+
extern crate t;
23+
pub struct Sierra;
24+
impl t::Tango for Sierra {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//@ build-aux-docs
2+
3+
4+
pub trait Foxtrot {}

0 commit comments

Comments
 (0)