Add tests to check create-missing.

This commit is contained in:
Chris Spiegel 2017-12-02 22:53:05 -08:00
parent dacc274e0d
commit 803df90efa
1 changed files with 51 additions and 0 deletions

View File

@ -2,17 +2,21 @@ extern crate mdbook;
#[macro_use] #[macro_use]
extern crate pretty_assertions; extern crate pretty_assertions;
extern crate select; extern crate select;
extern crate tempdir;
extern crate walkdir; extern crate walkdir;
mod dummy_book; mod dummy_book;
use dummy_book::{assert_contains_strings, DummyBook}; use dummy_book::{assert_contains_strings, DummyBook};
use std::fs::{File, remove_file};
use std::io::Write;
use std::path::Path; use std::path::Path;
use std::ffi::OsStr; use std::ffi::OsStr;
use walkdir::{DirEntry, WalkDir, WalkDirIterator}; use walkdir::{DirEntry, WalkDir, WalkDirIterator};
use select::document::Document; use select::document::Document;
use select::predicate::{Class, Name, Predicate}; use select::predicate::{Class, Name, Predicate};
use tempdir::TempDir;
use mdbook::errors::*; use mdbook::errors::*;
use mdbook::utils::fs::file_to_string; use mdbook::utils::fs::file_to_string;
use mdbook::MDBook; use mdbook::MDBook;
@ -227,3 +231,50 @@ fn check_spacers() {
doc.find(Class("chapter").descendant(Name("li").and(Class("spacer")))).count(); doc.find(Class("chapter").descendant(Name("li").and(Class("spacer")))).count();
assert_eq!(num_spacers, should_be); assert_eq!(num_spacers, should_be);
} }
/// Ensure building fails if `create-missing` is false and one of the files does
/// not exist.
#[test]
fn failure_on_missing_file() {
let (md, _temp) = create_missing_setup(Some(false));
// On failure, `build()` does not return a specific error, so assume
// any error is a failure due to a missing file.
assert!(md.read_config().unwrap().build().is_err());
}
/// Ensure a missing file is created if `create-missing` is true.
#[test]
fn create_missing_file_with_config() {
let (md, temp) = create_missing_setup(Some(true));
md.read_config().unwrap().build().unwrap();
assert!(temp.path().join("src").join("intro.md").exists());
}
/// Ensure a missing file is created if `create-missing` is not set (the default
/// is true).
#[test]
fn create_missing_file_without_config() {
let (md, temp) = create_missing_setup(None);
md.read_config().unwrap().build().unwrap();
assert!(temp.path().join("src").join("intro.md").exists());
}
fn create_missing_setup(create_missing: Option<bool>) -> (MDBook, TempDir) {
let temp = DummyBook::new().build().unwrap();
let md = MDBook::new(temp.path());
let mut file = File::create(temp.path().join("book.toml")).unwrap();
match create_missing {
Some(true) => file.write_all(b"[build]\ncreate-missing = true\n").unwrap(),
Some(false) => file.write_all(b"[build]\ncreate-missing = false\n").unwrap(),
None => (),
}
file.flush().unwrap();
remove_file(temp.path().join("src").join("intro.md")).unwrap();
(md, temp)
}