Merge pull request #10309 from samford/livecheck-dsl-avoid-url-symbol-conversion

Livecheck#url: Don't convert URL symbol to string
This commit is contained in:
Sam Ford 2021-01-15 14:23:31 -05:00 committed by GitHub
commit 8869208a60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 161 additions and 67 deletions

View File

@ -92,17 +92,11 @@ class Livecheck
# @param val [String, Symbol] URL to check for version information # @param val [String, Symbol] URL to check for version information
# @return [String, nil] # @return [String, nil]
def url(val = nil) def url(val = nil)
@url = case val case val
when nil when nil
return @url @url
when :url when String, :head, :homepage, :stable, :url
@formula_or_cask.url.to_s @url = val
when :head, :stable
@formula_or_cask.send(val).url
when :homepage
@formula_or_cask.homepage
when String
val
else else
raise TypeError, "Livecheck#url expects a String or valid Symbol" raise TypeError, "Livecheck#url expects a String or valid Symbol"
end end

View File

@ -311,6 +311,25 @@ module Homebrew
puts "#{formula_or_cask_s} : #{current_s} ==> #{latest_s}" puts "#{formula_or_cask_s} : #{current_s} ==> #{latest_s}"
end end
sig do
params(
livecheck_url: T.any(String, Symbol),
formula_or_cask: T.any(Formula, Cask::Cask),
).returns(T.nilable(String))
end
def livecheck_url_to_string(livecheck_url, formula_or_cask)
case livecheck_url
when String
livecheck_url
when :url
formula_or_cask.url&.to_s if formula_or_cask.is_a?(Cask::Cask)
when :head, :stable
formula_or_cask.send(livecheck_url)&.url if formula_or_cask.is_a?(Formula)
when :homepage
formula_or_cask.homepage
end
end
# Returns an Array containing the formula/cask URLs that can be used by livecheck. # Returns an Array containing the formula/cask URLs that can be used by livecheck.
sig { params(formula_or_cask: T.any(Formula, Cask::Cask)).returns(T::Array[String]) } sig { params(formula_or_cask: T.any(Formula, Cask::Cask)).returns(T::Array[String]) }
def checkable_urls(formula_or_cask) def checkable_urls(formula_or_cask)
@ -396,7 +415,9 @@ module Homebrew
livecheck_regex = livecheck.regex livecheck_regex = livecheck.regex
livecheck_strategy = livecheck.strategy livecheck_strategy = livecheck.strategy
urls = [livecheck_url] if livecheck_url.present? livecheck_url_string = livecheck_url_to_string(livecheck_url, formula_or_cask)
urls = [livecheck_url_string] if livecheck_url_string
urls ||= checkable_urls(formula_or_cask) urls ||= checkable_urls(formula_or_cask)
if debug if debug
@ -413,7 +434,12 @@ module Homebrew
urls.each_with_index do |original_url, i| urls.each_with_index do |original_url, i|
if debug if debug
puts puts
puts "URL: #{original_url}" if livecheck_url.is_a?(Symbol)
# This assumes the URL symbol will fit within the available space
puts "URL (#{livecheck_url}):".ljust(18, " ") + original_url
else
puts "URL: #{original_url}"
end
end end
# Skip Gists until/unless we create a method of identifying revisions # Skip Gists until/unless we create a method of identifying revisions
@ -514,15 +540,16 @@ module Homebrew
} }
if json && verbose if json && verbose
version_info[:meta] = { version_info[:meta] = {}
url: {
original: original_url, version_info[:meta][:url] = {}
}, version_info[:meta][:url][:symbol] = livecheck_url if livecheck_url.is_a?(Symbol) && livecheck_url_string
strategy: strategy.blank? ? nil : strategy_name, version_info[:meta][:url][:original] = original_url
}
version_info[:meta][:url][:processed] = url if url != original_url version_info[:meta][:url][:processed] = url if url != original_url
version_info[:meta][:url][:strategy] = strategy_data[:url] if strategy_data[:url] != url version_info[:meta][:url][:strategy] = strategy_data[:url] if strategy_data[:url] != url
version_info[:meta][:url][:final] = strategy_data[:final_url] if strategy_data[:final_url] version_info[:meta][:url][:final] = strategy_data[:final_url] if strategy_data[:final_url]
version_info[:meta][:strategy] = strategy.present? ? strategy_name : nil
version_info[:meta][:strategies] = strategies.map { |s| livecheck_strategy_names[s] } if strategies.present? version_info[:meta][:strategies] = strategies.map { |s| livecheck_strategy_names[s] } if strategies.present?
version_info[:meta][:regex] = regex.inspect if regex.present? version_info[:meta][:regex] = regex.inspect if regex.present?
version_info[:meta][:cached] = true if strategy_data[:cached] == true version_info[:meta][:cached] = true if strategy_data[:cached] == true

