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%.
This commit is contained in:
Sam Ford 2024-12-07 20:42:46 -05:00
parent 935eb89eca
commit ac4854ef9e
No known key found for this signature in database
GPG Key ID: 7AF5CBEE1DD6F76D

View File

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