diff --git a/tests/rendered_output.rs b/tests/rendered_output.rs index e66791f6..c75464b9 100644 --- a/tests/rendered_output.rs +++ b/tests/rendered_output.rs @@ -1,6 +1,6 @@ mod dummy_book; -use crate::dummy_book::{assert_contains_strings, assert_doesnt_contain_strings, DummyBook}; +use crate::dummy_book::{assert_contains_strings, DummyBook}; use anyhow::Context; use mdbook::book::Chapter; @@ -11,10 +11,8 @@ use mdbook::{BookItem, MDBook}; use pretty_assertions::assert_eq; use select::document::Document; use select::predicate::{Attr, Class, Name, Predicate}; -use std::collections::HashMap; use std::ffi::OsStr; use std::fs; -use std::io::Write; use std::path::{Component, Path, PathBuf}; use std::str::FromStr; use tempfile::Builder as TempFileBuilder; @@ -306,35 +304,6 @@ fn theme_dir_overrides_work_correctly() { dummy_book::assert_contains_strings(built_index, &["This is a modified index.hbs!"]); } -#[test] -fn redirects_are_emitted_correctly() { - let temp = DummyBook::new().build().unwrap(); - let mut md = MDBook::load(temp.path()).unwrap(); - - // override the "outputs.html.redirect" table - let redirects: HashMap = vec![ - (PathBuf::from("/overview.html"), String::from("index.html")), - ( - PathBuf::from("/nexted/page.md"), - String::from("https://rust-lang.org/"), - ), - ] - .into_iter() - .collect(); - md.config.set("output.html.redirect", &redirects).unwrap(); - - md.build().unwrap(); - - for (original, redirect) in &redirects { - let mut redirect_file = md.build_dir_for("html"); - // append everything except the bits that make it absolute - // (e.g. "/" or "C:\") - redirect_file.extend(remove_absolute_components(original)); - let contents = fs::read_to_string(&redirect_file).unwrap(); - assert!(contents.contains(redirect)); - } -} - #[test] fn edit_url_has_default_src_dir_edit_url() { let temp = DummyBook::new().build().unwrap(); @@ -386,11 +355,6 @@ fn edit_url_has_configured_src_dir_edit_url() { ); } -fn remove_absolute_components(path: &Path) -> impl Iterator + '_ { - path.components() - .skip_while(|c| matches!(c, Component::Prefix(_) | Component::RootDir)) -} - /// Checks formatting of summary names with inline elements. #[test] fn summary_with_markdown_formatting() { diff --git a/tests/testsuite/main.rs b/tests/testsuite/main.rs index 298cf12d..79502355 100644 --- a/tests/testsuite/main.rs +++ b/tests/testsuite/main.rs @@ -12,6 +12,7 @@ mod markdown; mod playground; mod preprocessor; mod print; +mod redirects; mod prelude { pub use crate::book_test::BookTest; diff --git a/tests/testsuite/redirects.rs b/tests/testsuite/redirects.rs new file mode 100644 index 00000000..3501c223 --- /dev/null +++ b/tests/testsuite/redirects.rs @@ -0,0 +1,18 @@ +//! Tests for the HTML redirect feature. + +use crate::prelude::*; +use snapbox::file; + +// Basic check of redirects. +#[test] +fn redirects_are_emitted_correctly() { + BookTest::from_dir("redirects/redirects_are_emitted_correctly") + .check_file( + "book/overview.html", + file!["redirects/redirects_are_emitted_correctly/expected/overview.html"], + ) + .check_file( + "book/nested/page.html", + file!["redirects/redirects_are_emitted_correctly/expected/nested/page.html"], + ); +} diff --git a/tests/testsuite/redirects/redirects_are_emitted_correctly/book.toml b/tests/testsuite/redirects/redirects_are_emitted_correctly/book.toml new file mode 100644 index 00000000..46e4b865 --- /dev/null +++ b/tests/testsuite/redirects/redirects_are_emitted_correctly/book.toml @@ -0,0 +1,6 @@ +[book] +title = "redirects_are_emitted_correctly" + +[output.html.redirect] +"/overview.html" = "index.html" +"/nested/page.html" = "https://rust-lang.org/" diff --git a/tests/testsuite/redirects/redirects_are_emitted_correctly/expected/nested/page.html b/tests/testsuite/redirects/redirects_are_emitted_correctly/expected/nested/page.html new file mode 100644 index 00000000..08d6e929 --- /dev/null +++ b/tests/testsuite/redirects/redirects_are_emitted_correctly/expected/nested/page.html @@ -0,0 +1,12 @@ + + + + + Redirecting... + + + + +

Redirecting to... https://rust-lang.org/.

+ + diff --git a/tests/testsuite/redirects/redirects_are_emitted_correctly/expected/overview.html b/tests/testsuite/redirects/redirects_are_emitted_correctly/expected/overview.html new file mode 100644 index 00000000..80323084 --- /dev/null +++ b/tests/testsuite/redirects/redirects_are_emitted_correctly/expected/overview.html @@ -0,0 +1,12 @@ + + + + + Redirecting... + + + + +

Redirecting to... index.html.

+ + diff --git a/tests/testsuite/redirects/redirects_are_emitted_correctly/src/SUMMARY.md b/tests/testsuite/redirects/redirects_are_emitted_correctly/src/SUMMARY.md new file mode 100644 index 00000000..4f683946 --- /dev/null +++ b/tests/testsuite/redirects/redirects_are_emitted_correctly/src/SUMMARY.md @@ -0,0 +1,3 @@ +# Redirects + +- [Chapter 1](chapter_1.md) diff --git a/tests/testsuite/redirects/redirects_are_emitted_correctly/src/chapter_1.md b/tests/testsuite/redirects/redirects_are_emitted_correctly/src/chapter_1.md new file mode 100644 index 00000000..b743fda3 --- /dev/null +++ b/tests/testsuite/redirects/redirects_are_emitted_correctly/src/chapter_1.md @@ -0,0 +1 @@ +# Chapter 1