Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(PDK-654) Allow rubocop to determine its own targets by default #594

Merged
merged 2 commits into from
Nov 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions lib/pdk/validate/base_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ class BaseValidator
# separately.
INVOKE_STYLE = :once

# Controls how the validator behaves if not passed any targets.
#
# true - PDK will not pass the globbed targets to the validator command
# and it will instead rely on the underlying tool to find its
# own default targets.
# false - PDK will pass the globbed targets to the validator command.
ALLOW_EMPTY_TARGETS = false

def self.cmd_path
File.join(PDK::Util.module_root, 'bin', cmd)
end
Expand All @@ -31,11 +39,8 @@ def self.cmd_path
def self.parse_targets(options)
# If no targets are specified, then we will run validations from the
# base module directory.
targets = if options[:targets].nil? || options[:targets].empty?
[PDK::Util.module_root]
else
options[:targets]
end

targets = options.fetch(:targets, []).empty? ? [PDK::Util.module_root] : options[:targets]

targets.map! { |r| r.gsub(File::ALT_SEPARATOR, File::SEPARATOR) } if File::ALT_SEPARATOR
skipped = []
Expand Down Expand Up @@ -123,6 +128,10 @@ def self.process_invalid(report, invalid = [])
end
end

def self.allow_empty_targets?
self::ALLOW_EMPTY_TARGETS == true
end

def self.invoke(report, options = {})
targets, skipped, invalid = parse_targets(options)

Expand All @@ -146,6 +155,10 @@ def self.invoke(report, options = {})
options[:split_exec] = PDK::CLI::ExecGroup.new(spinner_text(targets), parallel: false)
end

if options.fetch(:targets, []).empty? && allow_empty_targets?
targets = [[]]
end

exit_codes = []

targets.each do |invokation_targets|
Expand Down
2 changes: 2 additions & 0 deletions lib/pdk/validate/ruby/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
module PDK
module Validate
class Rubocop < BaseValidator
ALLOW_EMPTY_TARGETS = true

def self.name
'rubocop'
end
Expand Down
15 changes: 7 additions & 8 deletions spec/acceptance/validate_ruby_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
end

context 'when validating specific directories' do
another_violation_rb = File.join('lib', 'puppet', 'another_violation.rb')
another_violation_rb = File.join('spec', 'fixtures', 'test', 'another_violation.rb')

before(:all) do
FileUtils.mkdir_p(File.dirname(another_violation_rb))
Expand All @@ -43,7 +43,7 @@
end
end

describe command('pdk validate ruby lib') do
describe command('pdk validate ruby spec/fixtures') do
its(:exit_status) { is_expected.not_to eq(0) }
its(:stdout) { is_expected.to match(%r{#{Regexp.escape(another_violation_rb)}}) }
its(:stdout) { is_expected.not_to match(%r{#{Regexp.escape(spec_violation_rb)}}) }
Expand All @@ -58,8 +58,8 @@

its(:stdout) do
is_expected.to have_junit_testsuite('rubocop').with_attributes(
'failures' => a_value > 1,
'tests' => a_value >= 3,
'failures' => a_value >= 1,
'tests' => a_value >= 2,
)
end

Expand All @@ -71,10 +71,9 @@
end

its(:stdout) do
is_expected.to have_junit_testcase.in_testsuite('rubocop').with_attributes(
'classname' => a_string_matching(%r{UselessAssignment}),
'name' => a_string_starting_with(File.join('spec', 'violation.rb')),
).that_failed
is_expected.not_to have_junit_testcase.in_testsuite('rubocop').with_attributes(
'name' => a_string_starting_with(File.join('spec', 'fixtures')),
)
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions spec/unit/pdk/cli/validate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@
before(:each) do
allow(PDK::CLI::Util).to receive(:puppet_from_opts_or_env).with(hash_including(:'puppet-dev' => true)).and_return(puppet_env)
allow(PDK::Util::PuppetVersion).to receive(:fetch_puppet_dev).and_return(nil)
expect(validators).to all(receive(:invoke).with(any_args).and_return(0))
end

it 'activates puppet github source' do
Expand Down Expand Up @@ -225,6 +226,7 @@

before(:each) do
allow(PDK::CLI::Util).to receive(:puppet_from_opts_or_env).with(hash_including(:'puppet-version' => puppet_version)).and_return(puppet_env)
expect(validators).to all(receive(:invoke).with(any_args).and_return(0))
end

it 'activates resolved puppet version' do
Expand Down Expand Up @@ -255,6 +257,7 @@

before(:each) do
allow(PDK::CLI::Util).to receive(:puppet_from_opts_or_env).with(hash_including(:'pe-version' => pe_version)).and_return(puppet_env)
expect(validators).to all(receive(:invoke).with(any_args).and_return(0))
end

it 'activates resolved puppet version' do
Expand Down
9 changes: 1 addition & 8 deletions spec/unit/pdk/validate/rubocop_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,8 @@
context 'when given no targets' do
let(:targets) { [] }

let(:files) { [File.join('spec', 'spec_helper.rb')] }
let(:globbed_files) { files.map { |file| File.join(module_root, file) } }

before(:each) do
allow(Dir).to receive(:glob).with(glob_pattern).and_return(globbed_files)
end

it 'returns the module root' do
expect(target_files.first).to eq(files)
expect(target_files.first).to be_empty
end
end

Expand Down