Skip to content

Commit

Permalink
Merge pull request #405 from sunng87/fix/separator-check
Browse files Browse the repository at this point in the history
(fix) path trail slash check on Windows
  • Loading branch information
sunng87 committed Dec 29, 2020
1 parent 6139c2c commit 3236bfe
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/registry.rs
Expand Up @@ -244,7 +244,11 @@ impl<'reg> Registry<'reg> {
{
let dir_path = dir_path.as_ref();

let prefix_len = if dir_path.to_string_lossy().ends_with(path::MAIN_SEPARATOR) {
let prefix_len = if dir_path
.to_string_lossy()
.ends_with(|c| c == '\\' || c == '/')
// `/` will work on windows too so we still need to check
{
dir_path.to_string_lossy().len()
} else {
dir_path.to_string_lossy().len() + 1
Expand Down Expand Up @@ -706,6 +710,29 @@ mod test {

dir.close().unwrap();
}

{
let dir = tempdir().unwrap();

let file1_path = dir.path().join("t10.hbs");
let mut file1: File = File::create(&file1_path).unwrap();
writeln!(file1, "<h1>Bonjour {{world}}!</h1>").unwrap();

let mut dir_path = dir
.path()
.to_string_lossy()
.replace(std::path::MAIN_SEPARATOR, "/");
if !dir_path.ends_with("/") {
dir_path.push('/');
}
r.register_templates_directory(".hbs", dir_path).unwrap();

assert_eq!(r.templates.len(), 8);
assert_eq!(r.templates.contains_key("t10"), true);

drop(file1);
dir.close().unwrap();
}
}

#[test]
Expand Down

0 comments on commit 3236bfe

Please sign in to comment.