Class: PDK::Validate::BaseValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/pdk/validate/base_validator.rb

Constant Summary collapse

INVOKE_STYLE =

Controls how many times the validator is invoked.

:once -       The validator will be invoked once and passed all the
              targets.
:per_target - The validator will be invoked for each target
              separately.
:once
ALLOW_EMPTY_TARGETS =

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.
false
IGNORE_DOTFILES =
true

Class Method Summary collapse

Class Method Details

.allow_empty_targets?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/pdk/validate/base_validator.rb', line 137

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

.cmd_pathObject



26
27
28
# File 'lib/pdk/validate/base_validator.rb', line 26

def self.cmd_path
  File.join(PDK::Util.module_root, 'bin', cmd)
end

.ignore_dotfiles?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/pdk/validate/base_validator.rb', line 99

def self.ignore_dotfiles?
  self::IGNORE_DOTFILES
end

.ignore_pathspecObject



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/pdk/validate/base_validator.rb', line 87

def self.ignore_pathspec
  ignore_pathspec = PDK::Module.default_ignored_pathspec(ignore_dotfiles?)

  if respond_to?(:pattern_ignore)
    Array(pattern_ignore).each do |pattern|
      ignore_pathspec.add(pattern)
    end
  end

  ignore_pathspec
end

.invoke(report, options = {}) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/pdk/validate/base_validator.rb', line 141

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

  process_skipped(report, skipped)
  process_invalid(report, invalid)

  return 0 if targets.empty?

  PDK::Util::Bundler.ensure_binstubs!(cmd)

  # If invoking :per_target, split the targets array into an array of
  # single element arrays (one per target). If invoking :once, wrap the
  # targets array in another array. This is so we can loop through the
  # invokes with the same logic, regardless of which invoke style is
  # needed.
  #
  if self::INVOKE_STYLE == :per_target
    targets = targets.combination(1).to_a
  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)
        else
          c.add_spinner(spinner_text(invokation_targets))
        end
      end
    end

    if options[:split_exec]
      options[:split_exec].register do
        result = command.execute!

        begin
          parse_output(report, result, invokation_targets.compact)
        rescue PDK::Validate::ParseOutputError => e
          $stderr.puts e.message
        end
        result[:exit_code]
      end
    else
      result = command.execute!
      exit_codes << result[:exit_code]

      begin
        parse_output(report, result, invokation_targets.compact)
      rescue PDK::Validate::ParseOutputError => e
        $stderr.puts e.message
      end
    end
  end

  options.key?(:split_exec) ? options[:split_exec].exit_code : exit_codes.max
end

.parse_options(_options, targets) ⇒ Object



103
104
105
# File 'lib/pdk/validate/base_validator.rb', line 103

def self.parse_options(_options, targets)
  targets
end

.parse_targets(options) ⇒ Object

Parses the target strings provided from the CLI

Parameters:

  • options (Hash)

    A Hash containing the input options from the CLI.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/pdk/validate/base_validator.rb', line 41

def self.parse_targets(options)
  # If no targets are specified, then we will run validations from the
  # base module directory.

  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), File::FNM_DOTMATCH) }

        target_list = pattern_glob.flatten.map do |file|
          if File.fnmatch(File.join(File.expand_path(target), '*'), file, File::FNM_DOTMATCH)
            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
        elsif Array(pattern).any? { |p| File.fnmatch(File.expand_path(p), File.expand_path(target), File::FNM_DOTMATCH) }
          target
        else
          skipped << target
          next
        end
      else
        invalid << target
        next
      end
    else
      target
    end
  }.compact.flatten
  [matched, skipped, invalid]
end

.process_invalid(report, invalid = []) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/pdk/validate/base_validator.rb', line 124

def self.process_invalid(report, invalid = [])
  invalid.each do |invalid_target|
    PDK.logger.debug(_('%{validator}: Skipped \'%{target}\'. Target file not found.') % { validator: name, target: invalid_target })
    report.add_event(
      file:     invalid_target,
      source:   name,
      message:  _('File does not exist.'),
      severity: :error,
      state:    :error,
    )
  end
end

.process_skipped(report, skipped = []) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/pdk/validate/base_validator.rb', line 111

def self.process_skipped(report, skipped = [])
  skipped.each do |skipped_target|
    PDK.logger.debug(_('%{validator}: Skipped \'%{target}\'. Target does not contain any files to validate (%{pattern}).') % { validator: name, target: skipped_target, pattern: pattern })
    report.add_event(
      file:     skipped_target,
      source:   name,
      message:  _('Target does not contain any files to validate (%{pattern}).') % { pattern: pattern },
      severity: :info,
      state:    :skipped,
    )
  end
end

.spinner_text(_targets = nil) ⇒ Object



107
108
109
# File 'lib/pdk/validate/base_validator.rb', line 107

def self.spinner_text(_targets = nil)
  _('Invoking %{cmd}') % { cmd: cmd }
end