From ac4854ef9e525d3b5b080e302d10f1b71b7b54f6 Mon Sep 17 00:00:00 2001 From: Sam Ford <1584702+samford@users.noreply.github.com> Date: Sat, 7 Dec 2024 20:42:46 -0500 Subject: [PATCH] Pypi: Expand test coverage Among other things, the previous commit added a `provided_content` paramter to `Pypi::find_versions`, so this takes advantage of that to expand `Pypi` test coverage to 100%. --- .../test/livecheck/strategy/pypi_spec.rb | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/Library/Homebrew/test/livecheck/strategy/pypi_spec.rb b/Library/Homebrew/test/livecheck/strategy/pypi_spec.rb index 2ee5aa35e8..be93644477 100644 --- a/Library/Homebrew/test/livecheck/strategy/pypi_spec.rb +++ b/Library/Homebrew/test/livecheck/strategy/pypi_spec.rb @@ -8,12 +8,42 @@ RSpec.describe Homebrew::Livecheck::Strategy::Pypi do let(:pypi_url) { "https://files.pythonhosted.org/packages/ab/cd/efg/example-package-1.2.3.tar.gz" } let(:non_pypi_url) { "https://brew.sh/test" } + let(:regex) { /^v?(\d+(?:\.\d+)+)$/i } + let(:generated) do { url: "https://pypi.org/pypi/example-package/json", } end + # This is a limited subset of a PyPI JSON API response object, for the sake + # of testing. + let(:content) do + <<~JSON + { + "info": { + "version": "1.2.3" + } + } + JSON + end + + let(:matches) { ["1.2.3"] } + + let(:find_versions_return_hash) do + { + matches: { + "1.2.3" => Version.new("1.2.3"), + }, + regex: nil, + url: generated[:url], + } + end + + let(:find_versions_cached_return_hash) do + find_versions_return_hash.merge({ cached: true }) + end + describe "::match?" do it "returns true for a PyPI URL" do expect(pypi.match?(pypi_url)).to be true @@ -33,4 +63,59 @@ RSpec.describe Homebrew::Livecheck::Strategy::Pypi do expect(pypi.generate_input_values(non_pypi_url)).to eq({}) end end + + describe "::find_versions" do + let(:match_data) do + cached = { + matches: matches.to_h { |v| [v, Version.new(v)] }, + regex: nil, + url: generated[:url], + cached: true, + } + + { + cached:, + cached_default: cached.merge({ matches: {} }), + } + end + + it "finds versions in provided content" do + expect(pypi.find_versions(url: pypi_url, provided_content: content)) + .to eq(match_data[:cached]) + end + + it "finds versions in provided content using a block" do + # NOTE: We only use a regex here to make sure it can be passed into the + # block, if necessary. + expect(pypi.find_versions(url: pypi_url, regex:, provided_content: content) do |json, regex| + match = json.dig("info", "version")&.match(regex) + next if match.blank? + + match[1] + end).to eq(match_data[:cached].merge({ regex: })) + + expect(pypi.find_versions(url: pypi_url, provided_content: content) do |json| + json.dig("info", "version").presence + end).to eq(match_data[:cached]) + end + + it "returns default match_data when block doesn't return version information" do + expect(pypi.find_versions(url: pypi_url, provided_content: '{"info":{"version":""}}')) + .to eq(match_data[:cached_default]) + expect(pypi.find_versions(url: pypi_url, provided_content: '{"other":true}')) + .to eq(match_data[:cached_default]) + end + + it "returns default match_data when url is blank" do + expect(pypi.find_versions(url: "") { "1.2.3" }) + .to eq({ matches: {}, regex: nil, url: "" }) + end + + it "returns default match_data when content is blank" do + expect(pypi.find_versions(url: pypi_url, provided_content: "{}") { "1.2.3" }) + .to eq(match_data[:cached_default]) + expect(pypi.find_versions(url: pypi_url, provided_content: "") { "1.2.3" }) + .to eq(match_data[:cached_default]) + end + end end