From a60571321a8cbfeadea89a60f6799c5cc29f8c12 Mon Sep 17 00:00:00 2001 From: Clark Date: Tue, 26 Apr 2022 20:20:44 +0800 Subject: [PATCH 1/2] bail! in render() if specified theme directory does not exist --- src/renderer/html_handlebars/hbs_renderer.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 3d2d1afe..f477c43e 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -481,7 +481,13 @@ impl Renderer for HtmlHandlebars { let mut handlebars = Handlebars::new(); let theme_dir = match html_config.theme { - Some(ref theme) => ctx.root.join(theme), + Some(ref theme) => { + let dir = ctx.root.join(theme); + if !dir.is_dir() { + bail!("theme dir {} does not exist", dir.display()); + } + dir + } None => ctx.root.join("theme"), }; From fe8bb38ec130864b021e858583fe1de06ab6cdd7 Mon Sep 17 00:00:00 2001 From: Clark Date: Thu, 28 Apr 2022 13:13:58 +0800 Subject: [PATCH 2/2] add a test: Ensure building fails if `[output.html].theme` points to a non-existent directory --- tests/rendered_output.rs | 51 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/tests/rendered_output.rs b/tests/rendered_output.rs index 79c120bb..873a622d 100644 --- a/tests/rendered_output.rs +++ b/tests/rendered_output.rs @@ -657,6 +657,57 @@ fn summary_with_markdown_formatting() { ); } +/// Ensure building fails if `[output.html].theme` points to a non-existent directory +#[test] +fn failure_on_missing_theme_directory() { + // 1. Using default theme should work + let temp = DummyBook::new().build().unwrap(); + let book_toml = r#" + [book] + title = "implicit" + src = "src" + "#; + + write_file(temp.path(), "book.toml", book_toml.as_bytes()).unwrap(); + let md = MDBook::load(temp.path()).unwrap(); + let got = md.build(); + assert!(got.is_ok()); + + // 2. Pointing to a normal directory should work + let temp = DummyBook::new().build().unwrap(); + let created = fs::create_dir(temp.path().join("theme-directory")); + assert!(created.is_ok()); + let book_toml = r#" + [book] + title = "implicit" + src = "src" + + [output.html] + theme = "./theme-directory" + "#; + + write_file(temp.path(), "book.toml", book_toml.as_bytes()).unwrap(); + let md = MDBook::load(temp.path()).unwrap(); + let got = md.build(); + assert!(got.is_ok()); + + // 3. Pointing to a non-existent directory should fail + let temp = DummyBook::new().build().unwrap(); + let book_toml = r#" + [book] + title = "implicit" + src = "src" + + [output.html] + theme = "./non-existent-directory" + "#; + + write_file(temp.path(), "book.toml", book_toml.as_bytes()).unwrap(); + let md = MDBook::load(temp.path()).unwrap(); + let got = md.build(); + assert!(got.is_err()); +} + #[cfg(feature = "search")] mod search { use crate::dummy_book::DummyBook;