Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a --chapter option to mdbook test. #1741

Merged
merged 21 commits into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/book/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// and optionally testing only a signal named chapter while skipping the others.
/// and optionally testing only a single named chapter while skipping the others.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

pub fn test(&mut self, library_paths: Vec<&str>, chapter: Option<&str>) -> Result<()> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately we can't change the API as that would be a semver breaking change.

I think you will need to add a new method, and have the old test call it.

Copy link

@lovettchris lovettchris Aug 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, I added "test_chapter", but instead of duplicating the code I just make test call test_chapter and I added a couple new tests for "test_chapter".

let library_args: Vec<&str> = (0..library_paths.len())
.map(|_| "-L")
.zip(library_paths.into_iter())
Expand All @@ -270,8 +271,15 @@ impl MDBook {
_ => continue,
};

let path = self.source_dir().join(&chapter_path);
info!("Testing file: {:?}", path);
if chapter.is_some()
&& ch.name != chapter.unwrap()
&& chapter_path.to_str().unwrap() != chapter.unwrap()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to avoid using unwrap here. Perhaps more like this:

Suggested change
if chapter.is_some()
&& ch.name != chapter.unwrap()
&& chapter_path.to_str().unwrap() != chapter.unwrap()
if let Some(chapter) = chapter {
if ch.name != chapter && chapter_path.to_str() != Some(chapter) {

However, this logic seems wrong. It seems to require that the --chapter value passed in equals both the chapter name and its filename. I would expect it to match one or the other. Perhaps something more like?

!(ch.name == chapter || chapter_path.to_str() == Some(chapter))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it works, notice this is the inverse of the test !(ch.name == chapter || chapter_path == chapter)

{
debug!("Skipping chapter '{}'...", ch.name);
continue;
};

info!("Testing chapter '{}': {:?}", ch.name, chapter_path);

// write preprocessed file to tempdir
let path = temp_dir.path().join(&chapter_path);
Expand Down
14 changes: 12 additions & 2 deletions src/cmd/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ pub fn make_subcommand<'help>() -> App<'help> {
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(
Arg::new("chapter")
.short('c')
.long("chapter")
.value_name("chapter")
.help(
"Only test the specified chapter{n}\
Where the name of the chapter is defined in the SUMMARY.md file."
)
)
.arg(arg!([dir]
"Root directory for the book{n}\
Expand All @@ -41,14 +50,15 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
.values_of("library-path")
.map(std::iter::Iterator::collect)
.unwrap_or_default();
let chapter: Option<&str> = args.value_of("chapter");

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)?;
book.test(library_paths, chapter)?;

Ok(())
}
4 changes: 2 additions & 2 deletions src/theme/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -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('"')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes seem unrelated to the PR?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they were null reference bugs I ran into while testing this PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer unrelated changes to not be included.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a bit different as I couldn't get the tests to pass without this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which tests aren't passing? What kind of errors are you seeing? I don't believe there are any javascript tests, so it is not clear to me how this could have any impact on anything.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, trying to repro it I can't find it any more and I see there's a try{ } catch (e) { } around this whole thing, so it's possible I caught the exception in the debugger and mistook it for a bug when it was expecting the try/catch block to catch it and continue. So I reverted these changes.

localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
Expand Down
20 changes: 10 additions & 10 deletions tests/cli/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ 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 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##"rustdoc returned an error:\n\n"##).unwrap().not())
.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());
}

Expand All @@ -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 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##"rustdoc returned an error:\n\n"##).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());
}
4 changes: 2 additions & 2 deletions tests/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}",
Expand All @@ -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());
}