Migrate pass/fail mdbook test to BookTest

This commit is contained in:
Eric Huss 2025-04-22 10:56:05 -07:00
parent 0b577ebd76
commit 5a84d641cd
17 changed files with 138 additions and 56 deletions

View file

@ -1,3 +1,2 @@
mod build; mod build;
mod cmd; mod cmd;
mod test;

View file

@ -1,34 +0,0 @@
use crate::cli::cmd::mdbook_cmd;
use crate::dummy_book::DummyBook;
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 = 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##"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 = 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##"returned an error:\n\n"##).unwrap())
.stderr(predicates::str::is_match(r##"Nested_Chapter::Rustdoc_include_works_with_anchors_too \(line \d+\) ... FAILED"##).unwrap());
}

View file

@ -4,27 +4,6 @@ use crate::dummy_book::DummyBook;
use mdbook::MDBook; use mdbook::MDBook;
#[test]
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![]);
assert!(
result.is_ok(),
"Tests failed with {}",
result.err().unwrap()
);
}
#[test]
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());
}
#[test] #[test]
fn mdbook_test_chapter() { fn mdbook_test_chapter() {
let temp = DummyBook::new().with_passing_test(true).build().unwrap(); let temp = DummyBook::new().with_passing_test(true).build().unwrap();

View file

@ -17,6 +17,7 @@ mod renderer;
mod rendering; mod rendering;
#[cfg(feature = "search")] #[cfg(feature = "search")]
mod search; mod search;
mod test;
mod prelude { mod prelude {
pub use crate::book_test::BookTest; pub use crate::book_test::BookTest;

55
tests/testsuite/test.rs Normal file
View file

@ -0,0 +1,55 @@
//! Tests for the `mdbook test` command.
use crate::prelude::*;
// Simple test for passing tests.
#[test]
fn passing_tests() {
BookTest::from_dir("test/passing_tests").run("test", |cmd| {
cmd.expect_stdout(str![[""]]).expect_stderr(str![[r#"
[TIMESTAMP] [INFO] (mdbook::book): Testing chapter 'Intro': "intro.md"
[TIMESTAMP] [INFO] (mdbook::book): Testing chapter 'Passing 1': "passing1.md"
[TIMESTAMP] [INFO] (mdbook::book): Testing chapter 'Passing 2': "passing2.md"
"#]]);
});
}
// Test for a test failure
#[test]
fn failing_tests() {
BookTest::from_dir("test/failing_tests").run("test", |cmd| {
cmd.expect_code(101)
.expect_stdout(str![[""]])
// This redacts a large number of lines that come from rustdoc and
// libtest. If the output from those ever changes, then it would not
// make it possible to test against different versions of Rust. This
// still includes a little bit of output, so if that is a problem,
// add more redactions.
.expect_stderr(str![[r#"
[TIMESTAMP] [INFO] (mdbook::book): Testing chapter 'Failing Tests': "failing.md"
[TIMESTAMP] [ERROR] (mdbook::book): rustdoc returned an error:
--- stdout
...
test failing.md - Failing_Tests (line 3) ... FAILED
...
thread 'main' panicked at failing.md:3:1:
fail
...
[TIMESTAMP] [INFO] (mdbook::book): Testing chapter 'Failing Include': "failing_include.md"
[TIMESTAMP] [ERROR] (mdbook::book): rustdoc returned an error:
--- stdout
...
test failing_include.md - Failing_Include (line 3) ... FAILED
...
thread 'main' panicked at failing_include.md:3:1:
failing!
...
[TIMESTAMP] [ERROR] (mdbook::utils): Error: One or more tests failed
"#]]);
});
}

View file

@ -0,0 +1,2 @@
[book]
title = "failing_tests"

View file

@ -0,0 +1,4 @@
# Summary
- [Failing Tests](./failing.md)
- [Failing Include](./failing_include.md)

View file

@ -0,0 +1,5 @@
# Failing Tests
```rust
panic!("fail");
```

View file

@ -0,0 +1,5 @@
# Failing Include
```rust
{{#include test1.rs:FAILING}}
```

View file

@ -0,0 +1,11 @@
fn test2() {
println!("test2");
}
// ANCHOR: PASSING
println!("passing!");
// ANCHOR_END: PASSING
// ANCHOR: FAILING
panic!("failing!");
// ANCHOR_END: FAILING

View file

@ -0,0 +1,2 @@
[book]
title = "passing_tests"

View file

@ -0,0 +1,6 @@
# Summary
[Intro](./intro.md)
- [Passing 1](./passing1.md)
- [Passing 2](./passing2.md)

View file

@ -0,0 +1,29 @@
# Passing Tests 1
```rust
assert!(true);
```
```rust
println!("hello!");
```
## Also check includes
```rust
{{#include test1.rs}}
```
```rust
{{#include test2.rs:2}}
```
```rust
{{#include test2.rs:PASSING}}
```
```rust
{{#rustdoc_include test3.rs:2}}
```
{{#playground test1.rs}}

View file

@ -0,0 +1,5 @@
# Passing Tests 2
```rust
println!("also passing");
```

View file

@ -0,0 +1 @@
println!("test1");

View file

@ -0,0 +1,11 @@
fn test2() {
println!("test2");
}
// ANCHOR: PASSING
println!("passing!");
// ANCHOR_END: PASSING
// ANCHOR: FAILING
panic!("failing!");
// ANCHOR_END: FAILING

View file

@ -0,0 +1 @@
println!("test3");