mdbook/tests/testsuite
Jesús Pérez 0784554c88 feat: site-url absolute links on mdBook 0.5.3 (crates layout)
Port of the 0.4.x site-url absolute-links patch to the 0.5 crates/ layout,
toward upstreaming as PR #1802. When output.html.site-url is set, internal
links and assets are emitted as absolute URLs anchored at site-url, so the
book works under a sub-path (e.g. /cdcidao/) regardless of page depth.

- html/tree.rs: fix_link/fix_html_link rewrite ./ content, image and raw-HTML
  links to {site_url}...; schemes and fragments untouched
- html_handlebars/hbs_renderer.rs: path_to_root = site_url for normal and index
  pages; base_url = site_url only for the toc.html iframe (removed before the
  per-chapter clone so it cannot leak)
- html_handlebars/helpers/resources.rs: {{resource}} honors an explicit
  path_to_root from data (absolute assets) with stock fallback
- html/print.rs: print page honors site-url; internal cross-refs still fold to
  #anchors, non-chapter links keep absolute form
- cmd/serve.rs: --preserve-site-url flag; serve still forces site-url to / for
  local preview but logs the override
- tests/testsuite/rendering*: site_url fixture + tests (content, assets, print,
  no <base> leak, no-regression without site-url)
- guide: document the serve flag and the renderer behavior
2026-06-18 23:03:22 +01:00
..
build Add a basic book for GUI tests 2025-10-15 07:00:35 -07:00
cli Add some basic help tests 2025-04-22 20:50:20 -07:00
config/empty Deny all unknown config fields 2025-08-12 15:14:36 -07:00
includes/all_includes Remove the book.multilingual field 2025-07-26 08:10:20 -07:00
index/basic_readme Migrate by_default_mdbook_use_index_preprocessor_to_convert_readme_to_index to BookTest 2025-04-22 20:50:20 -07:00
init/init_from_summary/src Migrate run_mdbook_init_should_create_content_from_summary to BookTest 2025-04-22 20:50:20 -07:00
markdown Fix nested admonitions using wrong header color 2026-02-22 20:44:05 -05:00
playground Add more comprehensive tests for HTML rendering 2025-09-16 14:07:54 -07:00
preprocessor Add a test for extension compatibility 2025-08-29 18:40:26 -07:00
print Fix print page links for internal links to non-chapters 2025-10-30 18:37:12 -07:00
redirects Add support for fragment redirects 2025-07-08 15:37:46 -07:00
renderer Migrate backends_receive_render_context_via_stdin to BookTest 2025-04-22 21:11:53 -07:00
rendering feat: site-url absolute links on mdBook 0.5.3 (crates layout) 2026-06-18 23:03:22 +01:00
search Add a new HTML rendering pipeline 2025-09-16 20:26:35 -07:00
test Migrate pass/fail mdbook test to BookTest 2025-04-22 21:11:53 -07:00
theme Remove copy-fonts 2025-08-12 17:56:14 -07:00
toc Migrate summary_with_markdown_formatting to BookTest 2025-04-22 21:11:54 -07:00
book_test.rs Clean up some fs-related utilities 2025-09-20 17:13:31 -07:00
build.rs Clean up some fs-related utilities 2025-09-20 17:13:31 -07:00
cli.rs Add some basic help tests 2025-04-22 20:50:20 -07:00
config.rs Ignore invalid top-level environment variable config keys 2025-11-19 16:38:47 -08:00
includes.rs Add a new HTML rendering pipeline 2025-09-16 20:26:35 -07:00
index.rs Fix heading nav with folded chapters 2025-10-20 17:31:40 -07:00
init.rs Switch from log to tracing 2025-09-12 06:13:45 -07:00
main.rs Enable hash-files by default 2025-08-26 16:25:49 -07:00
markdown.rs Add a test for definition lists with manual HTML tags 2025-10-30 10:02:27 -07:00
playground.rs Add a new HTML rendering pipeline 2025-09-16 20:26:35 -07:00
preprocessor.rs Don't rebuild preprocessor map during test 2025-12-10 13:42:15 -08:00
print.rs Add more comprehensive tests for HTML rendering 2025-09-16 14:07:54 -07:00
README.md Introduce the new BookTest-based testsuite 2025-04-22 20:50:16 -07:00
redirects.rs Switch from log to tracing 2025-09-12 06:13:45 -07:00
renderer.rs Switch from log to tracing 2025-09-12 06:13:45 -07:00
rendering.rs feat: site-url absolute links on mdBook 0.5.3 (crates layout) 2026-06-18 23:03:22 +01:00
search.rs Add a new HTML rendering pipeline 2025-09-16 20:26:35 -07:00
test.rs Switch from log to tracing 2025-09-12 06:13:45 -07:00
theme.rs Switch from log to tracing 2025-09-12 06:13:45 -07:00
toc.rs Fix heading nav with folded chapters 2025-10-20 17:31:40 -07:00

Testsuite

Introduction

This is the main testsuite for exercising all functionality of mdBook.

Tests should be organized into modules based around major features. Tests should use BookTest to drive the test. BookTest will set up a temp directory, and provides a variety of methods to help create a build books.

Basic structure of a test

Using BookTest, you typically use it to copy a directory into a temp directory, and then run mdbook commands in that temp directory. You can run the mdbook executable, or use the mdbook API to perform whatever tasks you need. Running the executable has the benefit of being able to validate the console output.

See build::basic_build for a simple test example. I recommend reviewing the methods on BookTest to learn more, and reviewing some of the existing tests to get a feel for how they are structured.

For example, let's say you are creating a new theme test. In the testsuite/theme directory, create a new directory with the book source that you want to exercise. At a minimum, this needs a src/SUMMARY.md, but often you'll also want book.toml. Then, in testsuite/theme.rs, add a test with BookTest::from_dir("theme/mytest"), and then use the methods to perform whatever actions you want.

BookTest is designed to be able to chain a series of actions. For example, you can do something like:

BookTest::from_dir("theme/mytest")
    .build()
    .check_main_file("book/index.html", str![["file contents"]])
    .change_file("src/index.md", "new contents")
    .build()
    .check_main_file("book/index.html", str![["new contents"]]);

Snapbox

The testsuite uses snapbox to drive most of the tests. This library provides the ability to compare strings using a variety of methods. These strings are written in the source code using either the str! or file! macros.

The magic is that you can set the SNAPSHOTS=overwrite environment variable, and snapbox will automatically update the strings contents of str!, or the file contents of file!. This makes it easier to update tests. Snapbox provides nice diffing output, and quite a few other features.

Expected contents can have wildcards like ... (matches any lines) or [..] (matches any characters on a line). See snapbox filters for more info and other filters.

Typically when writing a test, I'll just start with an empty str! or file!, and let snapbox fill it in. Then I review the contents to make sure they are what I expect.

Note that there is some normalization applied to the strings. See book_test::assert for how some of these normalizations happen.