View File

@ -695,7 +695,7 @@ describe Formula do
end end
end end
expect(f.livecheck.url).to eq("https://brew.sh/test") expect(f.livecheck.url).to eq(:homepage)
end end
end end

View File

@ -6,6 +6,12 @@ require "livecheck/livecheck"
describe Homebrew::Livecheck do describe Homebrew::Livecheck do
subject(:livecheck) { described_class } subject(:livecheck) { described_class }
let(:cask_url) { "https://brew.sh/test-0.0.1.dmg" }
let(:head_url) { "https://github.com/Homebrew/brew.git" }
let(:homepage_url) { "https://brew.sh" }
let(:livecheck_url) { "https://formulae.brew.sh/api/formula/ruby.json" }
let(:stable_url) { "https://brew.sh/test-0.0.1.tgz" }
let(:f) do let(:f) do
formula("test") do formula("test") do
desc "Test formula" desc "Test formula"
@ -25,7 +31,7 @@ describe Homebrew::Livecheck do
cask "test" do cask "test" do
version "0.0.1,2" version "0.0.1,2"
url "https://brew.sh/test-0.0.1.tgz" url "https://brew.sh/test-0.0.1.dmg"
name "Test" name "Test"
desc "Test cask" desc "Test cask"
homepage "https://brew.sh" homepage "https://brew.sh"
@ -72,13 +78,63 @@ describe Homebrew::Livecheck do
end end
end end
describe "::livecheck_url_to_string" do
let(:f_livecheck_url) do
formula("test_livecheck_url") do
desc "Test Livecheck URL formula"
homepage "https://brew.sh"
url "https://brew.sh/test-0.0.1.tgz"
head "https://github.com/Homebrew/brew.git"
end
end
let(:c_livecheck_url) do
Cask::CaskLoader.load(+<<-RUBY)
cask "test_livecheck_url" do
version "0.0.1,2"
url "https://brew.sh/test-0.0.1.dmg"
name "Test"
desc "Test Livecheck URL cask"
homepage "https://brew.sh"
end
RUBY
end
it "returns a URL string when given a livecheck_url string" do
f_livecheck_url.livecheck.url(livecheck_url)
expect(livecheck.livecheck_url_to_string(livecheck_url, f_livecheck_url)).to eq(livecheck_url)
end
it "returns a URL symbol when given a valid livecheck_url symbol" do
f_livecheck_url.livecheck.url(:head)
expect(livecheck.livecheck_url_to_string(head_url, f_livecheck_url)).to eq(head_url)
f_livecheck_url.livecheck.url(:homepage)
expect(livecheck.livecheck_url_to_string(homepage_url, f_livecheck_url)).to eq(homepage_url)
c_livecheck_url.livecheck.url(:homepage)
expect(livecheck.livecheck_url_to_string(homepage_url, c_livecheck_url)).to eq(homepage_url)
f_livecheck_url.livecheck.url(:stable)
expect(livecheck.livecheck_url_to_string(stable_url, f_livecheck_url)).to eq(stable_url)
c_livecheck_url.livecheck.url(:url)
expect(livecheck.livecheck_url_to_string(cask_url, c_livecheck_url)).to eq(cask_url)
end
it "returns nil when not given a string or valid symbol" do
expect(livecheck.livecheck_url_to_string(nil, f_livecheck_url)).to eq(nil)
expect(livecheck.livecheck_url_to_string(nil, c_livecheck_url)).to eq(nil)
expect(livecheck.livecheck_url_to_string(:invalid_symbol, f_livecheck_url)).to eq(nil)
expect(livecheck.livecheck_url_to_string(:invalid_symbol, c_livecheck_url)).to eq(nil)
end
end
describe "::checkable_urls" do describe "::checkable_urls" do
it "returns the list of URLs to check" do it "returns the list of URLs to check" do
expect(livecheck.checkable_urls(f)) expect(livecheck.checkable_urls(f)).to eq([head_url, stable_url, homepage_url])
.to eq( expect(livecheck.checkable_urls(c)).to eq([cask_url, homepage_url])
["https://github.com/Homebrew/brew.git", "https://brew.sh/test-0.0.1.tgz", "https://brew.sh"],
)
expect(livecheck.checkable_urls(c)).to eq(["https://brew.sh/test-0.0.1.tgz", "https://brew.sh"])
end end
end end

