General package action raises exceptions instead of using env.error

This commit is contained in:
Mitchell Hashimoto 2010-08-28 13:54:59 -07:00
parent 3e1ccf0c4f
commit 98bce8f836
6 changed files with 57 additions and 45 deletions

View File

@ -27,9 +27,10 @@ module Vagrant
def call(env) def call(env)
@env = env @env = env
return env.error!(:package_output_exists) if File.exist?(tar_path) raise Errors::PackageOutputExists.new if File.exist?(tar_path)
return env.error!(:package_requires_directory) if !@env["package.directory"] || !File.directory?(@env["package.directory"]) raise Errors::PackageRequiresDirectory.new if !@env["package.directory"] || !File.directory?(@env["package.directory"])
return if !verify_included_files
verify_included_files
compress compress
@app.call(env) @app.call(env)
@ -42,13 +43,8 @@ module Vagrant
def verify_included_files def verify_included_files
@env["package.include"].each do |file| @env["package.include"].each do |file|
if !File.exist?(file) raise Errors::PackageIncludeMissing.new(:file => file) if !File.exist?(file)
@env.error!(:package_include_file_doesnt_exist, :filename => file)
return false
end
end end
true
end end
# This method copies the include files (passed in via command line) # This method copies the include files (passed in via command line)

View File

@ -10,8 +10,14 @@ module Vagrant
# I18n. # I18n.
class VagrantError < StandardError class VagrantError < StandardError
DEFAULT_NAMESPACE = "vagrant.errors" DEFAULT_NAMESPACE = "vagrant.errors"
@@used_codes = []
def self.status_code(code = nil) def self.status_code(code = nil)
if code
raise "Status code already in use: #{code}" if @@used_codes.include?(code)
@@used_codes << code
end
define_method(:status_code) { code } define_method(:status_code) { code }
end end
@ -34,7 +40,7 @@ module Vagrant
end end
class BaseVMNotFound < VagrantError class BaseVMNotFound < VagrantError
status_code(6) status_code(18)
error_key(:base_vm_not_found) error_key(:base_vm_not_found)
end end
@ -78,6 +84,21 @@ module Vagrant
error_key(:no_env) error_key(:no_env)
end end
class PackageIncludeMissing < VagrantError
status_code(20)
error_key(:include_file_missing, "vagrant.actions.general.package")
end
class PackageOutputExists < VagrantError
status_code(16)
error_key(:output_exists, "vagrant.actions.general.package")
end
class PackageRequiresDirectory < VagrantError
status_code(19)
error_key(:requires_directory, "vagrant.actions.general.package")
end
class SSHAuthenticationFailed < VagrantError class SSHAuthenticationFailed < VagrantError
status_code(11) status_code(11)
error_key(:ssh_authentication_failed) error_key(:ssh_authentication_failed)
@ -99,7 +120,7 @@ module Vagrant
end end
class VirtualBoxInvalidVersion < VagrantError class VirtualBoxInvalidVersion < VagrantError
status_code(9) status_code(17)
error_key(:virtualbox_invalid_version) error_key(:virtualbox_invalid_version)
end end

View File

@ -197,6 +197,14 @@ en:
package: package:
packaging: "Packaging additional file: %{file}" packaging: "Packaging additional file: %{file}"
compressing: "Compressing package to: %w{tar_path}" compressing: "Compressing package to: %w{tar_path}"
output_exists: |-
The specified file to save the package as already exists. Please
remove this file or specify a different filename for outputting.
requires_directory: |-
A directory was not specified to package. This should never happen
and is a result of an internal inconsistency.
include_file_missing: |-
Package include file doesn't exist: %{file}
hosts: hosts:
bsd: bsd:

View File

@ -98,15 +98,6 @@
This will cause your specified IP to be inaccessible. Please change This will cause your specified IP to be inaccessible. Please change
the IP or name of your host only network to not match that of the IP or name of your host only network to not match that of
a bridged or non-hostonly network. a bridged or non-hostonly network.
:package_include_file_doesnt_exist: |-
File specified to include: '<%= filename %>' does not exist!
:package_output_exists: |-
The specified file to save the package as already exists. Please
remove this file or specify a different filename for outputting.
:package_requires_directory: |-
A directory was not specified to package. This is an internal
issue. Please send the relevant stack trace (if any) and information
about this issue to the Vagrant team.
:package_requires_export: |- :package_requires_export: |-
Package must be used in conjunction with export. Package must be used in conjunction with export.
:provisioner_invalid_class: |- :provisioner_invalid_class: |-

View File

@ -62,39 +62,28 @@ class PackageGeneralActionTest < Test::Unit::TestCase
@instance.call(@env) @instance.call(@env)
end end
should "halt the chain if verify failed" do
@instance.expects(:verify_included_files).returns(false)
@instance.expects(:compress).never
@app.expects(:call).never
@instance.call(@env)
end
should "halt the chain if the output file already exists" do should "halt the chain if the output file already exists" do
File.expects(:exist?).returns(true) File.expects(:exist?).returns(true)
@app.expects(:call).never @app.expects(:call).never
@instance.call(@env) assert_raises(Vagrant::Errors::PackageOutputExists) {
@instance.call(@env)
assert @env.error? }
assert_equal :package_output_exists, @env.error.first
end end
should "halt the chain if directory isn't set" do should "halt the chain if directory isn't set" do
@env["package.directory"] = nil @env["package.directory"] = nil
@app.expects(:call).never @app.expects(:call).never
@instance.call(@env) assert_raises(Vagrant::Errors::PackageRequiresDirectory) {
@instance.call(@env)
assert @env.error? }
assert_equal :package_requires_directory, @env.error.first
end end
should "halt the chain if directory doesn't exist" do should "halt the chain if directory doesn't exist" do
File.expects(:directory?).with(@env["package.directory"]).returns(false) File.expects(:directory?).with(@env["package.directory"]).returns(false)
@app.expects(:call).never @app.expects(:call).never
@instance.call(@env) assert_raises(Vagrant::Errors::PackageRequiresDirectory) {
@instance.call(@env)
assert @env.error? }
assert_equal :package_requires_directory, @env.error.first
end end
end end
@ -129,9 +118,9 @@ class PackageGeneralActionTest < Test::Unit::TestCase
should "error if included file is not found" do should "error if included file is not found" do
File.expects(:exist?).with("foo").returns(false) File.expects(:exist?).with("foo").returns(false)
assert !@instance.verify_included_files assert_raises(Vagrant::Errors::PackageIncludeMissing) {
assert @env.error? @instance.verify_included_files
assert_equal :package_include_file_doesnt_exist, @env.error.first }
end end
should "return true if all exist" do should "return true if all exist" do

View File

@ -6,8 +6,15 @@ class ErrorsTest < Test::Unit::TestCase
end end
should "set the given status code" do should "set the given status code" do
klass = Class.new(@super) { status_code(4) } klass = Class.new(@super) { status_code(4444) }
assert_equal 4, klass.new.status_code assert_equal 4444, klass.new.status_code
end
should "raise an error if attempting to set the same status code twice" do
klass = Class.new(@super) { status_code(4445) }
assert_raises(RuntimeError) {
Class.new(@super) { status_code(4445) }
}
end end
should "use the given message if no set error key" do should "use the given message if no set error key" do