Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bail! in render() if specified theme directory does not exist #1791

Merged
merged 2 commits into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/renderer/html_handlebars/hbs_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
};

Expand Down
51 changes: 51 additions & 0 deletions tests/rendered_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down