jekyll_frontmatter_tests 0.0.13 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d5c718f92a10c456fc3de9200cb9ee118be1ae6d
4
- data.tar.gz: 30a742446ac86710459c13c3134f30dbf73b7bc2
3
+ metadata.gz: 00fc0de9a96a87565919528bf16f8cecad3c3df6
4
+ data.tar.gz: aa548a8fc5dee79b5b7ff7907987a15a4024dfe6
5
5
  SHA512:
6
- metadata.gz: 4b04dc99b1c587aa6f31fa96d519035584742fc8b85d243bd13cbf521066f5c717793c3c331e61f5b7e88eb563de0f15b5dd565622e06d12c4480c5ef431c903
7
- data.tar.gz: 009eacfd2f6f535fa7a8402198e52761ad588a8bd3d9143066466bac8bcc3c4f5ef8f17e57ac67c00c1610d2a0ca1403f2423ed24be19baf1757432554860bf5
6
+ metadata.gz: 48ebfb873111a3966f5bf0e074d64f1a487f60a4ad9b25c1485ef93229db913920eba78dd1eabf9c740109fb93fb021b4559700f47b8426568af47a1517e5b33
7
+ data.tar.gz: b9ca0920616cc2e705313ef047827cb965925dae96fa0dbd75883b515c9afcfd56ec0bf9b83a27e19c134708837936afe9c2a53f069a5d1989e1fe37e3e3678d
@@ -1,3 +1,7 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'jekyll_frontmatter_tests/version'
4
+
1
5
  Gem::Specification.new do |s|
2
6
  s.description = 'Tests the frontmatter of posts and other collection documents against a schema'
3
7
  s.summary = 'Tests jekyll documents for proper frontmatter'
@@ -6,7 +10,7 @@ Gem::Specification.new do |s|
6
10
  s.license = 'CC0'
7
11
  s.authors = ['Greg Boone']
8
12
  s.email = ['gregory.boone@gsa.gov']