View File

@ -5,111 +5,128 @@ require "formula"
require "livecheck" require "livecheck"
describe Livecheck do describe Livecheck do
HOMEPAGE_URL = "https://example.com/"
STABLE_URL = "https://example.com/example-1.2.3.tar.gz"
HEAD_URL = "https://example.com/example.git"
let(:f) do let(:f) do
formula do formula do
homepage HOMEPAGE_URL homepage "https://brew.sh"
url STABLE_URL url "https://brew.sh/test-0.0.1.tgz"
head HEAD_URL head "https://github.com/Homebrew/brew.git"
end end
end end
let(:livecheckable) { described_class.new(f) } let(:livecheckable_f) { described_class.new(f) }
let(:c) do
Cask::CaskLoader.load(+<<-RUBY)
cask "test" do
version "0.0.1,2"
url "https://brew.sh/test-0.0.1.dmg"
name "Test"
desc "Test cask"
homepage "https://brew.sh"
end
RUBY
end
let(:livecheckable_c) { described_class.new(c) }
describe "#regex" do describe "#regex" do
it "returns nil if not set" do it "returns nil if not set" do
expect(livecheckable.regex).to be nil expect(livecheckable_f.regex).to be nil
end end
it "returns the Regexp if set" do it "returns the Regexp if set" do
livecheckable.regex(/foo/) livecheckable_f.regex(/foo/)
expect(livecheckable.regex).to eq(/foo/) expect(livecheckable_f.regex).to eq(/foo/)
end end
it "raises a TypeError if the argument isn't a Regexp" do it "raises a TypeError if the argument isn't a Regexp" do
expect { expect {
livecheckable.regex("foo") livecheckable_f.regex("foo")
}.to raise_error(TypeError, "Livecheck#regex expects a Regexp") }.to raise_error(TypeError, "Livecheck#regex expects a Regexp")
end end
end end
describe "#skip" do describe "#skip" do
it "sets @skip to true when no argument is provided" do it "sets @skip to true when no argument is provided" do
expect(livecheckable.skip).to be true expect(livecheckable_f.skip).to be true
expect(livecheckable.instance_variable_get(:@skip)).to be true expect(livecheckable_f.instance_variable_get(:@skip)).to be true
expect(livecheckable.instance_variable_get(:@skip_msg)).to be nil expect(livecheckable_f.instance_variable_get(:@skip_msg)).to be nil
end end
it "sets @skip to true and @skip_msg to the provided String" do it "sets @skip to true and @skip_msg to the provided String" do
expect(livecheckable.skip("foo")).to be true expect(livecheckable_f.skip("foo")).to be true
expect(livecheckable.instance_variable_get(:@skip)).to be true expect(livecheckable_f.instance_variable_get(:@skip)).to be true
expect(livecheckable.instance_variable_get(:@skip_msg)).to eq("foo") expect(livecheckable_f.instance_variable_get(:@skip_msg)).to eq("foo")
end end
it "raises a TypeError if the argument isn't a String" do it "raises a TypeError if the argument isn't a String" do
expect { expect {
livecheckable.skip(/foo/) livecheckable_f.skip(/foo/)
}.to raise_error(TypeError, "Livecheck#skip expects a String") }.to raise_error(TypeError, "Livecheck#skip expects a String")
end end
end end
describe "#skip?" do describe "#skip?" do
it "returns the value of @skip" do it "returns the value of @skip" do
expect(livecheckable.skip?).to be false expect(livecheckable_f.skip?).to be false
livecheckable.skip livecheckable_f.skip
expect(livecheckable.skip?).to be true expect(livecheckable_f.skip?).to be true
end end
end end
describe "#strategy" do describe "#strategy" do
it "returns nil if not set" do it "returns nil if not set" do
expect(livecheckable.strategy).to be nil expect(livecheckable_f.strategy).to be nil
end end
it "returns the Symbol if set" do it "returns the Symbol if set" do
livecheckable.strategy(:page_match) livecheckable_f.strategy(:page_match)
expect(livecheckable.strategy).to eq(:page_match) expect(livecheckable_f.strategy).to eq(:page_match)
end end
it "raises a TypeError if the argument isn't a Symbol" do it "raises a TypeError if the argument isn't a Symbol" do
expect { expect {
livecheckable.strategy("page_match") livecheckable_f.strategy("page_match")
}.to raise_error(TypeError, "Livecheck#strategy expects a Symbol") }.to raise_error(TypeError, "Livecheck#strategy expects a Symbol")
end end
end end
describe "#url" do describe "#url" do
let(:url_string) { "https://brew.sh" }
it "returns nil if not set" do it "returns nil if not set" do
expect(livecheckable.url).to be nil expect(livecheckable_f.url).to be nil
end end
it "returns the URL if set" do it "returns a string when set to a string" do
livecheckable.url("foo") livecheckable_f.url(url_string)
expect(livecheckable.url).to eq("foo") expect(livecheckable_f.url).to eq(url_string)
livecheckable.url(:homepage)
expect(livecheckable.url).to eq(HOMEPAGE_URL)
livecheckable.url(:stable)
expect(livecheckable.url).to eq(STABLE_URL)
livecheckable.url(:head)
expect(livecheckable.url).to eq(HEAD_URL)
end end
it "raises a TypeError if the argument isn't a String or Symbol" do it "returns the URL symbol if valid" do
livecheckable_f.url(:head)
expect(livecheckable_f.url).to eq(:head)
livecheckable_f.url(:homepage)
expect(livecheckable_f.url).to eq(:homepage)
livecheckable_f.url(:stable)
expect(livecheckable_f.url).to eq(:stable)
livecheckable_c.url(:url)
expect(livecheckable_c.url).to eq(:url)
end
it "raises a TypeError if the argument isn't a String or valid Symbol" do
expect { expect {
livecheckable.url(/foo/) livecheckable_f.url(/foo/)
}.to raise_error(TypeError, "Livecheck#url expects a String or valid Symbol") }.to raise_error(TypeError, "Livecheck#url expects a String or valid Symbol")
end end
end end
describe "#to_hash" do describe "#to_hash" do
it "returns a Hash of all instance variables" do it "returns a Hash of all instance variables" do
expect(livecheckable.to_hash).to eq( expect(livecheckable_f.to_hash).to eq(
{ {
"regex" => nil, "regex" => nil,
"skip" => false, "skip" => false,