Skip to content

Commit 5a84d64

Browse files
committed
Migrate pass/fail mdbook test to BookTest
1 parent 0b577eb commit 5a84d64

File tree

17 files changed

+138
-56
lines changed

17 files changed

+138
-56
lines changed

tests/cli/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
mod build;
22
mod cmd;
3-
mod test;

tests/cli/test.rs

Lines changed: 0 additions & 34 deletions
This file was deleted.

tests/testing.rs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,6 @@ use crate::dummy_book::DummyBook;
44

55
use mdbook::MDBook;
66

7-
#[test]
8-
fn mdbook_can_correctly_test_a_passing_book() {
9-
let temp = DummyBook::new().with_passing_test(true).build().unwrap();
10-
let mut md = MDBook::load(temp.path()).unwrap();
11-
12-
let result = md.test(vec![]);
13-
assert!(
14-
result.is_ok(),
15-
"Tests failed with {}",
16-
result.err().unwrap()
17-
);
18-
}
19-
20-
#[test]
21-
fn mdbook_detects_book_with_failing_tests() {
22-
let temp = DummyBook::new().with_passing_test(false).build().unwrap();
23-
let mut md = MDBook::load(temp.path()).unwrap();
24-
25-
assert!(md.test(vec![]).is_err());
26-
}
27-
287
#[test]
298
fn mdbook_test_chapter() {
309
let temp = DummyBook::new().with_passing_test(true).build().unwrap();

tests/testsuite/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ mod renderer;
1717
mod rendering;
1818
#[cfg(feature = "search")]
1919
mod search;
20+
mod test;
2021

2122
mod prelude {
2223
pub use crate::book_test::BookTest;

tests/testsuite/test.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//! Tests for the `mdbook test` command.
2+
3+
use crate::prelude::*;
4+
5+
// Simple test for passing tests.
6+
#[test]
7+
fn passing_tests() {
8+
BookTest::from_dir("test/passing_tests").run("test", |cmd| {
9+
cmd.expect_stdout(str![[""]]).expect_stderr(str![[r#"
10+
[TIMESTAMP] [INFO] (mdbook::book): Testing chapter 'Intro': "intro.md"
11+
[TIMESTAMP] [INFO] (mdbook::book): Testing chapter 'Passing 1': "passing1.md"
12+
[TIMESTAMP] [INFO] (mdbook::book): Testing chapter 'Passing 2': "passing2.md"
13+
14+
"#]]);
15+
});
16+
}
17+
18+
// Test for a test failure
19+
#[test]
20+
fn failing_tests() {
21+
BookTest::from_dir("test/failing_tests").run("test", |cmd| {
22+
cmd.expect_code(101)
23+
.expect_stdout(str![[""]])
24+
// This redacts a large number of lines that come from rustdoc and
25+
// libtest. If the output from those ever changes, then it would not
26+
// make it possible to test against different versions of Rust. This
27+
// still includes a little bit of output, so if that is a problem,
28+
// add more redactions.
29+
.expect_stderr(str![[r#"
30+
[TIMESTAMP] [INFO] (mdbook::book): Testing chapter 'Failing Tests': "failing.md"
31+
[TIMESTAMP] [ERROR] (mdbook::book): rustdoc returned an error:
32+
33+
--- stdout
34+
35+
...
36+
test failing.md - Failing_Tests (line 3) ... FAILED
37+
...
38+
thread 'main' panicked at failing.md:3:1:
39+
fail
40+
...
41+
[TIMESTAMP] [INFO] (mdbook::book): Testing chapter 'Failing Include': "failing_include.md"
42+
[TIMESTAMP] [ERROR] (mdbook::book): rustdoc returned an error:
43+
44+
--- stdout
45+
...
46+
test failing_include.md - Failing_Include (line 3) ... FAILED
47+
...
48+
thread 'main' panicked at failing_include.md:3:1:
49+
failing!
50+
...
51+
[TIMESTAMP] [ERROR] (mdbook::utils): Error: One or more tests failed
52+
53+
"#]]);
54+
});
55+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[book]
2+
title = "failing_tests"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Summary
2+
3+
- [Failing Tests](./failing.md)
4+
- [Failing Include](./failing_include.md)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Failing Tests
2+
3+
```rust
4+
panic!("fail");
5+
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Failing Include
2+
3+
```rust
4+
{{#include test1.rs:FAILING}}
5+
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fn test2() {
2+
println!("test2");
3+
}
4+
5+
// ANCHOR: PASSING
6+
println!("passing!");
7+
// ANCHOR_END: PASSING
8+
9+
// ANCHOR: FAILING
10+
panic!("failing!");
11+
// ANCHOR_END: FAILING

0 commit comments

Comments
 (0)