2025-04-22 10:56:05 -07:00
|
|
|
//! 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#"
|
2025-09-12 06:13:45 -07:00
|
|
|
INFO Testing chapter 'Intro': "intro.md"
|
|
|
|
|
INFO Testing chapter 'Passing 1': "passing1.md"
|
|
|
|
|
INFO Testing chapter 'Passing 2': "passing2.md"
|
2025-04-22 10:56:05 -07:00
|
|
|
|
|
|
|
|
"#]]);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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#"
|
2025-09-12 06:13:45 -07:00
|
|
|
INFO Testing chapter 'Failing Tests': "failing.md"
|
|
|
|
|
ERROR rustdoc returned an error:
|
2025-04-22 10:56:05 -07:00
|
|
|
|
|
|
|
|
--- stdout
|
|
|
|
|
|
|
|
|
|
...
|
|
|
|
|
test failing.md - Failing_Tests (line 3) ... FAILED
|
|
|
|
|
...
|
2025-08-09 16:45:41 -07:00
|
|
|
thread [..] panicked at failing.md:3:1:
|
2025-04-22 10:56:05 -07:00
|
|
|
fail
|
|
|
|
|
...
|
2025-09-12 06:13:45 -07:00
|
|
|
INFO Testing chapter 'Failing Include': "failing_include.md"
|
|
|
|
|
ERROR rustdoc returned an error:
|
2025-04-22 10:56:05 -07:00
|
|
|
|
|
|
|
|
--- stdout
|
|
|
|
|
...
|
|
|
|
|
test failing_include.md - Failing_Include (line 3) ... FAILED
|
|
|
|
|
...
|
2025-08-09 16:45:41 -07:00
|
|
|
thread [..] panicked at failing_include.md:3:1:
|
2025-04-22 10:56:05 -07:00
|
|
|
failing!
|
|
|
|
|
...
|
2025-09-12 06:13:45 -07:00
|
|
|
ERROR One or more tests failed
|
2025-04-22 10:56:05 -07:00
|
|
|
|
|
|
|
|
"#]]);
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-04-22 10:58:27 -07:00
|
|
|
|
|
|
|
|
// Test with a specific chapter.
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_individual_chapter() {
|
|
|
|
|
let mut test = BookTest::from_dir("test/passing_tests");
|
|
|
|
|
test.run("test -c", |cmd| {
|
|
|
|
|
cmd.args(&["Passing 1"])
|
|
|
|
|
.expect_stdout(str![[""]])
|
|
|
|
|
.expect_stderr(str![[r#"
|
2025-09-12 06:13:45 -07:00
|
|
|
INFO Testing chapter 'Passing 1': "passing1.md"
|
2025-04-22 10:58:27 -07:00
|
|
|
|
|
|
|
|
"#]]);
|
|
|
|
|
})
|
|
|
|
|
// Can also be a source path.
|
|
|
|
|
.run("test -c passing2.md", |cmd| {
|
|
|
|
|
cmd.expect_stdout(str![[""]]).expect_stderr(str![[r#"
|
2025-09-12 06:13:45 -07:00
|
|
|
INFO Testing chapter 'Passing 2': "passing2.md"
|
2025-04-22 10:58:27 -07:00
|
|
|
|
|
|
|
|
"#]]);
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-04-22 10:58:55 -07:00
|
|
|
|
|
|
|
|
// Unknown chapter name.
|
|
|
|
|
#[test]
|
|
|
|
|
fn chapter_not_found() {
|
|
|
|
|
BookTest::from_dir("test/passing_tests").run("test -c bogus", |cmd| {
|
|
|
|
|
cmd.expect_failure()
|
|
|
|
|
.expect_stdout(str![[""]])
|
|
|
|
|
.expect_stderr(str![[r#"
|
2025-09-12 06:13:45 -07:00
|
|
|
ERROR Chapter not found: bogus
|
2025-04-22 10:58:55 -07:00
|
|
|
|
|
|
|
|
"#]]);
|
|
|
|
|
});
|
|
|
|
|
}
|