Migrate with_no_source_path to BookTest

This commit is contained in:
Eric Huss 2025-04-22 09:06:06 -07:00
parent 8bfa6462f8
commit 2056c87e28
2 changed files with 24 additions and 22 deletions

View file

@ -3,17 +3,16 @@ mod dummy_book;
use crate::dummy_book::{assert_contains_strings, DummyBook};
use anyhow::Context;
use mdbook::book::Chapter;
use mdbook::config::Config;
use mdbook::errors::*;
use mdbook::utils::fs::write_file;
use mdbook::{BookItem, MDBook};
use mdbook::MDBook;
use pretty_assertions::assert_eq;
use select::document::Document;
use select::predicate::{Attr, Class, Name, Predicate};
use std::ffi::OsStr;
use std::fs;
use std::path::{Path, PathBuf};
use std::path::Path;
use std::str::FromStr;
use tempfile::Builder as TempFileBuilder;
use walkdir::{DirEntry, WalkDir};
@ -508,21 +507,3 @@ fn custom_fonts() {
&["fonts.css", "myfont.woff"]
);
}
#[test]
fn with_no_source_path() {
// Test for a regression where search would fail if source_path is None.
let temp = DummyBook::new().build().unwrap();
let mut md = MDBook::load(temp.path()).unwrap();
let chapter = Chapter {
name: "Sample chapter".to_string(),
content: "".to_string(),
number: None,
sub_items: Vec::new(),
path: Some(PathBuf::from("sample.html")),
source_path: None,
parent_names: Vec::new(),
};
md.book.sections.push(BookItem::Chapter(chapter));
md.build().unwrap();
}

View file

@ -1,8 +1,10 @@
//! Tests for search support.
use crate::prelude::*;
use mdbook::book::Chapter;
use mdbook::BookItem;
use snapbox::file;
use std::path::Path;
use std::path::{Path, PathBuf};
fn read_book_index(root: &Path) -> serde_json::Value {
let index = root.join("book/searchindex.js");
@ -104,3 +106,22 @@ fn can_disable_individual_chapters() {
assert!(!contains("first/disable_me.html"));
assert!(contains("first/keep_me.html"));
}
// Test for a regression where search would fail if source_path is None.
// https://github.com/rust-lang/mdBook/pull/2550
#[test]
fn with_no_source_path() {
let test = BookTest::from_dir("search/reasonable_search_index");
let mut book = test.load_book();
let chapter = Chapter {
name: "Sample chapter".to_string(),
content: "".to_string(),
number: None,
sub_items: Vec::new(),
path: Some(PathBuf::from("sample.html")),
source_path: None,
parent_names: Vec::new(),
};
book.book.sections.push(BookItem::Chapter(chapter));
book.build().unwrap();
}