Migrate by_default_mdbook_use_index_preprocessor_to_convert_readme_to_index to BookTest

This commit is contained in:
Eric Huss 2025-04-21 18:53:36 -07:00
parent 0f397ebdb5
commit c6d9f15cba
8 changed files with 50 additions and 20 deletions

View file

@ -328,26 +328,6 @@ fn example_book_can_build() {
md.build().unwrap();
}
#[test]
fn by_default_mdbook_use_index_preprocessor_to_convert_readme_to_index() {
let temp = DummyBook::new().build().unwrap();
let mut cfg = Config::default();
cfg.set("book.src", "src2")
.expect("Couldn't set config.book.src to \"src2\".");
let md = MDBook::load_with_config(temp.path(), cfg).unwrap();
md.build().unwrap();
let first_index = temp.path().join("book").join("toc.js");
let expected_strings = vec![
r#"href="first/index.html""#,
r#"href="second/index.html""#,
"1st README",
"2nd README",
];
assert_contains_strings(&first_index, &expected_strings);
assert_doesnt_contain_strings(&first_index, &["README.html", "Second README"]);
}
#[test]
fn first_chapter_is_copied_as_index_even_if_not_first_elem() {
let temp = DummyBook::new().build().unwrap();

38
tests/testsuite/index.rs Normal file
View file

@ -0,0 +1,38 @@
//! Tests for the index preprocessor.
use crate::prelude::*;
// Checks basic README to index.html conversion.
#[test]
fn readme_to_index() {
let mut test = BookTest::from_dir("index/basic_readme");
test.check_main_file(
"book/index.html",
str![[r##"<h1 id="intro"><a class="header" href="#intro">Intro</a></h1>"##]],
)
.check_main_file(
"book/first/index.html",
str![[r##"<h1 id="first"><a class="header" href="#first">First</a></h1>"##]],
)
.check_main_file(
"book/second/index.html",
str![[r##"<h1 id="second"><a class="header" href="#second">Second</a></h1>"##]],
)
.check_toc_js(str![[r#"
<ol class="chapter">
<li class="chapter-item expanded affix ">
<a href="index.html">Intro</a>
</li>
<li class="chapter-item expanded ">
<a href="first/index.html">
<strong aria-hidden="true">1.</strong> First</a>
</li>
<li class="chapter-item expanded ">
<a href="second/index.html">
<strong aria-hidden="true">2.</strong> Second</a>
</li>
</ol>
"#]]);
assert!(test.dir.join("book/index.html").exists());
assert!(!test.dir.join("book/README.html").exists());
}

View file

@ -0,0 +1,2 @@
[book]
title = "basic_readme"

View file

@ -0,0 +1 @@
# Intro

View file

@ -0,0 +1,6 @@
# Summary
[Intro](./README.md)
- [First](first/README)
- [Second](second/Readme.md)

View file

@ -0,0 +1 @@
# First

View file

@ -0,0 +1 @@
# Second

View file

@ -6,6 +6,7 @@ mod book_test;
mod build;
mod cli;
mod includes;
mod index;
mod prelude {
pub use crate::book_test::BookTest;