Skip to content

Commit 6628757

Browse files
authored
Merge pull request #374 from Michael-F-Bryan/moar-tests
High level integration tests
2 parents 4f754a7 + e2eb40b commit 6628757

File tree

10 files changed

+316
-0
lines changed

10 files changed

+316
-0
lines changed

tests/dummy-book/SUMMARY.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Summary
2+
3+
[Introduction](intro.md)
4+
5+
- [First Chapter](./first/index.md)
6+
- [Nested Chapter](./first/nested.md)
7+
- [Second Chapter](./second.md)
8+
9+
[Conclusion](./conclusion.md)

tests/dummy-book/conclusion.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Conclusion

tests/dummy-book/first/index.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# First Chapter
2+
3+
more text.

tests/dummy-book/first/nested.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Nested Chapter
2+
3+
This file has some testable code.
4+
5+
```rust
6+
assert!($TEST_STATUS);
7+
```

tests/dummy-book/intro.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Introduction
2+
3+
Here's some interesting text...

tests/dummy-book/second.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Second Chapter

tests/helpers.rs

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
//! Helpers for tests which exercise the overall application, in particular
2+
//! the `MDBook` initialization and build/rendering process.
3+
//!
4+
//! This will create an entire book in a temporary directory using some
5+
//! dummy contents from the `tests/dummy-book/` directory.
6+
7+
8+
#![allow(dead_code, unused_variables, unused_imports)]
9+
extern crate tempdir;
10+
11+
use std::path::Path;
12+
use std::fs::{self, File};
13+
use std::io::{Read, Write};
14+
15+
use tempdir::TempDir;
16+
17+
18+
const SUMMARY_MD: &'static str = include_str!("dummy-book/SUMMARY.md");
19+
const INTRO: &'static str = include_str!("dummy-book/intro.md");
20+
const FIRST: &'static str = include_str!("dummy-book/first/index.md");
21+
const NESTED: &'static str = include_str!("dummy-book/first/nested.md");
22+
const SECOND: &'static str = include_str!("dummy-book/second.md");
23+
const CONCLUSION: &'static str = include_str!("dummy-book/conclusion.md");
24+
25+
26+
/// Create a dummy book in a temporary directory, using the contents of
27+
/// `SUMMARY_MD` as a guide.
28+
///
29+
/// The "Nested Chapter" file contains a code block with a single
30+
/// `assert!($TEST_STATUS)`. If you want to check MDBook's testing
31+
/// functionality, `$TEST_STATUS` can be substitute for either `true` or
32+
/// `false`. This is done using the `passing_test` parameter.
33+
#[derive(Clone, Debug, PartialEq)]
34+
pub struct DummyBook {
35+
passing_test: bool,
36+
}
37+
38+
impl DummyBook {
39+
/// Create a new `DummyBook` with all the defaults.
40+
pub fn new() -> DummyBook {
41+
DummyBook::default()
42+
}
43+
44+
/// Whether the doc-test included in the "Nested Chapter" should pass or
45+
/// fail (it passes by default).
46+
pub fn with_passing_test(&mut self, test_passes: bool) -> &mut Self {
47+
self.passing_test = test_passes;
48+
self
49+
}
50+
51+
/// Write a book to a temporary directory using the provided settings.
52+
///
53+
/// # Note
54+
///
55+
/// If this fails for any reason it will `panic!()`. If we can't write to a
56+
/// temporary directory then chances are you've got bigger problems...
57+
pub fn build(&self) -> TempDir {
58+
let temp = TempDir::new("dummy_book").unwrap();
59+
60+
let src = temp.path().join("src");
61+
fs::create_dir_all(&src).unwrap();
62+
63+
let first = src.join("first");
64+
fs::create_dir_all(&first).unwrap();
65+
66+
let to_substitute = if self.passing_test { "true" } else { "false" };
67+
let nested_text = NESTED.replace("$TEST_STATUS", to_substitute);
68+
69+
let inputs = vec![
70+
(src.join("SUMMARY.md"), SUMMARY_MD),
71+
(src.join("intro.md"), INTRO),
72+
(first.join("index.md"), FIRST),
73+
(first.join("nested.md"), &nested_text),
74+
(src.join("second.md"), SECOND),
75+
(src.join("conclusion.md"), CONCLUSION),
76+
];
77+
78+
for (path, content) in inputs {
79+
File::create(path)
80+
.unwrap()
81+
.write_all(content.as_bytes())
82+
.unwrap();
83+
}
84+
85+
temp
86+
}
87+
}
88+
89+
impl Default for DummyBook {
90+
fn default() -> DummyBook {
91+
DummyBook { passing_test: true }
92+
}
93+
}
94+
95+
96+
/// Read the contents of the provided file into memory and then iterate through
97+
/// the list of strings asserting that the file contains all of them.
98+
pub fn assert_contains_strings<P: AsRef<Path>>(filename: P, strings: &[&str]) {
99+
let filename = filename.as_ref();
100+
101+
let mut content = String::new();
102+
File::open(&filename)
103+
.expect("Couldn't open the provided file")
104+
.read_to_string(&mut content)
105+
.expect("Couldn't read the file's contents");
106+
107+
for s in strings {
108+
assert!(content.contains(s), "Searching for {:?} in {}\n\n{}", s, filename.display(), content);
109+
}
110+
}

