Add BottleCollector

The BottleCollector collects bottle tags and sha1s, and allows tags
to be fetched using more advanced logic than just fetching identical
tags.

Closes Homebrew/homebrew#23434.
This commit is contained in:
Misty De Meo 2013-10-21 21:03:14 -07:00
parent d6ab345be5
commit 079d0556ad
2 changed files with 79 additions and 0 deletions

View File

@ -96,3 +96,44 @@ def bottle_filename_formula_name filename
basename = path.basename.to_s
basename.rpartition("-#{version}").first
end
class BottleCollector
def initialize
@bottles = {}
end
def add(checksum, tag, url=nil)
@bottles[tag] = checksum
end
def fetch_bottle_for(tag)
return [@bottles[tag], tag] if @bottles[tag]
find_altivec_tag(tag) || find_or_later(tag)
end
# This allows generic Altivec PPC bottles to be supported in some
# formulae, while also allowing specific bottles in others; e.g.,
# sometimes a formula has just :tiger_altivec, other times it has
# :tiger_g4, :tiger_g5, etc.
def find_altivec_tag(tag)
if tag.to_s =~ /(\w+)_(g4|g4e|g5)$/
altitag = "#{$1}_altivec".to_sym
return [@bottles[altitag], altitag] if @bottles[altitag]
end
end
# Allows a bottle tag to specify a specific OS or later,
# so the same bottle can target multiple OSs.
# Not used in core, used in taps.
def find_or_later_tag(tag)
results = @bottles.find_all {|k,v| k.to_s =~ /_or_later$/}
results.each do |key, hsh|
later_tag = key.to_s[/(\w+)_or_later$/, 1].to_sym
bottle_version = MacOS::Version.from_symbol(later_tag)
return [hsh, key] if bottle_version <= MacOS::Version.from_symbol(tag)
end
nil
end
end

View File

@ -0,0 +1,38 @@
require 'testing_env'
require 'bottles'
class BottleCollectorTests < Test::Unit::TestCase
def setup
@collector = BottleCollector.new
end
def test_collector_returns_passed_tags
@collector.add('foo', :lion)
@collector.add('bar', :mountain_lion)
assert_equal ['bar', :mountain_lion], @collector.fetch_bottle_for(:mountain_lion)
end
def test_collector_returns_nil_on_no_matches
assert_nil @collector.fetch_bottle_for(:foo)
end
def test_collector_finds_or_later_tags
@collector.add('foo', :lion_or_later)
assert_equal ['foo', :lion_or_later], @collector.fetch_bottle_for(:mountain_lion)
assert_nil @collector.fetch_bottle_for(:snow_leopard)
end
def test_collector_prefers_exact_matches
@collector.add('foo', :lion_or_later)
@collector.add('bar', :mountain_lion)
assert_equal ['bar', :mountain_lion], @collector.fetch_bottle_for(:mountain_lion)
end
def test_collector_finds_altivec_tags
@collector.add('foo', :tiger_altivec)
assert_equal ['foo', :tiger_altivec], @collector.fetch_bottle_for(:tiger_g4)
assert_equal ['foo', :tiger_altivec], @collector.fetch_bottle_for(:tiger_g4e)
assert_equal ['foo', :tiger_altivec], @collector.fetch_bottle_for(:tiger_g5)
assert_nil @collector.fetch_bottle_for(:tiger_g3)
end
end