Migrate can_disable_individual_chapters to BookTest

This commit is contained in:
Eric Huss 2025-04-22 09:04:55 -07:00
parent a8660048ca
commit 8bfa6462f8
8 changed files with 34 additions and 38 deletions

View file

@ -381,44 +381,6 @@ mod search {
use crate::dummy_book::DummyBook;
use mdbook::utils::fs::write_file;
use mdbook::MDBook;
use std::fs;
use std::path::Path;
fn read_book_index(root: &Path) -> serde_json::Value {
let index = root.join("book/searchindex.js");
let index = fs::read_to_string(index).unwrap();
let index = index.trim_start_matches("window.search = JSON.parse('");
let index = index.trim_end_matches("');");
// We need unescape the string as it's supposed to be an escaped JS string.
serde_json::from_str(&index.replace("\\'", "'").replace("\\\\", "\\")).unwrap()
}
#[test]
fn can_disable_individual_chapters() {
let temp = DummyBook::new().build().unwrap();
let book_toml = r#"
[book]
title = "Search Test"
[output.html.search.chapter]
"second" = { enable = false }
"first/unicode.md" = { enable = false }
"#;
write_file(temp.path(), "book.toml", book_toml.as_bytes()).unwrap();
let md = MDBook::load(temp.path()).unwrap();
md.build().unwrap();
let index = read_book_index(temp.path());
let doc_urls = index["doc_urls"].as_array().unwrap();
let contains = |path| {
doc_urls
.iter()
.any(|p| p.as_str().unwrap().starts_with(path))
};
assert!(contains("second.html"));
assert!(!contains("second/"));
assert!(!contains("first/unicode.html"));
assert!(contains("first/markdown.html"));
}
#[test]
fn chapter_settings_validation_error() {

View file

@ -86,3 +86,21 @@ fn search_index_hasnt_changed_accidentally() {
file!["search/reasonable_search_index/expected_index.js"],
);
}
// Ability to disable search chapters.
#[test]
fn can_disable_individual_chapters() {
let mut test = BookTest::from_dir("search/disable_search_chapter");
test.build();
let index = read_book_index(&test.dir);
let doc_urls = index["doc_urls"].as_array().unwrap();
let contains = |path| {
doc_urls
.iter()
.any(|p| p.as_str().unwrap().starts_with(path))
};
assert!(contains("second.html"));
assert!(!contains("second/"));
assert!(!contains("first/disable_me.html"));
assert!(contains("first/keep_me.html"));
}

View file

@ -0,0 +1,6 @@
[book]
title = "disable_search_chapter"
[output.html.search.chapter]
"second" = { enable = false }
"first/disable_me.md" = { enable = false }

View file

@ -0,0 +1,6 @@
# Summary
- [Keep Me](first/keep_me.md)
- [Disable Me](first/disable_me.md)
- [Second](second.md)
- [Second Nested](second/nested.md)

View file

@ -0,0 +1 @@
# Disable Me

View file

@ -0,0 +1 @@
# Keep Me

View file

@ -0,0 +1 @@
# Second

View file

@ -0,0 +1 @@
# Second Nested