From 39ec66614b183d0784d8a77e93831f04277e07d2 Mon Sep 17 00:00:00 2001 From: Jack Nagel Date: Mon, 15 Oct 2012 01:19:31 -0500 Subject: [PATCH] Test coverage for DownloadStrategyDetector While at it, make it use class methods instead; no reason to instantiate an object for this. Eventually there should be some functional tests for the individual strategies as well. Signed-off-by: Jack Nagel --- Library/Homebrew/download_strategy.rb | 27 +++++++++---------- Library/Homebrew/formula_support.rb | 2 +- .../Homebrew/test/test_download_strategies.rb | 19 +++++++++++++ 3 files changed, 32 insertions(+), 16 deletions(-) create mode 100644 Library/Homebrew/test/test_download_strategies.rb diff --git a/Library/Homebrew/download_strategy.rb b/Library/Homebrew/download_strategy.rb index 6d16deb5f9..f0347d7621 100644 --- a/Library/Homebrew/download_strategy.rb +++ b/Library/Homebrew/download_strategy.rb @@ -586,23 +586,20 @@ class FossilDownloadStrategy < AbstractDownloadStrategy end class DownloadStrategyDetector - def initialize url, strategy=nil - @url = url - @strategy = strategy - end - - def detect - if @strategy.is_a? Class and @strategy.ancestors.include? AbstractDownloadStrategy - @strategy - elsif @strategy.is_a? Symbol then detect_from_symbol - else detect_from_url + def self.detect(url, strategy=nil) + if strategy.is_a? Class and strategy.ancestors.include? AbstractDownloadStrategy + strategy + elsif strategy.is_a? Symbol + detect_from_symbol(strategy) + else + detect_from_url(url) end end private - def detect_from_url - case @url + def self.detect_from_url(url) + case url # We use a special URL pattern for cvs when %r[^cvs://] then CVSDownloadStrategy # Standard URLs @@ -628,8 +625,8 @@ class DownloadStrategyDetector end end - def detect_from_symbol - case @strategy + def self.detect_from_symbol(symbol) + case symbol when :bzr then BazaarDownloadStrategy when :curl then CurlDownloadStrategy when :cvs then CVSDownloadStrategy @@ -639,7 +636,7 @@ class DownloadStrategyDetector when :post then CurlPostDownloadStrategy when :svn then SubversionDownloadStrategy else - raise "Unknown download strategy #{@strategy} was requested." + raise "Unknown download strategy #{strategy} was requested." end end end diff --git a/Library/Homebrew/formula_support.rb b/Library/Homebrew/formula_support.rb index 1bf3fbc585..f72358d4d3 100644 --- a/Library/Homebrew/formula_support.rb +++ b/Library/Homebrew/formula_support.rb @@ -12,7 +12,7 @@ class SoftwareSpec end def download_strategy - @download_strategy ||= DownloadStrategyDetector.new(@url, @using).detect + @download_strategy ||= DownloadStrategyDetector.detect(@url, @using) end def verify_download_integrity fn diff --git a/Library/Homebrew/test/test_download_strategies.rb b/Library/Homebrew/test/test_download_strategies.rb new file mode 100644 index 0000000000..13392272b9 --- /dev/null +++ b/Library/Homebrew/test/test_download_strategies.rb @@ -0,0 +1,19 @@ +require 'testing_env' +require 'download_strategy' +require 'bottles' # XXX: hoist these regexps into constants in Pathname? + +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 +end