brew/Library/Homebrew/cask/auditor.rb

116 lines
3.1 KiB
Ruby
Raw Normal View History

rubocop: Use `Sorbet/StrictSigil` as it's better than comments - Previously I thought that comments were fine to discourage people from wasting their time trying to bump things that used `undef` that Sorbet didn't support. But RuboCop is better at this since it'll complain if the comments are unnecessary. - Suggested in https://github.com/Homebrew/brew/pull/18018#issuecomment-2283369501. - I've gone for a mixture of `rubocop:disable` for the files that can't be `typed: strict` (use of undef, required before everything else, etc) and `rubocop:todo` for everything else that should be tried to make strictly typed. There's no functional difference between the two as `rubocop:todo` is `rubocop:disable` with a different name. - And I entirely disabled the cop for the docs/ directory since `typed: strict` isn't going to gain us anything for some Markdown linting config files. - This means that now it's easier to track what needs to be done rather than relying on checklists of files in our big Sorbet issue: ```shell $ git grep 'typed: true # rubocop:todo Sorbet/StrictSigil' | wc -l 268 ``` - And this is confirmed working for new files: ```shell $ git status On branch use-rubocop-for-sorbet-strict-sigils Untracked files: (use "git add <file>..." to include in what will be committed) Library/Homebrew/bad.rb Library/Homebrew/good.rb nothing added to commit but untracked files present (use "git add" to track) $ brew style Offenses: bad.rb:1:1: C: Sorbet/StrictSigil: Sorbet sigil should be at least strict got true. ^^^^^^^^^^^^^ 1340 files inspected, 1 offense detected ```
2024-08-12 10:30:59 +01:00
# typed: true # rubocop:todo Sorbet/StrictSigil
# frozen_string_literal: true
2020-08-09 01:34:07 +02:00
require "cask/audit"
2018-09-06 08:29:14 +02:00
module Cask
2020-08-24 21:33:45 +02:00
# Helper class for auditing all available languages of a cask.
2016-09-24 13:52:43 +02:00
class Auditor
def self.audit(cask, **options)
new(cask, **options).audit
end
attr_reader :cask, :language
def initialize(
cask,
audit_download: nil,
audit_online: nil,
audit_strict: nil,
2022-08-01 14:30:04 +02:00
audit_signing: nil,
audit_token_conflicts: nil,
audit_new_cask: nil,
quarantine: nil,
any_named_args: nil,
2021-03-21 13:59:43 -04:00
language: nil,
only: [],
except: []
)
@cask = cask
@audit_download = audit_download
2020-04-23 21:16:17 +02:00
@audit_online = audit_online
@audit_new_cask = audit_new_cask
@audit_strict = audit_strict
2022-08-01 14:30:04 +02:00
@audit_signing = audit_signing
@quarantine = quarantine
2020-04-23 21:16:17 +02:00
@audit_token_conflicts = audit_token_conflicts
@any_named_args = any_named_args
2020-08-01 02:30:46 +02:00
@language = language
@only = only
@except = except
end
LANGUAGE_BLOCK_LIMIT = 10
def audit
errors = Set.new
2020-08-01 02:30:46 +02:00
if !language && language_blocks
sample_languages = if language_blocks.length > LANGUAGE_BLOCK_LIMIT && !@audit_new_cask
sample_keys = language_blocks.keys.sample(LANGUAGE_BLOCK_LIMIT)
2024-02-04 15:19:29 +01:00
ohai "Auditing a sample of available languages for #{cask}: " \
"#{sample_keys.map { |lang| lang[0].to_s }.to_sentence}"
language_blocks.select { |k| sample_keys.include?(k) }
else
language_blocks
end
sample_languages.each_key do |l|
audit = audit_languages(l)
if audit.summary.present? && output_summary?(audit)
2021-03-21 13:59:43 -04:00
ohai "Auditing language: #{l.map { |lang| "'#{lang}'" }.to_sentence}" if output_summary?
puts audit.summary
2021-03-21 13:59:43 -04:00
end
errors += audit.errors
end
else
audit = audit_cask_instance(cask)
puts audit.summary if audit.summary.present? && output_summary?(audit)
errors += audit.errors
end
errors
end
private
def output_summary?(audit = nil)
return true if @any_named_args.present?
return true if @audit_strict.present?
return false if audit.blank?
audit.errors?
end
2021-03-21 13:59:43 -04:00
def audit_languages(languages)
2020-09-29 23:46:30 +02:00
original_config = cask.config
2024-03-07 16:20:20 +00:00
localized_config = original_config.merge(Config.new(explicit: { languages: }))
2020-09-29 23:46:30 +02:00
cask.config = localized_config
audit_cask_instance(cask)
ensure
cask.config = original_config
end
def audit_cask_instance(cask)
audit = Audit.new(
cask,
2020-09-04 05:19:33 +02:00
online: @audit_online,
strict: @audit_strict,
2022-08-01 14:30:04 +02:00
signing: @audit_signing,
2020-09-04 05:19:33 +02:00
new_cask: @audit_new_cask,
token_conflicts: @audit_token_conflicts,
download: @audit_download,
quarantine: @quarantine,
only: @only,
except: @except,
)
2016-09-24 13:52:43 +02:00
audit.run!
end
def language_blocks
cask.instance_variable_get(:@dsl).instance_variable_get(:@language_blocks)
end
2016-08-18 22:11:42 +03:00
end
end