jekyll_frontmatter_tests 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/jekyll_frontmatter_tests.rb +156 -0
  3. metadata +47 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3390788fc5e626d8e51c6abcddc9f25f0e9ba7b4
4
+ data.tar.gz: ea25e2525dd9916fb809bb7e2090b9d610534345
5
+ SHA512:
6
+ metadata.gz: 59ea56ad63966372f43ddb55f8f11d815bd2c9d172e6363abf89e9460bc6fa8ea20857a5d9bbf64c32296ba9986c583f1a5d5f4c14e00bba1419374552adb8b2
7
+ data.tar.gz: 92a55b109b7b71607f44dced8fb8d8c29f5ac2a481d076783be7110840d357d22e4ff971e0eef4dcd71fdefe028e093e5a8d7af4cf389435f625686453cc70c3
@@ -0,0 +1,156 @@
1
+ require 'yaml'
2
+ class FrontmatterTests < Jekyll::Command
3
+ class << self
4
+ def config
5
+ @config ||= YAML.load_file '_config.yml'
6
+ end
7
+ def loadschema(file)
8
+ schema = File.join("deploy", "tests", "schema", file)
9
+ if File.exists?(schema)
10
+ YAML.load_file(schema)
11
+ else
12
+ puts "No schema for #{file}"
13
+ exit 1
14
+ end
15
+ end
16
+
17
+ def process(schema)
18
+ dir = File.join(schema['config']['path'])
19
+ passfail = nil
20
+ Dir.open(dir).each do |f|
21
+ file = File.open(File.join(dir, f))
22
+ unless schema['config']['ignore'].include?(f)
23
+ data = YAML.load_file(file)
24
+ passfail = check_keys(data, schema.keys, f)
25
+ passfail = check_types(data, schema)
26
+ end
27
+ end
28
+ if passfail
29
+ return true
30
+ else
31
+ return false
32
+ end
33
+ end
34
+
35
+ def check_keys(data, keys, title)
36
+ keys = keys - ['config']
37
+ unless data.respond_to?('keys')
38
+ puts "The file #{title} is missing all frontmatter.".red
39
+ return false
40
+ end
41
+ diff = keys - data.keys
42
+ if diff == []
43
+ return true
44
+ else
45
+ puts "The file #{title} is missing the following keys:".red
46
+ for k in diff
47
+ puts " * #{k}\n".red
48
+ end
49
+ return false
50
+ end
51
+ end
52
+
53
+ # Works in progress: eventually, validate that the *values* match expected types
54
+ #
55
+ # For example, if we expect the `date` key to be in yyyy-mm-dd format, validate
56
+ # that it's been entered in that format. If we expect authors to be an array,
57
+ # make sure we're getting an array.
58
+ def check_types(data, schema)
59
+ unless data.respond_to?('keys')
60
+ return false
61
+ end
62
+ for s in schema
63
+ key = s[0]
64
+ if s[1].class == Hash
65
+ type = s[1]['type']
66
+ else
67
+ type = s[1]
68
+ end
69
+
70
+ if type == "Array" and data[key].class == Array
71
+ return true
72
+ elsif type == "String" and data[key].class == String
73
+ return true
74
+ elsif type == "Date"
75
+ require 'pry'; binding.pry
76
+ return true
77
+ else
78
+ puts " * Data is of the wrong type for key #{key}, expected #{type} but was #{data[key].class}\n\n"
79
+ return false
80
+ end
81
+ end
82
+ end
83
+
84
+ def test_posts
85
+ puts 'testing posts'.green
86
+ yepnope = process(loadschema('_posts.yml'))
87
+ puts 'Finished testing'.green
88
+ yepnope
89
+ end
90
+
91
+ def test_collections(collections)
92
+ yepnope = Array.new
93
+ unless collections.class == Array
94
+ require 'pry'; binding.pry
95
+ end
96
+ for c in collections
97
+ puts "Testing #{c}".green
98
+ yepnope.push process(loadschema("_#{c}.yml"))
99
+ puts "Finished testing #{c}".green
100
+ end
101
+ yepnope
102
+ end
103
+
104
+ def test_everything
105
+ schema = Dir.open('deploy/tests/schema')
106
+ yepnope = Array.new
107
+ schema.each { |s|
108
+ if s.start_with?('_')
109
+ puts "Testing #{s}".green
110
+ yepnope.push process(loadschema(s))
111
+ puts "Finished testing #{s}".green
112
+ end
113
+ }
114
+ yepnope
115
+ end
116
+
117
+ def test_frontmatter(args, options)
118
+ puts 'starting tests'
119
+ results = Array.new
120
+ if options['posts']
121
+ results.push test_posts
122
+ elsif options['collections']
123
+ collections = options['collections'].split(',')
124
+ results.push test_collections(collections)
125
+ else
126
+ results.push test_everything
127
+ end
128
+ results.keep_if { |t| t == false }
129
+ if results[0]
130
+ puts 'Tests finished!'
131
+ exit 0
132
+ else
133
+ puts "The test exited with errors, see above."
134
+ exit 1
135
+ end
136
+ end
137
+
138
+ def init_with_program(prog)
139
+ prog.command(:test) do |c|
140
+ c.syntax "test [options]"
141
+ c.description 'Test your site for frontmatter.'
142
+
143
+ c.option 'posts', '-p', 'Target only posts'
144
+ c.option 'collections', '-c [COLLECTION]', 'Target a specific collection'
145
+ c.option 'all', '-a', 'Test all collections (Default)'
146
+
147
+ c.action do |args, options|
148
+ if options.empty?
149
+ options = {"all" => true}
150
+ end
151
+ test_frontmatter(args, options)
152
+ end
153
+ end
154
+ end
155
+ end
156
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll_frontmatter_tests
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Greg Boone
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-30 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Tests the frontmatter of posts and other collection documents against
14
+ a schema
15
+ email:
16
+ - gregory.boone@gsa.gov
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/jekyll_frontmatter_tests.rb
22
+ homepage:
23
+ licenses:
24
+ - CC0
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.4.3
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Tests jekyll documents for proper frontmatter
46
+ test_files: []
47
+ has_rdoc: