Skip to content

Commit 433aae9

Browse files
committed
Auto merge of #69115 - ehuss:update-books, r=Dylan-DPC
Update books. This required some changes in how the books are tested due to some changes in rust-lang/book. It uses new syntax that is not compatible with bare `rustdoc --test`. This changes it so that it uses rustbook to run the tests, which is essentially the same as `mdbook test`. ## reference 7 commits in 11e893f..64239df 2020-01-18 21:24:08 +0100 to 2020-02-10 19:05:13 +0100 - Update for nested receivers. (rust-lang/reference#724) - clarify note re. leading `::` in 2018 (rust-lang/reference#752) - Update macro-ambiguity.md (rust-lang/reference#754) - typo fix: add missing `by` (rust-lang/reference#753) - fix `TypeParamBounds` link on trait objects (rust-lang/reference#749) - reorganize docs on references (rust-lang/reference#745) - add MacroRepOp usage for ? (rust-lang/reference#744) ## book 49 commits in 87dd6843678575f8dda962f239d14ef4be14b352..6fb3705e5230311b096d47f7e2c91f9ce24393d0 2020-01-20 15:20:40 -0500 to 2020-02-12 13:48:57 -0500 - Fix nomicon links. (rust-lang/book#2253) - Update to Rust 1.41.0 (rust-lang/book#2244) - Listing 19-6: use ptr.add instead of ptr.offset (rust-lang/book#2201) - Remove unneeded mutable reference - Clarify deref coercion explanation - Fix typo in link to 1.30 book - Acknowledge Murphy's Law - Clarify that buffer overread is UB in C - Change from "must" to "idiomatic" about comments - Fancy quotes - Make HashMap types match previous example; add fwd ref to ch 13 - Tweak wording to array clarification - Merge remote-tracking branch 'origin/pr/2236' - Update all our crates (rust-lang/book#2235) - Reword git caveat - Merge remote-tracking branch 'origin/pr/2234' - Merge remote-tracking branch 'origin/pr/2230' - println! is a macro (rust-lang/book#2224) - Update a translated version link (rust-lang/book#2221) - move `Macro invocation` from section on tuple to section on mac… (rust-lang/book#2206) - Do not limit `Self` usage in trait implementation (rust-lang/book#2197) - Merge remote-tracking branch 'origin/pr/2191' - Fix wrapping - Merge remote-tracking branch 'origin/pr/2187' - Updated appendix 07 to reflect deprecation of rustup install (rust-lang/book#2181) - Make links to the Nomicon consistent - Merge remote-tracking branch 'origin/pr/2180' - Merge remote-tracking branch 'origin/pr/2175' - Merge remote-tracking branch 'origin/pr/2171' - Merge remote-tracking branch 'origin/pr/2170' - Clarify and make consistent the explanation of unions - Merge remote-tracking branch 'origin/pr/2166' - Handle dev or test in the Finished output line - Link to macros by example rather than macros (rust-lang/book#2164) - Merge remote-tracking branch 'origin/pr/2147' - Fix parens (rust-lang/book#2132) - Clarify type inference with closures requires calling the closures - Update link to French translation (rust-lang/book#2119) - Merge remote-tracking branch 'origin/pr/2108' - Add an explicit cross reference to data type - Merge remote-tracking branch 'origin/pr/2105' - ch15-02-deref: Improve explanation on immut-to-mut (rust-lang/book#2030) - Remove unnecessary quotes - Make markdown link identifier match - Remove extra newline - Merge remote-tracking branch 'origin/pr/2004' - Extract code and output; script formatting and updating them (rust-lang/book#2231) - Switch "Finally" to "Next" to reflect new chapters having been… (rust-lang/book#2098) - ch19-06 added curly braces to macro output (rust-lang/book#2050) ## rust-by-example 2 commits in 1c2bd024d13f8011307e13386cf1fea2180352b5..32facd5522ddbbf37baf01e4e4b6562bc55c071a 2020-01-20 12:18:36 -0300 to 2020-02-11 09:25:06 -0300 - Add missing `dyn` in code sample (rust-lang/rust-by-example#1306) - Improve grammar in a few sections (rust-lang/rust-by-example#1305) ## edition-guide 1 commits in 1a2390247ad6d08160e0dd74f40a01a9578659c2..37f9e6848411188a1062ead1bd8ebe4b8aa16899 2019-12-29 10:40:55 -0800 to 2020-02-10 14:36:14 +0100 - Fixed typo (rust-lang/edition-guide#196) ## embedded-book 4 commits in 4d78994915af1bde9a95c04a8c27d8dca066232a..b2e1092bf67bd4d7686c4553f186edbb7f5f92db 2020-01-14 08:25:25 +0000 to 2020-01-30 08:45:46 +0000 - Make typestate initialization notes correct (rust-embedded/book#224) - Mention discovery book more prominently (rust-embedded/book#219) - Replace nursery links with rust-lang links (rust-embedded/book#222) - Add a Glossary appendix page (rust-embedded/book#223)
2 parents 35071e3 + 1e1b6ad commit 433aae9

