From 56663b5952c3c71f37842c2afb0b51045102d435 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 9 Dec 2011 15:18:43 -0800 Subject: [PATCH] Remove dependence on Vagrant::Environment on downloaders --- lib/vagrant/downloaders/base.rb | 5 ++ test/unit/vagrant/downloaders/base_test.rb | 18 +++++++ test/unit/vagrant/downloaders/file_test.rb | 38 +++++++++++++++ test/unit/vagrant/downloaders/http_test.rb | 19 ++++++++ .../vagrant/downloaders/base_test.rb | 28 ----------- .../vagrant/downloaders/file_test.rb | 48 ------------------- 6 files changed, 80 insertions(+), 76 deletions(-) create mode 100644 test/unit/vagrant/downloaders/base_test.rb create mode 100644 test/unit/vagrant/downloaders/file_test.rb create mode 100644 test/unit/vagrant/downloaders/http_test.rb delete mode 100644 test/unit_legacy/vagrant/downloaders/base_test.rb delete mode 100644 test/unit_legacy/vagrant/downloaders/file_test.rb diff --git a/lib/vagrant/downloaders/base.rb b/lib/vagrant/downloaders/base.rb index a853fba42..12fe8903d 100644 --- a/lib/vagrant/downloaders/base.rb +++ b/lib/vagrant/downloaders/base.rb @@ -9,6 +9,11 @@ module Vagrant @ui = ui end + # Tests whether a URL matches this download. Subclasses must + # override this and return `true` for any URLs they wish to + # handle. + def self.match?(url); false; end + # Called prior to execution so any error checks can be done def prepare(source_url); end diff --git a/test/unit/vagrant/downloaders/base_test.rb b/test/unit/vagrant/downloaders/base_test.rb new file mode 100644 index 000000000..963578d96 --- /dev/null +++ b/test/unit/vagrant/downloaders/base_test.rb @@ -0,0 +1,18 @@ +require File.expand_path("../../../base", __FILE__) + +describe Vagrant::Downloaders::Base do + let(:ui) { double("ui") } + let(:instance) { described_class.new(ui) } + + it "should not match anything by default" do + described_class.match?("foo").should_not be + end + + it "should implement `prepare`" do + instance.prepare("foo").should be_nil + end + + it "should implement `download!`" do + instance.download!("foo", "bar").should be_nil + end +end diff --git a/test/unit/vagrant/downloaders/file_test.rb b/test/unit/vagrant/downloaders/file_test.rb new file mode 100644 index 000000000..be5d29cef --- /dev/null +++ b/test/unit/vagrant/downloaders/file_test.rb @@ -0,0 +1,38 @@ +require File.expand_path("../../../base", __FILE__) + +describe Vagrant::Downloaders::File do + let(:ui) { double("ui") } + let(:instance) { described_class.new(ui) } + + describe "matching" do + it "should match an existing file" do + described_class.match?(__FILE__).should be + end + + it "should not match non-existent files" do + described_class.match?(File.join(__FILE__, "nowaywaywaywayayway")).should_not be + end + end + + describe "preparing" do + it "should raise an exception if the file does not exist" do + path = File.join(__FILE__, "nopenopenope") + File.exist?(path).should_not be + + expect { instance.prepare(path) }.to raise_error(Vagrant::Errors::DownloaderFileDoesntExist) + end + + it "should raise an exception if the file is a directory" do + path = File.dirname(__FILE__) + File.should be_directory(path) + + expect { instance.prepare(path) }.to raise_error(Vagrant::Errors::DownloaderFileDoesntExist) + end + end + + describe "downloading" do + it "should copy the source to the destination" do + pending "setup paths" + end + end +end diff --git a/test/unit/vagrant/downloaders/http_test.rb b/test/unit/vagrant/downloaders/http_test.rb new file mode 100644 index 000000000..dfd8782b9 --- /dev/null +++ b/test/unit/vagrant/downloaders/http_test.rb @@ -0,0 +1,19 @@ +require File.expand_path("../../../base", __FILE__) + +describe Vagrant::Downloaders::HTTP do + let(:ui) { double("ui") } + let(:instance) { described_class.new(ui) } + + describe "matching" do + it "should match URLs" do + described_class.match?("http://google.com/foo.box").should be + described_class.match?("https://google.com/foo.box").should be + described_class.match?("http://foo:bar@google.com/foo.box").should be + described_class.match?("http://google.com:8500/foo.box").should be + end + end + + describe "downloading" do + # Integration tests only. + end +end diff --git a/test/unit_legacy/vagrant/downloaders/base_test.rb b/test/unit_legacy/vagrant/downloaders/base_test.rb deleted file mode 100644 index 4cb37c6e6..000000000 --- a/test/unit_legacy/vagrant/downloaders/base_test.rb +++ /dev/null @@ -1,28 +0,0 @@ -require "test_helper" - -class BaseDownloaderTest < Test::Unit::TestCase - should "include the util class so subclasses have access to it" do - assert Vagrant::Downloaders::Base.include?(Vagrant::Util) - end - - context "base instance" do - setup do - @env = vagrant_env - @base = Vagrant::Downloaders::Base.new(@env) - end - - should "implement prepare which does nothing" do - assert_nothing_raised do - assert @base.respond_to?(:prepare) - @base.prepare("source") - end - end - - should "implement download! which does nothing" do - assert_nothing_raised do - assert @base.respond_to?(:download!) - @base.download!("source", "destination") - end - end - end -end diff --git a/test/unit_legacy/vagrant/downloaders/file_test.rb b/test/unit_legacy/vagrant/downloaders/file_test.rb deleted file mode 100644 index 271e01e6b..000000000 --- a/test/unit_legacy/vagrant/downloaders/file_test.rb +++ /dev/null @@ -1,48 +0,0 @@ -require "test_helper" - -class FileDownloaderTest < Test::Unit::TestCase - setup do - @downloader, @tempfile = vagrant_mock_downloader(Vagrant::Downloaders::File) - @uri = "foo.box" - end - - context "preparing" do - should "raise an exception if the file does not exist" do - File.expects(:file?).with(@uri).returns(false) - - assert_raises(Vagrant::Errors::DownloaderFileDoesntExist) { - @downloader.prepare(@uri) - } - end - end - - context "downloading" do - setup do - clean_paths - end - - should "cp the file" do - uri = tmp_path.join("foo_source") - dest = tmp_path.join("foo_dest") - - # Create the source file, then "download" it - File.open(uri, "w+") { |f| f.write("FOO") } - File.open(dest, "w+") do |dest_file| - @downloader.download!(uri, dest_file) - end - - # Finally, verify the destination file was properly created - assert File.file?(dest) - File.open(dest) do |f| - assert_equal "FOO", f.read - end - end - end - - context "matching a uri" do - should "return true if the File exists on the file system" do - File.expects(:exists?).with('foo').returns(true) - assert Vagrant::Downloaders::File.match?('foo') - end - end -end