2017-11-16 15:51:12 +08:00
|
|
|
mod dummy_book;
|
|
|
|
|
|
2025-04-21 19:25:44 -07:00
|
|
|
use crate::dummy_book::{assert_contains_strings, DummyBook};
|
2017-11-16 15:51:12 +08:00
|
|
|
|
2018-07-23 12:45:01 -05:00
|
|
|
use mdbook::config::Config;
|
2025-04-22 09:06:06 -07:00
|
|
|
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;
|
2017-12-10 23:13:46 +11:00
|
|
|
use std::fs;
|
2025-04-22 09:06:06 -07:00
|
|
|
use std::path::Path;
|
2018-07-23 12:45:01 -05:00
|
|
|
use walkdir::{DirEntry, WalkDir};
|
2017-07-10 18:17:19 +08:00
|
|
|
|
2019-05-07 01:20:58 +07:00
|
|
|
const BOOK_ROOT: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/dummy_book");
|
2017-11-16 15:51:12 +08:00
|
|
|
|
2017-07-10 18:17:19 +08:00
|
|
|
#[test]
|
|
|
|
|
fn by_default_mdbook_generates_rendered_content_in_the_book_directory() {
|
2017-11-16 15:51:12 +08:00
|
|
|
let temp = DummyBook::new().build().unwrap();
|
2018-01-07 22:10:48 +08:00
|
|
|
let md = MDBook::load(temp.path()).unwrap();
|
2017-07-10 18:17:19 +08:00
|
|
|
|
|
|
|
|
assert!(!temp.path().join("book").exists());
|
|
|
|
|
md.build().unwrap();
|
|
|
|
|
|
|
|
|
|
assert!(temp.path().join("book").exists());
|
2018-01-22 06:44:28 +08:00
|
|
|
let index_file = md.build_dir_for("html").join("index.html");
|
|
|
|
|
assert!(index_file.exists());
|
2017-07-10 18:17:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn check_correct_cross_links_in_nested_dir() {
|
2017-11-16 15:51:12 +08:00
|
|
|
let temp = DummyBook::new().build().unwrap();
|
2018-01-07 22:10:48 +08:00
|
|
|
let md = MDBook::load(temp.path()).unwrap();
|
2017-07-10 18:17:19 +08:00
|
|
|
md.build().unwrap();
|
|
|
|
|
|
|
|
|
|
let first = temp.path().join("book").join("first");
|
2017-09-01 16:54:57 -07:00
|
|
|
|
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
|
|
|
);
|
2017-07-10 18:17:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn chapter_content_appears_in_rendered_document() {
|
2017-11-18 21:22:30 +08:00
|
|
|
let content = vec![
|
2018-07-25 12:45:20 -05:00
|
|
|
("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"),
|
|
|
|
|
];
|
2017-07-10 18:17:19 +08:00
|
|
|
|
2017-11-16 15:51:12 +08:00
|
|
|
let temp = DummyBook::new().build().unwrap();
|
2018-01-07 22:10:48 +08:00
|
|
|
let md = MDBook::load(temp.path()).unwrap();
|
2017-07-10 18:17:19 +08:00
|
|
|
md.build().unwrap();
|
|
|
|
|
|
|
|
|
|
let destination = temp.path().join("book");
|
|
|
|
|
|
|
|
|
|
for (filename, text) in content {
|
|
|
|
|
let path = destination.join(filename);
|
2017-09-01 16:40:39 -07:00
|
|
|
assert_contains_strings(path, &[text]);
|
2017-07-10 18:17:19 +08:00
|
|
|
}
|
2017-09-01 16:40:39 -07:00
|
|
|
}
|
2017-11-16 15:51:12 +08:00
|
|
|
|
|
|
|
|
/// 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"))
|
2019-05-07 01:20:58 +07:00
|
|
|
.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"));
|
2017-11-16 15:51:12 +08:00
|
|
|
|
|
|
|
|
for chapter in chapter_files {
|
2018-08-02 20:22:49 -05:00
|
|
|
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()
|
|
|
|
|
);
|
2017-11-16 15:51:12 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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() {
|
2017-12-10 23:13:46 +11:00
|
|
|
let example_book_dir = dummy_book::new_copy_of_example_book().unwrap();
|
2017-12-04 15:38:57 +08:00
|
|
|
|
2018-01-07 22:10:48 +08:00
|
|
|
let md = MDBook::load(example_book_dir.path()).unwrap();
|
2017-12-04 15:38:57 +08:00
|
|
|
|
2018-01-07 22:10:48 +08:00
|
|
|
md.build().unwrap();
|
2017-12-10 23:13:46 +11:00
|
|
|
}
|