Skip to content

Commit 748b153

Browse files
authored
Merge pull request #467 from DannyBen/add/invalid-schema-tests
Improve JSON schema tests
2 parents 261d104 + b2d8447 commit 748b153

File tree

10 files changed

+74
-3
lines changed

10 files changed

+74
-3
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
This folder contains invalid schema validations.
2+
3+
Each subfolder is named after the schema.
4+
5+
The tests can be run with `run schema invalid` and are included in the
6+
`run schema all` test (which is executed in CI).
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
invalid: not allowed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
args:
2+
- name: path
3+
x_valid: allowed
4+
invalid: not allowed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
flags:
2+
- long: --path
3+
x_valid: allowed
4+
invalid: not allowed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
environment_variables:
2+
- name: api_key
3+
x_valid: allowed
4+
invalid: not allowed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo: bar
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
usage_colors: true
2+
partials_extension: [1, 2]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo: bar
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
usage: true

support/runfile/schema.runfile

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,24 @@ action :all do
66
check_settings
77
check_strings
88
check_arbitrary
9+
check_invalid
10+
end
11+
12+
help 'Test any file against any schema'
13+
usage 'file [--anti] PATH [SCHEMA]'
14+
option '-a, --anti', 'Run anticheck instead (expect failure)'
15+
param 'PATH', 'The path to the tested YAML file'
16+
param 'SCHEMA', 'The name of the schema (bashly, settings of strings) [default: bashly]'
17+
example 'run schema file -a spec/fixtures/schemas_invalid/bashly/1.yml'
18+
example 'run schema file -a spec/fixtures/schemas_invalid/settings/2.yml settings'
19+
action(:file) do |args|
20+
schema = args['SCHEMA'] || 'bashly'
21+
if args['--anti']
22+
schema_anticheck args['PATH'], schema
23+
else
24+
schema_check args['PATH'], schema
25+
end
26+
say "\ngub`PASS`"
927
end
1028

1129
help 'Test the bashly schema against a single example'
@@ -24,31 +42,52 @@ action(:strings) { check_strings }
2442
help 'Test the bashly schema against a bashly configuration that includes arbitrary (x-) keys'
2543
action(:arbitrary) { check_arbitrary }
2644

45+
help 'Verify that all the invalid schemas in spec/fixtures/schemas_invalid fail as expected'
46+
action(:invalid) { check_invalid }
47+
2748
helpers do
2849
def check_examples
2950
say "\ngub`Examples`"
3051
Example.all.each do |example|
3152
file = example.yaml_path
3253
schema_check file
3354
end
55+
say "\ngub`Examples PASS`"
3456
end
3557

3658
def check_settings
37-
say "\ngub`Settings`"
59+
say "\ngub`Settings schema`"
3860
file = 'lib/bashly/libraries/settings/settings.yml'
3961
schema_check file, :settings
62+
say "\ngub`Settings schema PASS`"
4063
end
4164

4265
def check_strings
43-
say "\ngub`Strings`"
66+
say "\ngub`Strings schema`"
4467
file = 'lib/bashly/libraries/strings/strings.yml'
4568
schema_check file, :strings
69+
say "\ngub`Strings schema PASS`"
4670
end
4771

4872
def check_arbitrary
49-
say "\ngub`Arbitrary`"
73+
say "\ngub`Arbitrary arguments`"
5074
file = 'spec/fixtures/script/x_arbitrary.yml'
5175
schema_check file
76+
say "\ngub`Arbitrary arguments PASS`"
77+
end
78+
79+
def check_invalid
80+
say "\ngub`Invalid files`"
81+
basedir = 'spec/fixtures/schemas_invalid'
82+
files = Dir.chdir(basedir) { Dir['**/*.yml'] }
83+
84+
files.each do |file|
85+
schema = File.dirname file
86+
path = "#{basedir}/#{file}"
87+
schema_anticheck path, schema
88+
end
89+
90+
say "\ngub`Invalid files PASS`"
5291
end
5392

5493
def schema_check(file, schema = 'bashly')
@@ -58,4 +97,12 @@ helpers do
5897

5998
abort 'Failed' unless success
6099
end
100+
101+
def schema_anticheck(file, schema = 'bashly')
102+
command = "check-jsonschema --schemafile schemas/#{schema}.json #{file}"
103+
say "\nnb`$ check-jsonschema` [m`#{schema}`] bb`#{file}`"
104+
success = system "#{command}"
105+
106+
abort 'Failed' if success
107+
end
61108
end

0 commit comments

Comments
 (0)