|
| 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 | +} |
0 commit comments