
Valid `strategy` block return types currently vary between strategies. Some only accept a string whereas others accept a string or array of strings. [`strategy` blocks also accept a `nil` return (to simplify early returns) but this was already standardized across strategies.] While some strategies only identify one version by default (where a string is an appropriate return type), it could be that a strategy block identifies more than one version. In this situation, the strategy would need to be modified to accept (and work with) an array from a `strategy` block. Rather than waiting for this to become a problem, this modifies all strategies to standardize on allowing `strategy` blocks to return a string or array of strings (even if only one of these is currently used in practice). Standardizing valid return types helps to further simplify the mental model for `strategy` blocks and reduce cognitive load. This commit extracts related logic from `#find_versions` into methods like `#versions_from_content`, which is conceptually similar to `PageMatch#page_matches` (renamed to `#versions_from_content` for consistency). This allows us to write tests for the related code without having to make network requests (or stub them) at this point. In general, this also helps to better align the structure of strategies and how the various `#find_versions` methods work with versions. There's still more planned work to be done here but this is a step in the right direction.
93 lines
3.0 KiB
Ruby
93 lines
3.0 KiB
Ruby
# typed: false
|
|
# frozen_string_literal: true
|
|
|
|
require "livecheck/strategy"
|
|
|
|
describe Homebrew::Livecheck::Strategy::Git do
|
|
subject(:git) { described_class }
|
|
|
|
let(:git_url) { "https://github.com/Homebrew/brew.git" }
|
|
let(:non_git_url) { "https://brew.sh/test" }
|
|
|
|
let(:tags) {
|
|
{
|
|
normal: ["brew/1.2", "brew/1.2.1", "brew/1.2.2", "brew/1.2.3", "brew/1.2.4", "1.2.5"],
|
|
hyphens: ["brew/1-2", "brew/1-2-1", "brew/1-2-2", "brew/1-2-3", "brew/1-2-4", "1-2-5"],
|
|
}
|
|
}
|
|
|
|
let(:regexes) {
|
|
{
|
|
standard: /^v?(\d+(?:\.\d+)+)$/i,
|
|
hyphens: /^v?(\d+(?:[.-]\d+)+)$/i,
|
|
brew: %r{^brew/v?(\d+(?:\.\d+)+)$}i,
|
|
}
|
|
}
|
|
|
|
let(:versions) {
|
|
{
|
|
default: ["1.2", "1.2.1", "1.2.2", "1.2.3", "1.2.4", "1.2.5"],
|
|
standard_regex: ["1.2.5"],
|
|
brew_regex: ["1.2", "1.2.1", "1.2.2", "1.2.3", "1.2.4"],
|
|
}
|
|
}
|
|
|
|
describe "::tag_info", :needs_network do
|
|
it "returns the Git tags for the provided remote URL that match the regex provided" do
|
|
expect(git.tag_info(git_url, regexes[:standard])).not_to be_empty
|
|
end
|
|
end
|
|
|
|
describe "::match?" do
|
|
it "returns true if the argument provided is a Git repository" do
|
|
expect(git.match?(git_url)).to be true
|
|
end
|
|
|
|
it "returns false if the argument provided is not a Git repository" do
|
|
expect(git.match?(non_git_url)).to be false
|
|
end
|
|
end
|
|
|
|
describe "::versions_from_tags" do
|
|
it "returns an empty array if tags array is empty" do
|
|
expect(git.versions_from_tags([])).to eq([])
|
|
end
|
|
|
|
it "returns an array of version strings when given tags" do
|
|
expect(git.versions_from_tags(tags[:normal])).to eq(versions[:default])
|
|
expect(git.versions_from_tags(tags[:normal], regexes[:standard])).to eq(versions[:standard_regex])
|
|
expect(git.versions_from_tags(tags[:normal], regexes[:brew])).to eq(versions[:brew_regex])
|
|
end
|
|
|
|
it "returns an array of version strings when given tags and a block" do
|
|
# Returning a string from block, default strategy regex
|
|
expect(git.versions_from_tags(tags[:normal]) { versions[:default].first }).to eq([versions[:default].first])
|
|
|
|
# Returning an array of strings from block, default strategy regex
|
|
expect(
|
|
git.versions_from_tags(tags[:hyphens]) do |tags, regex|
|
|
tags.map { |tag| tag[regex, 1]&.tr("-", ".") }
|
|
end,
|
|
).to eq(versions[:default])
|
|
|
|
# Returning an array of strings from block, explicit regex
|
|
expect(
|
|
git.versions_from_tags(tags[:hyphens], regexes[:hyphens]) do |tags, regex|
|
|
tags.map { |tag| tag[regex, 1]&.tr("-", ".") }
|
|
end,
|
|
).to eq(versions[:standard_regex])
|
|
|
|
expect(git.versions_from_tags(tags[:hyphens]) { "1.2.3" }).to eq(["1.2.3"])
|
|
end
|
|
|
|
it "allows a nil return from a block" do
|
|
expect(git.versions_from_tags(tags[:normal]) { next }).to eq([])
|
|
end
|
|
|
|
it "errors on an invalid return type from a block" do
|
|
expect { git.versions_from_tags(tags[:normal]) { 123 } }
|
|
.to raise_error(TypeError, Homebrew::Livecheck::Strategy::INVALID_BLOCK_RETURN_VALUE_MSG)
|
|
end
|
|
end
|
|
end
|