PageMatch: Expand #find_versions tests
This commit is contained in:
		
							parent
							
								
									e7fe728673
								
							
						
					
					
						commit
						e65c9d02ab
					
				@ -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?)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user