From 2bcae6b0a956ddda6dd5795ff657e168df169fc1 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Wed, 10 Dec 2025 12:51:19 -0800 Subject: [PATCH 1/2] Add a test with a custom preprocessor This test illustrates that the test function is not working correctly when there is a custom preprocessor. --- tests/testsuite/preprocessor.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/testsuite/preprocessor.rs b/tests/testsuite/preprocessor.rs index cd8b9efad1..7aa02bd55e 100644 --- a/tests/testsuite/preprocessor.rs +++ b/tests/testsuite/preprocessor.rs @@ -44,6 +44,20 @@ fn runs_preprocessors() { assert_eq!(inner.rendered_with, ["html"]); } +// Run tests with a custom preprocessor. +#[test] +fn test_with_custom_preprocessor() { + let test = BookTest::init(|_| {}); + let spy: Arc> = Default::default(); + let mut book = test.load_book(); + book.with_preprocessor(Spy(Arc::clone(&spy))); + book.test(vec![]).unwrap(); + + let inner = spy.lock().unwrap(); + assert_eq!(inner.run_count, 0); + assert_eq!(inner.rendered_with, Vec::::new()); +} + // No-op preprocessor works. #[test] fn nop_preprocessor() { From 7bdea7c0852e2ed765c25091f97b684ffdc9ba25 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Wed, 10 Dec 2025 13:42:15 -0800 Subject: [PATCH 2/2] Don't rebuild preprocessor map during test This fixes a problem where custom preprocessors were not being registered when running tests. This was caused by the test function rebuilding the preprocessor map. This removes the code that was rebuilding the preprocessors and removing the IndexPreprocessor when running tests. Skipping IndexPreprocessor was added back in https://github.com/rust-lang/mdBook/pull/741 to fix https://github.com/rust-lang/mdBook/issues/724 which was caused by https://github.com/rust-lang/mdBook/pull/685 which added the IndexPreprocessor. Additionally, https://github.com/rust-lang/mdBook/pull/1986 added running *all* preprocessors. The IndexPreprocessor was removed because in the past the code was testing against the source directly, and the path from `chapter.path` is the converted `index.md` file, and that filename does not exist in the source. This isn't a problem anymore because due to https://github.com/rust-lang/mdBook/pull/891 it is not reading from the `src` directory. Note that this results in a minor change where the chapter path changes from `README.md` to `index.md` in the output and the `--chapter` option. I think I'm ok with that change, though it would be easy to switch it back if that's an issue. --- crates/mdbook-driver/src/mdbook.rs | 5 ----- tests/testsuite/preprocessor.rs | 4 ++-- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/crates/mdbook-driver/src/mdbook.rs b/crates/mdbook-driver/src/mdbook.rs index e5dac9027f..03ebcb8c30 100644 --- a/crates/mdbook-driver/src/mdbook.rs +++ b/crates/mdbook-driver/src/mdbook.rs @@ -263,11 +263,6 @@ impl MDBook { } } - // Index Preprocessor is disabled so that chapter paths - // continue to point to the actual markdown files. - self.preprocessors = determine_preprocessors(&self.config, &self.root)?; - self.preprocessors - .shift_remove_entry(IndexPreprocessor::NAME); let (book, _) = self.preprocess_book(&TestRenderer)?; let color_output = std::io::stderr().is_terminal(); diff --git a/tests/testsuite/preprocessor.rs b/tests/testsuite/preprocessor.rs index 7aa02bd55e..33c320785e 100644 --- a/tests/testsuite/preprocessor.rs +++ b/tests/testsuite/preprocessor.rs @@ -54,8 +54,8 @@ fn test_with_custom_preprocessor() { book.test(vec![]).unwrap(); let inner = spy.lock().unwrap(); - assert_eq!(inner.run_count, 0); - assert_eq!(inner.rendered_with, Vec::::new()); + assert_eq!(inner.run_count, 1); + assert_eq!(inner.rendered_with, ["test"]); } // No-op preprocessor works.