jekyll_frontmatter_tests 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/jekyll_frontmatter_tests.rb +89 -33
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3390788fc5e626d8e51c6abcddc9f25f0e9ba7b4
4
- data.tar.gz: ea25e2525dd9916fb809bb7e2090b9d610534345
3
+ metadata.gz: 88e4c74f874f32f982b5e0023f1ddaa4f98b41ae
4
+ data.tar.gz: 4e91934e6e0d3071e829b02cbf665a2da0843dcc
5
5
  SHA512:
6
- metadata.gz: 59ea56ad63966372f43ddb55f8f11d815bd2c9d172e6363abf89e9460bc6fa8ea20857a5d9bbf64c32296ba9986c583f1a5d5f4c14e00bba1419374552adb8b2
7
- data.tar.gz: 92a55b109b7b71607f44dced8fb8d8c29f5ac2a481d076783be7110840d357d22e4ff971e0eef4dcd71fdefe028e093e5a8d7af4cf389435f625686453cc70c3
6
+ metadata.gz: d13d39f28494afb64a01d87ae1833d8ff8af4a2a721757f8cd7396d2ee462ef58fdd4db8826165a9f8d2c8ce6f2eb3c746ffc50f213f9dba84d05dda0e5fece8
7
+ data.tar.gz: 899b45eeab1d52196e8468fe6b2a65c543840e153da30c2011c93f39f2674380fbe37d03277cb1286577df50dae840c6cd6b3b432ddf8a7033c5a26712f47db8
@@ -1,9 +1,22 @@
1
1
  require 'yaml'
2
2
  class FrontmatterTests < Jekyll::Command
3
3
  class << self
4
+ # Public: load the configuration file
5
+ #
6
+ # Assumes this is a standard jekyll site
4
7
  def config
5
8
  @config ||= YAML.load_file '_config.yml'
6
9
  end
10
+
11
+ # Public: Load a schema from file.
12
+ #
13
+ # file - a string containing a filename
14
+ #
15
+ # Used throughout to load a specific file. In the future the directories
16
+ # where these schema files are located could be loaded from _config.yml
17
+ #
18
+ # Returns a hash loaded from the YAML doc or exits 1 if no schema file
19
+ # exists.
7
20
  def loadschema(file)
8
21
  schema = File.join("deploy", "tests", "schema", file)
9
22
  if File.exists?(schema)
@@ -14,6 +27,15 @@ class FrontmatterTests < Jekyll::Command
14
27
  end
15
28
  end
16
29
 
30
+ # Public: processes a collection against a schema
31
+ #
32
+ # schmea - the hash-representation of a schema file
33
+ #
34
+ # Opens each file in the collection's expected directory and checks the
35
+ # file's frontmatter for the expected keys and the expected format of the
36
+ # values.
37
+ #
38
+ # Returns true or false depending on the success of the check.
17
39
  def process(schema)
18
40
  dir = File.join(schema['config']['path'])
19
41
  passfail = nil
@@ -32,13 +54,19 @@ class FrontmatterTests < Jekyll::Command
32
54
  end
33
55
  end
34
56
 
35
- def check_keys(data, keys, title)
57
+ # Public: checks a hash for expected keys
58
+ #
59
+ # target - the hash under test
60
+ # keys - an array of keys the data is expected to have, usually loaded from
61
+ # a schema file by loadschema()
62
+ # title - A string representing `data`'s name
63
+ def check_keys(target, keys, title)
36
64
  keys = keys - ['config']
37
- unless data.respond_to?('keys')
65
+ unless target.respond_to?('keys')
38
66
  puts "The file #{title} is missing all frontmatter.".red
39
67
  return false
40
68
  end
41
- diff = keys - data.keys
69
+ diff = keys - target.keys
42
70
  if diff == []
43
71
  return true
44
72
  else
@@ -50,37 +78,10 @@ class FrontmatterTests < Jekyll::Command
50
78
  end
51
79
  end
52
80
 
