zola/components/site/tests/site.rs

446 lines
19 KiB
Rust
Raw Normal View History

2017-07-01 07:47:41 +00:00
extern crate site;
extern crate tempdir;
use std::env;
use std::path::Path;
use std::fs::File;
use std::io::prelude::*;
use tempdir::TempDir;
2017-07-01 07:47:41 +00:00
use site::Site;
#[test]
fn can_parse_site() {
let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
path.push("test_site");
let mut site = Site::new(&path, "config.toml").unwrap();
2017-03-21 07:57:00 +00:00
site.load().unwrap();
// Correct number of pages (sections are pages too)
assert_eq!(site.pages.len(), 14);
let posts_path = path.join("content").join("posts");
// Make sure we remove all the pwd + content from the sections
2017-03-19 10:29:43 +00:00
let basic = &site.pages[&posts_path.join("simple.md")];
assert_eq!(basic.file.components, vec!["posts".to_string()]);
// Make sure the page with a url doesn't have any sections
2017-03-19 10:29:43 +00:00
let url_post = &site.pages[&posts_path.join("fixed-url.md")];
assert_eq!(url_post.path, "a-fixed-url/");
// Make sure the article in a folder with only asset doesn't get counted as a section
2017-03-19 10:29:43 +00:00
let asset_folder_post = &site.pages[&posts_path.join("with-assets").join("index.md")];
assert_eq!(asset_folder_post.file.components, vec!["posts".to_string()]);
// That we have the right number of sections
assert_eq!(site.sections.len(), 7);
// And that the sections are correct
let index_section = &site.sections[&path.join("content").join("_index.md")];
assert_eq!(index_section.subsections.len(), 3);
2017-05-08 10:29:37 +00:00
assert_eq!(index_section.pages.len(), 1);
let posts_section = &site.sections[&posts_path.join("_index.md")];
assert_eq!(posts_section.subsections.len(), 1);
2017-09-25 09:55:43 +00:00
assert_eq!(posts_section.pages.len(), 6);
let tutorials_section = &site.sections[&posts_path.join("tutorials").join("_index.md")];
assert_eq!(tutorials_section.subsections.len(), 2);
2017-09-26 08:04:18 +00:00
assert_eq!(tutorials_section.subsections[0].clone().meta.title.unwrap(), "Programming");
assert_eq!(tutorials_section.subsections[1].clone().meta.title.unwrap(), "DevOps");
assert_eq!(tutorials_section.pages.len(), 0);
let devops_section = &site.sections[&posts_path.join("tutorials").join("devops").join("_index.md")];
assert_eq!(devops_section.subsections.len(), 0);
assert_eq!(devops_section.pages.len(), 2);
let prog_section = &site.sections[&posts_path.join("tutorials").join("programming").join("_index.md")];
assert_eq!(prog_section.subsections.len(), 0);
assert_eq!(prog_section.pages.len(), 2);
}
// 2 helper macros to make all the build testing more bearable
macro_rules! file_exists {
($root: expr, $path: expr) => {
{
let mut path = $root.clone();
for component in $path.split("/") {
path = path.join(component);
}
Path::new(&path).exists()
}
}
}
macro_rules! file_contains {
($root: expr, $path: expr, $text: expr) => {
{
let mut path = $root.clone();
for component in $path.split("/") {
path = path.join(component);
}
let mut file = File::open(&path).unwrap();
let mut s = String::new();
file.read_to_string(&mut s).unwrap();
2017-05-03 08:52:49 +00:00
println!("{}", s);
s.contains($text)
}
}
}
#[test]
fn can_build_site_without_live_reload() {
let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
path.push("test_site");
let mut site = Site::new(&path, "config.toml").unwrap();
2017-03-21 07:57:00 +00:00
site.load().unwrap();
let tmp_dir = TempDir::new("example").expect("create temp dir");
let public = &tmp_dir.path().join("public");
site.set_output_path(&public);
site.build().unwrap();
assert!(&public.exists());
assert!(file_exists!(public, "index.html"));
assert!(file_exists!(public, "sitemap.xml"));
2017-03-20 12:40:03 +00:00
assert!(file_exists!(public, "robots.txt"));
assert!(file_exists!(public, "a-fixed-url/index.html"));
assert!(file_exists!(public, "posts/python/index.html"));
2017-10-23 08:49:13 +00:00
// Shortcodes work
assert!(file_contains!(public, "posts/python/index.html", "Basic shortcode"));
2017-10-23 08:57:39 +00:00
assert!(file_contains!(public, "posts/python/index.html", "Arrrh Bob"));
assert!(file_contains!(public, "posts/python/index.html", "Arrrh Bob_Sponge"));
assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
assert!(file_exists!(public, "posts/with-assets/index.html"));
2017-05-08 10:29:37 +00:00
assert!(file_exists!(public, "posts/no-section/simple/index.html"));
// Sections
assert!(file_exists!(public, "posts/index.html"));
assert!(file_exists!(public, "posts/tutorials/index.html"));
assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
// Ensure subsection pages are correctly filled
assert!(file_contains!(public, "posts/tutorials/index.html", "Sub-pages: 2"));
// TODO: add assertion for syntax highlighting
2017-06-16 14:09:01 +00:00
// 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"));
// redirect_to works
assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
assert!(file_contains!(public, "posts/tutorials/devops/index.html", "docker"));
// No tags or categories
assert_eq!(file_exists!(public, "categories/index.html"), false);
assert_eq!(file_exists!(public, "tags/index.html"), false);
2017-08-23 10:17:24 +00:00
// Theme files are there
assert!(file_exists!(public, "sample.css"));
assert!(file_exists!(public, "some.js"));
// SASS and SCSS files compile correctly
assert!(file_exists!(public, "blog.css"));
assert!(file_contains!(public, "blog.css", "red"));
assert!(file_contains!(public, "blog.css", "blue"));
assert!(!file_contains!(public, "blog.css", "@import \"included\""));
assert!(file_contains!(public, "blog.css", "2rem")); // check include
assert!(!file_exists!(public, "_included.css"));
assert!(file_exists!(public, "scss.css"));
assert!(file_exists!(public, "nested_sass/scss.css"));
// no live reload code
assert_eq!(file_contains!(public, "index.html", "/livereload.js?port=1112&mindelay=10"), false);
2017-03-19 10:40:31 +00:00
// Both pages and sections are in the sitemap
assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/posts/simple/</loc>"));
assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/posts/</loc>"));
2017-09-25 09:55:43 +00:00
// Drafts are not in the sitemap
assert!(!file_contains!(public, "sitemap.xml", "draft"));
}
#[test]
fn can_build_site_with_live_reload() {
let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
path.push("test_site");
let mut site = Site::new(&path, "config.toml").unwrap();
2017-03-21 07:57:00 +00:00
site.load().unwrap();
let tmp_dir = TempDir::new("example").expect("create temp dir");
let public = &tmp_dir.path().join("public");
site.set_output_path(&public);
site.enable_live_reload();
site.build().unwrap();
assert!(Path::new(&public).exists());
assert!(file_exists!(public, "index.html"));
assert!(file_exists!(public, "sitemap.xml"));
2017-03-20 12:40:03 +00:00
assert!(file_exists!(public, "robots.txt"));
assert!(file_exists!(public, "a-fixed-url/index.html"));
assert!(file_exists!(public, "posts/python/index.html"));
assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
assert!(file_exists!(public, "posts/with-assets/index.html"));
// Sections
assert!(file_exists!(public, "posts/index.html"));
assert!(file_exists!(public, "posts/tutorials/index.html"));
assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
// TODO: add assertion for syntax highlighting
// No tags or categories
assert_eq!(file_exists!(public, "categories/index.html"), false);
assert_eq!(file_exists!(public, "tags/index.html"), false);
// no live reload code
assert!(file_contains!(public, "index.html", "/livereload.js?port=1112&mindelay=10"));
}
#[test]
fn can_build_site_with_categories() {
let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
path.push("test_site");
let mut site = Site::new(&path, "config.toml").unwrap();
2018-03-12 19:11:03 +00:00
site.config.generate_categories_pages = true;
2017-03-21 07:57:00 +00:00
site.load().unwrap();
2017-03-19 11:20:24 +00:00
2017-03-20 03:42:43 +00:00
for (i, page) in site.pages.values_mut().enumerate() {
2017-03-19 11:20:24 +00:00
page.meta.category = if i % 2 == 0 {
Some("A".to_string())
} else {
Some("B".to_string())
};
}
2017-03-21 07:57:00 +00:00
site.populate_tags_and_categories();
let tmp_dir = TempDir::new("example").expect("create temp dir");
2017-03-19 11:20:24 +00:00
let public = &tmp_dir.path().join("public");
site.set_output_path(&public);
site.build().unwrap();
2017-03-19 11:20:24 +00:00
assert!(Path::new(&public).exists());
2017-05-16 04:37:00 +00:00
assert_eq!(site.categories.unwrap().len(), 2);
2017-03-19 11:20:24 +00:00
assert!(file_exists!(public, "index.html"));
assert!(file_exists!(public, "sitemap.xml"));
2017-03-20 12:40:03 +00:00
assert!(file_exists!(public, "robots.txt"));
2017-03-19 11:20:24 +00:00
assert!(file_exists!(public, "a-fixed-url/index.html"));
assert!(file_exists!(public, "posts/python/index.html"));
assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
assert!(file_exists!(public, "posts/with-assets/index.html"));
// Sections
assert!(file_exists!(public, "posts/index.html"));
assert!(file_exists!(public, "posts/tutorials/index.html"));
assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
// TODO: add assertion for syntax highlighting
// Categories are there
assert!(file_exists!(public, "categories/index.html"));
assert!(file_exists!(public, "categories/a/index.html"));
assert!(file_exists!(public, "categories/b/index.html"));
2017-08-30 13:11:17 +00:00
// Extending from a theme works
assert!(file_contains!(public, "categories/a/index.html", "EXTENDED"));
2017-03-19 11:20:24 +00:00
// Tags aren't
assert_eq!(file_exists!(public, "tags/index.html"), false);
2017-03-20 03:42:43 +00:00
// Categories are in the sitemap
assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/categories/</loc>"));
assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/categories/a/</loc>"));
}
#[test]
fn can_build_site_with_tags() {
let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
path.push("test_site");
let mut site = Site::new(&path, "config.toml").unwrap();
2018-03-12 19:11:03 +00:00
site.config.generate_tags_pages = true;
2017-03-21 07:57:00 +00:00
site.load().unwrap();
2017-03-19 11:20:24 +00:00
2017-03-20 03:42:43 +00:00
for (i, page) in site.pages.values_mut().enumerate() {
2017-03-19 11:20:24 +00:00
page.meta.tags = if i % 2 == 0 {
Some(vec!["tag1".to_string(), "tag2".to_string()])
} else {
Some(vec!["tag with space".to_string()])
};
}
2017-03-21 07:57:00 +00:00
site.populate_tags_and_categories();
2017-03-19 11:20:24 +00:00
let tmp_dir = TempDir::new("example").expect("create temp dir");
2017-03-19 11:20:24 +00:00
let public = &tmp_dir.path().join("public");
site.set_output_path(&public);
site.build().unwrap();
2017-03-19 11:20:24 +00:00
assert!(Path::new(&public).exists());
2017-05-16 04:37:00 +00:00
assert_eq!(site.tags.unwrap().len(), 3);
2017-03-19 11:20:24 +00:00
assert!(file_exists!(public, "index.html"));
assert!(file_exists!(public, "sitemap.xml"));
2017-03-20 12:40:03 +00:00
assert!(file_exists!(public, "robots.txt"));
2017-03-19 11:20:24 +00:00
assert!(file_exists!(public, "a-fixed-url/index.html"));
assert!(file_exists!(public, "posts/python/index.html"));
assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
assert!(file_exists!(public, "posts/with-assets/index.html"));
// Sections
assert!(file_exists!(public, "posts/index.html"));
assert!(file_exists!(public, "posts/tutorials/index.html"));
assert!(file_exists!(public, "posts/tutorials/devops/index.html"));
assert!(file_exists!(public, "posts/tutorials/programming/index.html"));
// TODO: add assertion for syntax highlighting
// Tags are there
assert!(file_exists!(public, "tags/index.html"));
assert!(file_exists!(public, "tags/tag1/index.html"));
assert!(file_exists!(public, "tags/tag2/index.html"));
assert!(file_exists!(public, "tags/tag-with-space/index.html"));
// Categories aren't
assert_eq!(file_exists!(public, "categories/index.html"), false);
2017-03-20 03:42:43 +00:00
// Tags are in the sitemap
assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/tags/</loc>"));
assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/tags/tag-with-space/</loc>"));
}
2017-04-10 11:26:35 +00:00
#[test]
fn can_build_site_and_insert_anchor_links() {
let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
2017-04-10 11:26:35 +00:00
path.push("test_site");
let mut site = Site::new(&path, "config.toml").unwrap();
site.load().unwrap();
2017-04-10 11:26:35 +00:00
let tmp_dir = TempDir::new("example").expect("create temp dir");
let public = &tmp_dir.path().join("public");
site.set_output_path(&public);
site.build().unwrap();
assert!(Path::new(&public).exists());
// anchor link inserted
2017-05-23 10:49:10 +00:00
assert!(file_contains!(public, "posts/something-else/index.html", "<h1 id=\"title\"><a class=\"gutenberg-anchor\" href=\"#title\""));
2017-04-10 11:26:35 +00:00
}
2017-05-03 08:52:49 +00:00
#[test]
fn can_build_site_with_pagination_for_section() {
let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
2017-05-03 08:52:49 +00:00
path.push("test_site");
let mut site = Site::new(&path, "config.toml").unwrap();
site.load().unwrap();
for section in site.sections.values_mut(){
if section.is_index() {
continue;
}
2017-05-03 08:52:49 +00:00
section.meta.paginate_by = Some(2);
section.meta.template = Some("section_paginated.html".to_string());
}
let tmp_dir = TempDir::new("example").expect("create temp dir");
let public = &tmp_dir.path().join("public");
site.set_output_path(&public);
site.build().unwrap();
assert!(Path::new(&public).exists());
assert!(file_exists!(public, "index.html"));
assert!(file_exists!(public, "sitemap.xml"));
assert!(file_exists!(public, "robots.txt"));
assert!(file_exists!(public, "a-fixed-url/index.html"));
assert!(file_exists!(public, "posts/python/index.html"));
assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
assert!(file_exists!(public, "posts/with-assets/index.html"));
// Sections
assert!(file_exists!(public, "posts/index.html"));
// And pagination!
assert!(file_exists!(public, "posts/page/1/index.html"));
// even if there is no pages, only the section!
assert!(file_exists!(public, "paginated/page/1/index.html"));
assert!(file_exists!(public, "paginated/index.html"));
2017-05-03 08:52:49 +00:00
// should redirect to posts/
assert!(file_contains!(
public,
"posts/page/1/index.html",
"http-equiv=\"refresh\" content=\"0;url=https://replace-this-with-your-url.com/posts/\""
2017-05-03 08:52:49 +00:00
));
2017-05-08 10:29:37 +00:00
assert!(file_contains!(public, "posts/index.html", "Num pagers: 3"));
2017-05-03 08:52:49 +00:00
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_next"));
assert!(file_contains!(public, "posts/index.html", "First: https://replace-this-with-your-url.com/posts/"));
assert!(file_contains!(public, "posts/index.html", "Last: https://replace-this-with-your-url.com/posts/page/3/"));
2017-05-03 08:52:49 +00:00
assert_eq!(file_contains!(public, "posts/index.html", "has_prev"), false);
assert!(file_exists!(public, "posts/page/2/index.html"));
2017-05-08 10:29:37 +00:00
assert!(file_contains!(public, "posts/page/2/index.html", "Num pagers: 3"));
2017-05-03 08:52:49 +00:00
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"));
2017-05-08 10:29:37 +00:00
assert!(file_contains!(public, "posts/page/2/index.html", "has_next"));
assert!(file_contains!(public, "posts/page/2/index.html", "First: https://replace-this-with-your-url.com/posts/"));
assert!(file_contains!(public, "posts/page/2/index.html", "Last: https://replace-this-with-your-url.com/posts/page/3/"));
2017-05-03 08:52:49 +00:00
}
#[test]
fn can_build_site_with_pagination_for_index() {
let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
2017-05-03 08:52:49 +00:00
path.push("test_site");
let mut site = Site::new(&path, "config.toml").unwrap();
site.load().unwrap();
2017-05-08 10:29:37 +00:00
{
2017-09-25 09:55:43 +00:00
let index = site.sections.get_mut(&path.join("content").join("_index.md")).unwrap();
2017-05-08 10:29:37 +00:00
index.meta.paginate_by = Some(2);
index.meta.template = Some("index_paginated.html".to_string());
}
2017-05-03 08:52:49 +00:00
let tmp_dir = TempDir::new("example").expect("create temp dir");
let public = &tmp_dir.path().join("public");
site.set_output_path(&public);
site.build().unwrap();
assert!(Path::new(&public).exists());
assert!(file_exists!(public, "index.html"));
assert!(file_exists!(public, "sitemap.xml"));
assert!(file_exists!(public, "robots.txt"));
assert!(file_exists!(public, "a-fixed-url/index.html"));
assert!(file_exists!(public, "posts/python/index.html"));
assert!(file_exists!(public, "posts/tutorials/devops/nix/index.html"));
assert!(file_exists!(public, "posts/with-assets/index.html"));
// And pagination!
assert!(file_exists!(public, "page/1/index.html"));
// even if there is no pages, only the section!
assert!(file_exists!(public, "paginated/page/1/index.html"));
assert!(file_exists!(public, "paginated/index.html"));
2017-05-03 08:52:49 +00:00
// should redirect to index
assert!(file_contains!(
public,
"page/1/index.html",
"http-equiv=\"refresh\" content=\"0;url=https://replace-this-with-your-url.com/\""
));
2017-05-08 10:29:37 +00:00
assert!(file_contains!(public, "index.html", "Num pages: 1"));
2017-05-03 08:52:49 +00:00
assert!(file_contains!(public, "index.html", "Current index: 1"));
assert!(file_contains!(public, "index.html", "First: https://replace-this-with-your-url.com/"));
2017-05-08 10:29:37 +00:00
assert!(file_contains!(public, "index.html", "Last: https://replace-this-with-your-url.com/"));
2017-05-03 08:52:49 +00:00
assert_eq!(file_contains!(public, "index.html", "has_prev"), false);
2017-05-08 10:29:37 +00:00
assert_eq!(file_contains!(public, "index.html", "has_next"), false);
2017-05-03 08:52:49 +00:00
}
2017-07-15 03:51:32 +00:00
#[test]
fn can_build_rss_feed() {
let mut path = env::current_dir().unwrap().parent().unwrap().parent().unwrap().to_path_buf();
2017-07-15 03:51:32 +00:00
path.push("test_site");
let mut site = Site::new(&path, "config.toml").unwrap();
site.load().unwrap();
let tmp_dir = TempDir::new("example").expect("create temp dir");
let public = &tmp_dir.path().join("public");
site.set_output_path(&public);
site.build().unwrap();
assert!(Path::new(&public).exists());
assert!(file_exists!(public, "rss.xml"));
// latest article is posts/simple.md
assert!(file_contains!(public, "rss.xml", "Simple article with shortcodes"));
// Next is posts/python.md
assert!(file_contains!(public, "rss.xml", "Python in posts"));
}