Skip to content

Commit

Permalink
Merge pull request #1604 from notriddle/notriddle/test-cli
Browse files Browse the repository at this point in the history
Add CLI tests
  • Loading branch information
ehuss authored Sep 26, 2021
2 parents 601ebc5 + 9bede85 commit a306da3
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 0 deletions.
120 changes: 120 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ elasticlunr-rs = { version = "2.3", optional = true, default-features = false }
ammonia = { version = "3", optional = true }

[dev-dependencies]
assert_cmd = "1"
predicates = "2"
select = "0.5"
semver = "0.11.0"
pretty_assertions = "0.6"
Expand Down
29 changes: 29 additions & 0 deletions tests/cli/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::dummy_book::DummyBook;

use assert_cmd::Command;

#[test]
fn mdbook_cli_dummy_book_generates_index_html() {
let temp = DummyBook::new().build().unwrap();

// doesn't exist before
assert!(!temp.path().join("book").exists());

let mut cmd = Command::cargo_bin("mdbook").unwrap();
cmd.arg("build").current_dir(temp.path());
cmd.assert()
.success()
.stderr(
predicates::str::is_match(r##"Stack depth exceeded in first[\\/]recursive.md."##)
.unwrap(),
)
.stderr(predicates::str::contains(
r##"[INFO] (mdbook::book): Running the html backend"##,
));

// exists afterward
assert!(temp.path().join("book").exists());

let index_file = temp.path().join("book/index.html");
assert!(index_file.exists());
}
2 changes: 2 additions & 0 deletions tests/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod build;
mod test;
34 changes: 34 additions & 0 deletions tests/cli/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use crate::dummy_book::DummyBook;

use assert_cmd::Command;
use predicates::boolean::PredicateBooleanExt;

#[test]
fn mdbook_cli_can_correctly_test_a_passing_book() {
let temp = DummyBook::new().with_passing_test(true).build().unwrap();

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##"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());
}

#[test]
fn mdbook_cli_detects_book_with_failing_tests() {
let temp = DummyBook::new().with_passing_test(false).build().unwrap();

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##"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());
}
2 changes: 2 additions & 0 deletions tests/cli_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod cli;
mod dummy_book;

0 comments on commit a306da3

Please sign in to comment.