Class: PDK::Validate::Puppet::PuppetLintValidator

Inherits:
ExternalCommandValidator show all
Defined in:
lib/pdk/validate/puppet/puppet_lint_validator.rb

Instance Attribute Summary

Attributes inherited from ExternalCommandValidator

#commands

Attributes inherited from Validator

#context, #options, #prepared

Instance Method Summary collapse

Methods inherited from ExternalCommandValidator

#alternate_bin_paths, #cmd_path, #invoke, #prepare_invoke!, #spinner

Methods inherited from InvokableValidator

#allow_empty_targets?, #fnmatch?, #ignore_dotfiles?, #invoke_style, #parse_targets, #pattern_ignore, #prepare_invoke!, #process_invalid, #process_skipped, #spinner, #spinner_text, #valid_in_context?

Methods inherited from Validator

#initialize, #invoke, #prepare_invoke!, #spinner, #spinner_text, #spinners_enabled?, #start_spinner, #stop_spinner, #valid_in_context?

Constructor Details

This class inherits a constructor from PDK::Validate::Validator

Instance Method Details

#cmdObject



11
12
13
# File 'lib/pdk/validate/puppet/puppet_lint_validator.rb', line 11

def cmd
  'puppet-lint'
end

#nameObject



7
8
9
# File 'lib/pdk/validate/puppet/puppet_lint_validator.rb', line 7

def name
  'puppet-lint'
end

#parse_options(targets) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/pdk/validate/puppet/puppet_lint_validator.rb', line 23

def parse_options(targets)
  cmd_options = ['--json', '--relative']

  cmd_options << '--fix' if options[:auto_correct]

  cmd_options.concat(targets)
end

#parse_output(report, result, targets) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/pdk/validate/puppet/puppet_lint_validator.rb', line 31

def parse_output(report, result, targets)
  begin
    json_data = JSON.parse(result[:stdout]).flatten
  rescue JSON::ParserError
    raise PDK::Validate::ParseOutputError, result[:stdout]
  end

  # puppet-lint does not include files without problems in its JSON
  # output, so we need to go through the list of targets and add passing
  # events to the report for any target not listed in the JSON output.
  targets.reject { |target| json_data.any? { |j| j['path'] == target } }.each do |target|
    report.add_event(
      file: target,
      source: name,
      severity: 'ok',
      state: :passed
    )
  end

  json_data.each do |offense|
    report.add_event({
                       file: offense['path'],
                       source: name,
                       line: offense['line'],
                       column: offense['column'],
                       message: offense['message'],
                       test: offense['check'],
                       severity: offense['kind'] == 'fixed' ? 'corrected' : offense['kind'],
                       state: :failure
                     })
  end
end

#patternObject



15
16
17
# File 'lib/pdk/validate/puppet/puppet_lint_validator.rb', line 15

def pattern
  contextual_pattern('**/*.pp')
end

#spinner_text_for_targets(_targets) ⇒ Object



19
20
21
# File 'lib/pdk/validate/puppet/puppet_lint_validator.rb', line 19

def spinner_text_for_targets(_targets)
  format('Checking Puppet manifest style (%{pattern}).', pattern: pattern.join(' '))
end