From a91e8885755bfc1f39a9b2edd7af0d37cd89b4ee Mon Sep 17 00:00:00 2001 From: ISSOtm Date: Wed, 22 Jun 2022 22:55:52 +0200 Subject: [PATCH] Add test for index page --- tests/dummy_book/index_html_test/SUMMARY.md | 11 +++++++ tests/dummy_book/index_html_test/chapter_1.md | 1 + tests/rendered_output.rs | 30 ++++++++++++++++++- 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 tests/dummy_book/index_html_test/SUMMARY.md create mode 100644 tests/dummy_book/index_html_test/chapter_1.md diff --git a/tests/dummy_book/index_html_test/SUMMARY.md b/tests/dummy_book/index_html_test/SUMMARY.md new file mode 100644 index 00000000..37bf68cd --- /dev/null +++ b/tests/dummy_book/index_html_test/SUMMARY.md @@ -0,0 +1,11 @@ +# Summary + +--- + +- [None of these should be treated as the "index chapter"]() + +# Part 1 + +- [Not this either]() +- [Chapter 1](./chapter_1.md) +- [And not this]() diff --git a/tests/dummy_book/index_html_test/chapter_1.md b/tests/dummy_book/index_html_test/chapter_1.md new file mode 100644 index 00000000..b743fda3 --- /dev/null +++ b/tests/dummy_book/index_html_test/chapter_1.md @@ -0,0 +1 @@ +# Chapter 1 diff --git a/tests/rendered_output.rs b/tests/rendered_output.rs index c6267830..24d3427b 100644 --- a/tests/rendered_output.rs +++ b/tests/rendered_output.rs @@ -15,7 +15,7 @@ use select::predicate::{Class, Name, Predicate}; use std::collections::HashMap; use std::ffi::OsStr; use std::fs; -use std::io::Write; +use std::io::{Read, Write}; use std::path::{Component, Path, PathBuf}; use std::str::FromStr; use tempfile::Builder as TempFileBuilder; @@ -467,6 +467,34 @@ fn by_default_mdbook_use_index_preprocessor_to_convert_readme_to_index() { assert_doesnt_contain_strings(&second_index, &unexpected_strings); } +#[test] +fn first_chapter_is_copied_as_index_even_if_not_first_elem() { + let temp = DummyBook::new().build().unwrap(); + let mut cfg = Config::default(); + cfg.set("book.src", "index_html_test") + .expect("Couldn't set config.book.src to \"index_html_test\""); + let md = MDBook::load_with_config(temp.path(), cfg).unwrap(); + md.build().unwrap(); + + // In theory, just reading the entire files into memory and comparing is sufficient for *testing*, + // but since the files are temporary and get deleted when the test completes, we'll want to print + // the differences on failure. + // We could invoke `diff` on the files on failure, but that may not be portable (hi, Windows...) + // so we'll do the job ourselves—potentially a bit sloppily, but that can always be piped into + // `diff` manually afterwards. + let book_path = temp.path().join("book"); + let read_file = |path: &str| { + let mut buf = String::new(); + fs::File::open(book_path.join(path)) + .with_context(|| format!("Failed to read {}", path)) + .unwrap() + .read_to_string(&mut buf) + .unwrap(); + buf + }; + pretty_assertions::assert_eq!(read_file("chapter_1.html"), read_file("index.html")); +} + #[test] fn theme_dir_overrides_work_correctly() { let book_dir = dummy_book::new_copy_of_example_book().unwrap();