Skip to content

Commit b7eed98

Browse files
authored
Merge pull request #16932 from samford/livecheck/update-url-rubocop
rubocops/livecheck: Rework LivecheckUrlProvided
2 parents d704e00 + b3ab410 commit b7eed98

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

Library/Homebrew/rubocops/livecheck.rb

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,19 @@ def audit_formula(_node, _class_node, _parent_class_node, body_node)
4545
class LivecheckUrlProvided < FormulaCop
4646
def audit_formula(_node, _class_node, _parent_class_node, body_node)
4747
livecheck_node = find_block(body_node, :livecheck)
48-
return if livecheck_node.blank?
49-
50-
skip = find_every_method_call_by_name(livecheck_node, :skip).first
51-
return if skip.present?
48+
return unless livecheck_node
5249

53-
formula_node = find_every_method_call_by_name(livecheck_node, :formula).first
54-
cask_node = find_every_method_call_by_name(livecheck_node, :cask).first
55-
return if formula_node.present? || cask_node.present?
50+
url_node = find_every_method_call_by_name(livecheck_node, :url).first
51+
return if url_node
5652

57-
livecheck_url = find_every_method_call_by_name(livecheck_node, :url).first
58-
return if livecheck_url.present?
53+
# A regex and/or strategy is specific to a particular URL, so we
54+
# should require an explicit URL.
55+
regex_node = find_every_method_call_by_name(livecheck_node, :regex).first
56+
strategy_node = find_every_method_call_by_name(livecheck_node, :strategy).first
57+
return if !regex_node && !strategy_node
5958

6059
offending_node(livecheck_node)
61-
problem "A `url` must be provided to livecheck."
60+
problem "A `url` should be provided when `regex` or `strategy` are used."
6261
end
6362
end
6463

Library/Homebrew/test/rubocops/livecheck/url_provided_spec.rb

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,31 @@
55
RSpec.describe RuboCop::Cop::FormulaAudit::LivecheckUrlProvided do
66
subject(:cop) { described_class.new }
77

8-
it "reports an offense when a `url` is not specified in the livecheck block" do
8+
it "reports an offense when a `url` is not specified in a livecheck block" do
99
expect_offense(<<~RUBY)
1010
class Foo < Formula
1111
url "https://brew.sh/foo-1.0.tgz"
1212
1313
livecheck do
14-
^^^^^^^^^^^^ FormulaAudit/LivecheckUrlProvided: A `url` must be provided to livecheck.
14+
^^^^^^^^^^^^ FormulaAudit/LivecheckUrlProvided: A `url` should be provided when `regex` or `strategy` are used.
1515
regex(%r{href=.*?/formula[._-]v?(\\d+(?:\\.\\d+)+)\\.t}i)
1616
end
1717
end
1818
RUBY
19+
20+
expect_offense(<<~RUBY)
21+
class Foo < Formula
22+
url "https://brew.sh/foo-1.0.tgz"
23+
24+
livecheck do
25+
^^^^^^^^^^^^ FormulaAudit/LivecheckUrlProvided: A `url` should be provided when `regex` or `strategy` are used.
26+
strategy :page_match
27+
end
28+
end
29+
RUBY
1930
end
2031

21-
it "reports no offenses when a `url` is specified in the livecheck block" do
32+
it "reports no offenses when a `url` and `regex` are specified in the livecheck block" do
2233
expect_no_offenses(<<~RUBY)
2334
class Foo < Formula
2435
url "https://brew.sh/foo-1.0.tgz"
@@ -30,4 +41,17 @@ class Foo < Formula
3041
end
3142
RUBY
3243
end
44+
45+
it "reports no offenses when a `url` and `strategy` are specified in the livecheck block" do
46+
expect_no_offenses(<<~RUBY)
47+
class Foo < Formula
48+
url "https://brew.sh/foo-1.0.tgz"
49+
50+
livecheck do
51+
url :stable
52+
strategy :page_match
53+
end
54+
end
55+
RUBY
56+
end
3357
end

0 commit comments

Comments
 (0)