version/parser: add unit tests
This commit is contained in:
parent
568fc86676
commit
a06bd4e45d
81
Library/Homebrew/test/version/parser_spec.rb
Normal file
81
Library/Homebrew/test/version/parser_spec.rb
Normal file
@ -0,0 +1,81 @@
|
||||
# typed: false
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "version/parser"
|
||||
|
||||
describe Version::Parser do
|
||||
specify "::new" do
|
||||
expect { described_class.new }
|
||||
.to raise_error("Version::Parser is declared as abstract; it cannot be instantiated")
|
||||
end
|
||||
|
||||
describe Version::RegexParser do
|
||||
specify "::new" do
|
||||
# TODO: see https://github.com/sorbet/sorbet/issues/2374
|
||||
# expect { described_class.new(/[._-](\d+(?:\.\d+)+)/) }
|
||||
# .to raise_error("Version::RegexParser is declared as abstract; it cannot be instantiated")
|
||||
expect { described_class.new(/[._-](\d+(?:\.\d+)+)/) }.not_to raise_error
|
||||
end
|
||||
|
||||
specify "::process_spec" do
|
||||
expect { described_class.process_spec(Pathname(TEST_TMPDIR)) }
|
||||
.to raise_error("The method `process_spec` on #<Class:Version::RegexParser> is declared as `abstract`. " \
|
||||
"It does not have an implementation.")
|
||||
end
|
||||
end
|
||||
|
||||
describe Version::UrlParser do
|
||||
specify "::new" do
|
||||
expect { described_class.new(/[._-](\d+(?:\.\d+)+)/) }.not_to raise_error
|
||||
end
|
||||
|
||||
specify "::process_spec" do
|
||||
expect(described_class.process_spec(Pathname("#{TEST_TMPDIR}/testdir-0.1.test")))
|
||||
.to eq("#{TEST_TMPDIR}/testdir-0.1.test")
|
||||
|
||||
expect(described_class.process_spec(Pathname("https://sourceforge.net/foo_bar-1.21.tar.gz/download")))
|
||||
.to eq("https://sourceforge.net/foo_bar-1.21.tar.gz/download")
|
||||
|
||||
expect(described_class.process_spec(Pathname("https://sf.net/foo_bar-1.21.tar.gz/download")))
|
||||
.to eq("https://sf.net/foo_bar-1.21.tar.gz/download")
|
||||
|
||||
expect(described_class.process_spec(Pathname("https://brew.sh/testball-0.1")))
|
||||
.to eq("https://brew.sh/testball-0.1")
|
||||
|
||||
expect(described_class.process_spec(Pathname("https://brew.sh/testball-0.1.tgz")))
|
||||
.to eq("https://brew.sh/testball-0.1.tgz")
|
||||
end
|
||||
end
|
||||
|
||||
describe Version::StemParser do
|
||||
before { Pathname("#{TEST_TMPDIR}/testdir-0.1.test").mkpath }
|
||||
|
||||
after { Pathname("#{TEST_TMPDIR}/testdir-0.1.test").unlink }
|
||||
|
||||
specify "::new" do
|
||||
expect { described_class.new(/[._-](\d+(?:\.\d+)+)/) }.not_to raise_error
|
||||
end
|
||||
|
||||
describe "::process_spec" do
|
||||
it "works with directories" do
|
||||
expect(described_class.process_spec(Pathname("#{TEST_TMPDIR}/testdir-0.1.test"))).to eq("testdir-0.1.test")
|
||||
end
|
||||
|
||||
it "works with SourceForge URLs with /download suffix" do
|
||||
expect(described_class.process_spec(Pathname("https://sourceforge.net/foo_bar-1.21.tar.gz/download")))
|
||||
.to eq("foo_bar-1.21")
|
||||
|
||||
expect(described_class.process_spec(Pathname("https://sf.net/foo_bar-1.21.tar.gz/download")))
|
||||
.to eq("foo_bar-1.21")
|
||||
end
|
||||
|
||||
it "works with URLs without file extension" do
|
||||
expect(described_class.process_spec(Pathname("https://brew.sh/testball-0.1"))).to eq("testball-0.1")
|
||||
end
|
||||
|
||||
it "works with URLs with file extension" do
|
||||
expect(described_class.process_spec(Pathname("https://brew.sh/testball-0.1.tgz"))).to eq("testball-0.1")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -27,7 +27,7 @@ class Version
|
||||
|
||||
sig { override.params(spec: Pathname).returns(T.nilable(String)) }
|
||||
def parse(spec)
|
||||
match = @regex.match(process_spec(spec))
|
||||
match = @regex.match(self.class.process_spec(spec))
|
||||
return if match.blank?
|
||||
|
||||
version = match.captures.first
|
||||
@ -38,7 +38,7 @@ class Version
|
||||
end
|
||||
|
||||
sig { abstract.params(spec: Pathname).returns(String) }
|
||||
def process_spec(spec); end
|
||||
def self.process_spec(spec); end
|
||||
end
|
||||
|
||||
# @api private
|
||||
@ -46,7 +46,7 @@ class Version
|
||||
extend T::Sig
|
||||
|
||||
sig { override.params(spec: Pathname).returns(String) }
|
||||
def process_spec(spec)
|
||||
def self.process_spec(spec)
|
||||
spec.to_s
|
||||
end
|
||||
end
|
||||
@ -55,11 +55,11 @@ class Version
|
||||
class StemParser < RegexParser
|
||||
extend T::Sig
|
||||
|
||||
SOURCEFORGE_DOWNLOAD_REGEX = %r{((?:sourceforge\.net|sf\.net)/.*)/download$}.freeze
|
||||
SOURCEFORGE_DOWNLOAD_REGEX = %r{(?:sourceforge\.net|sf\.net)/.*/download$}.freeze
|
||||
NO_FILE_EXTENSION_REGEX = /\.[^a-zA-Z]+$/.freeze
|
||||
|
||||
sig { override.params(spec: Pathname).returns(String) }
|
||||
def process_spec(spec)
|
||||
def self.process_spec(spec)
|
||||
return spec.basename.to_s if spec.directory?
|
||||
|
||||
spec_s = spec.to_s
|
||||
|
Loading…
x
Reference in New Issue
Block a user