Migrate pass/fail mdbook test to BookTest
This commit is contained in:
parent
0b577ebd76
commit
5a84d641cd
17 changed files with 138 additions and 56 deletions
|
|
@ -1,3 +1,2 @@
|
|||
mod build;
|
||||
mod cmd;
|
||||
mod test;
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
}
|
||||
|
|
@ -4,27 +4,6 @@ use crate::dummy_book::DummyBook;
|
|||
|
||||
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]
|
||||
fn mdbook_test_chapter() {
|
||||
let temp = DummyBook::new().with_passing_test(true).build().unwrap();
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ mod renderer;
|
|||
mod rendering;
|
||||
#[cfg(feature = "search")]
|
||||
mod search;
|
||||
mod test;
|
||||
|
||||
mod prelude {
|
||||
pub use crate::book_test::BookTest;
|
||||
|
|
|
|||
55
tests/testsuite/test.rs
Normal file
55
tests/testsuite/test.rs
Normal 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
|
||||
|
||||
"#]]);
|
||||
});
|
||||
}
|
||||
2
tests/testsuite/test/failing_tests/book.toml
Normal file
2
tests/testsuite/test/failing_tests/book.toml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
[book]
|
||||
title = "failing_tests"
|
||||
4
tests/testsuite/test/failing_tests/src/SUMMARY.md
Normal file
4
tests/testsuite/test/failing_tests/src/SUMMARY.md
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
# Summary
|
||||
|
||||
- [Failing Tests](./failing.md)
|
||||
- [Failing Include](./failing_include.md)
|
||||
5
tests/testsuite/test/failing_tests/src/failing.md
Normal file
5
tests/testsuite/test/failing_tests/src/failing.md
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
# Failing Tests
|
||||
|
||||
```rust
|
||||
panic!("fail");
|
||||
```
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
# Failing Include
|
||||
|
||||
```rust
|
||||
{{#include test1.rs:FAILING}}
|
||||
```
|
||||
11
tests/testsuite/test/failing_tests/src/test1.rs
Normal file
11
tests/testsuite/test/failing_tests/src/test1.rs
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fn test2() {
|
||||
println!("test2");
|
||||
}
|
||||
|
||||
// ANCHOR: PASSING
|
||||
println!("passing!");
|
||||
// ANCHOR_END: PASSING
|
||||
|
||||
// ANCHOR: FAILING
|
||||
panic!("failing!");
|
||||
// ANCHOR_END: FAILING
|
||||
2
tests/testsuite/test/passing_tests/book.toml
Normal file
2
tests/testsuite/test/passing_tests/book.toml
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
[book]
|
||||
title = "passing_tests"
|
||||
6
tests/testsuite/test/passing_tests/src/SUMMARY.md
Normal file
6
tests/testsuite/test/passing_tests/src/SUMMARY.md
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
# Summary
|
||||
|
||||
[Intro](./intro.md)
|
||||
|
||||
- [Passing 1](./passing1.md)
|
||||
- [Passing 2](./passing2.md)
|
||||
29
tests/testsuite/test/passing_tests/src/passing1.md
Normal file
29
tests/testsuite/test/passing_tests/src/passing1.md
Normal 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}}
|
||||
5
tests/testsuite/test/passing_tests/src/passing2.md
Normal file
5
tests/testsuite/test/passing_tests/src/passing2.md
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
# Passing Tests 2
|
||||
|
||||
```rust
|
||||
println!("also passing");
|
||||
```
|
||||
1
tests/testsuite/test/passing_tests/src/test1.rs
Normal file
1
tests/testsuite/test/passing_tests/src/test1.rs
Normal file
|
|
@ -0,0 +1 @@
|
|||
println!("test1");
|
||||
11
tests/testsuite/test/passing_tests/src/test2.rs
Normal file
11
tests/testsuite/test/passing_tests/src/test2.rs
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
fn test2() {
|
||||
println!("test2");
|
||||
}
|
||||
|
||||
// ANCHOR: PASSING
|
||||
println!("passing!");
|
||||
// ANCHOR_END: PASSING
|
||||
|
||||
// ANCHOR: FAILING
|
||||
panic!("failing!");
|
||||
// ANCHOR_END: FAILING
|
||||
1
tests/testsuite/test/passing_tests/src/test3.rs
Normal file
1
tests/testsuite/test/passing_tests/src/test3.rs
Normal file
|
|
@ -0,0 +1 @@
|
|||
println!("test3");
|
||||
Loading…
Add table
Reference in a new issue