DannyBen/bashly

View on GitHub
support/runfile/schema.runfile

Summary

Maintainability
Test Coverage
summary 'Run json-schema checks on all examples'

help 'Generate the bashly.json schema from the bashly.yml template'
action :generate do
  cmd = 'yq -p yaml -o json support/schema/bashly.yml > schemas/bashly.json'
  say "gb`$` #{cmd}"
  success = system cmd
  abort 'Failed' unless success
end

help 'Run all the test actions in this runfile'
action :all do
  check_examples
  check_settings
  check_strings
  check_arbitrary
  check_invalid
end

help 'Test any file against any schema'
usage 'file [--anti] PATH [SCHEMA]'
option '-a, --anti', 'Run anticheck instead (expect failure)'
param 'PATH', 'The path to the tested YAML file'
param 'SCHEMA', 'The name of the schema (bashly, settings of strings) [default: bashly]'
example 'run schema file -a spec/fixtures/schemas_invalid/bashly/1.yml'
example 'run schema file -a spec/fixtures/schemas_invalid/settings/2.yml settings'
action(:file) do |args|
  schema = args['SCHEMA'] || 'bashly'
  if args['--anti']
    schema_anticheck args['PATH'], schema
  else
    schema_check args['PATH'], schema
  end
  say "\ngub`PASS`"
end

help 'Test the bashly schema against a single example'
usage 'example EXAMPLE'
action(:example) { |args| schema_check "examples/#{args['EXAMPLE']}/src/bashly.yml" }

help 'Test the bashly schema against all examples'
action(:examples) { check_examples }

help 'Test the settings schema against the default settings template'
action(:settings) { check_settings }

help 'Test the strings schema against the default strings template'
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`"
    Example.all.each do |example|
      file = example.yaml_path
      schema_check file
    end
    say "\ngub`Examples PASS`"
  end

  def check_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 schema`"
    file = 'lib/bashly/libraries/strings/strings.yml'
    schema_check file, :strings
    say "\ngub`Strings schema PASS`"
  end

  def check_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')
    command = "check-jsonschema --schemafile schemas/#{schema}.json #{file}"
    say "\nnb`$ check-jsonschema` [m`#{schema}`] bb`#{file}`"
    success = system command
    
    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