Skip to content

Test news generator #2713

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ CONFIG = "_config.yml"
task default: [:build]

desc "Run tests (test-linter, lint, build)"
task test: %i[test-linter lint build]
task test: %i[test-news-plugin test-linter lint build]

desc "Build the Jekyll site"
task :build do
Expand Down Expand Up @@ -136,3 +136,11 @@ Rake::TestTask.new(:"test-linter") do |t|
t.test_files = FileList['test/test_linter_*.rb']
t.verbose = true
end

require "rake/testtask"
Rake::TestTask.new(:"test-news-plugin") do |t|
t.description = "Run tests for the news archive plugin"
t.libs = ["test"]
t.test_files = FileList['test/test_plugin_news.rb']
t.verbose = true
end
10 changes: 10 additions & 0 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,13 @@ def linter_output

stdout
end

def file_must_exist(filename)
assert File.exist?(filename),
"Expected file `#{filename}' to exist."
end

def file_wont_exist(filename)
assert !File.exist?(filename),
"Expected file `#{filename}' to not exist."
end
124 changes: 124 additions & 0 deletions test/test_plugin_news.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# frozen_string_literal: true

require "helper"

require "jekyll"
require_relative "../_plugins/news"


describe NewsArchivePlugin do

before do
chdir_tempdir

content = <<~CONFIG
CONFIG

create_file("source/_config.yml", content)

content = <<~LOCALES
month_names:
- January
- February

news:
recent_news: Recent News
yearly_archive_title: "%Y Archives"
monthly_archive_title: "%B %Y Archives"
yearly_archive_link: "%Y Archives"
monthly_archive_link: "%B %Y"
LOCALES

create_file("source/_data/locales/en.yml", content)

content = <<~LAYOUT
---
layout: default
---
NEWS LAYOUT

{% for post in page.posts %}
{{ post.title }}
{% endfor %}
LAYOUT

create_file("source/_layouts/news.html", content)

content = <<~LAYOUT
---
layout: default
---
NEWS ARCHIVE YEAR LAYOUT

{% for post in page.posts %}
{{ post.title }}
{% endfor %}
LAYOUT

create_file("source/_layouts/news_archive_year.html", content)

content = <<~LAYOUT
---
layout: default
---
NEWS ARCHIVE MONTH LAYOUT

{% for post in page.posts %}
{{ post.title }}
{% endfor %}
LAYOUT

create_file("source/_layouts/news_archive_month.html", content)

content = <<~POST
---
title: "Post Jan 2020"
author: "stomar"
date: 2020-01-01 12:00:00 +0000
lang: en
---

Content
POST

create_file("source/en/news/_posts/2020-01-01-post.md", content)

config = Jekyll.configuration(
source: "source",
destination: "_site",
quiet: true
)
site = Jekyll::Site.new(config)

file_wont_exist("_site")
site.process
end

after do
teardown_tempdir
end

it "should create news page" do
file_must_exist("_site/en/news/index.html")
end

it "should use the correct layout for news page" do
_(File.read("_site/en/news/index.html")).must_match "NEWS LAYOUT"
end

it "should create news/2020 page" do
file_must_exist("_site/en/news/2020/index.html")
end

it "should use the correct layout for news/2020 page" do
_(File.read("_site/en/news/2020/index.html")).must_match "YEAR LAYOUT"
end

it "should create news/2020/01 page" do
file_must_exist("_site/en/news/2020/index.html")
end

it "should use the correct layout for news/2020/01 page" do
_(File.read("_site/en/news/2020/01/index.html")).must_match "MONTH LAYOUT"
end
end