From 18f761b0150d876639bb5232e37a6f06ae8a3503 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 17 Mar 2010 21:15:48 -0700 Subject: [PATCH] Unit tests for each error_and_exit now that the message is testable --- lib/vagrant/commands.rb | 2 +- lib/vagrant/env.rb | 2 +- templates/errors.yml | 6 +++--- test/vagrant/actions/box/unpackage_test.rb | 2 +- test/vagrant/actions/vm/boot_test.rb | 2 +- test/vagrant/actions/vm/move_hard_drive_test.rb | 2 +- test/vagrant/commands_test.rb | 12 ++++++------ test/vagrant/env_test.rb | 13 +++++++------ test/vagrant/ssh_test.rb | 2 +- 9 files changed, 22 insertions(+), 21 deletions(-) diff --git a/lib/vagrant/commands.rb b/lib/vagrant/commands.rb index 99fb465f3..4274b6d43 100644 --- a/lib/vagrant/commands.rb +++ b/lib/vagrant/commands.rb @@ -14,7 +14,7 @@ module Vagrant def init(default_box=nil) rootfile_path = File.join(Dir.pwd, Env::ROOTFILE_NAME) if File.exist?(rootfile_path) - error_and_exit(:rootfile_already_exists, :rootfile => Env::ROOTFILE_NAME) + error_and_exit(:rootfile_already_exists) end # Copy over the rootfile template into this directory diff --git a/lib/vagrant/env.rb b/lib/vagrant/env.rb index 00cdd6176..58412f92a 100644 --- a/lib/vagrant/env.rb +++ b/lib/vagrant/env.rb @@ -128,7 +128,7 @@ module Vagrant def require_root_path if !root_path - error_and_exit(:rootfile_not_found, :rootfile => ROOTFILE_NAME) + error_and_exit(:rootfile_not_found) end end diff --git a/templates/errors.yml b/templates/errors.yml index a6c9daf6a..68fe70c10 100644 --- a/templates/errors.yml +++ b/templates/errors.yml @@ -18,11 +18,11 @@ already be created, but unfortunately this vagrant still appears to have no box! You can setup the environment by setting up your <%= Vagrant::Env::ROOTFILE_NAME %> and running `vagrant up`" -:rootfile_already_exists: "It looks like this directory is already setup for vagrant! (A <%= rootfile %> +:rootfile_already_exists: "It looks like this directory is already setup for vagrant! (A <%= Vagrant::Env::ROOTFILE_NAME %> already exists.)" -:rootfile_not_found: "A `<%= rootfile %>` was not found! This file is required for vagrant to run +:rootfile_not_found: "A `<%= Vagrant::Env::ROOTFILE_NAME %>` was not found! This file is required for vagrant to run since it describes the expected environment that vagrant is supposed - to manage. Please create a `<%= rootfile %>` and place it in your project + to manage. Please create a `<%= Vagrant::Env::ROOTFILE_NAME %>` and place it in your project root." :virtualbox_invalid_version: "Vagrant has detected that you have VirtualBox version <%= version %> installed! Vagrant requires that you use at least VirtualBox version 3.1. Please install diff --git a/test/vagrant/actions/box/unpackage_test.rb b/test/vagrant/actions/box/unpackage_test.rb index 84c7b25e9..80fca672e 100644 --- a/test/vagrant/actions/box/unpackage_test.rb +++ b/test/vagrant/actions/box/unpackage_test.rb @@ -68,7 +68,7 @@ class UnpackageBoxActionTest < Test::Unit::TestCase should "error and exit if the directory exists" do File.expects(:directory?).returns(true) - @action.expects(:error_and_exit).once + @action.expects(:error_and_exit).with(:box_already_exists, :box_name => @runner.name).once @action.setup_box_dir end diff --git a/test/vagrant/actions/vm/boot_test.rb b/test/vagrant/actions/vm/boot_test.rb index 52c2cef28..740349fd7 100644 --- a/test/vagrant/actions/vm/boot_test.rb +++ b/test/vagrant/actions/vm/boot_test.rb @@ -27,7 +27,7 @@ class BootActionTest < Test::Unit::TestCase fail_boot_seq = sequence("fail_boot_seq") @action.expects(:boot).once.in_sequence(fail_boot_seq) @action.expects(:wait_for_boot).returns(false).in_sequence(fail_boot_seq) - @action.expects(:error_and_exit).once.in_sequence(fail_boot_seq) + @action.expects(:error_and_exit).with(:vm_failed_to_boot).once.in_sequence(fail_boot_seq) @action.execute! end end diff --git a/test/vagrant/actions/vm/move_hard_drive_test.rb b/test/vagrant/actions/vm/move_hard_drive_test.rb index 71e33eeae..f275493e0 100644 --- a/test/vagrant/actions/vm/move_hard_drive_test.rb +++ b/test/vagrant/actions/vm/move_hard_drive_test.rb @@ -28,7 +28,7 @@ class MoveHardDriveActionTest < Test::Unit::TestCase context "execution" do should "error and exit if the vm is not powered off" do @mock_vm.expects(:powered_off?).returns(false) - @action.expects(:error_and_exit).once + @action.expects(:error_and_exit).with(:vm_power_off_to_move_hd).once @action.execute! end diff --git a/test/vagrant/commands_test.rb b/test/vagrant/commands_test.rb index 236d5a689..3139ca34a 100644 --- a/test/vagrant/commands_test.rb +++ b/test/vagrant/commands_test.rb @@ -22,7 +22,7 @@ class CommandsTest < Test::Unit::TestCase should "error and exit if a rootfile already exists" do File.expects(:exist?).with(@rootfile_path).returns(true) - Vagrant::Commands.expects(:error_and_exit).once + Vagrant::Commands.expects(:error_and_exit).with(:rootfile_already_exists).once Vagrant::Commands.init end @@ -180,7 +180,7 @@ class CommandsTest < Test::Unit::TestCase should "error and exit if the VM is not powered off" do @persisted_vm.stubs(:powered_off?).returns(false) - Vagrant::Commands.expects(:error_and_exit).once + Vagrant::Commands.expects(:error_and_exit).with(:vm_power_off_to_package).once @persisted_vm.expects(:package).never Vagrant::Commands.package end @@ -216,12 +216,12 @@ class CommandsTest < Test::Unit::TestCase Vagrant::Commands.box(["add"]) end - should "error and exit if the first argument is not 'add' or 'remove'" do - Vagrant::Commands.expects(:error_and_exit).once + should "error and exit if the first argument is not a valid subcommand" do + Vagrant::Commands.expects(:error_and_exit).with(:command_box_invalid).once Vagrant::Commands.box(["foo"]) end - should "not error and exit if the first argument is 'add' or 'remove'" do + should "not error and exit if the first argument is a valid subcommand" do commands = ["add", "remove"] commands.each do |command| @@ -272,7 +272,7 @@ class CommandsTest < Test::Unit::TestCase should "error and exit if the box doesn't exist" do Vagrant::Box.expects(:find).returns(nil) - Vagrant::Commands.expects(:error_and_exit).once + Vagrant::Commands.expects(:error_and_exit).with(:box_remove_doesnt_exist).once Vagrant::Commands.box_remove(@name) end diff --git a/test/vagrant/env_test.rb b/test/vagrant/env_test.rb index 7abc5445e..c25f64099 100644 --- a/test/vagrant/env_test.rb +++ b/test/vagrant/env_test.rb @@ -29,20 +29,21 @@ class EnvTest < Test::Unit::TestCase end should "error and exit if VirtualBox is not installed or detected" do - Vagrant::Env.expects(:error_and_exit).once + Vagrant::Env.expects(:error_and_exit).with(:virtualbox_not_detected).once VirtualBox::Command.expects(:version).returns(nil) Vagrant::Env.check_virtualbox! end should "error and exit if VirtualBox is lower than version 3.1" do - Vagrant::Env.expects(:error_and_exit).once - VirtualBox::Command.expects(:version).returns("3.0.12r1041") + version = "3.0.12r1041" + Vagrant::Env.expects(:error_and_exit).with(:virtualbox_invalid_version, :version => version.to_s).once + VirtualBox::Command.expects(:version).returns(version) Vagrant::Env.check_virtualbox! end should "error and exit if the the vboxconfig is not set" do VirtualBox::Global.expects(:vboxconfig?).returns(false) - Vagrant::Env.expects(:error_and_exit).once + Vagrant::Env.expects(:error_and_exit).with(:virtualbox_xml_not_detected).once Vagrant::Env.check_virtualbox! end end @@ -60,7 +61,7 @@ class EnvTest < Test::Unit::TestCase should "error and exit if no persisted VM was found" do assert_nil Vagrant::Env.persisted_vm - Vagrant::Env.expects(:error_and_exit).once + Vagrant::Env.expects(:error_and_exit).with(:environment_not_created).once Vagrant::Env.require_persisted_vm end @@ -413,7 +414,7 @@ class EnvTest < Test::Unit::TestCase context "requiring root_path" do should "error and exit if no root_path is set" do Vagrant::Env.expects(:root_path).returns(nil) - Vagrant::Env.expects(:error_and_exit).once + Vagrant::Env.expects(:error_and_exit).with(:rootfile_not_found).once Vagrant::Env.require_root_path end diff --git a/test/vagrant/ssh_test.rb b/test/vagrant/ssh_test.rb index a0a88e53e..fd2b400da 100644 --- a/test/vagrant/ssh_test.rb +++ b/test/vagrant/ssh_test.rb @@ -107,7 +107,7 @@ class SshTest < Test::Unit::TestCase should "error and exit if a Net::SSH::AuthenticationFailed is raised" do Vagrant::SSH.expects(:execute).raises(Net::SSH::AuthenticationFailed) - Vagrant::SSH.expects(:error_and_exit).once + Vagrant::SSH.expects(:error_and_exit).with(:vm_ssh_auth_failed).once Vagrant::SSH.up? end end