From 483e51a1715d1c475a2d2481e8ca5aef2714b978 Mon Sep 17 00:00:00 2001 From: Ning Sun Date: Sun, 27 Dec 2020 17:10:04 +0800 Subject: [PATCH 1/2] (fix) path trail slash check on Windows Allows '/' as trail separator on Windows. --- src/registry.rs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/registry.rs b/src/registry.rs index 5252622bd..ef63dee64 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -272,7 +272,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 @@ -718,6 +722,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, "

Bonjour {{world}}!

").unwrap(); + + let mut dir_path = dir + .path() + .to_string_loosy() + .replace(path::MAIN_SEPARATOR, "/"); + if !dir_path.end_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] From f82678f835fe971c21a3836e4180de548e7a4724 Mon Sep 17 00:00:00 2001 From: Ning Sun Date: Sun, 27 Dec 2020 18:02:34 +0800 Subject: [PATCH 2/2] (test) fix typo on test --- src/registry.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/registry.rs b/src/registry.rs index ef63dee64..8722a3990 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -732,9 +732,9 @@ mod test { let mut dir_path = dir .path() - .to_string_loosy() - .replace(path::MAIN_SEPARATOR, "/"); - if !dir_path.end_with("/") { + .to_string_lossy() + .replace(std::path::MAIN_SEPARATOR, "/"); + if !dir_path.ends_with("/") { dir_path.push('/'); } r.register_templates_directory(".hbs", dir_path).unwrap();