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
  • Loading branch information
clarfonthey committed Oct 10, 2024
1 parent 44674cd commit 17ee076
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 11 deletions.
15 changes: 14 additions & 1 deletion 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 @@ -32,10 +32,23 @@ macro_rules! file_contains {
let mut s = String::new();
file.read_to_string(&mut s).unwrap();
println!("{}", 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
43 changes: 33 additions & 10 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,27 @@ 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>"
));

// 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 +237,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 @@ -417,7 +440,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 +453,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 +471,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 +488,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 +505,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
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.
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 17ee076

Please sign in to comment.