From b9a2855fcb64a82e9a6f8f024da26777c3dce920 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 17 Mar 2013 11:09:46 -0700 Subject: [PATCH] Tests for the downloader --- lib/vagrant/errors.rb | 4 ++ lib/vagrant/util/downloader.rb | 6 +-- test/unit/vagrant/util/downloader_test.rb | 57 +++++++++++++++++++++++ 3 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 test/unit/vagrant/util/downloader_test.rb diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index fbe062fe1..1bc02b020 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -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 diff --git a/lib/vagrant/util/downloader.rb b/lib/vagrant/util/downloader.rb index fd7a9516c..f67f61929 100644 --- a/lib/vagrant/util/downloader.rb +++ b/lib/vagrant/util/downloader.rb @@ -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 diff --git a/test/unit/vagrant/util/downloader_test.rb b/test/unit/vagrant/util/downloader_test.rb new file mode 100644 index 000000000..201c9e681 --- /dev/null +++ b/test/unit/vagrant/util/downloader_test.rb @@ -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