Skip to content

Commit 0bd0888

Browse files
committed
Add lint tests
1 parent b961c95 commit 0bd0888

File tree

7 files changed

+90
-1
lines changed

7 files changed

+90
-1
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
source "https://rubygems.org"
22

33
gem "github-pages"
4+
gem "rake"

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ GEM
104104
sawyer (~> 0.7.0, >= 0.5.3)
105105
pkg-config (1.1.7)
106106
public_suffix (1.5.3)
107+
rake (11.2.2)
107108
rb-fsevent (0.9.7)
108109
rb-inotify (0.9.7)
109110
ffi (>= 0.5.0)
@@ -125,6 +126,7 @@ PLATFORMS
125126

126127
DEPENDENCIES
127128
github-pages
129+
rake
128130

129131
BUNDLED WITH
130132
1.12.5

Rakefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require "rake/testtask"
2+
Rake::TestTask.new do |t|
3+
t.libs << "test"
4+
t.test_files = FileList["test/*_test.rb"]
5+
t.warning = false
6+
t.verbose = false
7+
end
8+
9+
task :default => :test

_config.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ baseurl: "/open-source-handbook"
22

33
exclude:
44
- docs
5+
- Gemfile*
6+
- Rakefile
57
- README.md
68
- script
7-
- Gemfile*
9+
- test
810

911
collections:
1012
content:

_data/fields.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Each piece of content has YAML front matter with these properties:
2+
3+
title:
4+
required: true
5+
6+
next:
7+
type: String
8+
9+
previous:
10+
type: String

test/helper.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require "minitest/autorun"
2+
require "jekyll"
3+
4+
module Helper
5+
class << self
6+
attr_accessor :config, :site
7+
end
8+
end
9+
10+
def source
11+
File.expand_path('../', File.dirname(__FILE__))
12+
end
13+
14+
def config
15+
Helper.config ||= Jekyll.configuration("source" => source)
16+
end
17+
18+
def content
19+
site.collections['content'].docs.map { |doc| doc.to_liquid }
20+
end
21+
22+
def site
23+
Helper.site ||= begin
24+
site = Jekyll::Site.new(config)
25+
site.reset
26+
site.read
27+
site
28+
end
29+
end

test/lint_test.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
require_relative "./helper"
2+
3+
describe "lint test" do
4+
5+
content.each do |page|
6+
7+
describe page["path"] do
8+
9+
describe "frontmatter" do
10+
11+
before do
12+
# Load raw metadata to skip defaults
13+
@data = SafeYAML.load_file(page["path"])
14+
end
15+
16+
it "contains supported fields" do
17+
extra_fields = @data.keys - site.data["fields"].keys
18+
assert extra_fields.empty?, "Unexpected metadata in #{page["path"]}: #{extra_fields.inspect}"
19+
end
20+
21+
site.data["fields"].each do |name, attrs|
22+
it "#{name} is required" do
23+
assert @data.key?(name), "#{name} is required"
24+
end if attrs["required"]
25+
26+
it "#{name} should be of type #{attrs["type"]}" do
27+
assert_kind_of Kernel.const_get(attrs["type"]), @data[name] if @data[name]
28+
end if attrs["type"]
29+
30+
it "#{name} has valid attributes" do
31+
end
32+
end
33+
end
34+
end
35+
end
36+
end

0 commit comments

Comments
 (0)