Class: PDK::Validate::Ruby::RubyRubocopValidator

Inherits:
ExternalCommandValidator show all
Defined in:
lib/pdk/validate/ruby/ruby_rubocop_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

#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

#allow_empty_targets?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/pdk/validate/ruby/ruby_rubocop_validator.rb', line 7

def allow_empty_targets?
  true
end

#cmdObject



15
16
17
# File 'lib/pdk/validate/ruby/ruby_rubocop_validator.rb', line 15

def cmd
  'rubocop'
end

#nameObject



11
12
13
# File 'lib/pdk/validate/ruby/ruby_rubocop_validator.rb', line 11

def name
  'rubocop'
end

#parse_options(targets) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/pdk/validate/ruby/ruby_rubocop_validator.rb', line 31

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

  if options[:auto_correct]
    cmd_options << '--auto-correct'
  end

  cmd_options.concat(targets)
end

#parse_output(report, result, _targets) ⇒ Object



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
# File 'lib/pdk/validate/ruby/ruby_rubocop_validator.rb', line 41

def parse_output(report, result, _targets)
  return if result[:stdout].empty?

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

  return unless json_data.key?('files')

  json_data['files'].each do |file_info|
    next unless file_info.key?('offenses')
    result = {
      file: file_info['path'],
      source: 'rubocop',
    }

    if file_info['offenses'].empty?
      report.add_event(result.merge(state: :passed, severity: :ok))
    else
      file_info['offenses'].each do |offense|
        report.add_event(
          result.merge(
            line:     offense['location']['line'],
            column:   offense['location']['column'],
            message:  offense['message'],
            severity: offense['corrected'] ? 'corrected' : offense['severity'],
            test:     offense['cop_name'],
            state:    :failure,
          ),
        )
      end
    end
  end
end

#patternObject



19
20
21
22
23
24
25
# File 'lib/pdk/validate/ruby/ruby_rubocop_validator.rb', line 19

def pattern
  if context.is_a?(PDK::Context::ControlRepo)
    ['Puppetfile', '**/**.rb']
  else
    '**/**.rb'
  end
end

#spinner_text_for_targets(_targets) ⇒ Object



27
28
29
# File 'lib/pdk/validate/ruby/ruby_rubocop_validator.rb', line 27

def spinner_text_for_targets(_targets)
  _('Checking Ruby code style (%{pattern}).') % { pattern: pattern }
end