2017-11-16 15:51:12 +08:00
|
|
|
//! 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_variables, unused_imports, unused_extern_crates)]
|
|
|
|
|
2020-05-21 05:32:00 +08:00
|
|
|
use anyhow::Context;
|
2017-11-16 15:51:12 +08:00
|
|
|
use mdbook::errors::*;
|
2020-05-21 05:32:00 +08:00
|
|
|
use mdbook::MDBook;
|
2018-07-24 01:45:01 +08:00
|
|
|
use std::fs::{self, File};
|
|
|
|
use std::io::{Read, Write};
|
|
|
|
use std::path::Path;
|
2019-05-26 02:50:41 +08:00
|
|
|
use tempfile::{Builder as TempFileBuilder, TempDir};
|
|
|
|
use walkdir::WalkDir;
|
2017-11-16 15:51:12 +08:00
|
|
|
|
|
|
|
/// Create a dummy book in a temporary directory, using the contents of
|
|
|
|
/// `SUMMARY_MD` as a guide.
|
|
|
|
///
|
|
|
|
/// The "Nested Chapter" file contains a code block with a single
|
|
|
|
/// `assert!($TEST_STATUS)`. If you want to check MDBook's testing
|
|
|
|
/// functionality, `$TEST_STATUS` can be substitute for either `true` or
|
|
|
|
/// `false`. This is done using the `passing_test` parameter.
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
|
|
pub struct DummyBook {
|
|
|
|
passing_test: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DummyBook {
|
|
|
|
/// Create a new `DummyBook` with all the defaults.
|
|
|
|
pub fn new() -> DummyBook {
|
|
|
|
DummyBook { passing_test: true }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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 DummyBook {
|
|
|
|
self.passing_test = test_passes;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Write a book to a temporary directory using the provided settings.
|
|
|
|
pub fn build(&self) -> Result<TempDir> {
|
2018-07-24 01:45:01 +08:00
|
|
|
let temp = TempFileBuilder::new()
|
2018-07-26 01:17:17 +08:00
|
|
|
.prefix("dummy_book-")
|
2018-07-24 01:45:01 +08:00
|
|
|
.tempdir()
|
2020-05-21 05:32:00 +08:00
|
|
|
.with_context(|| "Unable to create temp directory")?;
|
2017-11-16 15:51:12 +08:00
|
|
|
|
|
|
|
let dummy_book_root = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/dummy_book");
|
2020-05-21 05:32:00 +08:00
|
|
|
recursive_copy(&dummy_book_root, temp.path()).with_context(|| {
|
2018-07-24 01:45:01 +08:00
|
|
|
"Couldn't copy files into a \
|
|
|
|
temporary directory"
|
|
|
|
})?;
|
2017-11-16 15:51:12 +08:00
|
|
|
|
|
|
|
let sub_pattern = if self.passing_test { "true" } else { "false" };
|
2019-10-06 06:27:03 +08:00
|
|
|
let files_containing_tests = [
|
|
|
|
"src/first/nested.md",
|
|
|
|
"src/first/nested-test.rs",
|
|
|
|
"src/first/nested-test-with-anchors.rs",
|
|
|
|
"src/first/partially-included-test.rs",
|
|
|
|
"src/first/partially-included-test-with-anchors.rs",
|
|
|
|
];
|
2019-03-01 09:44:55 +08:00
|
|
|
for file in &files_containing_tests {
|
|
|
|
let path_containing_tests = temp.path().join(file);
|
|
|
|
replace_pattern_in_file(&path_containing_tests, "$TEST_STATUS", sub_pattern)?;
|
|
|
|
}
|
2017-11-16 15:51:12 +08:00
|
|
|
|
|
|
|
Ok(temp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn replace_pattern_in_file(filename: &Path, from: &str, to: &str) -> Result<()> {
|
2019-06-20 10:49:18 +08:00
|
|
|
let contents = fs::read_to_string(filename)?;
|
2017-11-16 15:51:12 +08:00
|
|
|
File::create(filename)?.write_all(contents.replace(from, to).as_bytes())?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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();
|
2019-06-20 10:49:18 +08:00
|
|
|
let content = fs::read_to_string(filename).expect("Couldn't read the file's contents");
|
2017-11-16 15:51:12 +08:00
|
|
|
|
|
|
|
for s in strings {
|
2018-07-24 01:45:01 +08:00
|
|
|
assert!(
|
|
|
|
content.contains(s),
|
|
|
|
"Searching for {:?} in {}\n\n{}",
|
|
|
|
s,
|
|
|
|
filename.display(),
|
|
|
|
content
|
|
|
|
);
|
2017-11-16 15:51:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-22 06:44:28 +08:00
|
|
|
pub fn assert_doesnt_contain_strings<P: AsRef<Path>>(filename: P, strings: &[&str]) {
|
|
|
|
let filename = filename.as_ref();
|
2019-06-20 10:49:18 +08:00
|
|
|
let content = fs::read_to_string(filename).expect("Couldn't read the file's contents");
|
2018-01-22 06:44:28 +08:00
|
|
|
|
|
|
|
for s in strings {
|
2018-07-24 01:45:01 +08:00
|
|
|
assert!(
|
|
|
|
!content.contains(s),
|
|
|
|
"Found {:?} in {}\n\n{}",
|
|
|
|
s,
|
|
|
|
filename.display(),
|
|
|
|
content
|
|
|
|
);
|
2018-01-22 06:44:28 +08:00
|
|
|
}
|
|
|
|
}
|
2017-11-16 15:51:12 +08:00
|
|
|
|
|
|
|
/// Recursively copy an entire directory tree to somewhere else (a la `cp -r`).
|
|
|
|
fn recursive_copy<A: AsRef<Path>, B: AsRef<Path>>(from: A, to: B) -> Result<()> {
|
|
|
|
let from = from.as_ref();
|
|
|
|
let to = to.as_ref();
|
|
|
|
|
2023-05-14 00:44:11 +08:00
|
|
|
for entry in WalkDir::new(from) {
|
2020-05-21 05:32:00 +08:00
|
|
|
let entry = entry.with_context(|| "Unable to inspect directory entry")?;
|
2017-11-16 15:51:12 +08:00
|
|
|
|
|
|
|
let original_location = entry.path();
|
2018-07-24 01:45:01 +08:00
|
|
|
let relative = original_location
|
2023-05-14 00:44:11 +08:00
|
|
|
.strip_prefix(from)
|
2018-07-24 01:45:01 +08:00
|
|
|
.expect("`original_location` is inside the `from` directory");
|
2017-11-16 15:51:12 +08:00
|
|
|
let new_location = to.join(relative);
|
|
|
|
|
|
|
|
if original_location.is_file() {
|
|
|
|
if let Some(parent) = new_location.parent() {
|
2020-05-21 05:32:00 +08:00
|
|
|
fs::create_dir_all(parent).with_context(|| "Couldn't create directory")?;
|
2017-11-16 15:51:12 +08:00
|
|
|
}
|
|
|
|
|
2023-05-14 00:44:11 +08:00
|
|
|
fs::copy(original_location, &new_location)
|
2020-05-21 05:32:00 +08:00
|
|
|
.with_context(|| "Unable to copy file contents")?;
|
2017-11-16 15:51:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2017-12-04 15:38:57 +08:00
|
|
|
|
|
|
|
pub fn new_copy_of_example_book() -> Result<TempDir> {
|
2020-09-23 09:16:09 +08:00
|
|
|
let temp = TempFileBuilder::new().prefix("guide").tempdir()?;
|
2023-12-30 02:18:48 +08:00
|
|
|
let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
|
2018-07-24 01:45:01 +08:00
|
|
|
|
2023-12-30 02:18:48 +08:00
|
|
|
recursive_copy(manifest_dir.join("guide"), temp.path().join("guide"))?;
|
|
|
|
recursive_copy(manifest_dir.join("examples"), temp.path().join("examples"))?;
|
2017-12-04 15:38:57 +08:00
|
|
|
|
|
|
|
Ok(temp)
|
2018-03-27 07:47:37 +08:00
|
|
|
}
|