From 3362df4a484e91690d542acf5f1b344b09308e43 Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Wed, 27 Nov 2019 19:50:46 +0100 Subject: [PATCH] Compute canonical path before adjusting parent path (#856) * Compute canonical path before adjusting parent path * Don't use adjusted `parent` to recalculate `canonical` in `find_language` * Add regression tests - Test for correct canonical field when calling `new_page` - Test for correct canonical field after calling `find_language` --- components/library/src/content/file_info.rs | 29 +++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/components/library/src/content/file_info.rs b/components/library/src/content/file_info.rs index 0fe0686..4030f53 100644 --- a/components/library/src/content/file_info.rs +++ b/components/library/src/content/file_info.rs @@ -56,6 +56,7 @@ impl FileInfo { let file_path = path.to_path_buf(); let mut parent = file_path.parent().expect("Get parent of page").to_path_buf(); let name = path.file_stem().unwrap().to_string_lossy().to_string(); + let canonical = parent.join(&name); let mut components = find_content_components(&file_path.strip_prefix(base_path).unwrap_or(&file_path)); let relative = if !components.is_empty() { @@ -78,7 +79,7 @@ impl FileInfo { path: file_path, // We don't care about grand parent for pages grand_parent: None, - canonical: parent.join(&name), + canonical, parent, name, components, @@ -135,7 +136,7 @@ impl FileInfo { } self.name = parts.swap_remove(0); - self.canonical = self.parent.join(&self.name); + self.canonical = self.path.parent().expect("Get parent of page path").join(&self.name); let lang = parts.swap_remove(0); Ok(lang) @@ -254,4 +255,28 @@ mod tests { assert!(res.is_ok()); assert_eq!(res.unwrap(), "fr"); } + + /// Regression test for https://github.com/getzola/zola/pull/856. + #[test] + fn correct_canonical_for_index() { + let file = FileInfo::new_page( + &Path::new("/home/vincent/code/site/content/posts/tutorials/python/index.md"), + &PathBuf::new(), + ); + assert_eq!(file.canonical, Path::new("/home/vincent/code/site/content/posts/tutorials/python/index")); + } + + /// Regression test for https://github.com/getzola/zola/pull/856. + #[test] + fn correct_canonical_after_find_language() { + let mut config = Config::default(); + config.languages.push(Language { code: String::from("fr"), rss: false, search: false }); + let mut file = FileInfo::new_page( + &Path::new("/home/vincent/code/site/content/posts/tutorials/python/index.fr.md"), + &PathBuf::new(), + ); + let res = file.find_language(&config); + assert!(res.is_ok()); + assert_eq!(file.canonical, Path::new("/home/vincent/code/site/content/posts/tutorials/python/index")); + } }