brew/Library/Homebrew/tap_auditor.rb

88 lines
2.7 KiB
Ruby
Raw Normal View History

2020-11-18 10:05:23 +01:00
# typed: true
# frozen_string_literal: true
module Homebrew
# Auditor for checking common violations in {Tap}s.
2020-11-18 10:05:23 +01:00
#
# @api private
class TapAuditor
extend T::Sig
2020-12-21 12:53:12 -05:00
attr_reader :name, :path, :formula_names, :cask_tokens, :tap_audit_exceptions, :tap_style_exceptions,
:tap_pypi_formula_mappings, :problems
2020-11-18 10:05:23 +01:00
sig { params(tap: Tap, strict: T.nilable(T::Boolean)).void }
2020-11-18 10:05:23 +01:00
def initialize(tap, strict:)
@name = tap.name
@path = tap.path
2020-12-21 12:53:12 -05:00
@formula_names = tap.formula_names
@cask_tokens = tap.cask_tokens
@tap_audit_exceptions = tap.audit_exceptions
@tap_style_exceptions = tap.style_exceptions
@tap_pypi_formula_mappings = tap.pypi_formula_mappings
@problems = []
2020-11-18 10:05:23 +01:00
end
sig { void }
def audit
audit_json_files
audit_tap_formula_lists
2020-11-18 10:05:23 +01:00
end
sig { void }
def audit_json_files
json_patterns = Tap::HOMEBREW_TAP_JSON_FILES.map { |pattern| @path/pattern }
Pathname.glob(json_patterns).each do |file|
JSON.parse file.read
rescue JSON::ParserError
problem "#{file.to_s.delete_prefix("#{@path}/")} contains invalid JSON"
end
end
sig { void }
def audit_tap_formula_lists
check_formula_list_directory "audit_exceptions", @tap_audit_exceptions
check_formula_list_directory "style_exceptions", @tap_style_exceptions
check_formula_list "pypi_formula_mappings", @tap_pypi_formula_mappings
end
2020-11-18 10:05:23 +01:00
sig { params(message: String).void }
def problem(message)
@problems << ({ message: message, location: nil })
end
2020-11-18 10:05:23 +01:00
private
2020-11-18 10:05:23 +01:00
sig { params(list_file: String, list: T.untyped).void }
def check_formula_list(list_file, list)
unless [Hash, Array].include? list.class
problem <<~EOS
#{list_file}.json should contain a JSON array
of formula names or a JSON object mapping formula names to values
EOS
return
end
2020-11-18 10:05:23 +01:00
2020-12-21 12:53:12 -05:00
list = list.keys if list.is_a? Hash
invalid_formulae = list.select do |formula_or_cask_name|
@formula_names.exclude?(formula_or_cask_name) && @cask_tokens.exclude?("#{@name}/#{formula_or_cask_name}")
2020-11-18 10:05:23 +01:00
end
return if invalid_formulae.empty?
problem <<~EOS
#{list_file}.json references
2020-12-21 12:53:12 -05:00
formulae or casks that are not found in the #{@name} tap.
Invalid formulae or casks: #{invalid_formulae.join(", ")}
EOS
2020-11-18 10:05:23 +01:00
end
sig { params(directory_name: String, lists: Hash).void }
def check_formula_list_directory(directory_name, lists)
lists.each do |list_name, list|
check_formula_list "#{directory_name}/#{list_name}", list
end
2020-11-18 10:05:23 +01:00
end
end
end