Skip to content

Commit

Permalink
(PDK-1200) Validate should honor testing from source
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryan Jen committed Oct 30, 2018
1 parent 2ae1dba commit a3737da
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
3 changes: 3 additions & 0 deletions lib/pdk/cli/validate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ module PDK::CLI
puppet_env = PDK::CLI::Util.puppet_from_opts_or_env(opts)
PDK::Util::PuppetVersion.fetch_puppet_dev if opts.key?(:'puppet-dev')
PDK::Util::RubyVersion.use(puppet_env[:ruby_version])

options.merge!(puppet_env[:gemset])

PDK::Util::Bundler.ensure_bundle!(puppet_env[:gemset])

exit_code = 0
Expand Down
1 change: 1 addition & 0 deletions lib/pdk/validate/base_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ def self.invoke(report, options = {})

command = PDK::CLI::Exec::Command.new(*cmd_argv).tap do |c|
c.context = :module
c.environment = { 'PUPPET_GEM_VERSION' => options[:puppet] } if options[:puppet]
unless options[:split_exec]
exec_group = options[:exec_group]
if exec_group
Expand Down
26 changes: 14 additions & 12 deletions spec/unit/pdk/cli/validate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
let(:validator_names) { validators.map(&:name).join(', ') }
let(:validator_success) { { exit_code: 0, stdout: 'success', stderr: '' } }
let(:report) { instance_double('PDK::Report').as_null_object }
let(:ruby_version) { '2.4.3' }
let(:puppet_version) { '5.4.0' }

before(:each) do
allow(Dir).to receive(:chdir) { |_dir, &block| block.call }

allow(PDK::CLI::Util).to receive(:puppet_from_opts_or_env).and_return(ruby_version: '2.4.3', gemset: { puppet: '5.4.0' })
allow(PDK::CLI::Util).to receive(:puppet_from_opts_or_env).and_return(ruby_version: ruby_version, gemset: { puppet: puppet_version })
allow(PDK::Util::RubyVersion).to receive(:use)
allow(PDK::Util::Bundler).to receive(:ensure_bundle!).with(hash_including(:puppet))

Expand All @@ -23,7 +25,7 @@

context 'when no arguments or options are provided' do
it 'invokes each validator with no report and no options and exits zero' do
expect(validators).to all(receive(:invoke).with(report, {}).and_return(0))
expect(validators).to all(receive(:invoke).with(report, puppet: puppet_version).and_return(0))

expect(logger).to receive(:info).with('Running all available validators...')

Expand Down Expand Up @@ -59,7 +61,7 @@
let(:validator) { PDK::Validate::MetadataValidator }

it 'only invokes the given validator and exits zero' do
expect(validator).to receive(:invoke).with(report, {}).and_return(0)
expect(validator).to receive(:invoke).with(report, puppet: puppet_version).and_return(0)

validators.reject { |r| r == validator }.each do |v|
expect(v).not_to receive(:invoke)
Expand All @@ -78,7 +80,7 @@
end

it 'invokes each given validator and exits zero' do
expect(invoked_validators).to all(receive(:invoke).with(report, {}).and_return(0))
expect(invoked_validators).to all(receive(:invoke).with(report, puppet: puppet_version).and_return(0))

(validators | invoked_validators).each do |validator|
expect(validator).not_to receive(:invoke)
Expand All @@ -93,7 +95,7 @@

it 'warns about unknown validators, invokes known validators, and exits zero' do
expect(logger).to receive(:warn).with(%r{Unknown validator 'bad-val'. Available validators: #{validator_names}}i)
expect(validator).to receive(:invoke).with(report, {}).and_return(0)
expect(validator).to receive(:invoke).with(report, puppet: puppet_version).and_return(0)

expect { PDK::CLI.run(['validate', 'puppet,bad-val']) }.to exit_zero
end
Expand All @@ -103,15 +105,15 @@
let(:validator) { PDK::Validate::MetadataValidator }

it 'invokes the specified validator with the target as an option' do
expect(validator).to receive(:invoke).with(report, targets: ['lib/', 'manifests/']).and_return(0)
expect(validator).to receive(:invoke).with(report, puppet: puppet_version, targets: ['lib/', 'manifests/']).and_return(0)

expect { PDK::CLI.run(['validate', 'metadata', 'lib/', 'manifests/']) }.to exit_zero
end
end

context 'when targets are provided as arguments and no validators are specified' do
it 'invokes all validators with the target as an option' do
expect(validators).to all(receive(:invoke).with(report, targets: ['lib/', 'manifests/']).and_return(0))
expect(validators).to all(receive(:invoke).with(report, puppet: puppet_version, targets: ['lib/', 'manifests/']).and_return(0))

expect(logger).to receive(:info).with('Running all available validators...')

Expand All @@ -121,7 +123,7 @@

context 'when no report formats are specified' do
it 'reports to stdout as text' do
expect(validators).to all(receive(:invoke).with(report, {}).and_return(0))
expect(validators).to all(receive(:invoke).with(report, puppet: puppet_version).and_return(0))
expect(report).to receive(:write_text).with($stdout)
expect(report).not_to receive(:write_junit)

Expand All @@ -131,7 +133,7 @@

context 'when a report format is specified' do
it 'reports to stdout as the specified format' do
expect(validators).to all(receive(:invoke).with(report, {}).and_return(0))
expect(validators).to all(receive(:invoke).with(report, puppet: puppet_version).and_return(0))
expect(report).to receive(:write_junit).with($stdout)
expect(report).not_to receive(:write_text)

Expand All @@ -141,7 +143,7 @@

context 'when multiple report formats are specified' do
it 'reports to each target as the specified format' do
expect(validators).to all(receive(:invoke).with(report, {}).and_return(0))
expect(validators).to all(receive(:invoke).with(report, puppet: puppet_version).and_return(0))
expect(report).to receive(:write_text).with($stderr)
expect(report).to receive(:write_text).with($stdout)
expect(report).to receive(:write_junit).with('testfile.xml')
Expand All @@ -155,7 +157,7 @@
context 'with --puppet-dev' do
let(:puppet_env) do
{
ruby_version: '2.4.3',
ruby_version: ruby_version,
gemset: { puppet: 'file://path/to/puppet' },
}
end
Expand Down Expand Up @@ -216,7 +218,7 @@
let(:puppet_version) { '5.3' }
let(:puppet_env) do
{
ruby_version: '2.4.3',
ruby_version: ruby_version,
gemset: { puppet: '5.3.5' },
}
end
Expand Down

0 comments on commit a3737da

Please sign in to comment.