Class: PDK::Validate::MetadataJSONLint

Inherits:
BaseValidator show all
Defined in:
lib/pdk/validate/metadata/metadata_json_lint.rb

Constant Summary collapse

INVOKE_STYLE =

Validate each metadata file separately, as metadata-json-lint does not support multiple targets.

:per_target

Constants inherited from BaseValidator

BaseValidator::ALLOW_EMPTY_TARGETS, BaseValidator::IGNORE_DOTFILES

Class Method Summary collapse

Methods inherited from BaseValidator

allow_empty_targets?, cmd_path, ignore_dotfiles?, ignore_pathspec, invoke, parse_targets, process_invalid, process_skipped

Class Method Details

.cmdObject



18
19
20
# File 'lib/pdk/validate/metadata/metadata_json_lint.rb', line 18

def self.cmd
  'metadata-json-lint'
end

.nameObject



14
15
16
# File 'lib/pdk/validate/metadata/metadata_json_lint.rb', line 14

def self.name
  'metadata-json-lint'
end

.parse_options(_options, targets) ⇒ Object



32
33
34
35
36
37
# File 'lib/pdk/validate/metadata/metadata_json_lint.rb', line 32

def self.parse_options(_options, targets)
  cmd_options = ['--format', 'json']
  cmd_options << '--strict-dependencies'

  cmd_options.concat(targets)
end

.parse_output(report, result, targets) ⇒ Object

Raises:

  • (ArgumentError)


39
40
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
# File 'lib/pdk/validate/metadata/metadata_json_lint.rb', line 39

def self.parse_output(report, result, targets)
  raise ArgumentError, _('More than 1 target provided to PDK::Validate::MetadataJSONLint.') if targets.count > 1

  if result[:stdout].strip.empty?
    # metadata-json-lint will print nothing if there are no problems with
    # the file being linted. This should be handled separately to
    # metadata-json-lint generating output that can not be parsed as JSON
    # (unhandled exception in metadata-json-lint).
    json_data = {}
  else
    begin
      json_data = JSON.parse(result[:stdout])
    rescue JSON::ParserError
      raise PDK::Validate::ParseOutputError, result[:stdout]
    end
  end

  if json_data.empty?
    report.add_event(
      file:     targets.first,
      source:   name,
      state:    :passed,
      severity: :ok,
    )
  else
    json_data.delete('result')
    json_data.keys.each do |type|
      json_data[type].each do |offense|
        # metadata-json-lint groups the offenses by type, so the type ends
        # up being `warnings` or `errors`. We want to convert that to the
        # singular noun for the event.
        event_type = type[%r{\A(.+?)s?\Z}, 1]

        report.add_event(
          file:     targets.first,
          source:   name,
          message:  offense['msg'],
          test:     offense['check'],
          severity: event_type,
          state:    :failure,
        )
      end
    end
  end
end

.patternObject



28
29
30
# File 'lib/pdk/validate/metadata/metadata_json_lint.rb', line 28

def self.pattern
  'metadata.json'
end

.spinner_text(targets = []) ⇒ Object



22
23
24
25
26
# File 'lib/pdk/validate/metadata/metadata_json_lint.rb', line 22

def self.spinner_text(targets = [])
  _('Checking module metadata style (%{targets}).') % {
    targets: PDK::Util.targets_relative_to_pwd(targets).join(' '),
  }
end