Skip to content

Commit

Permalink
Add additional tests for unrendered pages/sections:
Browse files Browse the repository at this point in the history
* They can be accessed through templates directly and through sections
* Unrendered pages are not added to sitemaps
* Unrendered pages are not added to taxonomies
  • Loading branch information
clarfonthey committed Oct 10, 2024
1 parent 896a805 commit 4dc7353
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 30 deletions.
17 changes: 15 additions & 2 deletions components/site/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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('/') {
Expand All @@ -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) {
Expand Down
93 changes: 65 additions & 28 deletions components/site/tests/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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: <ul>(<li>[^>]+</li>)*<li>/posts/render/</li>"#
));
// render = false sections can still be accessed directly
assert!(file_contains!(
public,
"posts/access-render/index.html",
"Pages in unrendered section: <ul><li>"
));
// render = false pages don't show up in taxonomies
assert!(!file_contains!(public, "podcast-authors/some-person/atom.xml", "/posts/render/"));
assert!(!file_contains!(public, "categories/a-category/atom.xml", "/posts/render/"));
// render = false pages don't even add terms to taxonomies
assert!(!file_exists!(public, "podcast-authors/other-person/atom.xml"));
assert!(!file_exists!(public, "categories/b-category/atom.xml"));

// aliases work
assert!(file_exists!(public, "an-old-url/old-page/index.html"));
assert!(file_contains!(public, "an-old-url/old-page/index.html", "something-else"));
Expand Down Expand Up @@ -216,6 +243,8 @@ fn can_build_site_without_live_reload() {
assert!(!file_contains!(public, "sitemap.xml", "draft"));
// render: false sections are not in the sitemap either
assert!(!file_contains!(public, "sitemap.xml", "posts/2018/</loc>"));
// render: false pages are not in the sitemap either
assert!(!file_contains!(public, "sitemap.xml", "posts/render/</loc>"));

// robots.txt has been rendered from the template
assert!(file_contains!(public, "robots.txt", "User-agent: zola"));
Expand Down Expand Up @@ -299,15 +328,19 @@ fn can_build_site_with_taxonomies() {
let mut pages = vec![];

let pages_data = std::mem::replace(&mut library.pages, AHashMap::new());
for (i, (_, mut page)) in pages_data.into_iter().enumerate() {
page.meta.taxonomies = {
let mut taxonomies = HashMap::new();
taxonomies.insert(
"categories".to_string(),
vec![if i % 2 == 0 { "A" } else { "B" }.to_string()],
);
taxonomies
};
let mut i = 0;
for (_, mut page) in pages_data.into_iter() {
if page.meta.render {
page.meta.taxonomies = {
let mut taxonomies = HashMap::new();
taxonomies.insert(
"categories".to_string(),
vec![if i % 2 == 0 { "A" } else { "B" }.to_string()],
);
taxonomies
};
i += 1;
}
pages.push(page);
}
for p in pages {
Expand Down Expand Up @@ -417,7 +450,7 @@ fn can_build_site_with_pagination_for_section() {
"posts/page/1/index.html",
"http-equiv=\"refresh\" content=\"0; url=https://replace-this-with-your-url.com/posts/\""
));
assert!(file_contains!(public, "posts/index.html", "Num pagers: 5"));
assert!(file_contains!(public, "posts/index.html", "Num pagers: 6"));
assert!(file_contains!(public, "posts/index.html", "Page size: 2"));
assert!(file_contains!(public, "posts/index.html", "Current index: 1"));
assert!(!file_contains!(public, "posts/index.html", "has_prev"));
Expand All @@ -430,12 +463,12 @@ fn can_build_site_with_pagination_for_section() {
assert!(file_contains!(
public,
"posts/index.html",
"Last: https://replace-this-with-your-url.com/posts/page/5/"
"Last: https://replace-this-with-your-url.com/posts/page/6/"
));
assert!(!file_contains!(public, "posts/index.html", "has_prev"));

assert!(file_exists!(public, "posts/page/2/index.html"));
assert!(file_contains!(public, "posts/page/2/index.html", "Num pagers: 5"));
assert!(file_contains!(public, "posts/page/2/index.html", "Num pagers: 6"));
assert!(file_contains!(public, "posts/page/2/index.html", "Page size: 2"));
assert!(file_contains!(public, "posts/page/2/index.html", "Current index: 2"));
assert!(file_contains!(public, "posts/page/2/index.html", "has_prev"));
Expand All @@ -448,11 +481,11 @@ fn can_build_site_with_pagination_for_section() {
assert!(file_contains!(
public,
"posts/page/2/index.html",
"Last: https://replace-this-with-your-url.com/posts/page/5/"
"Last: https://replace-this-with-your-url.com/posts/page/6/"
));

assert!(file_exists!(public, "posts/page/3/index.html"));
assert!(file_contains!(public, "posts/page/3/index.html", "Num pagers: 5"));
assert!(file_contains!(public, "posts/page/3/index.html", "Num pagers: 6"));
assert!(file_contains!(public, "posts/page/3/index.html", "Page size: 2"));
assert!(file_contains!(public, "posts/page/3/index.html", "Current index: 3"));
assert!(file_contains!(public, "posts/page/3/index.html", "has_prev"));
Expand All @@ -465,11 +498,11 @@ fn can_build_site_with_pagination_for_section() {
assert!(file_contains!(
public,
"posts/page/3/index.html",
"Last: https://replace-this-with-your-url.com/posts/page/5/"
"Last: https://replace-this-with-your-url.com/posts/page/6/"
));

assert!(file_exists!(public, "posts/page/4/index.html"));
assert!(file_contains!(public, "posts/page/4/index.html", "Num pagers: 5"));
assert!(file_contains!(public, "posts/page/4/index.html", "Num pagers: 6"));
assert!(file_contains!(public, "posts/page/4/index.html", "Page size: 2"));
assert!(file_contains!(public, "posts/page/4/index.html", "Current index: 4"));
assert!(file_contains!(public, "posts/page/4/index.html", "has_prev"));
Expand All @@ -482,7 +515,7 @@ fn can_build_site_with_pagination_for_section() {
assert!(file_contains!(
public,
"posts/page/4/index.html",
"Last: https://replace-this-with-your-url.com/posts/page/5/"
"Last: https://replace-this-with-your-url.com/posts/page/6/"
));

// sitemap contains the pager pages
Expand Down Expand Up @@ -589,15 +622,19 @@ fn can_build_site_with_pagination_for_taxonomy() {
let mut pages = vec![];

let pages_data = std::mem::replace(&mut library.pages, AHashMap::new());
for (i, (_, mut page)) in pages_data.into_iter().enumerate() {
page.meta.taxonomies = {
let mut taxonomies = HashMap::new();
taxonomies.insert(
"tags".to_string(),
vec![if i % 2 == 0 { "A" } else { "B" }.to_string()],
);
taxonomies
};
let mut i = 0;
for (_, mut page) in pages_data.into_iter() {
if page.meta.render {
page.meta.taxonomies = {
let mut taxonomies = HashMap::new();
taxonomies.insert(
"tags".to_string(),
vec![if i % 2 == 0 { "A" } else { "B" }.to_string()],
);
taxonomies
};
i += 1;
}
pages.push(page);
}
for p in pages {
Expand Down
9 changes: 9 additions & 0 deletions test_site/content/posts/access-render.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
+++
title = 'render = false tests'
slug = 'access-render'
template = 'access_render.html'
date = 2000-01-01
+++

This post exists to test that unrendered sections and pages are still accessible
via templates.
4 changes: 4 additions & 0 deletions test_site/content/posts/render.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ title = "Page with content but without generated folder"
description = ""
date = 2017-04-01
render = false

[taxonomies]
categories = ["a-category", "b-category"]
podcast_authors = ["Some Person", "Other Person"]
+++

Don't generate a folder for this page
15 changes: 15 additions & 0 deletions test_site/templates/access_render.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends "index.html" %}

{% block content %}
{% set unrendered_page = get_page(path='posts/render.md') %}
{% set section_with_unrendered = get_section(path='posts/_index.md') %}
{% set unrendered_section = get_section(path='posts/2018/_index.md') %}
Path of unrendered page: {{ unrendered_page.path | safe }}
Pages in section with unrendered page: <ul>{% for page in section_with_unrendered.pages -%}
<li>{{ page.path | safe }}</li>
{%- endfor %}</ul>
Pages in unrendered section: <ul>{% for page in unrendered_section.pages -%}
<li>{{ page.path | safe }}</li>
{%- endfor %}</ul>
{% endblock content %}

0 comments on commit 4dc7353

Please sign in to comment.