Remove dependence on Vagrant::Environment on downloaders

This commit is contained in:
Mitchell Hashimoto 2011-12-09 15:18:43 -08:00
parent 0304f78a84
commit 56663b5952
6 changed files with 80 additions and 76 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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