Fix build and test warnings

Move non-test test module files into their own directories to prevent
cargo from running them as tests. Then suppress the left-over warnings.

Move *dummy book* code and data into a shared folder, and leave the rest
of helper utilities (one function) in the original module.
This commit is contained in:
Behnam Esfahbod 2017-09-01 16:40:39 -07:00
parent 6bc3039b4f
commit cef62ec42e
11 changed files with 65 additions and 50 deletions

View File

@ -100,6 +100,8 @@ pub use book::BookItem;
pub use renderer::Renderer;
/// The error types used through out this crate.
// TODO: Drop after error_chain is fixed
#[allow(unused_doc_comment)]
pub mod errors {
error_chain!{
foreign_links {

View File

@ -1,3 +1,3 @@
# First Chapter
more text.
more text.

View File

@ -1,26 +1,23 @@
//! Helpers for tests which exercise the overall application, in particular
//! the `MDBook` initialization and build/rendering process.
//!
//! This will create an entire book in a temporary directory using some
//! dummy contents from the `tests/dummy-book/` directory.
// Not all features are used in all test crates, so...
#![allow(dead_code, unused_extern_crates)]
#![allow(dead_code, unused_variables, unused_imports)]
extern crate tempdir;
use std::path::Path;
use std::fs::{self, File};
use std::io::{Read, Write};
use std::fs::{create_dir_all, File};
use std::io::Write;
use tempdir::TempDir;
const SUMMARY_MD: &'static str = include_str!("dummy-book/SUMMARY.md");
const INTRO: &'static str = include_str!("dummy-book/intro.md");
const FIRST: &'static str = include_str!("dummy-book/first/index.md");
const NESTED: &'static str = include_str!("dummy-book/first/nested.md");
const SECOND: &'static str = include_str!("dummy-book/second.md");
const CONCLUSION: &'static str = include_str!("dummy-book/conclusion.md");
const SUMMARY_MD: &'static str = include_str!("book/SUMMARY.md");
const INTRO: &'static str = include_str!("book/intro.md");
const FIRST: &'static str = include_str!("book/first/index.md");
const NESTED: &'static str = include_str!("book/first/nested.md");
const SECOND: &'static str = include_str!("book/second.md");
const CONCLUSION: &'static str = include_str!("book/conclusion.md");
/// Create a dummy book in a temporary directory, using the contents of
@ -58,10 +55,10 @@ impl DummyBook {
let temp = TempDir::new("dummy_book").unwrap();
let src = temp.path().join("src");
fs::create_dir_all(&src).unwrap();
create_dir_all(&src).unwrap();
let first = src.join("first");
fs::create_dir_all(&first).unwrap();
create_dir_all(&first).unwrap();
let to_substitute = if self.passing_test { "true" } else { "false" };
let nested_text = NESTED.replace("$TEST_STATUS", to_substitute);
@ -91,20 +88,3 @@ impl Default for DummyBook {
DummyBook { passing_test: true }
}
}
/// 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.
pub fn assert_contains_strings<P: AsRef<Path>>(filename: P, strings: &[&str]) {
let filename = filename.as_ref();
let mut content = String::new();
File::open(&filename)
.expect("Couldn't open the provided file")
.read_to_string(&mut content)
.expect("Couldn't read the file's contents");
for s in strings {
assert!(content.contains(s), "Searching for {:?} in {}\n\n{}", s, filename.display(), content);
}
}

27
tests/helpers/mod.rs Normal file
View File

@ -0,0 +1,27 @@
//! Helpers for tests which exercise the overall application, in particular
//! the `MDBook` initialization and build/rendering process.
//!
//! This will create an entire book in a temporary directory using some
//! dummy contents from the `tests/dummy-book/` directory.
use std::path::Path;
use std::fs::File;
use std::io::Read;
/// 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.
pub fn assert_contains_strings<P: AsRef<Path>>(filename: P, strings: &[&str]) {
let filename = filename.as_ref();
let mut content = String::new();
File::open(&filename)
.expect("Couldn't open the provided file")
.read_to_string(&mut content)
.expect("Couldn't read the file's contents");
for s in strings {
assert!(content.contains(s), "Searching for {:?} in {}\n\n{}", s, filename.display(), content);
}
}

View File

@ -1,14 +1,18 @@
extern crate mdbook;
extern crate tempdir;
mod dummy;
mod helpers;
use dummy::DummyBook;
use helpers::assert_contains_strings;
use mdbook::MDBook;
/// Make sure you can load the dummy book and build it without panicking.
#[test]
fn build_the_dummy_book() {
let temp = helpers::DummyBook::default().build();
let temp = DummyBook::default().build();
let mut md = MDBook::new(temp.path());
md.build().unwrap();
@ -16,7 +20,7 @@ fn build_the_dummy_book() {
#[test]
fn by_default_mdbook_generates_rendered_content_in_the_book_directory() {
let temp = helpers::DummyBook::default().build();
let temp = DummyBook::default().build();
let mut md = MDBook::new(temp.path());
assert!(!temp.path().join("book").exists());
@ -28,7 +32,7 @@ fn by_default_mdbook_generates_rendered_content_in_the_book_directory() {
#[test]
fn make_sure_bottom_level_files_contain_links_to_chapters() {
let temp = helpers::DummyBook::default().build();
let temp = DummyBook::default().build();
let mut md = MDBook::new(temp.path());
md.build().unwrap();
@ -44,13 +48,13 @@ fn make_sure_bottom_level_files_contain_links_to_chapters() {
let files_in_bottom_dir = vec!["index.html", "intro.html", "second.html", "conclusion.html"];
for filename in files_in_bottom_dir {
helpers::assert_contains_strings(dest.join(filename), &links);
assert_contains_strings(dest.join(filename), &links);
}
}
#[test]
fn check_correct_cross_links_in_nested_dir() {
let temp = helpers::DummyBook::default().build();
let temp = DummyBook::default().build();
let mut md = MDBook::new(temp.path());
md.build().unwrap();
@ -67,23 +71,23 @@ fn check_correct_cross_links_in_nested_dir() {
let files_in_nested_dir = vec!["index.html", "nested.html"];
for filename in files_in_nested_dir {
helpers::assert_contains_strings(first.join(filename), &links);
assert_contains_strings(first.join(filename), &links);
}
}
#[test]
fn rendered_code_has_playpen_stuff() {
let temp = helpers::DummyBook::default().build();
let temp = DummyBook::default().build();
let mut md = MDBook::new(temp.path());
md.build().unwrap();
let nested = temp.path().join("book/first/nested.html");
let playpen_class = vec![r#"class="playpen""#];
helpers::assert_contains_strings(nested, &playpen_class);
assert_contains_strings(nested, &playpen_class);
let book_js = temp.path().join("book/book.js");
helpers::assert_contains_strings(book_js, &[".playpen"]);
assert_contains_strings(book_js, &[".playpen"]);
}
#[test]
@ -96,7 +100,7 @@ fn chapter_content_appears_in_rendered_document() {
("conclusion.html", "Conclusion"),
];
let temp = helpers::DummyBook::default().build();
let temp = DummyBook::default().build();
let mut md = MDBook::new(temp.path());
md.build().unwrap();
@ -104,6 +108,6 @@ fn chapter_content_appears_in_rendered_document() {
for (filename, text) in content {
let path = destination.join(filename);
helpers::assert_contains_strings(path, &[text]);
assert_contains_strings(path, &[text]);
}
}
}

View File

@ -1,13 +1,15 @@
extern crate tempdir;
extern crate mdbook;
extern crate tempdir;
mod helpers;
mod dummy;
use dummy::DummyBook;
use mdbook::MDBook;
#[test]
fn mdbook_can_correctly_test_a_passing_book() {
let temp = helpers::DummyBook::default()
let temp = DummyBook::default()
.with_passing_test(true)
.build();
let mut md = MDBook::new(temp.path());
@ -17,10 +19,10 @@ fn mdbook_can_correctly_test_a_passing_book() {
#[test]
fn mdbook_detects_book_with_failing_tests() {
let temp = helpers::DummyBook::default()
let temp = DummyBook::default()
.with_passing_test(false)
.build();
let mut md: MDBook = MDBook::new(temp.path());
assert!(md.test(vec![]).is_err());
}
}