rubocops/cask: Casks with zero on_* blocks are still valid

This commit is contained in:
Issy Long 2023-03-19 20:41:43 +00:00
parent 5ce4966f4a
commit dec1309140
No known key found for this signature in database
GPG Key ID: 8247C390DADC67D4
2 changed files with 21 additions and 3 deletions

View File

@ -8,19 +8,22 @@ module RuboCop
extend T::Sig
include CaskHelp
ON_SYSTEM_METHODS = RuboCop::Cask::Constants::ON_SYSTEM_METHODS
MESSAGE = <<~EOS
Do not use top-level `%<stanza>s` stanza as the default, add an `on_{system}` block instead.
Use `:or_older` or `:or_newer` to specify a range of macOS versions.
EOS
def on_cask(cask_block)
return if cask_block.toplevel_stanzas.empty?
return if (cask_stanzas = cask_block.toplevel_stanzas).empty?
# Skip if there are no `on_*` blocks.
return unless cask_stanzas.any? { |s| ON_SYSTEM_METHODS.include?(s.stanza_name) }
cask_block.toplevel_stanzas.each do |stanza|
cask_stanzas.each do |stanza|
# TODO: We probably only want to disallow `version`, `url`, and `sha256` stanzas being overridden?
next unless RuboCop::Cask::Constants::STANZA_ORDER.include?(stanza.stanza_name)
# Skip if the stanza we detect is already in an `on_*` block.
next if stanza.parent_node.block_type? && RuboCop::Cask::Constants::ON_SYSTEM_METHODS.include?(stanza.parent_node.method_name)
next if stanza.parent_node.block_type? && ON_SYSTEM_METHODS.include?(stanza.parent_node.method_name)
add_offense(stanza.source_range, message: format(MESSAGE, stanza: stanza.stanza_name))
end

View File

@ -9,6 +9,21 @@ describe RuboCop::Cop::Cask::NoOverrides do
subject(:cop) { described_class.new }
context "when there are no on_system blocks" do
let(:source) do
<<~CASK
cask 'foo' do
version '1.2.3'
url 'https://brew.sh/foo.pkg'
name 'Foo'
end
CASK
end
include_examples "does not report any offenses"
end
context "when there are no top-level standalone stanzas" do
let(:source) do
<<~CASK