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

Return empty list on invalid globs #824

Merged
merged 1 commit into from
Apr 16, 2023
Merged
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
13 changes: 10 additions & 3 deletions src/tera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,13 @@ impl Tera {
// See https://github.com/Keats/tera/issues/574 for the Tera discussion
// and https://github.com/Gilnaa/globwalk/issues/28 for the upstream issue.
let (parent_dir, glob_end) = glob.split_at(glob.find('*').unwrap());
let parent_dir = std::fs::canonicalize(parent_dir)?;
let parent_dir = match std::fs::canonicalize(parent_dir) {
Ok(d) => d,
// If canonicalize fails, just abort it and resume with the given path.
// Consumers expect invalid globs to just return the empty set instead of failing.
// See https://github.com/Keats/tera/issues/819#issuecomment-1480392230
Err(_) => std::path::PathBuf::from(parent_dir),
};
let dir = parent_dir.join(glob_end).into_os_string().into_string().unwrap();

// We are parsing all the templates on instantiation
Expand Down Expand Up @@ -1222,9 +1228,10 @@ mod tests {

// https://github.com/Keats/tera/issues/819
#[test]
fn doesnt_panic_on_invalid_glob() {
fn empty_list_on_invalid_glob() {
let tera = Tera::new("\\dev/null/*");
println!("{:?}", tera);
assert!(tera.is_err());
assert!(tera.is_ok());
assert!(tera.unwrap().templates.is_empty());
}
}