From ff63136c6f96f73f4641b4e5525e7d23fed3d7c0 Mon Sep 17 00:00:00 2001 From: Mohammad Zain Abbas Date: Thu, 30 Jun 2022 16:06:36 +0200 Subject: [PATCH 01/11] Added tests for `livecheck` in `resource_spec.rb` --- Library/Homebrew/test/resource_spec.rb | 47 ++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/Library/Homebrew/test/resource_spec.rb b/Library/Homebrew/test/resource_spec.rb index 63a14b0b1e..06117a9bff 100644 --- a/Library/Homebrew/test/resource_spec.rb +++ b/Library/Homebrew/test/resource_spec.rb @@ -2,6 +2,7 @@ # frozen_string_literal: true require "resource" +require "livecheck" describe Resource do subject(:resource) { described_class.new("test") } @@ -52,6 +53,52 @@ describe Resource do end end + describe Resource do + let(:r) do + livecheck do + url "https://brew.sh/foo-1.0.tar.gz" + regex(/foo/) + end + end + + let(:livecheckable_r) { described_class.new(r) } + + describe "#livecheck" do + it "returns nil if not set" do + expect(livecheckable_r.livecheck).to be_nil + end + + it "returns the string if set" do + livecheckable_r.livecheck("other-resource") + expect(livecheckable_r.livecheck).to eq("other-resource") + end + + it "raises a TypeError if the argument isn't a String" do + expect { + livecheckable_r.livecheck(123) + }.to raise_error(TypeError, "Resource#livecheck expects a String") + end + end + + describe "#regex" do + it "returns nil if not set" do + expect(livecheckable_r.regex).to be_nil + end + + it "returns the Regexp if set" do + livecheckable_r.regex(/foo/) + expect(livecheckable_r.regex).to eq(/foo/) + end + + it "raises a TypeError if the argument isn't a Regexp" do + expect { + livecheckable_r.regex("foo") + }.to raise_error(TypeError, "Resource#livecheck#regex expects a Regexp") + end + end + + end + describe "#version" do it "sets the version" do resource.version("1.0") From 1ba610707dff3fec62c35c94e942f8d6f6286deb Mon Sep 17 00:00:00 2001 From: Mohammad Zain Abbas Date: Thu, 30 Jun 2022 16:08:13 +0200 Subject: [PATCH 02/11] Extended the livecheck DSL to work for resources --- Library/Homebrew/resource.rb | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/Library/Homebrew/resource.rb b/Library/Homebrew/resource.rb index 49d9e362a8..6ee5a77214 100644 --- a/Library/Homebrew/resource.rb +++ b/Library/Homebrew/resource.rb @@ -5,6 +5,7 @@ require "download_strategy" require "checksum" require "version" require "mktemp" +require "livecheck" require "extend/on_os" # 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. + # + #
livecheck do
+  #   url "https://example.com/foo/releases"
+  #   regex /foo-(\d+(?:\.\d+)+)\.tar/
+  # end
+ def livecheck(&block) + @livecheck ||= Livecheck.new(self) + 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 From 7692098cd105ade885394e3bfd59fac168f892d4 Mon Sep 17 00:00:00 2001 From: Mohammad Zain Abbas Date: Thu, 30 Jun 2022 17:01:11 +0200 Subject: [PATCH 03/11] Minor fix: don't initialise livecheck unless needed --- Library/Homebrew/resource.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/resource.rb b/Library/Homebrew/resource.rb index 6ee5a77214..1170f2b4b3 100644 --- a/Library/Homebrew/resource.rb +++ b/Library/Homebrew/resource.rb @@ -183,7 +183,7 @@ class Resource # regex /foo-(\d+(?:\.\d+)+)\.tar/ # end def livecheck(&block) - @livecheck ||= Livecheck.new(self) + @livecheck ||= Livecheck.new(self) if block return @livecheck unless block @livecheckable = true From b3b17ee7bf21c06c4c443f213389c9c09dfe24f0 Mon Sep 17 00:00:00 2001 From: Mohammad Zain Abbas Date: Thu, 30 Jun 2022 17:06:32 +0200 Subject: [PATCH 04/11] Updated tests for livecheck block in resources --- Library/Homebrew/test/resource_spec.rb | 60 ++++++++++++-------------- 1 file changed, 27 insertions(+), 33 deletions(-) diff --git a/Library/Homebrew/test/resource_spec.rb b/Library/Homebrew/test/resource_spec.rb index 06117a9bff..a636e7a631 100644 --- a/Library/Homebrew/test/resource_spec.rb +++ b/Library/Homebrew/test/resource_spec.rb @@ -53,48 +53,42 @@ describe Resource do end end - describe Resource do + describe "#livecheck" do + it "returns nil if livecheck block is not set in resource" do + expect(resource.livecheck).to be_nil + end + let(:r) do - livecheck do - url "https://brew.sh/foo-1.0.tar.gz" - regex(/foo/) + Resource.new do + url "https://brew.sh/test-0.0.1.tgz" + livecheck do + url "https://brew.sh/foo-1.0.tar.gz" + regex(/foo/) + end end end - let(:livecheckable_r) { described_class.new(r) } + it "when livecheck block is set" do + expect(r.livecheck.url).to eq("https://brew.sh/foo-1.0.tar.gz") + expect(r.livecheck.regex).to eq(/foo/) + end + end - describe "#livecheck" do - it "returns nil if not set" do - expect(livecheckable_r.livecheck).to be_nil - end - - it "returns the string if set" do - livecheckable_r.livecheck("other-resource") - expect(livecheckable_r.livecheck).to eq("other-resource") - end - - it "raises a TypeError if the argument isn't a String" do - expect { - livecheckable_r.livecheck(123) - }.to raise_error(TypeError, "Resource#livecheck expects a String") - end + describe "#livecheckable?" do + it "returns false if livecheck block is not set in resource" do + expect(resource.livecheckable?).to be false end - describe "#regex" do - it "returns nil if not set" do - expect(livecheckable_r.regex).to be_nil + specify "livecheck block defined in resources" do + r = Resource.new do + url "https://brew.sh/test-1.0.tbz" + livecheck do + url "https://brew.sh/foo-1.0.tar.gz" + regex(/foo/) + end end - it "returns the Regexp if set" do - livecheckable_r.regex(/foo/) - expect(livecheckable_r.regex).to eq(/foo/) - end - - it "raises a TypeError if the argument isn't a Regexp" do - expect { - livecheckable_r.regex("foo") - }.to raise_error(TypeError, "Resource#livecheck#regex expects a Regexp") - end + expect(r.livecheckable?).to be true end end From ed37ee55a971079e0e52e9bdebb30df20dc0d40b Mon Sep 17 00:00:00 2001 From: Mohammad Zain Abbas Date: Thu, 30 Jun 2022 17:27:48 +0200 Subject: [PATCH 05/11] brew style fixes for `livecheck` tests in resource --- Library/Homebrew/test/resource_spec.rb | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/Library/Homebrew/test/resource_spec.rb b/Library/Homebrew/test/resource_spec.rb index a636e7a631..8694a4720e 100644 --- a/Library/Homebrew/test/resource_spec.rb +++ b/Library/Homebrew/test/resource_spec.rb @@ -58,17 +58,15 @@ describe Resource do expect(resource.livecheck).to be_nil end - let(:r) do - Resource.new do + specify "when livecheck block is set" do + r = described_class.new do url "https://brew.sh/test-0.0.1.tgz" livecheck do url "https://brew.sh/foo-1.0.tar.gz" regex(/foo/) end end - end - it "when livecheck block is set" do expect(r.livecheck.url).to eq("https://brew.sh/foo-1.0.tar.gz") expect(r.livecheck.regex).to eq(/foo/) end @@ -80,7 +78,7 @@ describe Resource do end specify "livecheck block defined in resources" do - r = Resource.new do + r = described_class.new do url "https://brew.sh/test-1.0.tbz" livecheck do url "https://brew.sh/foo-1.0.tar.gz" @@ -90,7 +88,6 @@ describe Resource do expect(r.livecheckable?).to be true end - end describe "#version" do From 8399c2638351484a047f538e5fbf5e6bf53dbef3 Mon Sep 17 00:00:00 2001 From: Mohammad Zain Abbas Date: Thu, 30 Jun 2022 17:46:53 +0200 Subject: [PATCH 06/11] Merged with master --- Library/Homebrew/resource.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/resource.rb b/Library/Homebrew/resource.rb index 1170f2b4b3..bb25d1fcb5 100644 --- a/Library/Homebrew/resource.rb +++ b/Library/Homebrew/resource.rb @@ -6,7 +6,7 @@ require "checksum" require "version" require "mktemp" require "livecheck" -require "extend/on_os" +require "extend/on_system" # Resource is the fundamental representation of an external resource. The # primary formula download, along with other declared resources, are instances From a874d833f969a66a35f4196c1e3d937b82c7b188 Mon Sep 17 00:00:00 2001 From: Mohammad Zain Abbas Date: Sat, 2 Jul 2022 13:01:07 +0500 Subject: [PATCH 07/11] Update Library/Homebrew/test/resource_spec.rb Co-authored-by: Sam Ford <1584702+samford@users.noreply.github.com> --- Library/Homebrew/test/resource_spec.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Library/Homebrew/test/resource_spec.rb b/Library/Homebrew/test/resource_spec.rb index 8694a4720e..e877ccac60 100644 --- a/Library/Homebrew/test/resource_spec.rb +++ b/Library/Homebrew/test/resource_spec.rb @@ -6,6 +6,18 @@ require "livecheck" describe Resource do subject(:resource) { described_class.new("test") } + + let(:livecheck_resource) { + described_class.new do + url "https://brew.sh/test-0.0.1.tgz" + sha256 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" + + livecheck do + url "https://brew.sh/foo-1.0.tar.gz" + regex(/foo/) + end + end + } describe "#url" do it "sets the URL" do From 090d71163b5ad51d7dfd6ef7c97b766fc3721877 Mon Sep 17 00:00:00 2001 From: Mohammad Zain Abbas Date: Sat, 2 Jul 2022 13:01:15 +0500 Subject: [PATCH 08/11] Update Library/Homebrew/test/resource_spec.rb Co-authored-by: Sam Ford <1584702+samford@users.noreply.github.com> --- Library/Homebrew/test/resource_spec.rb | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/Library/Homebrew/test/resource_spec.rb b/Library/Homebrew/test/resource_spec.rb index e877ccac60..d54c39f2f3 100644 --- a/Library/Homebrew/test/resource_spec.rb +++ b/Library/Homebrew/test/resource_spec.rb @@ -71,16 +71,8 @@ describe Resource do end specify "when livecheck block is set" do - r = described_class.new do - url "https://brew.sh/test-0.0.1.tgz" - livecheck do - url "https://brew.sh/foo-1.0.tar.gz" - regex(/foo/) - end - end - - expect(r.livecheck.url).to eq("https://brew.sh/foo-1.0.tar.gz") - expect(r.livecheck.regex).to eq(/foo/) + expect(livecheck_resource.livecheck.url).to eq("https://brew.sh/foo-1.0.tar.gz") + expect(livecheck_resource.livecheck.regex).to eq(/foo/) end end From 7ab79e54233e3604459c40b68e180fb5477d2be0 Mon Sep 17 00:00:00 2001 From: Mohammad Zain Abbas Date: Sat, 2 Jul 2022 13:01:30 +0500 Subject: [PATCH 09/11] Update Library/Homebrew/test/resource_spec.rb Co-authored-by: Sam Ford <1584702+samford@users.noreply.github.com> --- Library/Homebrew/test/resource_spec.rb | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/Library/Homebrew/test/resource_spec.rb b/Library/Homebrew/test/resource_spec.rb index d54c39f2f3..9758b376b9 100644 --- a/Library/Homebrew/test/resource_spec.rb +++ b/Library/Homebrew/test/resource_spec.rb @@ -82,15 +82,7 @@ describe Resource do end specify "livecheck block defined in resources" do - r = described_class.new do - url "https://brew.sh/test-1.0.tbz" - livecheck do - url "https://brew.sh/foo-1.0.tar.gz" - regex(/foo/) - end - end - - expect(r.livecheckable?).to be true + expect(livecheck_resource.livecheckable?).to be true end end From f4197fc96e0c320deada07fd1d0107e06c1d1cd8 Mon Sep 17 00:00:00 2001 From: Mohammad Zain Abbas Date: Sat, 2 Jul 2022 10:09:25 +0200 Subject: [PATCH 10/11] Minor documentation changes --- Library/Homebrew/livecheck.rb | 26 ++++++++++++++------------ Library/Homebrew/test/resource_spec.rb | 2 +- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/Library/Homebrew/livecheck.rb b/Library/Homebrew/livecheck.rb index 52a76e8425..66abd5142f 100644 --- a/Library/Homebrew/livecheck.rb +++ b/Library/Homebrew/livecheck.rb @@ -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. diff --git a/Library/Homebrew/test/resource_spec.rb b/Library/Homebrew/test/resource_spec.rb index 9758b376b9..ed34c294ed 100644 --- a/Library/Homebrew/test/resource_spec.rb +++ b/Library/Homebrew/test/resource_spec.rb @@ -6,7 +6,7 @@ require "livecheck" describe Resource do subject(:resource) { described_class.new("test") } - + let(:livecheck_resource) { described_class.new do url "https://brew.sh/test-0.0.1.tgz" From e69345b9421aa3e884c10d6b5213a6a10a8c301c Mon Sep 17 00:00:00 2001 From: Sam Ford <1584702+samford@users.noreply.github.com> Date: Mon, 4 Jul 2022 12:41:58 -0400 Subject: [PATCH 11/11] Update livecheck_resource test values --- Library/Homebrew/test/resource_spec.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Library/Homebrew/test/resource_spec.rb b/Library/Homebrew/test/resource_spec.rb index ed34c294ed..96541c3183 100644 --- a/Library/Homebrew/test/resource_spec.rb +++ b/Library/Homebrew/test/resource_spec.rb @@ -9,12 +9,12 @@ describe Resource do let(:livecheck_resource) { described_class.new do - url "https://brew.sh/test-0.0.1.tgz" + url "https://brew.sh/foo-1.0.tar.gz" sha256 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" livecheck do - url "https://brew.sh/foo-1.0.tar.gz" - regex(/foo/) + url "https://brew.sh/test/releases" + regex(/foo[._-]v?(\d+(?:\.\d+)+)\.t/i) end end } @@ -71,8 +71,8 @@ describe Resource do end specify "when livecheck block is set" do - expect(livecheck_resource.livecheck.url).to eq("https://brew.sh/foo-1.0.tar.gz") - expect(livecheck_resource.livecheck.regex).to eq(/foo/) + 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