mdbook/tests/rendered_output.rs

109 lines
3.2 KiB
Rust
Raw Normal View History

mod dummy_book;
use crate::dummy_book::{assert_contains_strings, DummyBook};
2018-07-23 12:45:01 -05:00
use mdbook::config::Config;
use mdbook::MDBook;
2023-01-15 11:42:46 -08:00
use pretty_assertions::assert_eq;
2018-07-23 12:45:01 -05:00
use std::ffi::OsStr;
use std::fs;
use std::path::Path;
2018-07-23 12:45:01 -05:00
use walkdir::{DirEntry, WalkDir};
const BOOK_ROOT: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/dummy_book");
#[test]
fn by_default_mdbook_generates_rendered_content_in_the_book_directory() {
let temp = DummyBook::new().build().unwrap();
let md = MDBook::load(temp.path()).unwrap();
assert!(!temp.path().join("book").exists());
md.build().unwrap();
assert!(temp.path().join("book").exists());
let index_file = md.build_dir_for("html").join("index.html");
assert!(index_file.exists());
}
#[test]
fn check_correct_cross_links_in_nested_dir() {
let temp = DummyBook::new().build().unwrap();
let md = MDBook::load(temp.path()).unwrap();
md.build().unwrap();
let first = temp.path().join("book").join("first");
2017-11-18 21:22:30 +08:00
assert_contains_strings(
first.join("index.html"),
2023-05-28 11:32:31 -07:00
&[r##"<h2 id="some-section"><a class="header" href="#some-section">"##],
2017-11-18 21:22:30 +08:00
);
assert_contains_strings(
first.join("nested.html"),
2023-05-28 11:32:31 -07:00
&[r##"<h2 id="some-section"><a class="header" href="#some-section">"##],
2017-11-18 21:22:30 +08:00
);
}
#[test]
fn chapter_content_appears_in_rendered_document() {
2017-11-18 21:22:30 +08:00
let content = vec![
("index.html", "This file is just here to cause the"),
("intro.html", "Here's some interesting text"),
2017-11-18 21:22:30 +08:00
("second.html", "Second Chapter"),
("first/nested.html", "testable code"),
("first/index.html", "more text"),
("conclusion.html", "Conclusion"),
];
let temp = DummyBook::new().build().unwrap();
let md = MDBook::load(temp.path()).unwrap();
md.build().unwrap();
let destination = temp.path().join("book");
for (filename, text) in content {
let path = destination.join(filename);
assert_contains_strings(path, &[text]);
}
}
/// Make sure that all `*.md` files (excluding `SUMMARY.md`) were rendered
/// and placed in the `book` directory with their extensions set to `*.html`.
#[test]
fn chapter_files_were_rendered_to_html() {
let temp = DummyBook::new().build().unwrap();
let src = Path::new(BOOK_ROOT).join("src");
2017-11-18 21:22:30 +08:00
let chapter_files = WalkDir::new(&src)
.into_iter()
.filter_entry(|entry| entry_ends_with(entry, ".md"))
.filter_map(std::result::Result::ok)
2017-11-18 21:22:30 +08:00
.map(|entry| entry.path().to_path_buf())
.filter(|path| path.file_name().and_then(OsStr::to_str) != Some("SUMMARY.md"));
for chapter in chapter_files {
let rendered_location = temp
.path()
2017-11-18 21:22:30 +08:00
.join(chapter.strip_prefix(&src).unwrap())
.with_extension("html");
assert!(
rendered_location.exists(),
"{} doesn't exits",
rendered_location.display()
);
}
}
fn entry_ends_with(entry: &DirEntry, ending: &str) -> bool {
entry.file_name().to_string_lossy().ends_with(ending)
}
2017-12-04 15:38:57 +08:00
#[test]
fn example_book_can_build() {
let example_book_dir = dummy_book::new_copy_of_example_book().unwrap();
2017-12-04 15:38:57 +08:00
let md = MDBook::load(example_book_dir.path()).unwrap();
2017-12-04 15:38:57 +08:00
md.build().unwrap();
}