lib/pdk/validate/base_validator.rb in pdk-1.7.1 vs lib/pdk/validate/base_validator.rb in pdk-1.8.0
- old
+ new
@@ -1,7 +1,8 @@
require 'pdk'
require 'pdk/cli/exec'
+require 'pdk/module'
module PDK
module Validate
class BaseValidator
# Controls how many times the validator is invoked.
@@ -10,10 +11,18 @@
# targets.
# :per_target - The validator will be invoked for each target
# 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
# Parses the target strings provided from the CLI
@@ -28,33 +37,31 @@
# @return invalid [Array] An Array of Strings containing targets that do
# not exist, and will not be run by validator.
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
- fixtures_pattern = File.join('**', 'spec', 'fixtures', '**', '*')
+ 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 = []
invalid = []
matched = targets.map { |target|
if respond_to?(:pattern)
if File.directory?(target)
target_root = PDK::Util.module_root
pattern_glob = Array(pattern).map { |p| Dir.glob(File.join(target_root, p)) }
- pattern_glob = pattern_glob.flatten.reject { |file| File.fnmatch(fixtures_pattern, file) }
- target_list = pattern_glob.map do |file|
+ target_list = pattern_glob.flatten.map do |file|
if File.fnmatch(File.join(File.expand_path(target), '*'), file)
Pathname.new(file).relative_path_from(Pathname.new(PDK::Util.module_root)).to_s
end
end
+ ignore_list = ignore_pathspec
+ target_list = target_list.reject { |file| ignore_list.match(file) }
+
skipped << target if target_list.flatten.empty?
target_list
elsif File.file?(target)
if Array(pattern).include? target
target
@@ -73,10 +80,22 @@
end
}.compact.flatten
[matched, skipped, invalid]
end
+ def self.ignore_pathspec
+ ignore_pathspec = PDK::Module.default_ignored_pathspec.dup
+
+ if respond_to?(:pattern_ignore)
+ Array(pattern_ignore).each do |pattern|
+ ignore_pathspec.add(pattern)
+ end
+ end
+
+ ignore_pathspec
+ end
+
def self.parse_options(_options, targets)
targets
end
def self.spinner_text(_targets = nil)
@@ -107,10 +126,14 @@
state: :error,
)
end
end
+ def self.allow_empty_targets?
+ self::ALLOW_EMPTY_TARGETS == true
+ end
+
def self.invoke(report, options = {})
targets, skipped, invalid = parse_targets(options)
process_skipped(report, skipped)
process_invalid(report, invalid)
@@ -130,17 +153,22 @@
else
targets = targets.each_slice(1000).to_a
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|
cmd_argv = parse_options(options, invokation_targets).unshift(cmd_path).compact
cmd_argv.unshift(File.join(PDK::Util::RubyVersion.bin_path, 'ruby.exe'), '-W0') if Gem.win_platform?
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
sub_spinner = exec_group.add_spinner(spinner_text(invokation_targets))
c.register_spinner(sub_spinner)