From a06bd4e45df2a98a05e6752d7a662807176b3a77 Mon Sep 17 00:00:00 2001 From: Seeker Date: Fri, 22 Jan 2021 11:26:46 -0800 Subject: [PATCH] version/parser: add unit tests --- Library/Homebrew/test/version/parser_spec.rb | 81 ++++++++++++++++++++ Library/Homebrew/version/parser.rb | 10 +-- 2 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 Library/Homebrew/test/version/parser_spec.rb diff --git a/Library/Homebrew/test/version/parser_spec.rb b/Library/Homebrew/test/version/parser_spec.rb new file mode 100644 index 0000000000..fe2afde9bc --- /dev/null +++ b/Library/Homebrew/test/version/parser_spec.rb @@ -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 # 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 diff --git a/Library/Homebrew/version/parser.rb b/Library/Homebrew/version/parser.rb index b978292078..c5127dd99e 100644 --- a/Library/Homebrew/version/parser.rb +++ b/Library/Homebrew/version/parser.rb @@ -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