From 69972080f055de0b97be1318835041cf616ab035 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Tue, 22 Apr 2025 11:23:56 -0700 Subject: [PATCH] Migrate check_link_target_fallback to BookTest --- tests/rendered_output.rs | 55 ---------------------------------------- tests/testsuite/toc.rs | 32 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 55 deletions(-) diff --git a/tests/rendered_output.rs b/tests/rendered_output.rs index f27c436e..c2e7c600 100644 --- a/tests/rendered_output.rs +++ b/tests/rendered_output.rs @@ -2,37 +2,15 @@ mod dummy_book; use crate::dummy_book::{assert_contains_strings, DummyBook}; -use anyhow::Context; use mdbook::config::Config; -use mdbook::errors::*; 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; use walkdir::{DirEntry, WalkDir}; const BOOK_ROOT: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/tests/dummy_book"); -const TOC_TOP_LEVEL: &[&str] = &[ - "1. First Chapter", - "2. Second Chapter", - "Conclusion", - "Dummy Book", - "Introduction", -]; -const TOC_SECOND_LEVEL: &[&str] = &[ - "1.1. Nested Chapter", - "1.2. Includes", - "1.3. Recursive", - "1.4. Markdown", - "1.5. Unicode", - "1.6. No Headers", - "1.7. Duplicate Headers", - "1.8. Heading Attributes", - "2.1. Nested Chapter", -]; #[test] fn by_default_mdbook_generates_rendered_content_in_the_book_directory() { @@ -120,39 +98,6 @@ fn entry_ends_with(entry: &DirEntry, ending: &str) -> bool { entry.file_name().to_string_lossy().ends_with(ending) } -/// Read the TOC fallback (`book/toc.html`) HTML and expose it as a DOM which we -/// can search with the `select` crate -fn toc_fallback_html() -> Result { - let temp = DummyBook::new() - .build() - .with_context(|| "Couldn't create the dummy book")?; - MDBook::load(temp.path())? - .build() - .with_context(|| "Book building failed")?; - - let toc_path = temp.path().join("book").join("toc.html"); - let html = fs::read_to_string(toc_path).with_context(|| "Unable to read index.html")?; - Ok(Document::from(html.as_str())) -} - -// don't use target="_parent" in IFRAME -#[test] -fn check_link_target_fallback() { - let doc = toc_fallback_html().unwrap(); - - let num_parent_links = doc - .find( - Class("chapter") - .descendant(Name("li")) - .descendant(Name("a").and(Attr("target", "_parent"))), - ) - .count(); - assert_eq!( - num_parent_links, - TOC_TOP_LEVEL.len() + TOC_SECOND_LEVEL.len() - ); -} - #[test] fn example_book_can_build() { let example_book_dir = dummy_book::new_copy_of_example_book().unwrap(); diff --git a/tests/testsuite/toc.rs b/tests/testsuite/toc.rs index 4d4743a8..77021f66 100644 --- a/tests/testsuite/toc.rs +++ b/tests/testsuite/toc.rs @@ -1,8 +1,11 @@ //! Tests for table of contents (sidebar). use crate::prelude::*; +use anyhow::Context; +use mdbook::errors::*; use select::document::Document; use select::predicate::{Attr, Class, Name, Predicate}; +use std::fs; const TOC_TOP_LEVEL: &[&str] = &[ "1. With Readme", @@ -40,6 +43,17 @@ fn toc_js_html() -> Document { Document::from(html.as_str()) } +/// Read the TOC fallback (`book/toc.html`) HTML and expose it as a DOM which we +/// can search with the `select` crate +fn toc_fallback_html() -> Result { + let mut test = BookTest::from_dir("toc/basic_toc"); + test.build(); + + let toc_path = test.dir.join("book").join("toc.html"); + let html = fs::read_to_string(toc_path).with_context(|| "Unable to read index.html")?; + Ok(Document::from(html.as_str())) +} + #[test] fn check_second_toc_level() { let doc = toc_js_html(); @@ -110,3 +124,21 @@ fn check_link_target_js() { .count(); assert_eq!(num_parent_links, 0); } + +// don't use target="_parent" in IFRAME +#[test] +fn check_link_target_fallback() { + let doc = toc_fallback_html().unwrap(); + + let num_parent_links = doc + .find( + Class("chapter") + .descendant(Name("li")) + .descendant(Name("a").and(Attr("target", "_parent"))), + ) + .count(); + assert_eq!( + num_parent_links, + TOC_TOP_LEVEL.len() + TOC_SECOND_LEVEL.len() + ); +}