Add test for decide_between_versions.

This commit is contained in:
Markus Reiter 2020-12-06 20:25:40 +01:00
parent 6d21df2c8b
commit 933499089c
2 changed files with 40 additions and 16 deletions

View File

@ -186,7 +186,7 @@ module Homebrew
end end
info_plist_paths.each do |info_plist_path| 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 return version
end end
end end
@ -211,7 +211,7 @@ module Homebrew
package_info_path = extract_dir/"PackageInfo" package_info_path = extract_dir/"PackageInfo"
if package_info_path.exist? 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 return version
end end
else else
@ -229,34 +229,33 @@ module Homebrew
end end
end end
sig { params(cask: Cask::Cask, info_plist_path: Pathname).returns(T.nilable(String)) } sig { params(info_plist_path: Pathname).returns(T.nilable(String)) }
def self.version_from_info_plist(cask, info_plist_path) def self.version_from_info_plist(info_plist_path)
plist = system_command!("plutil", args: ["-convert", "xml1", "-o", "-", info_plist_path]).plist plist = system_command!("plutil", args: ["-convert", "xml1", "-o", "-", info_plist_path]).plist
short_version = plist["CFBundleShortVersionString"] short_version = plist["CFBundleShortVersionString"]
version = plist["CFBundleVersion"] 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 end
sig { params(cask: Cask::Cask, package_info_path: Pathname).returns(T.nilable(String)) } sig { params(package_info_path: Pathname).returns(T.nilable(String)) }
def self.version_from_package_info(cask, package_info_path) def self.version_from_package_info(package_info_path)
contents = package_info_path.read contents = package_info_path.read
short_version = contents[/CFBundleShortVersionString="([^"]*)"/, 1] short_version = contents[/CFBundleShortVersionString="([^"]*)"/, 1]
version = contents[/CFBundleVersion="([^"]*)"/, 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 end
sig do 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)) .returns(T.nilable(String))
end end
def self.decide_between_versions(cask, short_version, version) def self.decide_between_versions(short_version, version)
return "#{short_version},#{version}" if short_version && version && cask.version.include?(",")
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/) short_version_match = short_version&.match?(/\A\d+(\.\d+)+\Z/)
version_match = 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 if short_version_match && version_match
return version if version.length > short_version.length && version.start_with?(short_version) 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) 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 end
return "#{short_version},#{version}" if short_version&.match?(/\A\d+(\.\d+)*\Z/) && version&.match?(/\A\d+\Z/)
short_version || version short_version || version
end end

View File

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