Got some feedback from azerupi and added a DummyBook builder
This commit is contained in:
parent
0c3a2b80f8
commit
e2eb40bded
|
@ -0,0 +1,9 @@
|
||||||
|
# Summary
|
||||||
|
|
||||||
|
[Introduction](intro.md)
|
||||||
|
|
||||||
|
- [First Chapter](./first/index.md)
|
||||||
|
- [Nested Chapter](./first/nested.md)
|
||||||
|
- [Second Chapter](./second.md)
|
||||||
|
|
||||||
|
[Conclusion](./conclusion.md)
|
|
@ -0,0 +1 @@
|
||||||
|
# Conclusion
|
|
@ -0,0 +1,3 @@
|
||||||
|
# First Chapter
|
||||||
|
|
||||||
|
more text.
|
|
@ -0,0 +1,7 @@
|
||||||
|
# Nested Chapter
|
||||||
|
|
||||||
|
This file has some testable code.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
assert!($TEST_STATUS);
|
||||||
|
```
|
|
@ -0,0 +1,3 @@
|
||||||
|
# Introduction
|
||||||
|
|
||||||
|
Here's some interesting text...
|
|
@ -0,0 +1 @@
|
||||||
|
# Second Chapter
|
139
tests/helpers.rs
139
tests/helpers.rs
|
@ -2,7 +2,8 @@
|
||||||
//! the `MDBook` initialization and build/rendering process.
|
//! the `MDBook` initialization and build/rendering process.
|
||||||
//!
|
//!
|
||||||
//! This will create an entire book in a temporary directory using some
|
//! This will create an entire book in a temporary directory using some
|
||||||
//! dummy content.
|
//! dummy contents from the `tests/dummy-book/` directory.
|
||||||
|
|
||||||
|
|
||||||
#![allow(dead_code, unused_variables, unused_imports)]
|
#![allow(dead_code, unused_variables, unused_imports)]
|
||||||
extern crate tempdir;
|
extern crate tempdir;
|
||||||
|
@ -14,36 +15,12 @@ use std::io::{Read, Write};
|
||||||
use tempdir::TempDir;
|
use tempdir::TempDir;
|
||||||
|
|
||||||
|
|
||||||
const SUMMARY_MD: &'static str = "# Summary
|
const SUMMARY_MD: &'static str = include_str!("dummy-book/SUMMARY.md");
|
||||||
|
const INTRO: &'static str = include_str!("dummy-book/intro.md");
|
||||||
[Introduction](intro.md)
|
const FIRST: &'static str = include_str!("dummy-book/first/index.md");
|
||||||
|
const NESTED: &'static str = include_str!("dummy-book/first/nested.md");
|
||||||
- [First Chapter](./first/index.md)
|
const SECOND: &'static str = include_str!("dummy-book/second.md");
|
||||||
- [Nested Chapter](./first/nested.md)
|
const CONCLUSION: &'static str = include_str!("dummy-book/conclusion.md");
|
||||||
- [Second Chapter](./second.md)
|
|
||||||
|
|
||||||
[Conclusion](./conclusion.md)
|
|
||||||
";
|
|
||||||
|
|
||||||
const INTRO: &'static str = "# Introduction
|
|
||||||
|
|
||||||
Here's some interesting text...";
|
|
||||||
|
|
||||||
const FIRST: &'static str = "# First Chapter
|
|
||||||
|
|
||||||
more text.";
|
|
||||||
|
|
||||||
const NESTED: &'static str = r#"# Nested Chapter
|
|
||||||
|
|
||||||
This file has some testable code.
|
|
||||||
|
|
||||||
```rust
|
|
||||||
assert!($TEST_STATUS);
|
|
||||||
```"#;
|
|
||||||
|
|
||||||
const SECOND: &'static str = "# Second Chapter";
|
|
||||||
|
|
||||||
const CONCLUSION: &'static str = "# Conclusion";
|
|
||||||
|
|
||||||
|
|
||||||
/// Create a dummy book in a temporary directory, using the contents of
|
/// Create a dummy book in a temporary directory, using the contents of
|
||||||
|
@ -53,47 +30,69 @@ const CONCLUSION: &'static str = "# Conclusion";
|
||||||
/// `assert!($TEST_STATUS)`. If you want to check MDBook's testing
|
/// `assert!($TEST_STATUS)`. If you want to check MDBook's testing
|
||||||
/// functionality, `$TEST_STATUS` can be substitute for either `true` or
|
/// functionality, `$TEST_STATUS` can be substitute for either `true` or
|
||||||
/// `false`. This is done using the `passing_test` parameter.
|
/// `false`. This is done using the `passing_test` parameter.
|
||||||
pub fn create_book(passing_test: bool) -> TempDir {
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
let temp = TempDir::new("dummy_book").unwrap();
|
pub struct DummyBook {
|
||||||
|
passing_test: bool,
|
||||||
let src = temp.path().join("src");
|
|
||||||
fs::create_dir_all(&src).unwrap();
|
|
||||||
|
|
||||||
File::create(src.join("SUMMARY.md"))
|
|
||||||
.unwrap()
|
|
||||||
.write_all(SUMMARY_MD.as_bytes())
|
|
||||||
.unwrap();
|
|
||||||
File::create(src.join("intro.md"))
|
|
||||||
.unwrap()
|
|
||||||
.write_all(INTRO.as_bytes())
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let first = src.join("first");
|
|
||||||
fs::create_dir_all(&first).unwrap();
|
|
||||||
File::create(first.join("index.md"))
|
|
||||||
.unwrap()
|
|
||||||
.write_all(FIRST.as_bytes())
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let to_substitute = if passing_test { "true" } else { "false" };
|
|
||||||
let nested_text = NESTED.replace("$TEST_STATUS", to_substitute);
|
|
||||||
File::create(first.join("nested.md"))
|
|
||||||
.unwrap()
|
|
||||||
.write_all(nested_text.as_bytes())
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
File::create(src.join("second.md"))
|
|
||||||
.unwrap()
|
|
||||||
.write_all(SECOND.as_bytes())
|
|
||||||
.unwrap();
|
|
||||||
File::create(src.join("conclusion.md"))
|
|
||||||
.unwrap()
|
|
||||||
.write_all(CONCLUSION.as_bytes())
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
temp
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl DummyBook {
|
||||||
|
/// Create a new `DummyBook` with all the defaults.
|
||||||
|
pub fn new() -> DummyBook {
|
||||||
|
DummyBook::default()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Whether the doc-test included in the "Nested Chapter" should pass or
|
||||||
|
/// fail (it passes by default).
|
||||||
|
pub fn with_passing_test(&mut self, test_passes: bool) -> &mut Self {
|
||||||
|
self.passing_test = test_passes;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Write a book to a temporary directory using the provided settings.
|
||||||
|
///
|
||||||
|
/// # Note
|
||||||
|
///
|
||||||
|
/// If this fails for any reason it will `panic!()`. If we can't write to a
|
||||||
|
/// temporary directory then chances are you've got bigger problems...
|
||||||
|
pub fn build(&self) -> TempDir {
|
||||||
|
let temp = TempDir::new("dummy_book").unwrap();
|
||||||
|
|
||||||
|
let src = temp.path().join("src");
|
||||||
|
fs::create_dir_all(&src).unwrap();
|
||||||
|
|
||||||
|
let first = src.join("first");
|
||||||
|
fs::create_dir_all(&first).unwrap();
|
||||||
|
|
||||||
|
let to_substitute = if self.passing_test { "true" } else { "false" };
|
||||||
|
let nested_text = NESTED.replace("$TEST_STATUS", to_substitute);
|
||||||
|
|
||||||
|
let inputs = vec![
|
||||||
|
(src.join("SUMMARY.md"), SUMMARY_MD),
|
||||||
|
(src.join("intro.md"), INTRO),
|
||||||
|
(first.join("index.md"), FIRST),
|
||||||
|
(first.join("nested.md"), &nested_text),
|
||||||
|
(src.join("second.md"), SECOND),
|
||||||
|
(src.join("conclusion.md"), CONCLUSION),
|
||||||
|
];
|
||||||
|
|
||||||
|
for (path, content) in inputs {
|
||||||
|
File::create(path)
|
||||||
|
.unwrap()
|
||||||
|
.write_all(content.as_bytes())
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
temp
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for DummyBook {
|
||||||
|
fn default() -> DummyBook {
|
||||||
|
DummyBook { passing_test: true }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Read the contents of the provided file into memory and then iterate through
|
/// Read the contents of the provided file into memory and then iterate through
|
||||||
/// the list of strings asserting that the file contains all of them.
|
/// the list of strings asserting that the file contains all of them.
|
||||||
pub fn assert_contains_strings<P: AsRef<Path>>(filename: P, strings: &[&str]) {
|
pub fn assert_contains_strings<P: AsRef<Path>>(filename: P, strings: &[&str]) {
|
||||||
|
|
|
@ -5,8 +5,10 @@ use tempdir::TempDir;
|
||||||
use mdbook::MDBook;
|
use mdbook::MDBook;
|
||||||
|
|
||||||
|
|
||||||
|
/// Run `mdbook init` in an empty directory and make sure the default files
|
||||||
|
/// are created.
|
||||||
#[test]
|
#[test]
|
||||||
fn run_mdbook_init() {
|
fn base_mdbook_init_should_create_default_content() {
|
||||||
let created_files = vec!["book", "src", "src/SUMMARY.md", "src/chapter_1.md"];
|
let created_files = vec!["book", "src", "src/SUMMARY.md", "src/chapter_1.md"];
|
||||||
|
|
||||||
let temp = TempDir::new("mdbook").unwrap();
|
let temp = TempDir::new("mdbook").unwrap();
|
||||||
|
@ -22,13 +24,15 @@ fn run_mdbook_init() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set some custom arguments for where to place the source and destination
|
||||||
|
/// files, then call `mdbook init`.
|
||||||
#[test]
|
#[test]
|
||||||
fn run_mdbook_init_with_custom_args() {
|
fn run_mdbook_init_with_custom_book_and_src_locations() {
|
||||||
let created_files = vec!["out", "in", "in/SUMMARY.md", "in/chapter_1.md"];
|
let created_files = vec!["out", "in", "in/SUMMARY.md", "in/chapter_1.md"];
|
||||||
|
|
||||||
let temp = TempDir::new("mdbook").unwrap();
|
let temp = TempDir::new("mdbook").unwrap();
|
||||||
for file in &created_files {
|
for file in &created_files {
|
||||||
assert!(!temp.path().join(file).exists());
|
assert!(!temp.path().join(file).exists(), "{} shouldn't exist yet!", file);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut md = MDBook::new(temp.path())
|
let mut md = MDBook::new(temp.path())
|
||||||
|
@ -38,6 +42,6 @@ fn run_mdbook_init_with_custom_args() {
|
||||||
md.init().unwrap();
|
md.init().unwrap();
|
||||||
|
|
||||||
for file in &created_files {
|
for file in &created_files {
|
||||||
assert!(temp.path().join(file).exists(), "{} doesn't exist", file);
|
assert!(temp.path().join(file).exists(), "{} should have been created by `mdbook init`", file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,9 +5,10 @@ mod helpers;
|
||||||
use mdbook::MDBook;
|
use mdbook::MDBook;
|
||||||
|
|
||||||
|
|
||||||
|
/// Make sure you can load the dummy book and build it without panicking.
|
||||||
#[test]
|
#[test]
|
||||||
fn build_the_dummy_book() {
|
fn build_the_dummy_book() {
|
||||||
let temp = helpers::create_book(true);
|
let temp = helpers::DummyBook::default().build();
|
||||||
let mut md = MDBook::new(temp.path());
|
let mut md = MDBook::new(temp.path());
|
||||||
|
|
||||||
md.build().unwrap();
|
md.build().unwrap();
|
||||||
|
@ -15,7 +16,7 @@ fn build_the_dummy_book() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn by_default_mdbook_generates_rendered_content_in_the_book_directory() {
|
fn by_default_mdbook_generates_rendered_content_in_the_book_directory() {
|
||||||
let temp = helpers::create_book(false);
|
let temp = helpers::DummyBook::default().build();
|
||||||
let mut md = MDBook::new(temp.path());
|
let mut md = MDBook::new(temp.path());
|
||||||
|
|
||||||
assert!(!temp.path().join("book").exists());
|
assert!(!temp.path().join("book").exists());
|
||||||
|
@ -27,7 +28,7 @@ fn by_default_mdbook_generates_rendered_content_in_the_book_directory() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn make_sure_bottom_level_files_contain_links_to_chapters() {
|
fn make_sure_bottom_level_files_contain_links_to_chapters() {
|
||||||
let temp = helpers::create_book(false);
|
let temp = helpers::DummyBook::default().build();
|
||||||
let mut md = MDBook::new(temp.path());
|
let mut md = MDBook::new(temp.path());
|
||||||
md.build().unwrap();
|
md.build().unwrap();
|
||||||
|
|
||||||
|
@ -49,7 +50,7 @@ fn make_sure_bottom_level_files_contain_links_to_chapters() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn check_correct_cross_links_in_nested_dir() {
|
fn check_correct_cross_links_in_nested_dir() {
|
||||||
let temp = helpers::create_book(false);
|
let temp = helpers::DummyBook::default().build();
|
||||||
let mut md = MDBook::new(temp.path());
|
let mut md = MDBook::new(temp.path());
|
||||||
md.build().unwrap();
|
md.build().unwrap();
|
||||||
|
|
||||||
|
@ -72,7 +73,7 @@ fn check_correct_cross_links_in_nested_dir() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn rendered_code_has_playpen_stuff() {
|
fn rendered_code_has_playpen_stuff() {
|
||||||
let temp = helpers::create_book(true);
|
let temp = helpers::DummyBook::default().build();
|
||||||
let mut md = MDBook::new(temp.path());
|
let mut md = MDBook::new(temp.path());
|
||||||
md.build().unwrap();
|
md.build().unwrap();
|
||||||
|
|
||||||
|
@ -95,7 +96,7 @@ fn chapter_content_appears_in_rendered_document() {
|
||||||
("conclusion.html", "Conclusion"),
|
("conclusion.html", "Conclusion"),
|
||||||
];
|
];
|
||||||
|
|
||||||
let temp = helpers::create_book(true);
|
let temp = helpers::DummyBook::default().build();
|
||||||
let mut md = MDBook::new(temp.path());
|
let mut md = MDBook::new(temp.path());
|
||||||
md.build().unwrap();
|
md.build().unwrap();
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,9 @@ use mdbook::MDBook;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn mdbook_can_correctly_test_a_passing_book() {
|
fn mdbook_can_correctly_test_a_passing_book() {
|
||||||
let temp = helpers::create_book(true);
|
let temp = helpers::DummyBook::default()
|
||||||
|
.with_passing_test(true)
|
||||||
|
.build();
|
||||||
let mut md = MDBook::new(temp.path());
|
let mut md = MDBook::new(temp.path());
|
||||||
|
|
||||||
assert!(md.test(vec![]).is_ok());
|
assert!(md.test(vec![]).is_ok());
|
||||||
|
@ -15,7 +17,9 @@ fn mdbook_can_correctly_test_a_passing_book() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn mdbook_detects_book_with_failing_tests() {
|
fn mdbook_detects_book_with_failing_tests() {
|
||||||
let temp = helpers::create_book(false);
|
let temp = helpers::DummyBook::default()
|
||||||
|
.with_passing_test(false)
|
||||||
|
.build();
|
||||||
let mut md: MDBook = MDBook::new(temp.path());
|
let mut md: MDBook = MDBook::new(temp.path());
|
||||||
|
|
||||||
assert!(md.test(vec![]).is_err());
|
assert!(md.test(vec![]).is_err());
|
||||||
|
|
Loading…
Reference in New Issue