9
- s.version = '0.0.13'
13
+ s.version = JekyllFrontmatterTests::VERSION
10
14
  s.files = %w{
11
15
  jekyll-frontmatter-tests.gemspec
12
16
  Gemfile
@@ -6,6 +6,7 @@ require_relative 'jekyll_frontmatter_tests/jekyll_frontmatter_tests_initializer'
6
6
  require_relative 'jekyll_frontmatter_tests/jekyll_frontmatter_tests_tester'
7
7
  require_relative 'jekyll_frontmatter_tests/jekyll_frontmatter_tests_loader'
8
8
  require_relative 'jekyll_frontmatter_tests/jekyll_frontmatter_tests_processor'
9
+ require_relative 'jekyll_frontmatter_tests/jekyll_frontmatter_tests_rules'
9
10
  require_relative 'jekyll_frontmatter_tests/jekyll_frontmatter_tests_validator'
10
11
  require_relative 'jekyll_frontmatter_tests/jekyll_frontmatter_tests_helper'
11
12
 
@@ -16,11 +16,28 @@ class FrontmatterTests < Jekyll::Command
16
16
  end
17
17
  end
18
18
 
19
+ def follows_rules?(value, rules)
20
+ if rules.include?('no-dash') && rules.include?('lowercase')
21
+ FrontmatterRules.dashless?(value) && FrontmatterRules.lowercase?(value)
22
+ elsif rules.include?('no-dash') && !rules.include?('lowercase')
23
+ FrontmatterRules.dashless?(value)
24
+ elsif !rules.include?('no-dash') && rules.include?('lowercase')
25
+ FrontmatterRules.lowercase?(value)
26
+ end
27
+ end
28
+
19
29
  def required?(key, schema)
20
- if schema['config']
21
- !schema['config']['optional'].include? key
30
+ is_required = true
31
+ is_primary = schema[key]
32
+ schema['config'] = schema['config'] || { 'optional': [] }
33
+ is_optional = schema['config']['optional'].include?(key)
34
+
35
+ if is_primary && !is_optional
36
+ is_required
37
+ elsif (is_primary && is_optional) || (!is_primary && is_optional)
38
+ !is_required
22
39
  else
23
- true
40
+ raise 'The key provided is not in the schema.'
24
41
  end
25
42
  end
26
43
  end
@@ -0,0 +1,52 @@
1
+ require 'pry'
2
+ require 'yaml'
3
+
4
+ class FrontmatterRules
5
+ class << self
6
+ # Returns true if there are no dashes in any of the values
7
+ def dashless?(values)
8
+ rules_config = RulesConfig.new
9
+ rules = rules_config.rules['no-dash'] || rules_config.empty_rule
10
+ exceptions = rules['exceptions'].compact
11
+ if values.instance_of?(Array)
12
+ no_dashes = values.map do |value|
13
+ (!value.include?('-') && !value.include?('–')) || exceptions.include?(value)
14
+ end
15
+ # no_dashes will only have false values if there are dashes present
16
+ !no_dashes.include? false
17
+ elsif values.instance_of?(String)
18
+ (!values.include?('-') && !values.include?('–')) || exceptions.include?(values)
19
+ end
20
+ end
21
+
22
+ # Returns true if there are no uppercase characters in any of the values
23
+ def lowercase?(values)
24
+ rules_config = RulesConfig.new
25
+ rules = rules_config.rules['lowercase'] || rules_config.empty_rule
26
+ exceptions = rules['exceptions'].compact
27
+ if values.instance_of?(Array)
28
+ all_lowercase = values.map do |value|
29
+ (value.downcase == value) || exceptions.include?(value)
30
+ end
31
+ # all_lowercase will only have false values if there are are uppercase
32
+ # characters present
33
+ !all_lowercase.include? false
34
+ elsif values.instance_of?(String)
35
+ (values.downcase === values) || exceptions.include?(values)
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ class RulesConfig
42
+ def initialize
43
+ rules_yml = File.join(Dir.pwd, 'tests', 'schema', 'rules.yml')
44
+ gem_path = $LOAD_PATH.select { |p| p.include? 'jekyll_frontmatter_tests' }
45
+ default_rules_yml = File.join(gem_path, 'rules.yml')
46
+ rules_config = File.exists?(rules_yml) ? rules_yml : default_rules_yml
47
+ @rules = YAML.load_file(rules_config)
48
+ @empty_rule = { "exceptions"=>[] }
49
+ end
50
+
51
+ attr_reader :rules, :empty_rule
52
+ end
@@ -27,6 +27,10 @@ class FrontmatterTests < Jekyll::Command
27
27
  end
28
28
  end
29
29
 
30
+ def basepath
31
+ File.join(Dir.pwd, 'tests', 'schema')
32
+ end
33
+
30
34
  # Internal: eventually, validate that the *values* match expected types
31
35
  #
32
36
  # For example, if we expect the `date` key to be in yyyy-mm-dd format, validate
@@ -42,22 +46,16 @@ class FrontmatterTests < Jekyll::Command
42
46
  else
43
47
  value
44
48
  end
49
+
45
50
  next unless required?(key, schema)
46
51
  if key == 'config'
47
52
  next
48
53
  elsif value.class == Hash
49
- if value.keys.include? 'one_of'
50
- if !one_of?(data[key], value['one_of'])
51
- puts " * '#{data[key]}' was not in the list " \
52
- "of expected values in #{file}.".red
53
- puts " expected one of the following: #{s[1]['one_of']}\n".red
54
- return false
55
- else
56
- next
57
- end
58
- else
59
- next
60
- end
54
+ next unless value.keys.include?('one_of') || value.keys.include?('rules')
55
+ violate_one_of = check_one_of(data, key, value) == false
56
+ violate_rules = check_rules(data, key, value) == false
57
+ return false if violate_one_of || violate_rules
58
+
61
59
  elsif type == 'Array' && data[key].class == Array
62
60
  next
63
61
  elsif type == 'Boolean' && data[key].is_a?(Boolean)
@@ -67,11 +65,30 @@ class FrontmatterTests < Jekyll::Command
67
65
  elsif type == 'Date'
68
66
  next
69
67
  else
70
- puts " * '#{key}' is not a valid key in #{file}. " \
68
+ puts " * invalid value for '#{key}' in #{file}. " \
71
69
  "Expected #{type} but was #{data[key].class}\n\n"
72
70
  return false
73
71
  end
74
72
  end
75
73
  end
74
+
75
+ def check_one_of(data, key, value)
76
+ if value.keys.include?('one_of') && !one_of?(data[key], value['one_of'])
77
+ puts " * One of error: One of '#{data[key]}' was not".red
78
+ puts " in the list of expected values in".red
79
+ puts " #{File.join(basepath, value['one_of'])}\n".yellow
80
+
81
+ false
82
+ end
83
+ end
84
+
85
+ def check_rules(data, key, value)
86
+ if value.keys.include?('rules') && !follows_rules?(data[key], value['rules'])
87
+ puts " * Rules error: One of '#{data[key]}'".red
88
+ puts " doesn't follow the rules defined in".red
89
+ puts " #{basepath}/rules.yml\n".yellow
90
+ false
91
+ end
92
+ end
76
93
  end
77
94
  end
@@ -0,0 +1,3 @@
1
+ module JekyllFrontmatterTests
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,4 @@
1
+ no-dash:
2
+ exceptions:
3
+ lowercase:
4
+ exceptions:
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.13
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Greg Boone
@@ -172,8 +172,11 @@ files:
172
172
  - lib/jekyll_frontmatter_tests/jekyll_frontmatter_tests_initializer.rb
173
173
  - lib/jekyll_frontmatter_tests/jekyll_frontmatter_tests_loader.rb
174
174
  - lib/jekyll_frontmatter_tests/jekyll_frontmatter_tests_processor.rb
175
+ - lib/jekyll_frontmatter_tests/jekyll_frontmatter_tests_rules.rb
175
176
  - lib/jekyll_frontmatter_tests/jekyll_frontmatter_tests_tester.rb
176
177
  - lib/jekyll_frontmatter_tests/jekyll_frontmatter_tests_validator.rb
178
+ - lib/jekyll_frontmatter_tests/version.rb
179
+ - lib/rules.yml
177
180
  homepage: https://rubygems.org/gems/jekyll_frontmatter_tests
178
181
  licenses:
179
182
  - CC0