Type livecheck.rb.

This commit is contained in:
Markus Reiter 2023-04-21 01:21:38 +02:00
parent c1fdb59081
commit dfc9d94c5b
No known key found for this signature in database
GPG Key ID: 245293B51702655B
3 changed files with 60 additions and 71 deletions

View File

@ -327,7 +327,7 @@ module Cask
# @api public # @api public
def livecheck(&block) def livecheck(&block)
@livecheck ||= Livecheck.new(self) @livecheck ||= Livecheck.new(cask)
return @livecheck unless block return @livecheck unless block
if !@cask.allow_reassignment && @livecheckable if !@cask.allow_reassignment && @livecheckable

View File

@ -16,9 +16,10 @@ class Livecheck
# A very brief description of why the formula/cask/resource is skipped (e.g. # A very brief description of why the formula/cask/resource is skipped (e.g.
# `No longer developed or maintained`). # `No longer developed or maintained`).
# @return [String, nil] sig { returns(T.nilable(String)) }
attr_reader :skip_msg attr_reader :skip_msg
sig { params(package_or_resource: T.any(Cask::Cask, T.class_of(Formula), Resource)).void }
def initialize(package_or_resource) def initialize(package_or_resource)
@package_or_resource = package_or_resource @package_or_resource = package_or_resource
@referenced_cask_name = nil @referenced_cask_name = nil
@ -27,6 +28,7 @@ class Livecheck
@skip = false @skip = false
@skip_msg = nil @skip_msg = nil
@strategy = nil @strategy = nil
@strategy_block = nil
@url = nil @url = nil
end end
@ -34,17 +36,18 @@ class Livecheck
# or returns the `@referenced_cask_name` instance variable when no argument # or returns the `@referenced_cask_name` instance variable when no argument
# is provided. Inherited livecheck values from the referenced cask # is provided. Inherited livecheck values from the referenced cask
# (e.g. regex) can be overridden in the livecheck block. # (e.g. regex) can be overridden in the livecheck block.
# sig {
# @param cask_name [String] name of cask to inherit livecheck info from params(
# @return [String, nil] # Name of cask to inherit livecheck info from.
def cask(cask_name = nil) cask_name: String,
).returns(T.nilable(String))
}
def cask(cask_name = T.unsafe(nil))
case cask_name case cask_name
when nil when nil
@referenced_cask_name @referenced_cask_name
when String when String
@referenced_cask_name = cask_name @referenced_cask_name = cask_name
else
raise TypeError, "Livecheck#cask expects a String"
end end
end end
@ -52,33 +55,35 @@ class Livecheck
# `String` or returns the `@referenced_formula_name` instance variable when # `String` or returns the `@referenced_formula_name` instance variable when
# no argument is provided. Inherited livecheck values from the referenced # no argument is provided. Inherited livecheck values from the referenced
# formula (e.g. regex) can be overridden in the livecheck block. # formula (e.g. regex) can be overridden in the livecheck block.
# sig {
# @param formula_name [String] name of formula to inherit livecheck info from params(
# @return [String, nil] # Name of formula to inherit livecheck info from.
def formula(formula_name = nil) formula_name: String,
).returns(T.nilable(String))
}
def formula(formula_name = T.unsafe(nil))
case formula_name case formula_name
when nil when nil
@referenced_formula_name @referenced_formula_name
when String when String
@referenced_formula_name = formula_name @referenced_formula_name = formula_name
else
raise TypeError, "Livecheck#formula expects a String"
end end
end end
# Sets the `@regex` instance variable to the provided `Regexp` or returns the # Sets the `@regex` instance variable to the provided `Regexp` or returns the
# `@regex` instance variable when no argument is provided. # `@regex` instance variable when no argument is provided.
# sig {
# @param pattern [Regexp] regex to use for matching versions in content params(
# @return [Regexp, nil] # Regex to use for matching versions in content.
def regex(pattern = nil) pattern: Regexp,
).returns(T.nilable(Regexp))
}
def regex(pattern = T.unsafe(nil))
case pattern case pattern
when nil when nil
@regex @regex
when Regexp when Regexp
@regex = pattern @regex = pattern
else
raise TypeError, "Livecheck#regex expects a Regexp"
end end
end end
@ -87,20 +92,20 @@ class Livecheck
# that the formula/cask/resource should be skipped and the `skip_msg` very # that the formula/cask/resource should be skipped and the `skip_msg` very
# briefly describes why it is skipped (e.g. "No longer developed or # briefly describes why it is skipped (e.g. "No longer developed or
# maintained"). # maintained").
# sig {
# @param skip_msg [String] string describing why the formula/cask is skipped params(
# @return [Boolean] # String describing why the formula/cask is skipped.
def skip(skip_msg = nil) skip_msg: String,
if skip_msg.is_a?(String) ).returns(T::Boolean)
@skip_msg = skip_msg }
elsif skip_msg.present? def skip(skip_msg = T.unsafe(nil))
raise TypeError, "Livecheck#skip expects a String" @skip_msg = skip_msg if skip_msg.is_a?(String)
end
@skip = true @skip = true
end end
# Should `livecheck` skip this formula/cask/resource? # Should `livecheck` skip this formula/cask/resource?
sig { returns(T::Boolean) }
def skip? def skip?
@skip @skip
end end
@ -109,10 +114,14 @@ class Livecheck
# the `@strategy` instance variable when no argument is provided. The strategy # the `@strategy` instance variable when no argument is provided. The strategy
# symbols use snake case (e.g. `:page_match`) and correspond to the strategy # symbols use snake case (e.g. `:page_match`) and correspond to the strategy
# file name. # file name.
# sig {
# @param symbol [Symbol] symbol for the desired strategy params(
# @return [Symbol, nil] # Symbol for the desired strategy.
def strategy(symbol = nil, &block) symbol: Symbol,
block: T.nilable(Proc),
).returns(T.nilable(Symbol))
}
def strategy(symbol = T.unsafe(nil), &block)
@strategy_block = block if block @strategy_block = block if block
case symbol case symbol
@ -120,27 +129,30 @@ class Livecheck
@strategy @strategy
when Symbol when Symbol
@strategy = symbol @strategy = symbol
else
raise TypeError, "Livecheck#strategy expects a Symbol"
end end
end end
sig { returns(T.nilable(Proc)) }
attr_reader :strategy_block attr_reader :strategy_block
# Sets the `@url` instance variable to the provided argument or returns the # Sets the `@url` instance variable to the provided argument or returns the
# `@url` instance variable when no argument is provided. The argument can be # `@url` instance variable when no argument is provided. The argument can be
# a `String` (a URL) or a supported `Symbol` corresponding to a URL in the # a `String` (a URL) or a supported `Symbol` corresponding to a URL in the
# formula/cask/resource (e.g. `:stable`, `:homepage`, `:head`, `:url`). # formula/cask/resource (e.g. `:stable`, `:homepage`, `:head`, `:url`).
# @param val [String, Symbol] URL to check for version information sig {
# @return [String, nil] params(
def url(val = nil) # URL to check for version information.
case val url: T.any(String, Symbol),
).returns(T.nilable(T.any(String, Symbol)))
}
def url(url = T.unsafe(nil))
case url
when nil when nil
@url @url
when String, :head, :homepage, :stable, :url when String, :head, :homepage, :stable, :url
@url = val @url = url
else when Symbol
raise TypeError, "Livecheck#url expects a String or valid Symbol" raise ArgumentError, "#{url.inspect} is not a valid URL shorthand"
end end
end end
@ -150,6 +162,7 @@ class Livecheck
# Returns a `Hash` of all instance variable values. # Returns a `Hash` of all instance variable values.
# @return [Hash] # @return [Hash]
sig { returns(T::Hash[String, T.untyped]) }
def to_hash def to_hash
{ {
"cask" => @referenced_cask_name, "cask" => @referenced_cask_name,

View File

@ -11,7 +11,7 @@ describe Livecheck do
head "https://github.com/Homebrew/brew.git" head "https://github.com/Homebrew/brew.git"
end end
end end
let(:livecheckable_f) { described_class.new(f) } let(:livecheckable_f) { described_class.new(f.class) }
let(:c) do let(:c) do
Cask::CaskLoader.load(+<<-RUBY) Cask::CaskLoader.load(+<<-RUBY)
@ -40,7 +40,7 @@ describe Livecheck do
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 do expect do
livecheckable_f.formula(123) livecheckable_f.formula(123)
end.to raise_error(TypeError, "Livecheck#formula expects a String") end.to raise_error TypeError
end end
end end
@ -53,12 +53,6 @@ describe Livecheck do
livecheckable_c.cask("other-cask") livecheckable_c.cask("other-cask")
expect(livecheckable_c.cask).to eq("other-cask") expect(livecheckable_c.cask).to eq("other-cask")
end end
it "raises a TypeError if the argument isn't a String" do
expect do
livecheckable_c.cask(123)
end.to raise_error(TypeError, "Livecheck#cask expects a String")
end
end end
describe "#regex" do describe "#regex" do
@ -70,12 +64,6 @@ describe Livecheck do
livecheckable_f.regex(/foo/) livecheckable_f.regex(/foo/)
expect(livecheckable_f.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
expect do
livecheckable_f.regex("foo")
end.to raise_error(TypeError, "Livecheck#regex expects a Regexp")
end
end end
describe "#skip" do describe "#skip" do
@ -90,12 +78,6 @@ describe Livecheck do
expect(livecheckable_f.instance_variable_get(:@skip)).to be true expect(livecheckable_f.instance_variable_get(:@skip)).to be true
expect(livecheckable_f.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
expect do
livecheckable_f.skip(/foo/)
end.to raise_error(TypeError, "Livecheck#skip expects a String")
end
end end
describe "#skip?" do describe "#skip?" do
@ -116,12 +98,6 @@ describe Livecheck do
livecheckable_f.strategy(:page_match) livecheckable_f.strategy(:page_match)
expect(livecheckable_f.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
expect do
livecheckable_f.strategy("page_match")
end.to raise_error(TypeError, "Livecheck#strategy expects a Symbol")
end
end end
describe "#url" do describe "#url" do
@ -150,10 +126,10 @@ describe Livecheck do
expect(livecheckable_c.url).to eq(:url) expect(livecheckable_c.url).to eq(:url)
end end
it "raises a TypeError if the argument isn't a String or valid Symbol" do it "raises an ArgumentError if the argument isn't a valid Symbol" do
expect do expect do
livecheckable_f.url(/foo/) livecheckable_f.url(:not_a_valid_symbol)
end.to raise_error(TypeError, "Livecheck#url expects a String or valid Symbol") end.to raise_error ArgumentError
end end
end end