Raise when given an invalid download strategy spec

When DownloadStrategyDetector.detect is given a second argument, and
that argument is not a symbol or an AbstractDownloadStrategy subclass,
it is silently ignored, and we fall back to guessing the strategy based
on the URL.

This means I can do

  url 'http://foo.com/bar.tar.gz', :using => Class.new

and things will appear to work, even though I have clearly passed an
invalid value for :using.

A more useful behavior is to raise an exception for unknown strategy
specifications.
This commit is contained in:
Jack Nagel 2013-09-26 16:59:45 -05:00
parent 1fb9f85c5f
commit d1ce5bafc9
2 changed files with 13 additions and 4 deletions

View File

@ -761,12 +761,15 @@ end
class DownloadStrategyDetector class DownloadStrategyDetector
def self.detect(url, strategy=nil) def self.detect(url, strategy=nil)
if strategy.is_a? Class and strategy.ancestors.include? AbstractDownloadStrategy if strategy.nil?
strategy detect_from_url(url)
elsif strategy.is_a? Symbol elsif Class === strategy && strategy < AbstractDownloadStrategy
strategy
elsif Symbol === strategy
detect_from_symbol(strategy) detect_from_symbol(strategy)
else else
detect_from_url(url) raise TypeError,
"Unknown download strategy specification #{strategy.inspect}"
end end
end end

View File

@ -51,4 +51,10 @@ class DownloadStrategyDetectorTests < Test::Unit::TestCase
@d = DownloadStrategyDetector.detect(Object.new) @d = DownloadStrategyDetector.detect(Object.new)
assert_equal CurlDownloadStrategy, @d assert_equal CurlDownloadStrategy, @d
end end
def test_raises_when_passed_unrecognized_strategy
assert_raises(TypeError) do
DownloadStrategyDetector.detect("foo", Class.new)
end
end
end end