53
- # Works in progress: eventually, validate that the *values* match expected types
81
+ # Public: tests all documents that are "posts"
54
82
  #
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
-
83
+ # Loads a schema called _posts.yml and processes all post documents against
84
+ # it.
84
85
  def test_posts
85
86
  puts 'testing posts'.green
86
87
  yepnope = process(loadschema('_posts.yml'))
@@ -88,6 +89,12 @@ class FrontmatterTests < Jekyll::Command
88
89
  yepnope
89
90
  end
90
91
 
92
+ # Public: Tests only specific collection documents
93
+ #
94
+ # collections - a comma separated string of collection names.
95
+ #
96
+ # `collections` is split into an array and each document is loaded and
97
+ # processed against its respective schema.
91
98
  def test_collections(collections)
92
99
  yepnope = Array.new
93
100
  unless collections.class == Array
@@ -101,6 +108,8 @@ class FrontmatterTests < Jekyll::Command
101
108
  yepnope
102
109
  end
103
110
 
111
+ # Public: Tests all collections described by a schema file at
112
+ # `deploy/tests/scema`
104
113
  def test_everything
105
114
  schema = Dir.open('deploy/tests/schema')
106
115
  yepnope = Array.new
@@ -114,6 +123,19 @@ class FrontmatterTests < Jekyll::Command
114
123
  yepnope
115
124
  end
116
125
 
126
+ # Public: Processes options passed throguh the command line, runs
127
+ # the appropriate tests.
128
+ #
129
+ # args - command line arguments (example: jekyll test [ARG])
130
+ # options - command line options (example: jekyll test -[option] [value])
131
+ #
132
+ # Depending on the flag passed (see `init_with_program`), runs the expected # test.
133
+ #
134
+ # Example: the following comamnd `jekyll test -p` will pass ``{'posts' =>
135
+ # true}` as `options`. This will cause `test_frontmatter` to
136
+ # compare all docs in _posts with the provided schema.
137
+ #
138
+ # The test runner pushes the result of each test into a `results` array and # exits `1` if any tests fail or `0` if all is well.
117
139
  def test_frontmatter(args, options)
118
140
  puts 'starting tests'
119
141
  results = Array.new
@@ -135,6 +157,10 @@ class FrontmatterTests < Jekyll::Command
135
157
  end
136
158
  end
137
159
 
160
+ # Internal: fired when `jekyll test` is run.
161
+ #
162
+ # When `jekyll test` runs, `test_frontmatter` is fired with options and args
163
+ # passed from the command line.
138
164
  def init_with_program(prog)
139
165
  prog.command(:test) do |c|
140
166
  c.syntax "test [options]"
@@ -152,5 +178,35 @@ class FrontmatterTests < Jekyll::Command
152
178
  end
153
179
  end
154
180
  end
181
+ # Internal: eventually, validate that the *values* match expected types
182
+ #
183
+ # For example, if we expect the `date` key to be in yyyy-mm-dd format, validate
184
+ # that it's been entered in that format. If we expect authors to be an array,
185
+ # make sure we're getting an array.
186
+ def check_types(data, schema)
187
+ unless data.respond_to?('keys')
188
+ return false
189
+ end
190
+ for s in schema
191
+ key = s[0]
192
+ if s[1].class == Hash
193
+ type = s[1]['type']
194
+ else
195
+ type = s[1]
196
+ end
197
+
198
+ if type == "Array" and data[key].class == Array
199
+ return true
200
+ elsif type == "String" and data[key].class == String
201
+ return true
202
+ elsif type == "Date"
203
+ require 'pry'; binding.pry
204
+ return true
205
+ else
206
+ puts " * Data is of the wrong type for key #{key}, expected #{type} but was #{data[key].class}\n\n"
207
+ return false
208
+ end
209
+ end
210
+ end
155
211
  end
156
212
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll_frontmatter_tests
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Greg Boone
@@ -19,7 +19,7 @@ extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
21
  - lib/jekyll_frontmatter_tests.rb
22
- homepage:
22
+ homepage: https://github.com/18f/jekyll-frontmatter-tests
23
23
  licenses:
24
24
  - CC0
25
25
  metadata: {}