Skip to content

Commit 41c3d06

Browse files
authored
Merge pull request #2713 from stomar/test-news-generator
Add tests for news archives plugin
2 parents d03e60d + 1e3c921 commit 41c3d06

File tree

3 files changed

+143
-1
lines changed

3 files changed

+143
-1
lines changed

Rakefile

+9-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ CONFIG = "_config.yml"
1414
task default: [:build]
1515

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

1919
desc "Build the Jekyll site"
2020
task :build do
@@ -136,3 +136,11 @@ Rake::TestTask.new(:"test-linter") do |t|
136136
t.test_files = FileList['test/test_linter_*.rb']
137137
t.verbose = true
138138
end
139+
140+
require "rake/testtask"
141+
Rake::TestTask.new(:"test-news-plugin") do |t|
142+
t.description = "Run tests for the news archive plugin"
143+
t.libs = ["test"]
144+
t.test_files = FileList['test/test_plugin_news.rb']
145+
t.verbose = true
146+
end

test/helper.rb

+10
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,13 @@ def linter_output
3737

3838
stdout
3939
end
40+
41+
def file_must_exist(filename)
42+
assert File.exist?(filename),
43+
"Expected file `#{filename}' to exist."
44+
end
45+
46+
def file_wont_exist(filename)
47+
assert !File.exist?(filename),
48+
"Expected file `#{filename}' to not exist."
49+
end

test/test_plugin_news.rb

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# frozen_string_literal: true
2+
3+
require "helper"
4+
5+
require "jekyll"
6+
require_relative "../_plugins/news"
7+
8+
9+
describe NewsArchivePlugin do
10+
11+
before do
12+
chdir_tempdir
13+
14+
content = <<~CONFIG
15+
CONFIG
16+
17+
create_file("source/_config.yml", content)
18+
19+
content = <<~LOCALES
20+
month_names:
21+
- January
22+
- February
23+
24+
news:
25+
recent_news: Recent News
26+
yearly_archive_title: "%Y Archives"
27+
monthly_archive_title: "%B %Y Archives"
28+
yearly_archive_link: "%Y Archives"
29+
monthly_archive_link: "%B %Y"
30+
LOCALES
31+
32+
create_file("source/_data/locales/en.yml", content)
33+
34+
content = <<~LAYOUT
35+
---
36+
layout: default
37+
---
38+
NEWS LAYOUT
39+
40+
{% for post in page.posts %}
41+
{{ post.title }}
42+
{% endfor %}
43+
LAYOUT
44+
45+
create_file("source/_layouts/news.html", content)
46+
47+
content = <<~LAYOUT
48+
---
49+
layout: default
50+
---
51+
NEWS ARCHIVE YEAR LAYOUT
52+
53+
{% for post in page.posts %}
54+
{{ post.title }}
55+
{% endfor %}
56+
LAYOUT
57+
58+
create_file("source/_layouts/news_archive_year.html", content)
59+
60+
content = <<~LAYOUT
61+
---
62+
layout: default
63+
---
64+
NEWS ARCHIVE MONTH LAYOUT
65+
66+
{% for post in page.posts %}
67+
{{ post.title }}
68+
{% endfor %}
69+
LAYOUT
70+
71+
create_file("source/_layouts/news_archive_month.html", content)
72+
73+
content = <<~POST
74+
---
75+
title: "Post Jan 2020"
76+
author: "stomar"
77+
date: 2020-01-01 12:00:00 +0000
78+
lang: en
79+
---
80+
81+
Content
82+
POST
83+
84+
create_file("source/en/news/_posts/2020-01-01-post.md", content)
85+
86+
config = Jekyll.configuration(
87+
source: "source",
88+
destination: "_site",
89+
quiet: true
90+
)
91+
site = Jekyll::Site.new(config)
92+
93+
file_wont_exist("_site")
94+
site.process
95+
end
96+
97+
after do
98+
teardown_tempdir
99+
end
100+
101+
it "should create news page" do
102+
file_must_exist("_site/en/news/index.html")
103+
end
104+
105+
it "should use the correct layout for news page" do
106+
_(File.read("_site/en/news/index.html")).must_match "NEWS LAYOUT"
107+
end
108+
109+
it "should create news/2020 page" do
110+
file_must_exist("_site/en/news/2020/index.html")
111+
end
112+
113+
it "should use the correct layout for news/2020 page" do
114+
_(File.read("_site/en/news/2020/index.html")).must_match "YEAR LAYOUT"
115+
end
116+
117+
it "should create news/2020/01 page" do
118+
file_must_exist("_site/en/news/2020/index.html")
119+
end
120+
121+
it "should use the correct layout for news/2020/01 page" do
122+
_(File.read("_site/en/news/2020/01/index.html")).must_match "MONTH LAYOUT"
123+
end
124+
end

0 commit comments

Comments
 (0)