From e4156909d475c3f195e310aba2f6bb35ee752e21 Mon Sep 17 00:00:00 2001 From: Issy Long Date: Thu, 23 Mar 2023 17:43:25 +0000 Subject: [PATCH] Only take overrideable stanzas into account - This skips over stanza names that are not overrideable in `on_*` blocks, with the positive side effect that `on_*` blocks themselves aren't in the list so we can get rid of another conditional. - Stanzas overrideable in blocks are defined in `Cask::DSL` by each of the methods calling `set_unique_stanza`. --- .../Homebrew/rubocops/cask/no_overrides.rb | 10 ++++++++-- .../test/rubocops/cask/no_overrides_spec.rb | 20 +++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/rubocops/cask/no_overrides.rb b/Library/Homebrew/rubocops/cask/no_overrides.rb index a7a65c751f..8a72402cf1 100644 --- a/Library/Homebrew/rubocops/cask/no_overrides.rb +++ b/Library/Homebrew/rubocops/cask/no_overrides.rb @@ -9,6 +9,12 @@ module RuboCop include CaskHelp ON_SYSTEM_METHODS = RuboCop::Cask::Constants::ON_SYSTEM_METHODS + # These stanzas can be overridden by `on_*` blocks, so take them into account. + # TODO: Update this list if new stanzas are added to `Cask::DSL` that call `set_unique_stanza`. + OVERRIDEABLE_METHODS = [ + :appcast, :arch, :auto_updates, :conflicts_with, :container, + :desc, :homepage, :sha256, :url, :version + ].freeze MESSAGE = <<~EOS Do not use a top-level `%s` stanza as the default. Add it to an `on_{system}` block instead. Use `:or_older` or `:or_newer` to specify a range of macOS versions. @@ -23,8 +29,8 @@ module RuboCop stanzas_in_blocks = on_system_stanzas(on_blocks) cask_stanzas.each do |stanza| - # Skip if the stanza is itself an `on_*` block. - next if ON_SYSTEM_METHODS.include?(stanza.stanza_name) + # Skip if the stanza is not allowed to be overridden. + next unless OVERRIDEABLE_METHODS.include?(stanza.stanza_name) # Skip if the stanza outside of a block is not also in an `on_*` block. next unless stanzas_in_blocks.include?(stanza.stanza_name) diff --git a/Library/Homebrew/test/rubocops/cask/no_overrides_spec.rb b/Library/Homebrew/test/rubocops/cask/no_overrides_spec.rb index 312035d140..4227b90dd3 100644 --- a/Library/Homebrew/test/rubocops/cask/no_overrides_spec.rb +++ b/Library/Homebrew/test/rubocops/cask/no_overrides_spec.rb @@ -38,6 +38,26 @@ describe RuboCop::Cop::Cask::NoOverrides do include_examples "does not report any offenses" end + context "when there are top-level stanzas also in `on_*` blocks that should not override" do + let(:source) do + <<~CASK + cask 'foo' do + version '1.2.3' + + on_arm do + binary "foo-\#{version}-arm64" + end + + app "foo-\#{version}.app" + + binary "foo-\#{version}" + end + CASK + end + + include_examples "does not report any offenses" + end + context "when there are `arch` variables in the `url` in the `on_*` blocks" do let(:source) do <<~CASK