Skip to content

Commit d0cdcd2

Browse files
committed
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
1 parent ee41a9a commit d0cdcd2

File tree

5 files changed

+96
-12
lines changed

5 files changed

+96
-12
lines changed

Diff for: components/site/tests/common.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ macro_rules! file_exists {
2222

2323
#[macro_export]
2424
macro_rules! file_contains {
25-
($root: expr, $path: expr, $text: expr) => {{
25+
(@impl $root: expr, $path: expr) => {{
2626
use std::io::prelude::*;
2727
let mut path = $root.clone();
2828
for component in $path.split('/') {
@@ -31,11 +31,24 @@ macro_rules! file_contains {
3131
let mut file = std::fs::File::open(&path).expect(&format!("Failed to open {:?}", $path));
3232
let mut s = String::new();
3333
file.read_to_string(&mut s).unwrap();
34-
println!("{}", s);
34+
println!("{path:?} {s}");
35+
s
36+
}};
37+
($root: expr, $path: expr, $text: expr) => {{
38+
let s = file_contains!(@impl $root, $path);
3539
s.contains($text)
3640
}};
3741
}
3842

43+
#[macro_export]
44+
macro_rules! file_contains_regex {
45+
($root: expr, $path: expr, $pat: expr) => {{
46+
let s = file_contains!(@impl $root, $path);
47+
let re = libs::regex::Regex::new($pat).unwrap();
48+
re.is_match(&s)
49+
}};
50+
}
51+
3952
/// We return the tmpdir otherwise it would get out of scope and be deleted
4053
/// The tests can ignore it if they dont need it by prefixing it with a `_`
4154
pub fn build_site(name: &str) -> (Site, TempDir, PathBuf) {

Diff for: components/site/tests/site.rs

+53-10
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn can_parse_site() {
2121
let library = site.library.read().unwrap();
2222

2323
// Correct number of pages (sections do not count as pages, draft are ignored)
24-
assert_eq!(library.pages.len(), 36);
24+
assert_eq!(library.pages.len(), 37);
2525
let posts_path = path.join("content").join("posts");
2626

2727
// Make sure the page with a url doesn't have any sections
@@ -44,7 +44,7 @@ fn can_parse_site() {
4444

4545
let posts_section = library.sections.get(&posts_path.join("_index.md")).unwrap();
4646
assert_eq!(posts_section.subsections.len(), 2);
47-
assert_eq!(posts_section.pages.len(), 10); // 11 with 1 draft == 10
47+
assert_eq!(posts_section.pages.len(), 12); // 13 with 1 draft == 12
4848
assert_eq!(posts_section.ancestors, vec![index_section.file.relative.clone()]);
4949

5050
// Make sure we remove all the pwd + content from the sections
@@ -155,6 +155,47 @@ fn can_build_site_without_live_reload() {
155155
"posts/tutorials/devops/nix.md"
156156
));
157157

158+
assert!(file_exists!(public, "posts/access-render/index.html"));
159+
160+
// render = false pages can still be accessed directly
161+
assert!(file_contains!(
162+
public,
163+
"posts/access-render/index.html",
164+
"Path of unrendered page: /posts/render/"
165+
));
166+
// render = false pages can still be accessed through sections
167+
assert!(file_contains_regex!(
168+
public,
169+
"posts/access-render/index.html",
170+
r#"Pages in section with unrendered page: <ul>(<li>[^>]+</li>)*<li>/posts/render/</li>"#
171+
));
172+
// render = false sections can still be accessed directly
173+
assert!(file_contains!(
174+
public,
175+
"posts/access-render/index.html",
176+
"Pages in unrendered section: <ul><li>"
177+
));
178+
// render = false pages don't show up in taxonomies
179+
assert!(!file_contains!(
180+
public,
181+
"podcast-authors/some-person/atom.xml",
182+
"/posts/render/"
183+
));
184+
assert!(!file_contains!(
185+
public,
186+
"categories/a-category/atom.xml",
187+
"/posts/render/"
188+
));
189+
// render = false pages don't even add terms to taxonomies
190+
assert!(!file_exists!(
191+
public,
192+
"podcast-authors/other-person/atom.xml"
193+
));
194+
assert!(!file_exists!(
195+
public,
196+
"categories/b-category/atom.xml"
197+
));
198+
158199
// aliases work
159200
assert!(file_exists!(public, "an-old-url/old-page/index.html"));
160201
assert!(file_contains!(public, "an-old-url/old-page/index.html", "something-else"));
@@ -216,6 +257,8 @@ fn can_build_site_without_live_reload() {
216257
assert!(!file_contains!(public, "sitemap.xml", "draft"));
217258
// render: false sections are not in the sitemap either
218259
assert!(!file_contains!(public, "sitemap.xml", "posts/2018/</loc>"));
260+
// render: false pages are not in the sitemap either
261+
assert!(!file_contains!(public, "sitemap.xml", "posts/render/</loc>"));
219262

220263
// robots.txt has been rendered from the template
221264
assert!(file_contains!(public, "robots.txt", "User-agent: zola"));
@@ -417,7 +460,7 @@ fn can_build_site_with_pagination_for_section() {
417460
"posts/page/1/index.html",
418461
"http-equiv=\"refresh\" content=\"0; url=https://replace-this-with-your-url.com/posts/\""
419462
));
420-
assert!(file_contains!(public, "posts/index.html", "Num pagers: 5"));
463+
assert!(file_contains!(public, "posts/index.html", "Num pagers: 6"));
421464
assert!(file_contains!(public, "posts/index.html", "Page size: 2"));
422465
assert!(file_contains!(public, "posts/index.html", "Current index: 1"));
423466
assert!(!file_contains!(public, "posts/index.html", "has_prev"));
@@ -430,12 +473,12 @@ fn can_build_site_with_pagination_for_section() {
430473
assert!(file_contains!(
431474
public,
432475
"posts/index.html",
433-
"Last: https://replace-this-with-your-url.com/posts/page/5/"
476+
"Last: https://replace-this-with-your-url.com/posts/page/6/"
434477
));
435478
assert!(!file_contains!(public, "posts/index.html", "has_prev"));
436479

437480
assert!(file_exists!(public, "posts/page/2/index.html"));
438-
assert!(file_contains!(public, "posts/page/2/index.html", "Num pagers: 5"));
481+
assert!(file_contains!(public, "posts/page/2/index.html", "Num pagers: 6"));
439482
assert!(file_contains!(public, "posts/page/2/index.html", "Page size: 2"));
440483
assert!(file_contains!(public, "posts/page/2/index.html", "Current index: 2"));
441484
assert!(file_contains!(public, "posts/page/2/index.html", "has_prev"));
@@ -448,11 +491,11 @@ fn can_build_site_with_pagination_for_section() {
448491
assert!(file_contains!(
449492
public,
450493
"posts/page/2/index.html",
451-
"Last: https://replace-this-with-your-url.com/posts/page/5/"
494+
"Last: https://replace-this-with-your-url.com/posts/page/6/"
452495
));
453496

454497
assert!(file_exists!(public, "posts/page/3/index.html"));
455-
assert!(file_contains!(public, "posts/page/3/index.html", "Num pagers: 5"));
498+
assert!(file_contains!(public, "posts/page/3/index.html", "Num pagers: 6"));
456499
assert!(file_contains!(public, "posts/page/3/index.html", "Page size: 2"));
457500
assert!(file_contains!(public, "posts/page/3/index.html", "Current index: 3"));
458501
assert!(file_contains!(public, "posts/page/3/index.html", "has_prev"));
@@ -465,11 +508,11 @@ fn can_build_site_with_pagination_for_section() {
465508
assert!(file_contains!(
466509
public,
467510
"posts/page/3/index.html",
468-
"Last: https://replace-this-with-your-url.com/posts/page/5/"
511+
"Last: https://replace-this-with-your-url.com/posts/page/6/"
469512
));
470513

471514
assert!(file_exists!(public, "posts/page/4/index.html"));
472-
assert!(file_contains!(public, "posts/page/4/index.html", "Num pagers: 5"));
515+
assert!(file_contains!(public, "posts/page/4/index.html", "Num pagers: 6"));
473516
assert!(file_contains!(public, "posts/page/4/index.html", "Page size: 2"));
474517
assert!(file_contains!(public, "posts/page/4/index.html", "Current index: 4"));
475518
assert!(file_contains!(public, "posts/page/4/index.html", "has_prev"));
@@ -482,7 +525,7 @@ fn can_build_site_with_pagination_for_section() {
482525
assert!(file_contains!(
483526
public,
484527
"posts/page/4/index.html",
485-
"Last: https://replace-this-with-your-url.com/posts/page/5/"
528+
"Last: https://replace-this-with-your-url.com/posts/page/6/"
486529
));
487530

488531
// sitemap contains the pager pages

Diff for: test_site/content/posts/access-render.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
+++
2+
title = 'render = false tests'
3+
slug = 'access-render'
4+
template = 'access_render.html'
5+
date = 2000-01-01
6+
+++
7+
8+
This post exists to test that unrendered sections and pages are still accessible
9+
via templates.

Diff for: test_site/content/posts/render.md

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ title = "Page with content but without generated folder"
33
description = ""
44
date = 2017-04-01
55
render = false
6+
7+
[taxonomies]
8+
categories = ["a-category", "b-category"]
9+
podcast_authors = ["Some Person", "Other Person"]
610
+++
711

812
Don't generate a folder for this page

Diff for: test_site/templates/access_render.html

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{% extends "index.html" %}
2+
3+
{% block content %}
4+
{% set unrendered_page = get_page(path='posts/render.md') %}
5+
{% set section_with_unrendered = get_section(path='posts/_index.md') %}
6+
{% set unrendered_section = get_section(path='posts/2018/_index.md') %}
7+
Path of unrendered page: {{ unrendered_page.path | safe }}
8+
Pages in section with unrendered page: <ul>{% for page in section_with_unrendered.pages -%}
9+
<li>{{ page.path | safe }}</li>
10+
{%- endfor %}</ul>
11+
Pages in unrendered section: <ul>{% for page in unrendered_section.pages -%}
12+
<li>{{ page.path | safe }}</li>
13+
{%- endfor %}</ul>
14+
{% endblock content %}
15+

0 commit comments

Comments
 (0)