Merge pull request #13496 from mohammadzainabbas/mohammad

Extend the `livecheck` DSL to work for resources
This commit is contained in:
Nanda H Krishna 2022-07-04 13:35:19 -04:00 committed by GitHub
commit 39c9e2ddbc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 77 additions and 12 deletions

View File

@ -1,9 +1,10 @@
# typed: true
# frozen_string_literal: true
# The {Livecheck} class implements the DSL methods used in a formula's or cask's
# `livecheck` block and stores related instance variables. Most of these methods
# also return the related instance variable when no argument is provided.
# The {Livecheck} class implements the DSL methods used in a formula's, cask's
# or resource's `livecheck` block and stores related instance variables. Most
# of these methods also return the related instance variable when no argument
# is provided.
#
# This information is used by the `brew livecheck` command to control its
# behavior. Example `livecheck` blocks can be found in the
@ -11,13 +12,13 @@
class Livecheck
extend Forwardable
# A very brief description of why the formula/cask is skipped (e.g. `No longer
# developed or maintained`).
# A very brief description of why the formula/cask/resource is skipped (e.g.
# `No longer developed or maintained`).
# @return [String, nil]
attr_reader :skip_msg
def initialize(formula_or_cask)
@formula_or_cask = formula_or_cask
def initialize(package_or_resource)
@package_or_resource = package_or_resource
@referenced_cask_name = nil
@referenced_formula_name = nil
@regex = nil
@ -81,8 +82,9 @@ class Livecheck
# Sets the `@skip` instance variable to `true` and sets the `@skip_msg`
# instance variable if a `String` is provided. `@skip` is used to indicate
# that the formula/cask should be skipped and the `skip_msg` very briefly
# describes why it is skipped (e.g. "No longer developed or maintained").
# 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]
@ -96,7 +98,7 @@ class Livecheck
@skip = true
end
# Should `livecheck` skip this formula/cask?
# Should `livecheck` skip this formula/cask/resource?
def skip?
@skip
end
@ -126,7 +128,7 @@ class Livecheck
# 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 (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
# @return [String, nil]
def url(val = nil)
@ -140,7 +142,7 @@ class Livecheck
end
end
delegate version: :@formula_or_cask
delegate version: :@package_or_resource
private :version
# Returns a `Hash` of all instance variable values.

View File

@ -5,6 +5,7 @@ require "download_strategy"
require "checksum"
require "version"
require "mktemp"
require "livecheck"
require "extend/on_system"
# Resource is the fundamental representation of an external resource. The
@ -36,6 +37,8 @@ class Resource
@checksum = nil
@using = nil
@patches = []
@livecheck = nil
@livecheckable = false
instance_eval(&block) if block
end
@ -168,6 +171,32 @@ class Resource
EOS
end
# @!attribute [w] livecheck
# {Livecheck} can be used to check for newer versions of the software.
# This method evaluates the DSL specified in the livecheck block of the
# {Resource} (if it exists) and sets the instance variables of a {Livecheck}
# object accordingly. This is used by `brew livecheck` to check for newer
# versions of the software.
#
# <pre>livecheck do
# url "https://example.com/foo/releases"
# regex /foo-(\d+(?:\.\d+)+)\.tar/
# end</pre>
def livecheck(&block)
@livecheck ||= Livecheck.new(self) if block
return @livecheck unless block
@livecheckable = true
@livecheck.instance_eval(&block)
end
# Whether a livecheck specification is defined or not.
# It returns true when a livecheck block is present in the {Resource} and
# false otherwise, and is used by livecheck.
def livecheckable?
@livecheckable == true
end
def sha256(val)
@checksum = Checksum.new(val)
end

View File

@ -2,10 +2,23 @@
# frozen_string_literal: true
require "resource"
require "livecheck"
describe Resource do
subject(:resource) { described_class.new("test") }
let(:livecheck_resource) {
described_class.new do
url "https://brew.sh/foo-1.0.tar.gz"
sha256 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
livecheck do
url "https://brew.sh/test/releases"
regex(/foo[._-]v?(\d+(?:\.\d+)+)\.t/i)
end
end
}
describe "#url" do
it "sets the URL" do
resource.url("foo")
@ -52,6 +65,27 @@ describe Resource do
end
end
describe "#livecheck" do
it "returns nil if livecheck block is not set in resource" do
expect(resource.livecheck).to be_nil
end
specify "when livecheck block is set" do
expect(livecheck_resource.livecheck.url).to eq("https://brew.sh/test/releases")
expect(livecheck_resource.livecheck.regex).to eq(/foo[._-]v?(\d+(?:\.\d+)+)\.t/i)
end
end
describe "#livecheckable?" do
it "returns false if livecheck block is not set in resource" do
expect(resource.livecheckable?).to be false
end
specify "livecheck block defined in resources" do
expect(livecheck_resource.livecheckable?).to be true
end
end
describe "#version" do
it "sets the version" do
resource.version("1.0")