Skip to content

Commit

Permalink
Merge pull request #18622 from Homebrew/livecheck/error-on-invalid-ur…
Browse files Browse the repository at this point in the history
…l-symbol

livecheck: error on invalid url symbol
  • Loading branch information
MikeMcQuaid authored Oct 26, 2024
2 parents b34e6ee + bfdb84f commit 368d086
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
12 changes: 9 additions & 3 deletions Library/Homebrew/livecheck/livecheck.rb
Original file line number Diff line number Diff line change
Expand Up @@ -508,10 +508,10 @@ def self.status_hash(package_or_resource, status_str, messages = nil, full_name:
params(
livecheck_url: T.any(String, Symbol),
package_or_resource: T.any(Formula, Cask::Cask, Resource),
).returns(T.nilable(String))
).returns(String)
}
def self.livecheck_url_to_string(livecheck_url, package_or_resource)
case livecheck_url
livecheck_url_string = case livecheck_url
when String
livecheck_url
when :url
Expand All @@ -521,6 +521,12 @@ def self.livecheck_url_to_string(livecheck_url, package_or_resource)
when :homepage
package_or_resource.homepage unless package_or_resource.is_a?(Resource)
end

if livecheck_url.is_a?(Symbol) && !livecheck_url_string
raise ArgumentError, "`url #{livecheck_url.inspect}` does not reference a checkable URL"
end

livecheck_url_string
end

# Returns an Array containing the formula/cask/resource URLs that can be used by livecheck.
Expand Down Expand Up @@ -846,7 +852,7 @@ def self.resource_version(
livecheck_strategy = livecheck.strategy
livecheck_strategy_block = livecheck.strategy_block

livecheck_url_string = livecheck_url_to_string(livecheck_url, resource)
livecheck_url_string = livecheck_url_to_string(livecheck_url, resource) if livecheck_url

urls = [livecheck_url_string] if livecheck_url_string
urls ||= checkable_urls(resource)
Expand Down
54 changes: 51 additions & 3 deletions Library/Homebrew/test/livecheck/livecheck_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@
end
end

let(:f_stable_url_only) do
stable_url_s = stable_url

formula("test_stable_url_only") do
desc "Test formula with only a stable URL"
url stable_url_s
end
end

let(:r) { f.resources.first }

let(:c) do
Expand All @@ -56,6 +65,17 @@
RUBY
end

let(:c_no_checkable_urls) do
Cask::CaskLoader.load(+<<-RUBY)
cask "test_no_checkable_urls" do
version "1.2.3"
name "Test"
desc "Test cask with no checkable URLs"
end
RUBY
end

describe "::resolve_livecheck_reference" do
context "when a formula/cask has a livecheck block without formula/cask methods" do
it "returns [nil, []]" do
Expand Down Expand Up @@ -167,9 +187,35 @@
end

it "returns nil when not given a string or valid symbol" do
expect(livecheck.livecheck_url_to_string(:invalid_symbol, f_livecheck_url)).to be_nil
expect(livecheck.livecheck_url_to_string(:invalid_symbol, c_livecheck_url)).to be_nil
expect(livecheck.livecheck_url_to_string(:invalid_symbol, r_livecheck_url)).to be_nil
error_text = "`url :%<symbol>s` does not reference a checkable URL"

# Invalid symbol in any context
expect { livecheck.livecheck_url_to_string(:invalid_symbol, f_livecheck_url) }
.to raise_error(ArgumentError, format(error_text, symbol: :invalid_symbol))
expect { livecheck.livecheck_url_to_string(:invalid_symbol, c_livecheck_url) }
.to raise_error(ArgumentError, format(error_text, symbol: :invalid_symbol))
expect { livecheck.livecheck_url_to_string(:invalid_symbol, r_livecheck_url) }
.to raise_error(ArgumentError, format(error_text, symbol: :invalid_symbol))

# Valid symbol in provided context but referenced URL is not present
expect { livecheck.livecheck_url_to_string(:head, f_stable_url_only) }
.to raise_error(ArgumentError, format(error_text, symbol: :head))
expect { livecheck.livecheck_url_to_string(:homepage, f_stable_url_only) }
.to raise_error(ArgumentError, format(error_text, symbol: :homepage))
expect { livecheck.livecheck_url_to_string(:homepage, c_no_checkable_urls) }
.to raise_error(ArgumentError, format(error_text, symbol: :homepage))
expect { livecheck.livecheck_url_to_string(:url, c_no_checkable_urls) }
.to raise_error(ArgumentError, format(error_text, symbol: :url))

# Valid symbol but not in the provided context
expect { livecheck.livecheck_url_to_string(:head, c_livecheck_url) }
.to raise_error(ArgumentError, format(error_text, symbol: :head))
expect { livecheck.livecheck_url_to_string(:homepage, r_livecheck_url) }
.to raise_error(ArgumentError, format(error_text, symbol: :homepage))
expect { livecheck.livecheck_url_to_string(:stable, c_livecheck_url) }
.to raise_error(ArgumentError, format(error_text, symbol: :stable))
expect { livecheck.livecheck_url_to_string(:url, f_livecheck_url) }
.to raise_error(ArgumentError, format(error_text, symbol: :url))
end
end

Expand All @@ -189,6 +235,8 @@
expect(livecheck.checkable_urls(c)).to eq([cask_url, homepage_url])
expect(livecheck.checkable_urls(r)).to eq([resource_url])
expect(livecheck.checkable_urls(f_duplicate_urls)).to eq([stable_url, head_url])
expect(livecheck.checkable_urls(f_stable_url_only)).to eq([stable_url])
expect(livecheck.checkable_urls(c_no_checkable_urls)).to eq([])
end
end

Expand Down

0 comments on commit 368d086

Please sign in to comment.