Skip to content

Commit

Permalink
Merge pull request #18404 from Homebrew/forbid-url-do-homebrew-cask
Browse files Browse the repository at this point in the history
  • Loading branch information
Rylan12 authored Sep 25, 2024
2 parents e58f717 + d7e0c8f commit 479eeaa
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
9 changes: 9 additions & 0 deletions Library/Homebrew/rubocops/cask/mixin/cask_help.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def on_block(block_node)

return unless block_node.cask_block?

@file_path = processed_source.file_path

cask_block = RuboCop::Cask::AST::CaskBlock.new(block_node, comments)
on_cask(cask_block)
end
Expand All @@ -39,6 +41,13 @@ def inner_stanzas(block_node, comments)
inner_nodes = block_contents.map(&:child_nodes).flatten.select(&:send_type?)
inner_nodes.map { |n| RuboCop::Cask::AST::Stanza.new(n, comments) }
end

sig { returns(T.nilable(String)) }
def cask_tap
return unless (match_obj = @file_path.match(%r{/(homebrew-\w+)/}))

match_obj[1]
end
end
end
end
Expand Down
7 changes: 6 additions & 1 deletion Library/Homebrew/rubocops/cask/url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ class Url < Base
include UrlHelper

def on_url_stanza(stanza)
return if stanza.stanza_node.block_type?
if stanza.stanza_node.block_type?
if cask_tap == "homebrew-cask"
add_offense(stanza.stanza_node, message: 'Do not use `url "..." do` blocks in Homebrew/homebrew-cask.')
end
return
end

url_stanza = stanza.stanza_node.first_argument
hash_node = stanza.stanza_node.last_argument
Expand Down
39 changes: 39 additions & 0 deletions Library/Homebrew/test/rubocops/cask/url_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,45 @@
require "rubocops/rubocop-cask"

RSpec.describe RuboCop::Cop::Cask::Url, :config do
it "allows regular `url` blocks in homebrew-cask" do
expect_no_offenses <<~CASK, "/homebrew-cask/Casks/f/foo.rb"
cask "foo" do
url "https://example.com/download/foo-v1.2.0.dmg",
verified: "example.com/download/"
end
CASK
end

it "does not allow `url do` blocks in homebrew-cask" do
expect_offense <<~CASK, "/homebrew-cask/Casks/f/foo.rb"
cask "foo" do
url "https://example.com/download/foo-v1.2.0.dmg" do |url|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Do not use `url "..." do` blocks in Homebrew/homebrew-cask.
url
end
end
CASK
end

it "allows regular `url` blocks in a non-homebrew-cask tap" do
expect_no_offenses <<~CASK, "/homebrew-tap/Casks/f/foo.rb"
cask "foo" do
url "https://example.com/download/foo-v1.2.0.dmg",
verified: "example.com/download/"
end
CASK
end

it "allows `url do` blocks in a non-homebrew-cask tap" do
expect_no_offenses <<~CASK, "/homebrew-tap/Casks/f/foo.rb"
cask "foo" do
url "https://example.com/download/foo-v1.2.0.dmg" do |url|
url
end
end
CASK
end

it "accepts a `verified` value that does not start with a protocol" do
expect_no_offenses <<~CASK
cask "foo" do
Expand Down

0 comments on commit 479eeaa

Please sign in to comment.