From 974492bb7b62e91ad65d0e367225da58c3445328 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Fri, 22 Feb 2019 21:48:30 +0100 Subject: [PATCH] Ensure we don't delete root index without adding back default in rebuild Fix #620 --- components/rebuild/src/lib.rs | 3 +++ components/rebuild/tests/rebuild.rs | 17 ++++++++++++ components/site/src/lib.rs | 42 ++++++++++++++++------------- test_site/content/_index.md | 2 ++ 4 files changed, 46 insertions(+), 18 deletions(-) create mode 100644 test_site/content/_index.md diff --git a/components/rebuild/src/lib.rs b/components/rebuild/src/lib.rs index ca4250b..a93ef7f 100644 --- a/components/rebuild/src/lib.rs +++ b/components/rebuild/src/lib.rs @@ -116,6 +116,9 @@ fn delete_element(site: &mut Site, path: &Path, is_section: bool) -> Result<()> } } + // We might have delete the root _index.md so ensure we have at least the default one + // before populating + site.create_default_index_sections()?; site.populate_sections(); site.populate_taxonomies()?; // Ensure we have our fn updated so it doesn't contain the permalink(s)/section/page deleted diff --git a/components/rebuild/tests/rebuild.rs b/components/rebuild/tests/rebuild.rs index 621e8a8..f35c0c2 100644 --- a/components/rebuild/tests/rebuild.rs +++ b/components/rebuild/tests/rebuild.rs @@ -269,3 +269,20 @@ Edite assert!(res.is_ok()); assert!(file_contains!(site_path, "public/fr/blog/with-assets/index.html", "Edite")); } + +// https://github.com/getzola/zola/issues/620 +#[test] +fn can_rebuild_after_renaming_section_and_deleting_file() { + let tmp_dir = tempdir().expect("create temp dir"); + let (site_path, mut site) = load_and_build_site!(tmp_dir, "test_site"); + let (old_path, new_path) = rename!(site_path, "content/posts/", "post/"); + let res = after_content_rename(&mut site, &old_path, &new_path); + assert!(res.is_ok()); + + let path = site_path.join("content").join("_index.md"); + fs::remove_file(&path).unwrap(); + + let res = after_content_change(&mut site, &path); + println!("{:?}", res); + assert!(res.is_ok()); +} diff --git a/components/site/src/lib.rs b/components/site/src/lib.rs index 9d40f75..643a10d 100644 --- a/components/site/src/lib.rs +++ b/components/site/src/lib.rs @@ -234,8 +234,30 @@ impl Site { self.add_section(s, false)?; } - // Insert a default index section for each language if necessary so we don't need to create - // a _index.md to render the index page at the root of the site + self.create_default_index_sections()?; + + let mut pages_insert_anchors = HashMap::new(); + for page in pages { + let p = page?; + pages_insert_anchors.insert( + p.file.path.clone(), + self.find_parent_section_insert_anchor(&p.file.parent.clone(), &p.lang), + ); + self.add_page(p, false)?; + } + + self.register_early_global_fns(); + self.populate_sections(); + self.render_markdown()?; + self.populate_taxonomies()?; + self.register_tera_global_fns(); + + Ok(()) + } + + /// Insert a default index section for each language if necessary so we don't need to create + /// a _index.md to render the index page at the root of the site + pub fn create_default_index_sections(&mut self) -> Result<()> { for (index_path, lang) in self.index_section_paths() { if let Some(ref index_section) = self.library.read().unwrap().get_section(&index_path) { if self.config.build_search_index && !index_section.meta.in_search_index { @@ -270,22 +292,6 @@ impl Site { } } - let mut pages_insert_anchors = HashMap::new(); - for page in pages { - let p = page?; - pages_insert_anchors.insert( - p.file.path.clone(), - self.find_parent_section_insert_anchor(&p.file.parent.clone(), &p.lang), - ); - self.add_page(p, false)?; - } - - self.register_early_global_fns(); - self.populate_sections(); - self.render_markdown()?; - self.populate_taxonomies()?; - self.register_tera_global_fns(); - Ok(()) } diff --git a/test_site/content/_index.md b/test_site/content/_index.md new file mode 100644 index 0000000..ac36e06 --- /dev/null +++ b/test_site/content/_index.md @@ -0,0 +1,2 @@ ++++ ++++