File tree

9 files changed

+95
-36
lines changed

9 files changed

+95
-36
lines changed

src/bootstrap/test.rs

+58-24
Original file line numberDiff line numberDiff line change
@@ -1264,28 +1264,75 @@ impl Step for Compiletest {
12641264
}
12651265
}
12661266

1267-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1268-
struct DocTest {
1267+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1268+
struct BookTest {
12691269
compiler: Compiler,
1270-
path: &'static str,
1270+
path: PathBuf,
12711271
name: &'static str,
12721272
is_ext_doc: bool,
12731273
}
12741274

1275-
impl Step for DocTest {
1275+
impl Step for BookTest {
12761276
type Output = ();
12771277
const ONLY_HOSTS: bool = true;
12781278

12791279
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
12801280
run.never()
12811281
}
12821282

1283-
/// Runs `rustdoc --test` for all documentation in `src/doc`.
1283+
/// Runs the documentation tests for a book in `src/doc`.
12841284
///
1285-
/// This will run all tests in our markdown documentation (e.g., the book)
1286-
/// located in `src/doc`. The `rustdoc` that's run is the one that sits next to
1287-
/// `compiler`.
1285+
/// This uses the `rustdoc` that sits next to `compiler`.
12881286
fn run(self, builder: &Builder<'_>) {
1287+
// External docs are different from local because:
1288+
// - Some books need pre-processing by mdbook before being tested.
1289+
// - They need to save their state to toolstate.
1290+
// - They are only tested on the "checktools" builders.
1291+
//
1292+
// The local docs are tested by default, and we don't want to pay the
1293+
// cost of building mdbook, so they use `rustdoc --test` directly.
1294+
// Also, the unstable book is special because SUMMARY.md is generated,
1295+
// so it is easier to just run `rustdoc` on its files.
1296+
if self.is_ext_doc {
1297+
self.run_ext_doc(builder);
1298+
} else {
1299+
self.run_local_doc(builder);
1300+
}
1301+
}
1302+
}
1303+
1304+
impl BookTest {
1305+
/// This runs the equivalent of `mdbook test` (via the rustbook wrapper)
1306+
/// which in turn runs `rustdoc --test` on each file in the book.
1307+
fn run_ext_doc(self, builder: &Builder<'_>) {
1308+
let compiler = self.compiler;
1309+
1310+
builder.ensure(compile::Std { compiler, target: compiler.host });
1311+
1312+
// mdbook just executes a binary named "rustdoc", so we need to update
1313+
// PATH so that it points to our rustdoc.
1314+
let mut rustdoc_path = builder.rustdoc(compiler);
1315+
rustdoc_path.pop();
1316+
let old_path = env::var_os("PATH").unwrap_or_default();
1317+
let new_path = env::join_paths(iter::once(rustdoc_path).chain(env::split_paths(&old_path)))
1318+
.expect("could not add rustdoc to PATH");
1319+
1320+
let mut rustbook_cmd = builder.tool_cmd(Tool::Rustbook);
1321+
let path = builder.src.join(&self.path);
1322+
rustbook_cmd.env("PATH", new_path).arg("test").arg(path);
1323+
builder.add_rust_test_threads(&mut rustbook_cmd);
1324+
builder.info(&format!("Testing rustbook {}", self.path.display()));
1325+
let _time = util::timeit(&builder);
1326+
let toolstate = if try_run(builder, &mut rustbook_cmd) {
1327+
ToolState::TestPass
1328+
} else {
1329+
ToolState::TestFail
1330+
};
1331+
builder.save_toolstate(self.name, toolstate);
1332+
}
1333+
1334+
/// This runs `rustdoc --test` on all `.md` files in the path.
1335+
fn run_local_doc(self, builder: &Builder<'_>) {
12891336
let compiler = self.compiler;
12901337

12911338
builder.ensure(compile::Std { compiler, target: compiler.host });
@@ -1294,7 +1341,6 @@ impl Step for DocTest {
12941341
// tests for all files that end in `*.md`
12951342
let mut stack = vec![builder.src.join(self.path)];
12961343
let _time = util::timeit(&builder);
1297-
12981344
let mut files = Vec::new();
12991345
while let Some(p) = stack.pop() {
13001346
if p.is_dir() {
@@ -1306,25 +1352,13 @@ impl Step for DocTest {
13061352
continue;
13071353
}
13081354

1309-
// The nostarch directory in the book is for no starch, and so isn't
1310-
// guaranteed to builder. We don't care if it doesn't build, so skip it.
1311-
if p.to_str().map_or(false, |p| p.contains("nostarch")) {
1312-
continue;
1313-
}
1314-
13151355
files.push(p);
13161356
}
13171357

13181358
files.sort();
13191359

1320-
let mut toolstate = ToolState::TestPass;
13211360
for file in files {
1322-
if !markdown_test(builder, compiler, &file) {
1323-
toolstate = ToolState::TestFail;
1324-
}
1325-
}
1326-
if self.is_ext_doc {
1327-
builder.save_toolstate(self.name, toolstate);
1361+
markdown_test(builder, compiler, &file);
13281362
}
13291363
}
13301364
}
@@ -1353,9 +1387,9 @@ macro_rules! test_book {
13531387
}
13541388

13551389
fn run(self, builder: &Builder<'_>) {
1356-
builder.ensure(DocTest {
1390+
builder.ensure(BookTest {
13571391
compiler: self.compiler,
1358-
path: $path,
1392+
path: PathBuf::from($path),
13591393
name: $book_name,
13601394
is_ext_doc: !$default,
13611395
});

src/doc/book

Submodule book updated 1993 files

src/doc/rustdoc/book.toml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[book]
2+
authors = ["The Rust Project Developers"]
3+
src = "src"
4+
title = "The rustdoc book"

src/tools/rustbook/src/main.rs

+27-7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ fn main() {
2525
.arg_from_usage(d_message)
2626
.arg_from_usage(dir_message),
2727
)
28+
.subcommand(
29+
SubCommand::with_name("test")
30+
.about("Tests that a book's Rust code samples compile")
31+
.arg_from_usage(dir_message),
32+
)
2833
.subcommand(
2934
SubCommand::with_name("linkcheck")
3035
.about("Run linkcheck with mdBook 3")
@@ -36,13 +41,12 @@ fn main() {
3641
match matches.subcommand() {
3742
("build", Some(sub_matches)) => {
3843
if let Err(e) = build(sub_matches) {
39-
eprintln!("Error: {}", e);
40-
41-
for cause in e.iter().skip(1) {
42-
eprintln!("\tCaused By: {}", cause);
43-
}
44-
45-
::std::process::exit(101);
44+
handle_error(e);
45+
}
46+
}
47+
("test", Some(sub_matches)) => {
48+
if let Err(e) = test(sub_matches) {
49+
handle_error(e);
4650
}
4751
}
4852
("linkcheck", Some(sub_matches)) => {
@@ -148,6 +152,12 @@ pub fn build(args: &ArgMatches<'_>) -> Result3<()> {
148152
Ok(())
149153
}
150154

155+
fn test(args: &ArgMatches<'_>) -> Result3<()> {
156+
let book_dir = get_book_dir(args);
157+
let mut book = MDBook::load(&book_dir)?;
158+
book.test(vec![])
159+
}
160+
151161
fn get_book_dir(args: &ArgMatches<'_>) -> PathBuf {
152162
if let Some(dir) = args.value_of("dir") {
153163
// Check if path is relative from current dir, or absolute...
@@ -157,3 +167,13 @@ fn get_book_dir(args: &ArgMatches<'_>) -> PathBuf {
157167
env::current_dir().unwrap()
158168
}
159169
}
170+
171+
fn handle_error(error: mdbook::errors::Error) -> ! {
172+
eprintln!("Error: {}", error);
173+
174+
for cause in error.iter().skip(1) {
175+
eprintln!("\tCaused By: {}", cause);
176+
}
177+
178+
::std::process::exit(101);
179+
}

src/tools/tidy/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ fn filter_dirs(path: &Path) -> bool {
5858
"src/tools/rls",
5959
"src/tools/rust-installer",
6060
"src/tools/rustfmt",
61+
"src/doc/book",
6162
// Filter RLS output directories
6263
"target/rls",
6364
];

0 commit comments

Comments
 (0)