Browse Source

Use the targets attribute of Sanity::Options as intended

develop
Christian Aust 5 months ago
parent
commit
3a8b54910a
  1. 4
      exe/sanity
  2. 54
      lib/sanity/options.rb
  3. 36
      lib/sanity/runner.rb
  4. 12
      spec/sanity/options_spec.rb
  5. 10
      spec/sanity/runner_spec.rb

4
exe/sanity

@ -10,8 +10,8 @@ require 'sanity'
# load .env file from current directory
Dotenv.load
targets, options = Sanity::Options.parse(ARGV)
Sanity::Runner.new(targets, options).tap do |runner|
options = Sanity::Options.parse(ARGV)
Sanity::Runner.new(options).tap do |runner|
unless options.validation
if runner.suites.empty?
runner.log.info 'No suites loaded, exiting'

54
lib/sanity/options.rb

@ -8,11 +8,11 @@ module Sanity
log_level: 'info',
outputs: %w[console junit report],
path: 'run',
target: 'targets.json',
targets: %w[targets.json],
validation: false
}.freeze
attr_writer :log_level, :outputs, :path, :target, :validation
attr_writer :log_level, :outputs, :path, :targets, :validation
def log_level
@log_level || DEFAULTS[:log_level]
@ -26,45 +26,43 @@ module Sanity
@path || DEFAULTS[:path]
end
def target
@target || DEFAULTS[:target]
def targets
@targets || DEFAULTS[:targets]
end
def validation
@validation.nil? ? DEFAULTS[:validation] : @validation
end
def self.parse(cmd_line_options)
app_options = Options.new
class << self
def parse(argv)
app_options = Options.new
op = OptionParser.new do |opts|
opts.banner = "Usage: #{File.basename($PROGRAM_NAME)} [options] TARGET [TARGET...]"
opts.separator ''
op = OptionParser.new do |opts|
opts.banner = "Usage: #{File.basename($PROGRAM_NAME)} [options] TARGET [TARGET...]"
opts.separator ''
opts.on('-l', '--log-level LEVEL', 'Log level [debug|info|warn|error]') do |value|
app_options.log_level = value
end
opts.on('-l', '--log-level LEVEL', 'Log level [debug|info|warn|error]') do |value|
app_options.log_level = value
end
opts.on('--outputs x,y,z', Array, 'List of outputs, separated by comma') do |list|
app_options.outputs = list
end
opts.on('--outputs x,y,z', Array, 'List of outputs, separated by comma') do |list|
app_options.outputs = list
end
opts.on('-v', '--validate', 'Validate input, do not perform tests') do
app_options.validation = true
end
opts.on('-v', '--validate', 'Validate input, do not perform tests') do
app_options.validation = true
end
opts.on('-h', '--help', 'Prints this help') do
puts opts
exit
opts.on('-h', '--help', 'Prints this help') do
puts opts
exit
end
end
end
op.parse!(cmd_line_options)
if cmd_line_options.empty?
puts op.banner
exit 1
end
op.parse!(argv)
[cmd_line_options, app_options]
app_options.tap { |opts| opts.targets = argv }
end
end
end
end

36
lib/sanity/runner.rb

@ -8,35 +8,43 @@ module Sanity
class Runner
attr_reader :options, :specs
def initialize(src, options)
def initialize(options)
@options = options
log.level = options.log_level
log.info "Running Sanity #{Sanity::VERSION}"
if src.respond_to?(:read)
if options.targets.respond_to?(:read)
# general IO
@specs = JSON.parse src.read
log.info "Loaded from IO #{src}"
elsif src.is_a?(Array)
@specs = JSON.parse options.targets.read
log.info "Loaded from IO #{options.targets}"
elsif options.targets.is_a?(Array)
# file-path IO, multiple sources
@specs = Hash.new
src.each do |s|
Dir.glob(s).each do|file|
options.targets.each do |s|
files = Dir.glob(s)
if files.empty?
Runner.log.error "No file matched #{s}"
end
files.each do|file|
File.open(file) do |f|
@specs.merge! JSON.parse(File.new(f).read)
end
Runner.log.info "Loaded #{file}"
end
end
elsif src.is_a?(String)
# file-path IO
File.open(src) do |f|
@specs = JSON.parse File.new(f).read
Runner.log.info "Loaded #{src}"
elsif options.targets.is_a?(String)
begin
# file-path IO
File.open(options.targets) do |f|
@specs = JSON.parse File.new(f).read
Runner.log.info "Loaded #{options.targets}"
end
rescue Errno::ENOENT => ex
Runner.log.error "No such file #{options.targets}"
end
else
raise ArgumentError, "Requires IO or path as string, #{src.class} provided"
raise ArgumentError, "Requires IO or path as string, #{options.targets.class} provided"
end
end
@ -45,7 +53,7 @@ module Sanity
end
def suites
@suites ||= specs.map do |name, roles|
@suites ||= (specs || []).map do |name, roles|
Suite.new(name).tap do |suite|
roles.each do |role_name, data|
klass = "Sanity::Tests::#{role_name}".to_class

12
spec/sanity/options_spec.rb

@ -5,7 +5,7 @@ RSpec.describe Sanity::Options do
expect(Sanity::Options::DEFAULTS).not_to be_nil
end
%w[log_level outputs path target validation].map(&:to_sym).each do |prop|
%w[log_level outputs path targets validation].map(&:to_sym).each do |prop|
it { is_expected.to respond_to(prop) }
describe "##{prop}" do
@ -18,16 +18,16 @@ RSpec.describe Sanity::Options do
describe '::parse' do
subject { described_class }
let(:argv) { [] }
let(:argv) { ['foo'] }
it { is_expected.to respond_to(:parse) }
it 'returns an instance of Options' do
it 'returns options' do
expect(subject.parse(argv)).to be_an(Sanity::Options)
end
it 'exits cleanly when -h is used' do
expect { subject.parse(['-h']) }.to output.to_stdout.and raise_error SystemExit
expect { subject.parse(['-h']) }.to output.to_stdout.and raise_error(SystemExit)
end
it 'supports --outputs as list' do
@ -36,12 +36,12 @@ RSpec.describe Sanity::Options do
end
it 'supports --log-level as string' do
options = subject.parse(['--log-level', 'ERROR'])
options = subject.parse(['--log-level', 'ERROR'] + argv)
expect(options.log_level).to eq 'ERROR'
end
it 'supports --validate' do
options = subject.parse(['--validate'])
options = subject.parse(['--validate'] + argv)
expect(options.validation).to eq true
end
end

10
spec/sanity/runner_spec.rb

@ -39,16 +39,22 @@ RSpec.describe Sanity::Runner do
allow(Sanity::Runner).to receive(:log).and_return(silent_logger)
end
subject { Sanity::Runner.new(StringIO.new(json), options) }
subject { Sanity::Runner.new(options) }
describe '#initialize' do
context 'with IO' do
before do
options.targets = StringIO.new(json)
end
it_behaves_like 'initialized runner'
end
context 'with string-based path' do
let(:file) { Tempfile.new('rspec-simple').tap { |f| f.write json; f.close } }
subject { Sanity::Runner.new file.path, options }
before do
options.targets = file.path
end
it_behaves_like 'initialized runner'
end

Loading…
Cancel
Save