Skip to content

Commit

Permalink
- Improve JSON schema tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DannyBen committed Dec 19, 2023
1 parent 9c5eb67 commit 6bd7a1b
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 3 deletions.
Empty file added bashly}.json
Empty file.
6 changes: 6 additions & 0 deletions spec/fixtures/schemas_invalid/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
This folder contains invalid schema validations.

Each subfolder is named after the schema.

The tests can be run with `run schema invalid` and are included in the
`run schema all` test (which is executed in CI).
1 change: 1 addition & 0 deletions spec/fixtures/schemas_invalid/bashly/1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo: bar
1 change: 1 addition & 0 deletions spec/fixtures/schemas_invalid/settings/1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo: bar
1 change: 1 addition & 0 deletions spec/fixtures/schemas_invalid/strings/1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
foo: bar
35 changes: 32 additions & 3 deletions support/runfile/schema.runfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ action :all do
check_settings
check_strings
check_arbitrary
check_invalid
end

help 'Test the bashly schema against a single example'
Expand All @@ -24,6 +25,9 @@ action(:strings) { check_strings }
help 'Test the bashly schema against a bashly configuration that includes arbitrary (x-) keys'
action(:arbitrary) { check_arbitrary }

help 'Verify that all the invalid schemas in spec/fixtures/schemas_invalid fail as expected'
action(:invalid) { check_invalid }

helpers do
def check_examples
say "\ngub`Examples`"
Expand All @@ -34,21 +38,38 @@ helpers do
end

def check_settings
say "\ngub`Settings`"
say "\ngub`Settings schema`"
file = 'lib/bashly/libraries/settings/settings.yml'
schema_check file, :settings
say "\ngub`Settings schema PASS`"
end

def check_strings
say "\ngub`Strings`"
say "\ngub`Strings schema`"
file = 'lib/bashly/libraries/strings/strings.yml'
schema_check file, :strings
say "\ngub`Strings schema PASS`"
end

def check_arbitrary
say "\ngub`Arbitrary`"
say "\ngub`Arbitrary arguments`"
file = 'spec/fixtures/script/x_arbitrary.yml'
schema_check file
say "\ngub`Arbitrary arguments PASS`"
end

def check_invalid
say "\ngub`Invalid files`"
basedir = 'spec/fixtures/schemas_invalid'
files = Dir.chdir(basedir) { Dir['**/*.yml'] }

files.each do |file|
schema = File.dirname file
path = "#{basedir}/#{file}"
schema_anticheck path, schema
end

say "\ngub`Invalid files PASS`"
end

def schema_check(file, schema = 'bashly')
Expand All @@ -58,4 +79,12 @@ helpers do

abort 'Failed' unless success
end

def schema_anticheck(file, schema = 'bashly')
command = "check-jsonschema --schemafile schemas/#{schema}.json #{file}"
say "\nnb`$ check-jsonschema` [m`#{schema}`] bb`#{file}`"
success = system "#{command}"

abort 'Failed' if success
end
end

0 comments on commit 6bd7a1b

Please sign in to comment.