diff --git a/tests/build_process.rs b/tests/build_process.rs index 10d0b4a9..c59db328 100644 --- a/tests/build_process.rs +++ b/tests/build_process.rs @@ -1,10 +1,8 @@ mod dummy_book; use crate::dummy_book::DummyBook; -use mdbook::book::Book; use mdbook::config::Config; use mdbook::errors::*; -use mdbook::preprocess::{Preprocessor, PreprocessorContext}; use mdbook::renderer::{RenderContext, Renderer}; use mdbook::MDBook; use std::sync::{Arc, Mutex}; @@ -14,20 +12,6 @@ struct Spy(Arc>); #[derive(Debug, Default)] struct Inner { run_count: usize, - rendered_with: Vec, -} - -impl Preprocessor for Spy { - fn name(&self) -> &str { - "dummy" - } - - fn run(&self, ctx: &PreprocessorContext, book: Book) -> Result { - let mut inner = self.0.lock().unwrap(); - inner.run_count += 1; - inner.rendered_with.push(ctx.renderer.clone()); - Ok(book) - } } impl Renderer for Spy { @@ -42,26 +26,6 @@ impl Renderer for Spy { } } -#[test] -fn mdbook_runs_preprocessors() { - let spy: Arc> = Default::default(); - - let temp = DummyBook::new().build().unwrap(); - let cfg = Config::default(); - - let mut book = MDBook::load_with_config(temp.path(), cfg).unwrap(); - book.with_preprocessor(Spy(Arc::clone(&spy))); - book.build().unwrap(); - - let inner = spy.lock().unwrap(); - assert_eq!(inner.run_count, 1); - assert_eq!(inner.rendered_with.len(), 1); - assert_eq!( - "html", inner.rendered_with[0], - "We should have been run with the default HTML renderer" - ); -} - #[test] fn mdbook_runs_renderers() { let spy: Arc> = Default::default(); diff --git a/tests/testsuite/main.rs b/tests/testsuite/main.rs index 5b2d7db1..daec2814 100644 --- a/tests/testsuite/main.rs +++ b/tests/testsuite/main.rs @@ -10,6 +10,7 @@ mod index; mod init; mod markdown; mod playground; +mod preprocessor; mod prelude { pub use crate::book_test::BookTest; diff --git a/tests/testsuite/preprocessor.rs b/tests/testsuite/preprocessor.rs new file mode 100644 index 00000000..8c8793a6 --- /dev/null +++ b/tests/testsuite/preprocessor.rs @@ -0,0 +1,42 @@ +//! Tests for custom preprocessors. + +use crate::prelude::*; +use mdbook::book::Book; +use mdbook::errors::Result; +use mdbook::preprocess::{Preprocessor, PreprocessorContext}; +use std::sync::{Arc, Mutex}; + +struct Spy(Arc>); + +#[derive(Debug, Default)] +struct Inner { + run_count: usize, + rendered_with: Vec, +} + +impl Preprocessor for Spy { + fn name(&self) -> &str { + "dummy" + } + + fn run(&self, ctx: &PreprocessorContext, book: Book) -> Result { + let mut inner = self.0.lock().unwrap(); + inner.run_count += 1; + inner.rendered_with.push(ctx.renderer.clone()); + Ok(book) + } +} + +// Test that preprocessor gets run. +#[test] +fn runs_preprocessors() { + let test = BookTest::init(|_| {}); + let spy: Arc> = Default::default(); + let mut book = test.load_book(); + book.with_preprocessor(Spy(Arc::clone(&spy))); + book.build().unwrap(); + + let inner = spy.lock().unwrap(); + assert_eq!(inner.run_count, 1); + assert_eq!(inner.rendered_with, ["html"]); +}