From 4dc73537a199fec94f1e12581c831dbeee81f2b2 Mon Sep 17 00:00:00 2001 From: ltdk Date: Wed, 24 Jul 2024 18:04:18 -0400 Subject: [PATCH] Add additional tests for unrendered pages/sections: * They can be accessed through templates directly and through sections * Unrendered pages are not added to sitemaps * Unrendered pages are not added to taxonomies --- components/site/tests/common.rs | 17 ++++- components/site/tests/site.rs | 93 +++++++++++++++++------- test_site/content/posts/access-render.md | 9 +++ test_site/content/posts/render.md | 4 + test_site/templates/access_render.html | 15 ++++ 5 files changed, 108 insertions(+), 30 deletions(-) create mode 100644 test_site/content/posts/access-render.md create mode 100644 test_site/templates/access_render.html diff --git a/components/site/tests/common.rs b/components/site/tests/common.rs index f5caaf1824..267bdf8019 100644 --- a/components/site/tests/common.rs +++ b/components/site/tests/common.rs @@ -22,7 +22,7 @@ macro_rules! file_exists { #[macro_export] macro_rules! file_contains { - ($root: expr, $path: expr, $text: expr) => {{ + (@impl $root: expr, $path: expr) => {{ use std::io::prelude::*; let mut path = $root.clone(); for component in $path.split('/') { @@ -31,11 +31,24 @@ macro_rules! file_contains { let mut file = std::fs::File::open(&path).expect(&format!("Failed to open {:?}", $path)); let mut s = String::new(); file.read_to_string(&mut s).unwrap(); - println!("{}", s); + println!("{path:?} {s}"); + s + }}; + ($root: expr, $path: expr, $text: expr) => {{ + let s = file_contains!(@impl $root, $path); s.contains($text) }}; } +#[macro_export] +macro_rules! file_contains_regex { + ($root: expr, $path: expr, $pat: expr) => {{ + let s = file_contains!(@impl $root, $path); + let re = libs::regex::Regex::new($pat).unwrap(); + re.is_match(&s) + }}; +} + /// We return the tmpdir otherwise it would get out of scope and be deleted /// The tests can ignore it if they dont need it by prefixing it with a `_` pub fn build_site(name: &str) -> (Site, TempDir, PathBuf) { diff --git a/components/site/tests/site.rs b/components/site/tests/site.rs index bf0811e15a..f86181caab 100644 --- a/components/site/tests/site.rs +++ b/components/site/tests/site.rs @@ -21,7 +21,7 @@ fn can_parse_site() { let library = site.library.read().unwrap(); // Correct number of pages (sections do not count as pages, draft are ignored) - assert_eq!(library.pages.len(), 36); + assert_eq!(library.pages.len(), 37); let posts_path = path.join("content").join("posts"); // Make sure the page with a url doesn't have any sections @@ -44,7 +44,7 @@ fn can_parse_site() { let posts_section = library.sections.get(&posts_path.join("_index.md")).unwrap(); assert_eq!(posts_section.subsections.len(), 2); - assert_eq!(posts_section.pages.len(), 10); // 11 with 1 draft == 10 + assert_eq!(posts_section.pages.len(), 12); // 13 with 1 draft == 12 assert_eq!(posts_section.ancestors, vec![index_section.file.relative.clone()]); // Make sure we remove all the pwd + content from the sections @@ -155,6 +155,33 @@ fn can_build_site_without_live_reload() { "posts/tutorials/devops/nix.md" )); + assert!(file_exists!(public, "posts/access-render/index.html")); + + // render = false pages can still be accessed directly + assert!(file_contains!( + public, + "posts/access-render/index.html", + "Path of unrendered page: /posts/render/" + )); + // render = false pages can still be accessed through sections + assert!(file_contains_regex!( + public, + "posts/access-render/index.html", + r#"Pages in section with unrendered page: