From f0189cd5a006aed0146870f74207ad9b3995c20d Mon Sep 17 00:00:00 2001 From: Chris Lovett Date: Wed, 9 Feb 2022 13:45:39 -0800 Subject: [PATCH 01/17] add a --chapter option to mdbook test. --- src/book/mod.rs | 12 +++++++++--- src/cmd/test.rs | 11 ++++++++++- src/theme/index.hbs | 4 ++-- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/book/mod.rs b/src/book/mod.rs index 3370d92c01..d728e1b81c 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -244,8 +244,9 @@ impl MDBook { self } - /// Run `rustdoc` tests on the book, linking against the provided libraries. - pub fn test(&mut self, library_paths: Vec<&str>) -> Result<()> { + /// Run `rustdoc` tests on the book, linking against the provided libraries + /// and optionally testing only a signal named chapter while skipping the others. + pub fn test(&mut self, library_paths: Vec<&str>, chapter: &str) -> Result<()> { let library_args: Vec<&str> = (0..library_paths.len()) .map(|_| "-L") .zip(library_paths.into_iter()) @@ -270,8 +271,13 @@ impl MDBook { _ => continue, }; + if !chapter.is_empty() && ch.name != chapter { + info!("Skipping chapter '{}'...", ch.name); + continue; + }; + let path = self.source_dir().join(&chapter_path); - info!("Testing file: {:?}", path); + info!("Testing chapter '{}': {:?}", ch.name, path); // write preprocessed file to tempdir let path = temp_dir.path().join(&chapter_path); diff --git a/src/cmd/test.rs b/src/cmd/test.rs index f6d97aa656..f7d4aebeb6 100644 --- a/src/cmd/test.rs +++ b/src/cmd/test.rs @@ -11,7 +11,11 @@ pub fn make_subcommand<'a, 'b>() -> App<'a, 'b> { "-d, --dest-dir=[dest-dir] 'Output directory for the book{n}\ Relative paths are interpreted relative to the book's root directory.{n}\ If omitted, mdBook uses build.build-dir from book.toml or defaults to `./book`.'", + ).arg_from_usage( + "-c, --chapter=[name] 'Only test the specified chapter{n}\ + Where the name of the chapter is defined in the SUMMARY.md file.'" ) + .arg_from_usage( "[dir] 'Root directory for the book{n}\ (Defaults to the Current Directory when omitted)'", @@ -39,8 +43,13 @@ pub fn execute(args: &ArgMatches) -> Result<()> { if let Some(dest_dir) = args.value_of("dest-dir") { book.config.build.build_dir = dest_dir.into(); } + let chapter: &str = if args.is_present("chapter") { + args.value_of("chapter").unwrap_or_default() + } else { + "" + }; - book.test(library_paths)?; + book.test(library_paths, chapter)?; Ok(()) } diff --git a/src/theme/index.hbs b/src/theme/index.hbs index 966eedbcef..89ec0509ac 100644 --- a/src/theme/index.hbs +++ b/src/theme/index.hbs @@ -67,11 +67,11 @@ var theme = localStorage.getItem('mdbook-theme'); var sidebar = localStorage.getItem('mdbook-sidebar'); - if (theme.startsWith('"') && theme.endsWith('"')) { + if (theme && theme.startsWith('"') && theme.endsWith('"')) { localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1)); } - if (sidebar.startsWith('"') && sidebar.endsWith('"')) { + if (sidebar && sidebar.startsWith('"') && sidebar.endsWith('"')) { localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1)); } } catch (e) { } From 4d781195e48ce2753331f3a6b1e9923d7caf49a2 Mon Sep 17 00:00:00 2001 From: Chris Lovett Date: Wed, 9 Feb 2022 14:31:57 -0800 Subject: [PATCH 02/17] fix unit tests --- tests/testing.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/testing.rs b/tests/testing.rs index 2b2c0fd0d7..0d90e842bd 100644 --- a/tests/testing.rs +++ b/tests/testing.rs @@ -9,7 +9,7 @@ fn mdbook_can_correctly_test_a_passing_book() { let temp = DummyBook::new().with_passing_test(true).build().unwrap(); let mut md = MDBook::load(temp.path()).unwrap(); - let result = md.test(vec![]); + let result = md.test(vec![], ""); assert!( result.is_ok(), "Tests failed with {}", @@ -22,5 +22,5 @@ fn mdbook_detects_book_with_failing_tests() { let temp = DummyBook::new().with_passing_test(false).build().unwrap(); let mut md = MDBook::load(temp.path()).unwrap(); - assert!(md.test(vec![]).is_err()); + assert!(md.test(vec![], "").is_err()); } From bf6d452a7bcce03784e070e5591ba22e4bfc95c6 Mon Sep 17 00:00:00 2001 From: Chris Lovett Date: Wed, 9 Feb 2022 15:17:17 -0800 Subject: [PATCH 03/17] fix expected output --- tests/cli/test.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/cli/test.rs b/tests/cli/test.rs index d974654d99..a642cbe56f 100644 --- a/tests/cli/test.rs +++ b/tests/cli/test.rs @@ -10,10 +10,10 @@ fn mdbook_cli_can_correctly_test_a_passing_book() { let mut cmd = Command::cargo_bin("mdbook").unwrap(); cmd.arg("test").current_dir(temp.path()); cmd.assert().success() - .stderr(predicates::str::is_match(r##"Testing file: "([^"]+)[\\/]README.md""##).unwrap()) - .stderr(predicates::str::is_match(r##"Testing file: "([^"]+)[\\/]intro.md""##).unwrap()) - .stderr(predicates::str::is_match(r##"Testing file: "([^"]+)[\\/]first[\\/]index.md""##).unwrap()) - .stderr(predicates::str::is_match(r##"Testing file: "([^"]+)[\\/]first[\\/]nested.md""##).unwrap()) + .stderr(predicates::str::is_match(r##"Testing chapter [^:]*: "([^"]+)[\\/]README.md""##).unwrap()) + .stderr(predicates::str::is_match(r##"Testing chapter [^:]*: "([^"]+)[\\/]intro.md""##).unwrap()) + .stderr(predicates::str::is_match(r##"Testing chapter [^:]*: "([^"]+)[\\/]first[\\/]index.md""##).unwrap()) + .stderr(predicates::str::is_match(r##"Testing chapter [^:]*: "([^"]+)[\\/]first[\\/]nested.md""##).unwrap()) .stderr(predicates::str::is_match(r##"rustdoc returned an error:\n\n"##).unwrap().not()) .stderr(predicates::str::is_match(r##"Nested_Chapter::Rustdoc_include_works_with_anchors_too \(line \d+\) ... FAILED"##).unwrap().not()); } @@ -25,10 +25,10 @@ fn mdbook_cli_detects_book_with_failing_tests() { let mut cmd = Command::cargo_bin("mdbook").unwrap(); cmd.arg("test").current_dir(temp.path()); cmd.assert().failure() - .stderr(predicates::str::is_match(r##"Testing file: "([^"]+)[\\/]README.md""##).unwrap()) - .stderr(predicates::str::is_match(r##"Testing file: "([^"]+)[\\/]intro.md""##).unwrap()) - .stderr(predicates::str::is_match(r##"Testing file: "([^"]+)[\\/]first[\\/]index.md""##).unwrap()) - .stderr(predicates::str::is_match(r##"Testing file: "([^"]+)[\\/]first[\\/]nested.md""##).unwrap()) + .stderr(predicates::str::is_match(r##"Testing chapter [^:]*: "([^"]+)[\\/]README.md""##).unwrap()) + .stderr(predicates::str::is_match(r##"Testing chapter [^:]*: "([^"]+)[\\/]intro.md""##).unwrap()) + .stderr(predicates::str::is_match(r##"Testing chapter [^:]*: "([^"]+)[\\/]first[\\/]index.md""##).unwrap()) + .stderr(predicates::str::is_match(r##"Testing chapter [^:]*: "([^"]+)[\\/]first[\\/]nested.md""##).unwrap()) .stderr(predicates::str::is_match(r##"rustdoc returned an error:\n\n"##).unwrap()) .stderr(predicates::str::is_match(r##"Nested_Chapter::Rustdoc_include_works_with_anchors_too \(line \d+\) ... FAILED"##).unwrap()); } From 71e490d156732a033bcd6ee36237a52b52a30c79 Mon Sep 17 00:00:00 2001 From: Chris Lovett Date: Wed, 9 Feb 2022 16:06:14 -0800 Subject: [PATCH 04/17] move command line parsing code and make test output comparison agnostic to the name of the rustdoc tool. --- src/cmd/test.rs | 12 ++++++------ tests/cli/test.rs | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/cmd/test.rs b/src/cmd/test.rs index f7d4aebeb6..f447fb1aeb 100644 --- a/src/cmd/test.rs +++ b/src/cmd/test.rs @@ -37,18 +37,18 @@ pub fn execute(args: &ArgMatches) -> Result<()> { .values_of("library-path") .map(std::iter::Iterator::collect) .unwrap_or_default(); - let book_dir = get_book_dir(args); - let mut book = MDBook::load(&book_dir)?; - - if let Some(dest_dir) = args.value_of("dest-dir") { - book.config.build.build_dir = dest_dir.into(); - } let chapter: &str = if args.is_present("chapter") { args.value_of("chapter").unwrap_or_default() } else { "" }; + let book_dir = get_book_dir(args); + let mut book = MDBook::load(&book_dir)?; + + if let Some(dest_dir) = args.value_of("dest-dir") { + book.config.build.build_dir = dest_dir.into(); + } book.test(library_paths, chapter)?; Ok(()) diff --git a/tests/cli/test.rs b/tests/cli/test.rs index a642cbe56f..29c00bd672 100644 --- a/tests/cli/test.rs +++ b/tests/cli/test.rs @@ -14,7 +14,7 @@ fn mdbook_cli_can_correctly_test_a_passing_book() { .stderr(predicates::str::is_match(r##"Testing chapter [^:]*: "([^"]+)[\\/]intro.md""##).unwrap()) .stderr(predicates::str::is_match(r##"Testing chapter [^:]*: "([^"]+)[\\/]first[\\/]index.md""##).unwrap()) .stderr(predicates::str::is_match(r##"Testing chapter [^:]*: "([^"]+)[\\/]first[\\/]nested.md""##).unwrap()) - .stderr(predicates::str::is_match(r##"rustdoc returned an error:\n\n"##).unwrap().not()) + .stderr(predicates::str::is_match(r##"returned an error:\n\n"##).unwrap().not()) .stderr(predicates::str::is_match(r##"Nested_Chapter::Rustdoc_include_works_with_anchors_too \(line \d+\) ... FAILED"##).unwrap().not()); } @@ -29,6 +29,6 @@ fn mdbook_cli_detects_book_with_failing_tests() { .stderr(predicates::str::is_match(r##"Testing chapter [^:]*: "([^"]+)[\\/]intro.md""##).unwrap()) .stderr(predicates::str::is_match(r##"Testing chapter [^:]*: "([^"]+)[\\/]first[\\/]index.md""##).unwrap()) .stderr(predicates::str::is_match(r##"Testing chapter [^:]*: "([^"]+)[\\/]first[\\/]nested.md""##).unwrap()) - .stderr(predicates::str::is_match(r##"rustdoc returned an error:\n\n"##).unwrap()) + .stderr(predicates::str::is_match(r##"returned an error:\n\n"##).unwrap()) .stderr(predicates::str::is_match(r##"Nested_Chapter::Rustdoc_include_works_with_anchors_too \(line \d+\) ... FAILED"##).unwrap()); } From 99a8c0df570c6ae39ad0837c2fd3a3630da452c9 Mon Sep 17 00:00:00 2001 From: Chris Lovett Date: Mon, 2 May 2022 17:11:38 -0700 Subject: [PATCH 05/17] CR feedback, switch to Option<&str> for chatper parameter. --- src/book/mod.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/book/mod.rs b/src/book/mod.rs index d728e1b81c..47973c5280 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -246,7 +246,7 @@ impl MDBook { /// Run `rustdoc` tests on the book, linking against the provided libraries /// and optionally testing only a signal named chapter while skipping the others. - pub fn test(&mut self, library_paths: Vec<&str>, chapter: &str) -> Result<()> { + pub fn test(&mut self, library_paths: Vec<&str>, chapter: Option<&str>) -> Result<()> { let library_args: Vec<&str> = (0..library_paths.len()) .map(|_| "-L") .zip(library_paths.into_iter()) @@ -271,13 +271,13 @@ impl MDBook { _ => continue, }; - if !chapter.is_empty() && ch.name != chapter { - info!("Skipping chapter '{}'...", ch.name); + if chapter.is_some() && ch.name != chapter.unwrap() && chapter_path.to_str().unwrap() != chapter.unwrap() { + debug!("Skipping chapter '{}'...", ch.name); continue; }; let path = self.source_dir().join(&chapter_path); - info!("Testing chapter '{}': {:?}", ch.name, path); + info!("Testing chapter '{}': {:?}", ch.name, chapter_path); // write preprocessed file to tempdir let path = temp_dir.path().join(&chapter_path); From f1c07b01cfe88a34f0fc80b5084baef00ea32d0b Mon Sep 17 00:00:00 2001 From: Chris Lovett Date: Mon, 2 May 2022 17:16:06 -0700 Subject: [PATCH 06/17] Fix tests --- tests/testing.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/testing.rs b/tests/testing.rs index 0d90e842bd..217a77dd0e 100644 --- a/tests/testing.rs +++ b/tests/testing.rs @@ -9,7 +9,7 @@ fn mdbook_can_correctly_test_a_passing_book() { let temp = DummyBook::new().with_passing_test(true).build().unwrap(); let mut md = MDBook::load(temp.path()).unwrap(); - let result = md.test(vec![], ""); + let result = md.test(vec![], None); assert!( result.is_ok(), "Tests failed with {}", @@ -22,5 +22,5 @@ fn mdbook_detects_book_with_failing_tests() { let temp = DummyBook::new().with_passing_test(false).build().unwrap(); let mut md = MDBook::load(temp.path()).unwrap(); - assert!(md.test(vec![], "").is_err()); + assert!(md.test(vec![], None).is_err()); } From 96245d227e2c5a5052c05a0d9e6fb239bbe1c527 Mon Sep 17 00:00:00 2001 From: Chris Lovett Date: Mon, 1 Aug 2022 16:38:11 -0700 Subject: [PATCH 07/17] fix: unit test expected output has changed. --- src/book/mod.rs | 1 - tests/cli/test.rs | 16 ++++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/book/mod.rs b/src/book/mod.rs index c8bc6d9507..3a973d184c 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -276,7 +276,6 @@ impl MDBook { continue; }; - let path = self.source_dir().join(&chapter_path); info!("Testing chapter '{}': {:?}", ch.name, chapter_path); // write preprocessed file to tempdir diff --git a/tests/cli/test.rs b/tests/cli/test.rs index 9409b3f7c2..63114d3a19 100644 --- a/tests/cli/test.rs +++ b/tests/cli/test.rs @@ -10,10 +10,10 @@ fn mdbook_cli_can_correctly_test_a_passing_book() { let mut cmd = mdbook_cmd(); cmd.arg("test").current_dir(temp.path()); cmd.assert().success() - .stderr(predicates::str::is_match(r##"Testing chapter [^:]*: "([^"]+)[\\/]README.md""##).unwrap()) - .stderr(predicates::str::is_match(r##"Testing chapter [^:]*: "([^"]+)[\\/]intro.md""##).unwrap()) - .stderr(predicates::str::is_match(r##"Testing chapter [^:]*: "([^"]+)[\\/]first[\\/]index.md""##).unwrap()) - .stderr(predicates::str::is_match(r##"Testing chapter [^:]*: "([^"]+)[\\/]first[\\/]nested.md""##).unwrap()) + .stderr(predicates::str::is_match(r##"Testing chapter [^:]*: "README.md""##).unwrap()) + .stderr(predicates::str::is_match(r##"Testing chapter [^:]*: "intro.md""##).unwrap()) + .stderr(predicates::str::is_match(r##"Testing chapter [^:]*: "first[\\/]index.md""##).unwrap()) + .stderr(predicates::str::is_match(r##"Testing chapter [^:]*: "first[\\/]nested.md""##).unwrap()) .stderr(predicates::str::is_match(r##"returned an error:\n\n"##).unwrap().not()) .stderr(predicates::str::is_match(r##"Nested_Chapter::Rustdoc_include_works_with_anchors_too \(line \d+\) ... FAILED"##).unwrap().not()); } @@ -25,10 +25,10 @@ fn mdbook_cli_detects_book_with_failing_tests() { let mut cmd = mdbook_cmd(); cmd.arg("test").current_dir(temp.path()); cmd.assert().failure() - .stderr(predicates::str::is_match(r##"Testing chapter [^:]*: "([^"]+)[\\/]README.md""##).unwrap()) - .stderr(predicates::str::is_match(r##"Testing chapter [^:]*: "([^"]+)[\\/]intro.md""##).unwrap()) - .stderr(predicates::str::is_match(r##"Testing chapter [^:]*: "([^"]+)[\\/]first[\\/]index.md""##).unwrap()) - .stderr(predicates::str::is_match(r##"Testing chapter [^:]*: "([^"]+)[\\/]first[\\/]nested.md""##).unwrap()) + .stderr(predicates::str::is_match(r##"Testing chapter [^:]*: "README.md""##).unwrap()) + .stderr(predicates::str::is_match(r##"Testing chapter [^:]*: "intro.md""##).unwrap()) + .stderr(predicates::str::is_match(r##"Testing chapter [^:]*: "first[\\/]index.md""##).unwrap()) + .stderr(predicates::str::is_match(r##"Testing chapter [^:]*: "first[\\/]nested.md""##).unwrap()) .stderr(predicates::str::is_match(r##"returned an error:\n\n"##).unwrap()) .stderr(predicates::str::is_match(r##"Nested_Chapter::Rustdoc_include_works_with_anchors_too \(line \d+\) ... FAILED"##).unwrap()); } From 7c558696ccab40780b3c6a40d653e82c2c6b49b2 Mon Sep 17 00:00:00 2001 From: Chris Lovett Date: Mon, 1 Aug 2022 17:00:41 -0700 Subject: [PATCH 08/17] fix: fmt --check --- src/book/mod.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/book/mod.rs b/src/book/mod.rs index 3a973d184c..ffb42b2a8f 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -271,7 +271,10 @@ impl MDBook { _ => continue, }; - if chapter.is_some() && ch.name != chapter.unwrap() && chapter_path.to_str().unwrap() != chapter.unwrap() { + if chapter.is_some() + && ch.name != chapter.unwrap() + && chapter_path.to_str().unwrap() != chapter.unwrap() + { debug!("Skipping chapter '{}'...", ch.name); continue; }; From 0926c52dcc9fcc8853a3ed9516d1f545feed8846 Mon Sep 17 00:00:00 2001 From: Chris Lovett Date: Wed, 10 Aug 2022 17:06:24 -0700 Subject: [PATCH 09/17] fix: add test_chapter method so test can remain api compatible. test: add unit tests for test_chapter --- src/book/mod.rs | 33 +++++++++++++++++++++++++-------- src/cmd/test.rs | 5 ++++- tests/testing.rs | 25 +++++++++++++++++++++++-- 3 files changed, 52 insertions(+), 11 deletions(-) diff --git a/src/book/mod.rs b/src/book/mod.rs index ffb42b2a8f..896572730f 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -244,9 +244,13 @@ impl MDBook { self } - /// Run `rustdoc` tests on the book, linking against the provided libraries - /// and optionally testing only a signal named chapter while skipping the others. - pub fn test(&mut self, library_paths: Vec<&str>, chapter: Option<&str>) -> Result<()> { + /// Run `rustdoc` tests on the book, linking against the provided libraries. + pub fn test(&mut self, library_paths: Vec<&str>) -> Result<()> { + self.test_chapter(library_paths, None) + } + + /// Run `rustdoc` tests on a specific chapter of the book, linking against the provided libraries. + pub fn test_chapter(&mut self, library_paths: Vec<&str>, chapter: Option<&str>) -> Result<()> { let library_args: Vec<&str> = (0..library_paths.len()) .map(|_| "-L") .zip(library_paths.into_iter()) @@ -255,6 +259,13 @@ impl MDBook { let temp_dir = TempFileBuilder::new().prefix("mdbook-").tempdir()?; + let chapter_name: &str = match chapter { + Some(p) => p, + None => "", + }; + + let mut chapter_found = false; + // FIXME: Is "test" the proper renderer name to use here? let preprocess_context = PreprocessorContext::new(self.root.clone(), self.config.clone(), "test".to_string()); @@ -271,14 +282,17 @@ impl MDBook { _ => continue, }; - if chapter.is_some() - && ch.name != chapter.unwrap() - && chapter_path.to_str().unwrap() != chapter.unwrap() - { - debug!("Skipping chapter '{}'...", ch.name); + let cp = match chapter_path.to_str() { + Some(s) => s, + None => "", + }; + + if chapter_name != "" && cp != "" && ch.name != chapter_name && cp != chapter_name { + info!("Skipping chapter '{}'...", ch.name); continue; }; + chapter_found = true; info!("Testing chapter '{}': {:?}", ch.name, chapter_path); // write preprocessed file to tempdir @@ -319,6 +333,9 @@ impl MDBook { if failed { bail!("One or more tests failed"); } + if chapter_name != "" && !chapter_found { + bail!(format!("Chapter not found: {}", chapter_name)); + } Ok(()) } diff --git a/src/cmd/test.rs b/src/cmd/test.rs index c06c22d92e..3d73282d6d 100644 --- a/src/cmd/test.rs +++ b/src/cmd/test.rs @@ -58,7 +58,10 @@ pub fn execute(args: &ArgMatches) -> Result<()> { if let Some(dest_dir) = args.value_of("dest-dir") { book.config.build.build_dir = dest_dir.into(); } - book.test(library_paths, chapter)?; + match chapter { + Some(_) => book.test_chapter(library_paths, chapter), + None => book.test(library_paths), + }?; Ok(()) } diff --git a/tests/testing.rs b/tests/testing.rs index 217a77dd0e..3030c5cb66 100644 --- a/tests/testing.rs +++ b/tests/testing.rs @@ -9,7 +9,7 @@ fn mdbook_can_correctly_test_a_passing_book() { let temp = DummyBook::new().with_passing_test(true).build().unwrap(); let mut md = MDBook::load(temp.path()).unwrap(); - let result = md.test(vec![], None); + let result = md.test(vec![]); assert!( result.is_ok(), "Tests failed with {}", @@ -22,5 +22,26 @@ fn mdbook_detects_book_with_failing_tests() { let temp = DummyBook::new().with_passing_test(false).build().unwrap(); let mut md = MDBook::load(temp.path()).unwrap(); - assert!(md.test(vec![], None).is_err()); + assert!(md.test(vec![]).is_err()); +} + +#[test] +fn mdbook_test_chapter() { + let temp = DummyBook::new().with_passing_test(true).build().unwrap(); + let mut md = MDBook::load(temp.path()).unwrap(); + + let result = md.test_chapter(vec![], Some("Introduction")); + assert!( + result.is_ok(), + "test_chapter failed with {}", + result.err().unwrap() + ); +} + +#[test] +fn mdbook_test_chapter_not_found() { + let temp = DummyBook::new().with_passing_test(true).build().unwrap(); + let mut md = MDBook::load(temp.path()).unwrap(); + + assert!(md.test_chapter(vec![], Some("Bogus Chapter Name")).is_err()); } From 05ed667037927961c79a1c64c8a83dcd190a4e95 Mon Sep 17 00:00:00 2001 From: clovett Date: Wed, 10 Aug 2022 17:16:53 -0700 Subject: [PATCH 10/17] doc: add documentation of new --chapter option. --- guide/src/cli/test.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/guide/src/cli/test.md b/guide/src/cli/test.md index e134dc9b2c..fcf7338698 100644 --- a/guide/src/cli/test.md +++ b/guide/src/cli/test.md @@ -43,7 +43,7 @@ mdbook test path/to/book The `--library-path` (`-L`) option allows you to add directories to the library search path used by `rustdoc` when it builds and tests the examples. Multiple directories can be specified with multiple options (`-L foo -L bar`) or with a -comma-delimited list (`-L foo,bar`). The path should point to the Cargo +comma-delimited list (`-L foo,bar`). The path should point to the Cargo [build cache](https://doc.rust-lang.org/cargo/guide/build-cache.html) `deps` directory that contains the build output of your project. For example, if your Rust project's book is in a directory named `my-book`, the following command would include the crate's dependencies when running `test`: @@ -61,3 +61,8 @@ The `--dest-dir` (`-d`) option allows you to change the output directory for the book. Relative paths are interpreted relative to the book's root directory. If not specified it will default to the value of the `build.build-dir` key in `book.toml`, or to `./book`. + +#### --chapter + +The `--chapter` (`-d`) option allows you to test a specific chapter of the +book using the chapter name or the relative path to the chapter. \ No newline at end of file From 601b6dfd3a1853da9ba283f078a3c6f6f9c84253 Mon Sep 17 00:00:00 2001 From: clovett Date: Wed, 10 Aug 2022 17:17:36 -0700 Subject: [PATCH 11/17] doc: fix typo. --- guide/src/cli/test.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/guide/src/cli/test.md b/guide/src/cli/test.md index fcf7338698..a542f3cec1 100644 --- a/guide/src/cli/test.md +++ b/guide/src/cli/test.md @@ -64,5 +64,5 @@ not specified it will default to the value of the `build.build-dir` key in #### --chapter -The `--chapter` (`-d`) option allows you to test a specific chapter of the +The `--chapter` (`-c`) option allows you to test a specific chapter of the book using the chapter name or the relative path to the chapter. \ No newline at end of file From 20cca385969720ae4f51d78161b20f0d91afbcc0 Mon Sep 17 00:00:00 2001 From: clovett Date: Wed, 10 Aug 2022 17:24:34 -0700 Subject: [PATCH 12/17] fix: remove redundant check. --- src/book/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/book/mod.rs b/src/book/mod.rs index 896572730f..5ff4b8843e 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -287,7 +287,7 @@ impl MDBook { None => "", }; - if chapter_name != "" && cp != "" && ch.name != chapter_name && cp != chapter_name { + if chapter_name != "" && ch.name != chapter_name && cp != chapter_name { info!("Skipping chapter '{}'...", ch.name); continue; }; From 5ec488cbeca19d6de37afd9e2ede5e7520429dff Mon Sep 17 00:00:00 2001 From: Chris Lovett Date: Mon, 15 Aug 2022 12:56:49 -0700 Subject: [PATCH 13/17] doc: add clarifying comments. --- src/book/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/book/mod.rs b/src/book/mod.rs index 5ff4b8843e..95c020ebdc 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -246,10 +246,12 @@ impl MDBook { /// Run `rustdoc` tests on the book, linking against the provided libraries. pub fn test(&mut self, library_paths: Vec<&str>) -> Result<()> { + // test_chapter with chapter:None will run all tests. self.test_chapter(library_paths, None) } /// Run `rustdoc` tests on a specific chapter of the book, linking against the provided libraries. + /// If `chapter` is `None`, all tests will be run. pub fn test_chapter(&mut self, library_paths: Vec<&str>, chapter: Option<&str>) -> Result<()> { let library_args: Vec<&str> = (0..library_paths.len()) .map(|_| "-L") From c1004f7101c5e28c3d46fb5131cc4334054c406e Mon Sep 17 00:00:00 2001 From: Chris Lovett Date: Mon, 15 Aug 2022 13:11:43 -0700 Subject: [PATCH 14/17] fix: revert changes from code review. --- src/theme/index.hbs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/theme/index.hbs b/src/theme/index.hbs index 1c45623ba5..18d984a2b7 100644 --- a/src/theme/index.hbs +++ b/src/theme/index.hbs @@ -67,11 +67,11 @@ var theme = localStorage.getItem('mdbook-theme'); var sidebar = localStorage.getItem('mdbook-sidebar'); - if (theme && theme.startsWith('"') && theme.endsWith('"')) { + if (theme.startsWith('"') && theme.endsWith('"')) { localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1)); } - if (sidebar && sidebar.startsWith('"') && sidebar.endsWith('"')) { + if (sidebar.startsWith('"') && sidebar.endsWith('"')) { localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1)); } } catch (e) { } From e979012006e6168f7920c008d46dcfd3baa5ada7 Mon Sep 17 00:00:00 2001 From: clovett Date: Wed, 17 Aug 2022 10:41:02 -0700 Subject: [PATCH 15/17] fix: cr feedback no need to unwrap Option<&str> when comparing chapter names. --- src/book/mod.rs | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/book/mod.rs b/src/book/mod.rs index 5ff4b8843e..71f1d7e538 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -259,11 +259,6 @@ impl MDBook { let temp_dir = TempFileBuilder::new().prefix("mdbook-").tempdir()?; - let chapter_name: &str = match chapter { - Some(p) => p, - None => "", - }; - let mut chapter_found = false; // FIXME: Is "test" the proper renderer name to use here? @@ -282,12 +277,10 @@ impl MDBook { _ => continue, }; - let cp = match chapter_path.to_str() { - Some(s) => s, - None => "", - }; + let cp = chapter_path.to_str(); + let cn: Option<&str> = Some(&ch.name); - if chapter_name != "" && ch.name != chapter_name && cp != chapter_name { + if chapter.is_some() && cn != chapter && cp != chapter { info!("Skipping chapter '{}'...", ch.name); continue; }; @@ -333,8 +326,9 @@ impl MDBook { if failed { bail!("One or more tests failed"); } - if chapter_name != "" && !chapter_found { - bail!(format!("Chapter not found: {}", chapter_name)); + + if chapter.is_some() && !chapter_found { + bail!(format!("Chapter not found: {:?}", chapter)); } Ok(()) } From 4ead38f766ab090c628ad3b5108cdc6be4426a39 Mon Sep 17 00:00:00 2001 From: clovett Date: Wed, 24 Aug 2022 13:25:48 -0700 Subject: [PATCH 16/17] fix: code review comments. --- src/book/mod.rs | 11 +++++++---- src/cmd/test.rs | 3 ++- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/book/mod.rs b/src/book/mod.rs index 710b060396..3e81d4cf90 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -283,7 +283,9 @@ impl MDBook { let cn: Option<&str> = Some(&ch.name); if chapter.is_some() && cn != chapter && cp != chapter { - info!("Skipping chapter '{}'...", ch.name); + if chapter == Some("?") { + info!("Skipping chapter '{}'...", ch.name); + } continue; }; @@ -328,9 +330,10 @@ impl MDBook { if failed { bail!("One or more tests failed"); } - - if chapter.is_some() && !chapter_found { - bail!(format!("Chapter not found: {:?}", chapter)); + if let Some(chapter) = chapter { + if !chapter_found { + bail!("Chapter not found: {}", chapter); + } } Ok(()) } diff --git a/src/cmd/test.rs b/src/cmd/test.rs index 3d73282d6d..f5ca3ee419 100644 --- a/src/cmd/test.rs +++ b/src/cmd/test.rs @@ -24,7 +24,8 @@ pub fn make_subcommand<'help>() -> App<'help> { .value_name("chapter") .help( "Only test the specified chapter{n}\ - Where the name of the chapter is defined in the SUMMARY.md file." + Where the name of the chapter is defined in the SUMMARY.md file.{n}\ + Use the special name \"?\" to the list of chapter names." ) ) .arg(arg!([dir] From 17470947bda0f9208b0ef211ca379755b52e3d99 Mon Sep 17 00:00:00 2001 From: clovett Date: Wed, 24 Aug 2022 13:30:16 -0700 Subject: [PATCH 17/17] fix: code review cleanup --- src/book/mod.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/book/mod.rs b/src/book/mod.rs index 3e81d4cf90..e407ccc335 100644 --- a/src/book/mod.rs +++ b/src/book/mod.rs @@ -279,16 +279,14 @@ impl MDBook { _ => continue, }; - let cp = chapter_path.to_str(); - let cn: Option<&str> = Some(&ch.name); - - if chapter.is_some() && cn != chapter && cp != chapter { - if chapter == Some("?") { - info!("Skipping chapter '{}'...", ch.name); + if let Some(chapter) = chapter { + if ch.name != chapter && chapter_path.to_str() != Some(chapter) { + if chapter == "?" { + info!("Skipping chapter '{}'...", ch.name); + } + continue; } - continue; - }; - + } chapter_found = true; info!("Testing chapter '{}': {:?}", ch.name, chapter_path);