2017-07-10 18:17:19 +08:00
|
|
|
extern crate mdbook;
|
2017-11-16 15:51:12 +08:00
|
|
|
#[macro_use]
|
|
|
|
extern crate pretty_assertions;
|
|
|
|
extern crate select;
|
2018-03-27 07:47:37 +08:00
|
|
|
extern crate tempfile;
|
2017-11-16 15:51:12 +08:00
|
|
|
extern crate walkdir;
|
|
|
|
|
|
|
|
mod dummy_book;
|
|
|
|
|
2018-01-22 06:44:28 +08:00
|
|
|
use dummy_book::{assert_contains_strings, assert_doesnt_contain_strings, DummyBook};
|
2017-11-16 15:51:12 +08:00
|
|
|
|
2018-07-24 01:45:01 +08:00
|
|
|
use mdbook::config::Config;
|
|
|
|
use mdbook::errors::*;
|
|
|
|
use mdbook::utils::fs::{file_to_string, write_file};
|
|
|
|
use mdbook::MDBook;
|
|
|
|
use select::document::Document;
|
|
|
|
use select::predicate::{Class, Name, Predicate};
|
|
|
|
use std::ffi::OsStr;
|
2017-12-10 20:13:46 +08:00
|
|
|
use std::fs;
|
2018-01-07 00:02:23 +08:00
|
|
|
use std::io::Write;
|
2017-11-16 15:51:12 +08:00
|
|
|
use std::path::Path;
|
2018-03-27 07:47:37 +08:00
|
|
|
use tempfile::Builder as TempFileBuilder;
|
2018-07-24 01:45:01 +08:00
|
|
|
use walkdir::{DirEntry, WalkDir};
|
2017-07-10 18:17:19 +08:00
|
|
|
|
2019-05-07 02:20:58 +08:00
|
|
|
const BOOK_ROOT: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/dummy_book");
|
|
|
|
const TOC_TOP_LEVEL: &[&str] = &[
|
2017-11-18 21:22:30 +08:00
|
|
|
"1. First Chapter",
|
|
|
|
"2. Second Chapter",
|
|
|
|
"Conclusion",
|
2018-07-26 01:45:20 +08:00
|
|
|
"Dummy Book",
|
2017-11-18 21:22:30 +08:00
|
|
|
"Introduction",
|
|
|
|
];
|
2019-05-07 02:20:58 +08:00
|
|
|
const TOC_SECOND_LEVEL: &[&str] = &[
|
2019-05-05 22:57:43 +08:00
|
|
|
"1.1. Nested Chapter",
|
|
|
|
"1.2. Includes",
|
|
|
|
"2.1. Nested Chapter",
|
|
|
|
"1.3. Recursive",
|
|
|
|
];
|
2017-11-16 15:51:12 +08:00
|
|
|
|
2017-08-02 22:29:28 +08:00
|
|
|
/// Make sure you can load the dummy book and build it without panicking.
|
2017-07-10 18:17:19 +08:00
|
|
|
#[test]
|
|
|
|
fn build_the_dummy_book() {
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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 make_sure_bottom_level_files_contain_links_to_chapters() {
|
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 dest = temp.path().join("book");
|
2017-11-18 21:22:30 +08:00
|
|
|
let links = vec![
|
|
|
|
r#"href="intro.html""#,
|
2017-11-18 22:16:35 +08:00
|
|
|
r#"href="first/index.html""#,
|
|
|
|
r#"href="first/nested.html""#,
|
|
|
|
r#"href="second.html""#,
|
|
|
|
r#"href="conclusion.html""#,
|
2017-11-18 21:22:30 +08:00
|
|
|
];
|
2017-07-10 18:17:19 +08:00
|
|
|
|
|
|
|
let files_in_bottom_dir = vec!["index.html", "intro.html", "second.html", "conclusion.html"];
|
|
|
|
|
|
|
|
for filename in files_in_bottom_dir {
|
2017-09-02 07:40:39 +08:00
|
|
|
assert_contains_strings(dest.join(filename), &links);
|
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-11-18 21:22:30 +08:00
|
|
|
let links = vec![
|
2018-07-11 21:33:44 +08:00
|
|
|
r#"href="../intro.html""#,
|
|
|
|
r#"href="../first/index.html""#,
|
|
|
|
r#"href="../first/nested.html""#,
|
|
|
|
r#"href="../second.html""#,
|
|
|
|
r#"href="../conclusion.html""#,
|
2017-11-18 21:22:30 +08:00
|
|
|
];
|
2017-07-10 18:17:19 +08:00
|
|
|
|
|
|
|
let files_in_nested_dir = vec!["index.html", "nested.html"];
|
|
|
|
|
|
|
|
for filename in files_in_nested_dir {
|
2017-09-02 07:40:39 +08:00
|
|
|
assert_contains_strings(first.join(filename), &links);
|
2017-07-10 18:17:19 +08:00
|
|
|
}
|
2017-09-02 07:54:57 +08:00
|
|
|
|
2017-11-18 21:22:30 +08:00
|
|
|
assert_contains_strings(
|
|
|
|
first.join("index.html"),
|
2018-07-24 01:45:01 +08:00
|
|
|
&[r##"href="#some-section" id="some-section""##],
|
2017-11-18 21:22:30 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_contains_strings(
|
|
|
|
first.join("nested.html"),
|
2018-07-24 01:45:01 +08:00
|
|
|
&[r##"href="#some-section" id="some-section""##],
|
2017-11-18 21:22:30 +08:00
|
|
|
);
|
2017-07-10 18:17:19 +08:00
|
|
|
}
|
|
|
|
|
2019-01-15 04:49:23 +08:00
|
|
|
#[test]
|
|
|
|
fn check_correct_relative_links_in_print_page() {
|
|
|
|
let temp = DummyBook::new().build().unwrap();
|
|
|
|
let md = MDBook::load(temp.path()).unwrap();
|
|
|
|
md.build().unwrap();
|
|
|
|
|
|
|
|
let first = temp.path().join("book");
|
|
|
|
|
|
|
|
assert_contains_strings(
|
|
|
|
first.join("print.html"),
|
|
|
|
&[r##"<a href="second/../first/nested.html">the first section</a>,"##],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-07-10 18:17:19 +08:00
|
|
|
#[test]
|
|
|
|
fn rendered_code_has_playpen_stuff() {
|
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 nested = temp.path().join("book/first/nested.html");
|
|
|
|
let playpen_class = vec![r#"class="playpen""#];
|
|
|
|
|
2017-09-02 07:40:39 +08:00
|
|
|
assert_contains_strings(nested, &playpen_class);
|
2017-07-10 18:17:19 +08:00
|
|
|
|
|
|
|
let book_js = temp.path().join("book/book.js");
|
2017-09-02 07:40:39 +08:00
|
|
|
assert_contains_strings(book_js, &[".playpen"]);
|
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-26 01:45:20 +08: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-02 07:40:39 +08:00
|
|
|
assert_contains_strings(path, &[text]);
|
2017-07-10 18:17:19 +08:00
|
|
|
}
|
2017-09-02 07:40:39 +08:00
|
|
|
}
|
2017-11-16 15:51:12 +08:00
|
|
|
|
|
|
|
/// Apply a series of predicates to some root predicate, where each
|
|
|
|
/// successive predicate is the descendant of the last one. Similar to how you
|
|
|
|
/// might do `ul.foo li a` in CSS to access all anchor tags in the `foo` list.
|
|
|
|
macro_rules! descendants {
|
|
|
|
($root:expr, $($child:expr),*) => {
|
|
|
|
$root
|
|
|
|
$(
|
|
|
|
.descendant($child)
|
|
|
|
)*
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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 02:20:58 +08: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-03 09:22:49 +08: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)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read the main page (`book/index.html`) and expose it as a DOM which we
|
|
|
|
/// can search with the `select` crate
|
|
|
|
fn root_index_html() -> Result<Document> {
|
2017-11-18 21:22:30 +08:00
|
|
|
let temp = DummyBook::new()
|
|
|
|
.build()
|
|
|
|
.chain_err(|| "Couldn't create the dummy book")?;
|
|
|
|
MDBook::load(temp.path())?
|
|
|
|
.build()
|
|
|
|
.chain_err(|| "Book building failed")?;
|
2017-11-16 15:51:12 +08:00
|
|
|
|
|
|
|
let index_page = temp.path().join("book").join("index.html");
|
|
|
|
let html = file_to_string(&index_page).chain_err(|| "Unable to read index.html")?;
|
|
|
|
|
|
|
|
Ok(Document::from(html.as_str()))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn check_second_toc_level() {
|
|
|
|
let doc = root_index_html().unwrap();
|
|
|
|
let mut should_be = Vec::from(TOC_SECOND_LEVEL);
|
|
|
|
should_be.sort();
|
|
|
|
|
|
|
|
let pred = descendants!(Class("chapter"), Name("li"), Name("li"), Name("a"));
|
|
|
|
|
2018-08-03 09:22:49 +08:00
|
|
|
let mut children_of_children: Vec<_> = doc
|
|
|
|
.find(pred)
|
2017-11-18 21:22:30 +08:00
|
|
|
.map(|elem| elem.text().trim().to_string())
|
|
|
|
.collect();
|
2017-11-16 15:51:12 +08:00
|
|
|
children_of_children.sort();
|
|
|
|
|
|
|
|
assert_eq!(children_of_children, should_be);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn check_first_toc_level() {
|
|
|
|
let doc = root_index_html().unwrap();
|
|
|
|
let mut should_be = Vec::from(TOC_TOP_LEVEL);
|
|
|
|
|
|
|
|
should_be.extend(TOC_SECOND_LEVEL);
|
|
|
|
should_be.sort();
|
|
|
|
|
|
|
|
let pred = descendants!(Class("chapter"), Name("li"), Name("a"));
|
|
|
|
|
2018-08-03 09:22:49 +08:00
|
|
|
let mut children: Vec<_> = doc
|
|
|
|
.find(pred)
|
2017-11-18 21:22:30 +08:00
|
|
|
.map(|elem| elem.text().trim().to_string())
|
|
|
|
.collect();
|
2017-11-16 15:51:12 +08:00
|
|
|
children.sort();
|
|
|
|
|
|
|
|
assert_eq!(children, should_be);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn check_spacers() {
|
|
|
|
let doc = root_index_html().unwrap();
|
|
|
|
let should_be = 1;
|
|
|
|
|
2018-08-03 09:22:49 +08:00
|
|
|
let num_spacers = doc
|
|
|
|
.find(Class("chapter").descendant(Name("li").and(Class("spacer"))))
|
2017-11-18 21:22:30 +08:00
|
|
|
.count();
|
2017-11-16 15:51:12 +08:00
|
|
|
assert_eq!(num_spacers, should_be);
|
|
|
|
}
|
2017-12-03 14:53:05 +08:00
|
|
|
|
|
|
|
/// Ensure building fails if `create-missing` is false and one of the files does
|
|
|
|
/// not exist.
|
|
|
|
#[test]
|
|
|
|
fn failure_on_missing_file() {
|
2017-12-11 07:32:35 +08:00
|
|
|
let temp = DummyBook::new().build().unwrap();
|
|
|
|
fs::remove_file(temp.path().join("src").join("intro.md")).unwrap();
|
|
|
|
|
|
|
|
let mut cfg = Config::default();
|
|
|
|
cfg.build.create_missing = false;
|
2017-12-03 14:53:05 +08:00
|
|
|
|
2017-12-11 07:32:35 +08:00
|
|
|
let got = MDBook::load_with_config(temp.path(), cfg);
|
|
|
|
assert!(got.is_err());
|
2017-12-03 14:53:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Ensure a missing file is created if `create-missing` is true.
|
|
|
|
#[test]
|
|
|
|
fn create_missing_file_with_config() {
|
2017-11-18 23:21:59 +08:00
|
|
|
let temp = DummyBook::new().build().unwrap();
|
2017-12-10 20:13:46 +08:00
|
|
|
fs::remove_file(temp.path().join("src").join("intro.md")).unwrap();
|
2017-11-18 23:21:59 +08:00
|
|
|
|
2017-12-11 07:32:35 +08:00
|
|
|
let mut cfg = Config::default();
|
|
|
|
cfg.build.create_missing = true;
|
|
|
|
|
|
|
|
assert!(!temp.path().join("src").join("intro.md").exists());
|
|
|
|
let _md = MDBook::load_with_config(temp.path(), cfg).unwrap();
|
|
|
|
assert!(temp.path().join("src").join("intro.md").exists());
|
2017-12-03 14:53:05 +08:00
|
|
|
}
|
|
|
|
|
2017-11-18 23:21:59 +08:00
|
|
|
/// This makes sure you can include a Rust file with `{{#playpen example.rs}}`.
|
|
|
|
/// Specification is in `book-example/src/format/rust.md`
|
|
|
|
#[test]
|
2018-01-22 06:44:28 +08:00
|
|
|
fn able_to_include_playpen_files_in_chapters() {
|
2017-12-03 14:53:05 +08:00
|
|
|
let temp = DummyBook::new().build().unwrap();
|
2018-01-07 22:10:48 +08:00
|
|
|
let md = MDBook::load(temp.path()).unwrap();
|
2017-11-18 23:21:59 +08:00
|
|
|
md.build().unwrap();
|
2017-12-03 14:53:05 +08:00
|
|
|
|
2017-11-18 23:21:59 +08:00
|
|
|
let second = temp.path().join("book/second.html");
|
2017-12-03 14:53:05 +08:00
|
|
|
|
2017-11-18 23:21:59 +08:00
|
|
|
let playpen_strings = &[
|
|
|
|
r#"class="playpen""#,
|
|
|
|
r#"println!("Hello World!");"#,
|
|
|
|
];
|
2018-01-22 06:44:28 +08:00
|
|
|
|
|
|
|
assert_contains_strings(&second, playpen_strings);
|
|
|
|
assert_doesnt_contain_strings(&second, &["{{#playpen example.rs}}"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This makes sure you can include a Rust file with `{{#include ../SUMMARY.md}}`.
|
|
|
|
#[test]
|
|
|
|
fn able_to_include_files_in_chapters() {
|
|
|
|
let temp = DummyBook::new().build().unwrap();
|
|
|
|
let md = MDBook::load(temp.path()).unwrap();
|
|
|
|
md.build().unwrap();
|
|
|
|
|
|
|
|
let includes = temp.path().join("book/first/includes.html");
|
|
|
|
|
|
|
|
let summary_strings = &["<h1>Summary</h1>", ">First Chapter</a>"];
|
|
|
|
assert_contains_strings(&includes, summary_strings);
|
|
|
|
|
|
|
|
assert_doesnt_contain_strings(&includes, &["{{#include ../SUMMARY.md::}}"]);
|
2017-12-03 14:53:05 +08:00
|
|
|
}
|
2017-12-04 15:38:57 +08:00
|
|
|
|
2018-05-20 18:36:19 +08:00
|
|
|
/// Ensure cyclic includes are capped so that no exceptions occur
|
|
|
|
#[test]
|
|
|
|
fn recursive_includes_are_capped() {
|
|
|
|
let temp = DummyBook::new().build().unwrap();
|
|
|
|
let md = MDBook::load(temp.path()).unwrap();
|
|
|
|
md.build().unwrap();
|
|
|
|
|
|
|
|
let recursive = temp.path().join("book/first/recursive.html");
|
|
|
|
let content = &["Around the world, around the world
|
|
|
|
Around the world, around the world
|
|
|
|
Around the world, around the world"];
|
|
|
|
assert_contains_strings(&recursive, content);
|
|
|
|
}
|
|
|
|
|
2017-12-04 15:38:57 +08:00
|
|
|
#[test]
|
|
|
|
fn example_book_can_build() {
|
2017-12-10 20:13:46 +08: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 20:13:46 +08:00
|
|
|
}
|
2018-01-07 00:02:23 +08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn book_with_a_reserved_filename_does_not_build() {
|
2018-03-27 07:47:37 +08:00
|
|
|
let tmp_dir = TempFileBuilder::new().prefix("mdBook").tempdir().unwrap();
|
2018-01-07 00:02:23 +08:00
|
|
|
let src_path = tmp_dir.path().join("src");
|
|
|
|
fs::create_dir(&src_path).unwrap();
|
|
|
|
|
|
|
|
let summary_path = src_path.join("SUMMARY.md");
|
|
|
|
let print_path = src_path.join("print.md");
|
|
|
|
|
|
|
|
fs::File::create(print_path).unwrap();
|
|
|
|
let mut summary_file = fs::File::create(summary_path).unwrap();
|
|
|
|
writeln!(summary_file, "[print](print.md)").unwrap();
|
|
|
|
|
2018-01-08 00:25:45 +08:00
|
|
|
let md = MDBook::load(tmp_dir.path()).unwrap();
|
2018-01-07 00:02:23 +08:00
|
|
|
let got = md.build();
|
|
|
|
assert!(got.is_err());
|
|
|
|
}
|
2018-03-07 21:02:06 +08:00
|
|
|
|
2018-05-04 19:41:28 +08:00
|
|
|
#[test]
|
|
|
|
fn by_default_mdbook_use_index_preprocessor_to_convert_readme_to_index() {
|
|
|
|
let temp = DummyBook::new().build().unwrap();
|
|
|
|
let mut cfg = Config::default();
|
2018-07-24 01:45:01 +08:00
|
|
|
cfg.set("book.src", "src2")
|
|
|
|
.expect("Couldn't set config.book.src to \"src2\".");
|
2018-05-04 19:41:28 +08:00
|
|
|
let md = MDBook::load_with_config(temp.path(), cfg).unwrap();
|
|
|
|
md.build().unwrap();
|
|
|
|
|
2018-07-24 01:45:01 +08:00
|
|
|
let first_index = temp.path().join("book").join("first").join("index.html");
|
2018-05-04 19:41:28 +08:00
|
|
|
let expected_strings = vec![
|
2018-07-11 21:33:44 +08:00
|
|
|
r#"href="../first/index.html""#,
|
|
|
|
r#"href="../second/index.html""#,
|
2018-05-04 19:41:28 +08:00
|
|
|
"First README",
|
|
|
|
];
|
|
|
|
assert_contains_strings(&first_index, &expected_strings);
|
2019-05-07 02:20:58 +08:00
|
|
|
assert_doesnt_contain_strings(&first_index, &["README.html"]);
|
2018-05-04 19:41:28 +08:00
|
|
|
|
2018-07-24 01:45:01 +08:00
|
|
|
let second_index = temp.path().join("book").join("second").join("index.html");
|
|
|
|
let unexpected_strings = vec!["Second README"];
|
2018-05-04 19:41:28 +08:00
|
|
|
assert_doesnt_contain_strings(&second_index, &unexpected_strings);
|
|
|
|
}
|
|
|
|
|
2018-05-15 03:52:29 +08:00
|
|
|
#[test]
|
|
|
|
fn theme_dir_overrides_work_correctly() {
|
|
|
|
let book_dir = dummy_book::new_copy_of_example_book().unwrap();
|
|
|
|
let book_dir = book_dir.path();
|
|
|
|
let theme_dir = book_dir.join("theme");
|
|
|
|
|
|
|
|
let mut index = ::mdbook::theme::INDEX.to_vec();
|
|
|
|
index.extend_from_slice(b"\n<!-- This is a modified index.hbs! -->");
|
|
|
|
|
|
|
|
write_file(&theme_dir, "index.hbs", &index).unwrap();
|
|
|
|
|
|
|
|
let md = MDBook::load(book_dir).unwrap();
|
|
|
|
md.build().unwrap();
|
|
|
|
|
|
|
|
let built_index = book_dir.join("book").join("index.html");
|
|
|
|
dummy_book::assert_contains_strings(built_index, &["This is a modified index.hbs!"]);
|
|
|
|
}
|
|
|
|
|
2018-03-07 21:02:06 +08:00
|
|
|
#[cfg(feature = "search")]
|
|
|
|
mod search {
|
|
|
|
extern crate serde_json;
|
2018-07-24 01:45:01 +08:00
|
|
|
use dummy_book::DummyBook;
|
2018-03-07 21:02:06 +08:00
|
|
|
use mdbook::utils::fs::file_to_string;
|
|
|
|
use mdbook::MDBook;
|
2018-07-24 01:45:01 +08:00
|
|
|
use std::fs::File;
|
|
|
|
use std::path::Path;
|
2018-03-07 21:02:06 +08:00
|
|
|
|
|
|
|
fn read_book_index(root: &Path) -> serde_json::Value {
|
|
|
|
let index = root.join("book/searchindex.js");
|
|
|
|
let index = file_to_string(index).unwrap();
|
2019-05-08 06:29:46 +08:00
|
|
|
let index = index.trim_start_matches("Object.assign(window.search, ");
|
|
|
|
let index = index.trim_end_matches(");");
|
2018-03-07 21:02:06 +08:00
|
|
|
serde_json::from_str(&index).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-05-07 02:20:58 +08:00
|
|
|
#[allow(clippy::float_cmp)]
|
2018-03-07 21:02:06 +08:00
|
|
|
fn book_creates_reasonable_search_index() {
|
|
|
|
let temp = DummyBook::new().build().unwrap();
|
|
|
|
let md = MDBook::load(temp.path()).unwrap();
|
|
|
|
md.build().unwrap();
|
|
|
|
|
|
|
|
let index = read_book_index(temp.path());
|
|
|
|
|
2018-06-14 04:15:58 +08:00
|
|
|
let doc_urls = index["doc_urls"].as_array().unwrap();
|
2018-07-24 01:45:01 +08:00
|
|
|
let get_doc_ref =
|
|
|
|
|url: &str| -> String { doc_urls.iter().position(|s| s == url).unwrap().to_string() };
|
2018-06-14 04:15:58 +08:00
|
|
|
|
|
|
|
let first_chapter = get_doc_ref("first/index.html#first-chapter");
|
|
|
|
let introduction = get_doc_ref("intro.html#introduction");
|
|
|
|
let some_section = get_doc_ref("first/index.html#some-section");
|
|
|
|
let summary = get_doc_ref("first/includes.html#summary");
|
|
|
|
let conclusion = get_doc_ref("conclusion.html#conclusion");
|
|
|
|
|
2018-03-07 21:02:06 +08:00
|
|
|
let bodyidx = &index["index"]["index"]["body"]["root"];
|
|
|
|
let textidx = &bodyidx["t"]["e"]["x"]["t"];
|
|
|
|
assert_eq!(textidx["df"], 2);
|
2018-06-14 04:15:58 +08:00
|
|
|
assert_eq!(textidx["docs"][&first_chapter]["tf"], 1.0);
|
|
|
|
assert_eq!(textidx["docs"][&introduction]["tf"], 1.0);
|
2018-03-07 21:02:06 +08:00
|
|
|
|
|
|
|
let docs = &index["index"]["documentStore"]["docs"];
|
2018-06-14 04:15:58 +08:00
|
|
|
assert_eq!(docs[&first_chapter]["body"], "more text.");
|
|
|
|
assert_eq!(docs[&some_section]["body"], "");
|
2018-03-07 21:02:06 +08:00
|
|
|
assert_eq!(
|
2018-06-14 04:15:58 +08:00
|
|
|
docs[&summary]["body"],
|
2019-01-15 04:49:23 +08:00
|
|
|
"Dummy Book Introduction First Chapter Nested Chapter Includes Recursive Second Chapter Nested Chapter Conclusion"
|
2018-03-07 21:02:06 +08:00
|
|
|
);
|
2018-07-24 01:45:01 +08:00
|
|
|
assert_eq!(docs[&summary]["breadcrumbs"], "First Chapter » Summary");
|
|
|
|
assert_eq!(docs[&conclusion]["body"], "I put <HTML> in here!");
|
2018-03-07 21:02:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setting this to `true` may cause issues with `cargo watch`,
|
|
|
|
// since it may not finish writing the fixture before the tests
|
|
|
|
// are run again.
|
2018-06-14 04:15:58 +08:00
|
|
|
const GENERATE_FIXTURE: bool = false;
|
2018-03-07 21:02:06 +08:00
|
|
|
|
|
|
|
fn get_fixture() -> serde_json::Value {
|
|
|
|
if GENERATE_FIXTURE {
|
|
|
|
let temp = DummyBook::new().build().unwrap();
|
|
|
|
let md = MDBook::load(temp.path()).unwrap();
|
|
|
|
md.build().unwrap();
|
|
|
|
|
|
|
|
let src = read_book_index(temp.path());
|
|
|
|
|
|
|
|
let dest = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/searchindex_fixture.json");
|
|
|
|
let dest = File::create(&dest).unwrap();
|
|
|
|
serde_json::to_writer_pretty(dest, &src).unwrap();
|
|
|
|
|
|
|
|
src
|
|
|
|
} else {
|
|
|
|
let json = include_str!("searchindex_fixture.json");
|
|
|
|
serde_json::from_str(json).expect("Unable to deserialize the fixture")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// So you've broken the test. If you changed dummy_book, it's probably
|
|
|
|
// safe to regenerate the fixture. If you haven't then make sure that the
|
|
|
|
// search index still works. Run `cargo run -- serve tests/dummy_book`
|
|
|
|
// and try some searches. Are you getting results? Do the teasers look OK?
|
|
|
|
// Are there new errors in the JS console?
|
|
|
|
//
|
|
|
|
// If you're pretty sure you haven't broken anything, change `GENERATE_FIXTURE`
|
|
|
|
// above to `true`, and run `cargo test` to generate a new fixture. Then
|
2018-06-14 04:15:58 +08:00
|
|
|
// **change it back to `false`**. Include the changed `searchindex_fixture.json` in your commit.
|
2018-03-07 21:02:06 +08:00
|
|
|
#[test]
|
|
|
|
fn search_index_hasnt_changed_accidentally() {
|
|
|
|
let temp = DummyBook::new().build().unwrap();
|
|
|
|
let md = MDBook::load(temp.path()).unwrap();
|
|
|
|
md.build().unwrap();
|
|
|
|
|
|
|
|
let book_index = read_book_index(temp.path());
|
|
|
|
|
|
|
|
let fixture_index = get_fixture();
|
|
|
|
|
|
|
|
// Uncomment this if you're okay with pretty-printing 32KB of JSON
|
|
|
|
//assert_eq!(fixture_index, book_index);
|
|
|
|
|
|
|
|
if book_index != fixture_index {
|
|
|
|
panic!("The search index has changed from the fixture");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|