Tests for the downloader

This commit is contained in:
Mitchell Hashimoto 2013-03-17 11:09:46 -07:00
parent 913e0e529d
commit b9a2855fcb
3 changed files with 62 additions and 5 deletions

View File

@ -163,6 +163,10 @@ module Vagrant
error_key(:dotfile_upgrade_json_error)
end
class DownloaderError < VagrantError
error_key(:downloader_error)
end
class DownloaderFileDoesntExist < VagrantError
error_key(:file_missing, "vagrant.downloaders.file")
end

View File

@ -87,11 +87,7 @@ module Vagrant
# show an error message.
if result.exit_code != 0
parts = result.stderr.split(/\ncurl:\s+\(\d+\)\s*/, 2)
# If the length is correct, we properly parsed an error message
if parts.length == 2
# TODO: Raise the error
end
raise Errors::DownloaderError, :message => parts[1]
end
# Everything succeeded

View File

@ -0,0 +1,57 @@
require File.expand_path("../../../base", __FILE__)
require "vagrant/util/downloader"
describe Vagrant::Util::Downloader do
let(:source) { "foo" }
let(:destination) { "bar" }
let(:exit_code) { 0 }
let(:subprocess_result) do
double("subprocess_result").tap do |result|
result.stub(:exit_code => exit_code)
result.stub(:stderr => "")
end
end
subject { described_class.new(source, destination) }
before :each do
Vagrant::Util::Subprocess.stub(:execute).and_return(subprocess_result)
end
describe "#download!" do
context "with a good exit status" do
let(:exit_code) { 0 }
it "downloads the file and returns true" do
curl_options = ["--fail", "--output", destination, source]
Vagrant::Util::Subprocess.should_receive(:execute).
with("curl", *curl_options).
and_return(subprocess_result)
subject.download!.should be
end
end
context "with a bad exit status" do
let(:exit_code) { 1 }
it "raises an exception" do
curl_options = ["--fail", "--output", destination, source]
Vagrant::Util::Subprocess.should_receive(:execute).
with("curl", *curl_options).
and_return(subprocess_result)
expect { subject.download! }.
to raise_error(Vagrant::Errors::DownloaderError)
end
end
context "with a UI" do
pending "tests for a UI"
end
end
end