Merge pull request #11889 from samford/livecheck/PageMatch-expand-find_versions-tests

PageMatch: Expand #find_versions tests
This commit is contained in:
Sam Ford 2021-08-19 16:27:56 -04:00 committed by GitHub
commit 30e081baad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 7 deletions

View File

@ -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?)

View File

@ -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