tests/init.rs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
extern crate mdbook;
2+
extern crate tempdir;
3+
4+
use tempdir::TempDir;
5+
use mdbook::MDBook;
6+
7+
8+
/// Run `mdbook init` in an empty directory and make sure the default files
9+
/// are created.
10+
#[test]
11+
fn base_mdbook_init_should_create_default_content() {
12+
let created_files = vec!["book", "src", "src/SUMMARY.md", "src/chapter_1.md"];
13+
14+
let temp = TempDir::new("mdbook").unwrap();
15+
for file in &created_files {
16+
assert!(!temp.path().join(file).exists());
17+
}
18+
19+
let mut md = MDBook::new(temp.path());
20+
md.init().unwrap();
21+
22+
for file in &created_files {
23+
assert!(temp.path().join(file).exists(), "{} doesn't exist", file);
24+
}
25+
}
26+
27+
/// Set some custom arguments for where to place the source and destination
28+
/// files, then call `mdbook init`.
29+
#[test]
30+
fn run_mdbook_init_with_custom_book_and_src_locations() {
31+
let created_files = vec!["out", "in", "in/SUMMARY.md", "in/chapter_1.md"];
32+
33+
let temp = TempDir::new("mdbook").unwrap();
34+
for file in &created_files {
35+
assert!(!temp.path().join(file).exists(), "{} shouldn't exist yet!", file);
36+
}
37+
38+
let mut md = MDBook::new(temp.path())
39+
.with_source("in")
40+
.with_destination("out");
41+
42+
md.init().unwrap();
43+
44+
for file in &created_files {
45+
assert!(temp.path().join(file).exists(), "{} should have been created by `mdbook init`", file);
46+
}
47+
}

tests/rendered_output.rs

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
extern crate mdbook;
2+
extern crate tempdir;
3+
4+
mod helpers;
5+
use mdbook::MDBook;
6+
7+
8+
/// Make sure you can load the dummy book and build it without panicking.
9+
#[test]
10+
fn build_the_dummy_book() {
11+
let temp = helpers::DummyBook::default().build();
12+
let mut md = MDBook::new(temp.path());
13+
14+
md.build().unwrap();
15+
}
16+
17+
#[test]
18+
fn by_default_mdbook_generates_rendered_content_in_the_book_directory() {
19+
let temp = helpers::DummyBook::default().build();
20+
let mut md = MDBook::new(temp.path());
21+
22+
assert!(!temp.path().join("book").exists());
23+
md.build().unwrap();
24+
25+
assert!(temp.path().join("book").exists());
26+
assert!(temp.path().join("book").join("index.html").exists());
27+
}
28+
29+
#[test]
30+
fn make_sure_bottom_level_files_contain_links_to_chapters() {
31+
let temp = helpers::DummyBook::default().build();
32+
let mut md = MDBook::new(temp.path());
33+
md.build().unwrap();
34+
35+
let dest = temp.path().join("book");
36+
let links = vec![
37+
"intro.html",
38+
"first/index.html",
39+
"first/nested.html",
40+
"second.html",
41+
"conclusion.html",
42+
];
43+
44+
let files_in_bottom_dir = vec!["index.html", "intro.html", "second.html", "conclusion.html"];
45+
46+
for filename in files_in_bottom_dir {
47+
helpers::assert_contains_strings(dest.join(filename), &links);
48+
}
49+
}
50+
51+
#[test]
52+
fn check_correct_cross_links_in_nested_dir() {
53+
let temp = helpers::DummyBook::default().build();
54+
let mut md = MDBook::new(temp.path());
55+
md.build().unwrap();
56+
57+
let first = temp.path().join("book").join("first");
58+
let links = vec![
59+
r#"<base href="../">"#,
60+
"intro.html",
61+
"first/index.html",
62+
"first/nested.html",
63+
"second.html",
64+
"conclusion.html",
65+
];
66+
67+
let files_in_nested_dir = vec!["index.html", "nested.html"];
68+
69+
for filename in files_in_nested_dir {
70+
helpers::assert_contains_strings(first.join(filename), &links);
71+
}
72+
}
73+
74+
#[test]
75+
fn rendered_code_has_playpen_stuff() {
76+
let temp = helpers::DummyBook::default().build();
77+
let mut md = MDBook::new(temp.path());
78+
md.build().unwrap();
79+
80+
let nested = temp.path().join("book/first/nested.html");
81+
let playpen_class = vec![r#"class="playpen""#];
82+
83+
helpers::assert_contains_strings(nested, &playpen_class);
84+
85+
let book_js = temp.path().join("book/book.js");
86+
helpers::assert_contains_strings(book_js, &[".playpen"]);
87+
}
88+
89+
#[test]
90+
fn chapter_content_appears_in_rendered_document() {
91+
let content = vec![
92+
("index.html", "Here's some interesting text"),
93+
("second.html", "Second Chapter"),
94+
("first/nested.html", "testable code"),
95+
("first/index.html", "more text"),
96+
("conclusion.html", "Conclusion"),
97+
];
98+
99+
let temp = helpers::DummyBook::default().build();
100+
let mut md = MDBook::new(temp.path());
101+
md.build().unwrap();
102+
103+
let destination = temp.path().join("book");
104+
105+
for (filename, text) in content {
106+
let path = destination.join(filename);
107+
helpers::assert_contains_strings(path, &[text]);
108+
}
109+
}

tests/testing.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
extern crate tempdir;
2+
extern crate mdbook;
3+
4+
mod helpers;
5+
use mdbook::MDBook;
6+
7+
8+
#[test]
9+
fn mdbook_can_correctly_test_a_passing_book() {
10+
let temp = helpers::DummyBook::default()
11+
.with_passing_test(true)
12+
.build();
13+
let mut md = MDBook::new(temp.path());
14+
15+
assert!(md.test(vec![]).is_ok());
16+
}
17+
18+
#[test]
19+
fn mdbook_detects_book_with_failing_tests() {
20+
let temp = helpers::DummyBook::default()
21+
.with_passing_test(false)
22+
.build();
23+
let mut md: MDBook = MDBook::new(temp.path());
24+
25+
assert!(md.test(vec![]).is_err());
26+
}

0 commit comments

Comments
 (0)