Migrate redirects_are_emitted_correctly to BookTest

This commit is contained in:
Eric Huss 2025-04-21 19:25:44 -07:00
parent 50dfa365c7
commit cb2a63ea0a
8 changed files with 54 additions and 37 deletions

View file

@ -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<PathBuf, String> = 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<Item = Component> + '_ {
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() {

View file

@ -12,6 +12,7 @@ mod markdown;
mod playground;
mod preprocessor;
mod print;
mod redirects;
mod prelude {
pub use crate::book_test::BookTest;

View file

@ -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"],
);
}

View file

@ -0,0 +1,6 @@
[book]
title = "redirects_are_emitted_correctly"
[output.html.redirect]
"/overview.html" = "index.html"
"/nested/page.html" = "https://rust-lang.org/"

View file

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Redirecting...</title>
<meta http-equiv="refresh" content="0; URL=https://rust-lang.org/">
<link rel="canonical" href="https://rust-lang.org/">
</head>
<body>
<p>Redirecting to... <a href="https://rust-lang.org/">https://rust-lang.org/</a>.</p>
</body>
</html>

View file

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Redirecting...</title>
<meta http-equiv="refresh" content="0; URL=index.html">
<link rel="canonical" href="index.html">
</head>
<body>
<p>Redirecting to... <a href="index.html">index.html</a>.</p>
</body>
</html>

View file

@ -0,0 +1,3 @@
# Redirects
- [Chapter 1](chapter_1.md)

View file

@ -0,0 +1 @@
# Chapter 1