From 5c9f5b95064bd25f959f42a3cefc85d186cb453d Mon Sep 17 00:00:00 2001 From: Sam Ford <1584702+samford@users.noreply.github.com> Date: Wed, 23 Dec 2020 09:12:53 -0500 Subject: [PATCH] Xorg: Use PageMatch#find_versions --- .../Homebrew/livecheck/strategy/page_match.rb | 25 ++++++++++++++++--- Library/Homebrew/livecheck/strategy/xorg.rb | 17 +++++-------- .../livecheck/strategy/page_match_spec.rb | 23 +++++++++++++++++ 3 files changed, 50 insertions(+), 15 deletions(-) diff --git a/Library/Homebrew/livecheck/strategy/page_match.rb b/Library/Homebrew/livecheck/strategy/page_match.rb index ac910a7ff8..0d611936f7 100644 --- a/Library/Homebrew/livecheck/strategy/page_match.rb +++ b/Library/Homebrew/livecheck/strategy/page_match.rb @@ -71,12 +71,29 @@ module Homebrew # Checks the content at the URL for new versions, using the provided # regex for matching. - sig { params(url: String, regex: T.nilable(Regexp)).returns(T::Hash[Symbol, T.untyped]) } - def self.find_versions(url, regex, &block) + # + # @param url [String] the URL of the content to check + # @param regex [Regexp] a regex used for matching versions in content + # @param provided_content [String] page content to use in place of + # fetching via Strategy#page_content + # @return [Hash] + sig do + params( + url: String, + regex: T.nilable(Regexp), + provided_content: T.nilable(String), + block: T.nilable(T.proc.params(arg0: String).returns(T.any(T::Array[String], String))), + ).returns(T::Hash[Symbol, T.untyped]) + end + def self.find_versions(url, regex, provided_content = nil, &block) match_data = { matches: {}, regex: regex, url: url } - match_data.merge!(Strategy.page_content(url)) - content = match_data.delete(:content) + content = if provided_content.present? + provided_content + else + match_data.merge!(Strategy.page_content(url)) + match_data.delete(:content) + end return match_data if content.blank? page_matches(content, regex, &block).each do |match_text| diff --git a/Library/Homebrew/livecheck/strategy/xorg.rb b/Library/Homebrew/livecheck/strategy/xorg.rb index 18417207b0..abe9a36e1d 100644 --- a/Library/Homebrew/livecheck/strategy/xorg.rb +++ b/Library/Homebrew/livecheck/strategy/xorg.rb @@ -1,8 +1,6 @@ # typed: false # frozen_string_literal: true -require "open-uri" - module Homebrew module Livecheck module Strategy @@ -74,7 +72,6 @@ module Homebrew # @return [Hash] def self.find_versions(url, regex) file_name = File.basename(url) - /^(?.+)-\d+/i =~ file_name # /pub/ URLs redirect to the same URL with /archive/, so we replace @@ -82,17 +79,15 @@ module Homebrew # the URL gives us the relevant directory listing page. page_url = url.sub("x.org/pub/", "x.org/archive/").delete_suffix(file_name) + # Example regex: /href=.*?example[._-]v?(\d+(?:\.\d+)+)\.t/i regex ||= /href=.*?#{Regexp.escape(module_name)}[._-]v?(\d+(?:\.\d+)+)\.t/i - match_data = { matches: {}, regex: regex, url: page_url } + # Use the cached page content to avoid duplicate fetches + cached_content = @page_data[page_url] + match_data = Homebrew::Livecheck::Strategy::PageMatch.find_versions(page_url, regex, cached_content) - # Cache responses to avoid unnecessary duplicate fetches - @page_data[page_url] = URI.parse(page_url).open.read unless @page_data.key?(page_url) - - matches = @page_data[page_url].scan(regex) - matches.map(&:first).uniq.each do |match| - match_data[:matches][match] = Version.new(match) - end + # Cache any new page content + @page_data[page_url] = match_data[:content] if match_data[:content].present? match_data end diff --git a/Library/Homebrew/test/livecheck/strategy/page_match_spec.rb b/Library/Homebrew/test/livecheck/strategy/page_match_spec.rb index 4f853fee17..4c364f1d8a 100644 --- a/Library/Homebrew/test/livecheck/strategy/page_match_spec.rb +++ b/Library/Homebrew/test/livecheck/strategy/page_match_spec.rb @@ -34,7 +34,24 @@ describe Homebrew::Livecheck::Strategy::PageMatch do EOS } + let(:page_content_matches) { ["2.6.0", "2.5.0", "2.4.0", "2.3.0", "2.2.0", "2.1.0", "2.0.0", "1.9.0"] } + let(:find_versions_return_hash) { + { + matches: { + "2.6.0" => Version.new("2.6.0"), + "2.5.0" => Version.new("2.5.0"), + "2.4.0" => Version.new("2.4.0"), + "2.3.0" => Version.new("2.3.0"), + "2.2.0" => Version.new("2.2.0"), + "2.1.0" => Version.new("2.1.0"), + "2.0.0" => Version.new("2.0.0"), + "1.9.0" => Version.new("1.9.0"), + }, + regex: regex, + url: url, + } + } describe "::match?" do it "returns true for any URL" do @@ -52,4 +69,10 @@ describe Homebrew::Livecheck::Strategy::PageMatch do .to eq(page_content_matches) end end + + describe "::find_versions?" do + it "finds versions in provided_content" do + expect(page_match.find_versions(url, regex, page_content)).to eq(find_versions_return_hash) + end + end end