From 933499089cbba28ba37cc8671685c85d6cd9c6e0 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sun, 6 Dec 2020 20:25:40 +0100 Subject: [PATCH] Add test for `decide_between_versions`. --- .../dev-cmd/bump-unversioned-casks.rb | 29 +++++++++---------- .../dev-cmd/bump-unversioned-casks_spec.rb | 27 +++++++++++++++++ 2 files changed, 40 insertions(+), 16 deletions(-) create mode 100644 Library/Homebrew/test/dev-cmd/bump-unversioned-casks_spec.rb diff --git a/Library/Homebrew/dev-cmd/bump-unversioned-casks.rb b/Library/Homebrew/dev-cmd/bump-unversioned-casks.rb index 8d720e10bf..eea043e0aa 100644 --- a/Library/Homebrew/dev-cmd/bump-unversioned-casks.rb +++ b/Library/Homebrew/dev-cmd/bump-unversioned-casks.rb @@ -186,7 +186,7 @@ module Homebrew end info_plist_paths.each do |info_plist_path| - if (version = version_from_info_plist(cask, info_plist_path)) + if (version = version_from_info_plist(info_plist_path)) return version end end @@ -211,7 +211,7 @@ module Homebrew package_info_path = extract_dir/"PackageInfo" if package_info_path.exist? - if (version = version_from_package_info(cask, package_info_path)) + if (version = version_from_package_info(package_info_path)) return version end else @@ -229,34 +229,33 @@ module Homebrew end end - sig { params(cask: Cask::Cask, info_plist_path: Pathname).returns(T.nilable(String)) } - def self.version_from_info_plist(cask, info_plist_path) + sig { params(info_plist_path: Pathname).returns(T.nilable(String)) } + def self.version_from_info_plist(info_plist_path) plist = system_command!("plutil", args: ["-convert", "xml1", "-o", "-", info_plist_path]).plist short_version = plist["CFBundleShortVersionString"] version = plist["CFBundleVersion"] - return decide_between_versions(cask, short_version, version) if short_version && version + return decide_between_versions(short_version, version) if short_version && version end - sig { params(cask: Cask::Cask, package_info_path: Pathname).returns(T.nilable(String)) } - def self.version_from_package_info(cask, package_info_path) + sig { params(package_info_path: Pathname).returns(T.nilable(String)) } + def self.version_from_package_info(package_info_path) contents = package_info_path.read short_version = contents[/CFBundleShortVersionString="([^"]*)"/, 1] version = contents[/CFBundleVersion="([^"]*)"/, 1] - return decide_between_versions(cask, short_version, version) if short_version && version + return decide_between_versions(short_version, version) if short_version && version end sig do - params(cask: Cask::Cask, short_version: T.nilable(String), version: T.nilable(String)) + params(short_version: T.nilable(String), version: T.nilable(String)) .returns(T.nilable(String)) end - def self.decide_between_versions(cask, short_version, version) - return "#{short_version},#{version}" if short_version && version && cask.version.include?(",") + def self.decide_between_versions(short_version, version) - return cask.version.to_s if [short_version, version].include?(cask.version.to_s) + return short_version if short_version == version short_version_match = short_version&.match?(/\A\d+(\.\d+)+\Z/) version_match = version&.match?(/\A\d+(\.\d+)+\Z/) @@ -264,12 +263,10 @@ module Homebrew if short_version_match && version_match return version if version.length > short_version.length && version.start_with?(short_version) return short_version if short_version.length > version.length && short_version.start_with?(version) - elsif short_version_match - return short_version - elsif version_match - return version end + return "#{short_version},#{version}" if short_version&.match?(/\A\d+(\.\d+)*\Z/) && version&.match?(/\A\d+\Z/) + short_version || version end diff --git a/Library/Homebrew/test/dev-cmd/bump-unversioned-casks_spec.rb b/Library/Homebrew/test/dev-cmd/bump-unversioned-casks_spec.rb new file mode 100644 index 0000000000..bd6d76dd79 --- /dev/null +++ b/Library/Homebrew/test/dev-cmd/bump-unversioned-casks_spec.rb @@ -0,0 +1,27 @@ +# typed: false +# frozen_string_literal: true + +require "cmd/shared_examples/args_parse" +require "dev-cmd/bump-unversioned-casks" + +describe "Homebrew.bump_unversioned_casks_args" do + it_behaves_like "parseable arguments" + + describe "::decide_between_versions" do + expected_mappings = { + [nil, nil] => nil, + ["1.2", nil] => "1.2", + [nil, "1.2.3"] => "1.2.3", + ["1.2", "1.2.3"] => "1.2.3", + ["1.2.3", "1.2"] => "1.2.3", + ["1.2.3", "8312"] => "1.2.3,8312", + ["2021", "2006"] => "2021,2006", + } + + expected_mappings.each do |(short_version, version), expected_version| + it "maps (#{short_version}, #{version}) to #{expected_version}" do + expect(Homebrew.decide_between_versions(short_version, version)).to eq expected_version + end + end + end +end