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

View File

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