From e65c9d02abb5a0566f416e665c5f1503a7d9dc5e Mon Sep 17 00:00:00 2001 From: Sam Ford <1584702+samford@users.noreply.github.com> Date: Thu, 19 Aug 2021 10:20:11 -0400 Subject: [PATCH] PageMatch: Expand #find_versions tests --- .../Homebrew/livecheck/strategy/page_match.rb | 15 ++++++++------- .../test/livecheck/strategy/page_match_spec.rb | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/Library/Homebrew/livecheck/strategy/page_match.rb b/Library/Homebrew/livecheck/strategy/page_match.rb index 1ae9aba489..de5094821f 100644 --- a/Library/Homebrew/livecheck/strategy/page_match.rb +++ b/Library/Homebrew/livecheck/strategy/page_match.rb @@ -44,20 +44,21 @@ module Homebrew # With either approach, an array of unique matches is returned. # # @param content [String] the page content to check - # @param regex [Regexp] a regex used for matching versions in the + # @param regex [Regexp, nil] a regex used for matching versions in the # content # @return [Array] sig { params( content: String, - regex: Regexp, + regex: T.nilable(Regexp), block: T.nilable( - T.proc.params(arg0: String, arg1: Regexp).returns(T.any(String, T::Array[String], NilClass)), + T.proc.params(arg0: String, arg1: T.nilable(Regexp)).returns(T.any(String, T::Array[String], NilClass)), ), ).returns(T::Array[String]) } def self.versions_from_content(content, regex, &block) return Strategy.handle_block_return(block.call(content, regex)) if block + return [] if regex.blank? content.scan(regex).map do |match| case match @@ -73,22 +74,22 @@ module Homebrew # regex for matching. # # @param url [String] the URL of the content to check - # @param regex [Regexp] a regex used for matching versions + # @param regex [Regexp, nil] a regex used for matching versions # @param provided_content [String, nil] page content to use in place of # fetching via Strategy#page_content # @return [Hash] sig { params( url: String, - regex: Regexp, + regex: T.nilable(Regexp), provided_content: T.nilable(String), _unused: T.nilable(T::Hash[Symbol, T.untyped]), block: T.nilable( - T.proc.params(arg0: String, arg1: Regexp).returns(T.any(String, T::Array[String], NilClass)), + T.proc.params(arg0: String, arg1: T.nilable(Regexp)).returns(T.any(String, T::Array[String], NilClass)), ), ).returns(T::Hash[Symbol, T.untyped]) } - def self.find_versions(url:, regex:, provided_content: nil, **_unused, &block) + def self.find_versions(url:, regex: nil, provided_content: nil, **_unused, &block) match_data = { matches: {}, regex: regex, url: url } return match_data if url.blank? || (regex.blank? && block.blank?) diff --git a/Library/Homebrew/test/livecheck/strategy/page_match_spec.rb b/Library/Homebrew/test/livecheck/strategy/page_match_spec.rb index 42154f10b7..5a933de23b 100644 --- a/Library/Homebrew/test/livecheck/strategy/page_match_spec.rb +++ b/Library/Homebrew/test/livecheck/strategy/page_match_spec.rb @@ -108,6 +108,23 @@ describe Homebrew::Livecheck::Strategy::PageMatch do it "finds versions in provided_content" do expect(page_match.find_versions(url: http_url, regex: regex, provided_content: content)) .to eq(find_versions_cached_return_hash) + + # NOTE: Ideally, a regex should always be provided to `#find_versions` + # for `PageMatch` but there are currently some `livecheck` blocks in + # casks where `#regex` isn't used and the regex only exists within a + # `strategy` block. This isn't ideal but, for the moment, we allow a + # `strategy` block to act as a substitution for a regex and we need to + # test this scenario to ensure it works. + # + # Under normal circumstances, a regex should be established in a + # `livecheck` block using `#regex` and passed into the `strategy` block + # using `do |page, regex|`. Hopefully over time we can address related + # issues and get to a point where regexes are always established using + # `#regex`. + expect(page_match.find_versions(url: http_url, provided_content: content) do |page| + page.scan(%r{href=.*?/homebrew[._-]v?(\d+(?:\.\d+)+)/?["' >]}i).map(&:first) + end) + .to eq(find_versions_cached_return_hash.merge({ regex: nil })) end end end