Class: PDK::Validate::Metadata::MetadataJSONLintValidator

Inherits:
ExternalCommandValidator show all
Defined in:
lib/pdk/validate/metadata/metadata_json_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?, #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



17
18
19
# File 'lib/pdk/validate/metadata/metadata_json_lint_validator.rb', line 17

def cmd
  'metadata-json-lint'
end

#invoke_styleObject

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



9
10
11
# File 'lib/pdk/validate/metadata/metadata_json_lint_validator.rb', line 9

def invoke_style
  :per_target
end

#nameObject



13
14
15
# File 'lib/pdk/validate/metadata/metadata_json_lint_validator.rb', line 13

def name
  'metadata-json-lint'
end

#parse_options(targets) ⇒ Object



29
30
31
32
33
34
# File 'lib/pdk/validate/metadata/metadata_json_lint_validator.rb', line 29

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

  cmd_options.concat(targets)
end

#parse_output(report, result, targets) ⇒ Object

Raises:

  • (ArgumentError)


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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/pdk/validate/metadata/metadata_json_lint_validator.rb', line 36

def parse_output(report, result, targets)
  raise ArgumentError, 'More than 1 target provided to PDK::Validate::MetadataJSONLintValidator.' 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.each_key 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[/\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



25
26
27
# File 'lib/pdk/validate/metadata/metadata_json_lint_validator.rb', line 25

def pattern
  contextual_pattern('metadata.json')
end

#spinner_text_for_targets(targets) ⇒ Object



21
22
23
# File 'lib/pdk/validate/metadata/metadata_json_lint_validator.rb', line 21

def spinner_text_for_targets(targets)
  format('Checking module metadata style (%{targets}).', targets: PDK::Util.targets_relative_to_pwd(targets.flatten).join(' '))
end