brew/Library/Homebrew/test/test_download_strategies.rb
Jack Nagel 57560c03e6 Handle invalid names in download strategies
When subformulae are initialized without a name parameter, Homebrew
assigns the name "__UNKNOWN__". This may cause collisions in the cache.

Currently CurlDownloadStrategy and its descendants handles this by
extracting the basename form the URL and using that as the cached
filename. However, other strategies simply raise an exception.

We can improve the other strategies by URL-encoding the URL string and
using that as the cached directory name.

Note that this happens very rarely, especially now that resources (which
always have a name) are preferred to subformulae. The most common case
is a subformula that specifies a head download.

Closes Homebrew/homebrew#22949.
2013-09-30 22:56:02 -05:00

87 lines
2.3 KiB
Ruby

require 'testing_env'
require 'download_strategy'
require 'bottles' # XXX: hoist these regexps into constants in Pathname?
class ResourceDouble
attr_reader :url, :specs
def initialize(url="http://foo.com/bar.tar.gz", specs={})
@url = url
@specs = specs
end
end
class AbstractDownloadStrategyTests < Test::Unit::TestCase
def setup
@name = "foo"
@resource = ResourceDouble.new
@strategy = AbstractDownloadStrategy.new(@name, @resource)
@args = %w{foo bar baz}
end
def test_expand_safe_system_args_with_explicit_quiet_flag
@args << { :quiet_flag => '--flag' }
expanded_args = @strategy.expand_safe_system_args(@args)
assert_equal %w{foo bar baz --flag}, expanded_args
end
def test_expand_safe_system_args_with_implicit_quiet_flag
expanded_args = @strategy.expand_safe_system_args(@args)
assert_equal %w{foo bar -q baz}, expanded_args
end
def test_expand_safe_system_args_does_not_mutate_argument
result = @strategy.expand_safe_system_args(@args)
assert_equal %w{foo bar baz}, @args
assert_not_same @args, result
end
end
class DownloadStrategyCheckoutNameTests < Test::Unit::TestCase
def setup
@resource = ResourceDouble.new("http://foo.com/bar")
@strategy = AbstractDownloadStrategy
end
def escaped(tag)
"#{ERB::Util.url_encode(@resource.url)}--#{tag}"
end
def test_explicit_name
downloader = @strategy.new("baz", @resource)
assert_equal "baz--foo", downloader.checkout_name("foo")
end
def test_empty_name
downloader = @strategy.new("", @resource)
assert_equal escaped("foo"), downloader.checkout_name("foo")
end
def test_unknown_name
downloader = @strategy.new("__UNKNOWN__", @resource)
assert_equal escaped("foo"), downloader.checkout_name("foo")
end
end
class DownloadStrategyDetectorTests < Test::Unit::TestCase
def setup
@d = DownloadStrategyDetector.new
end
def test_detect_git_download_startegy
@d = DownloadStrategyDetector.detect("git://foo.com/bar.git")
assert_equal GitDownloadStrategy, @d
end
def test_default_to_curl_strategy
@d = DownloadStrategyDetector.detect(Object.new)
assert_equal CurlDownloadStrategy, @d
end
def test_raises_when_passed_unrecognized_strategy
assert_raises(TypeError) do
DownloadStrategyDetector.detect("foo", Class.new)
end
end
end