diff --git a/lib/vagrant/cli.rb b/lib/vagrant/cli.rb index 76ebc9abe..17fa09462 100644 --- a/lib/vagrant/cli.rb +++ b/lib/vagrant/cli.rb @@ -37,7 +37,14 @@ module Vagrant @logger.debug("Invoking command class: #{command_class} #{@sub_args.inspect}") # Initialize and execute the command class, returning the exit status. - result = command_class.new(@sub_args, @env).execute + result = 0 + begin + result = command_class.new(@sub_args, @env).execute + rescue Interrupt + @env.ui.info(I18n.t("vagrant.cli_interrupt")) + result = 1 + end + result = 0 if !result.is_a?(Fixnum) return result end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 5eb503e8d..63fd4fbec 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -37,6 +37,8 @@ en: to automatically delete Chef nodes and clients. chef_run_list_empty: |- Warning: Chef run list is empty. This may not be what you want. + cli_interrupt: |- + Exiting due to interrupt. docker_auto_start_not_available: |- Unable to configure automatic restart of Docker containers on the guest machine diff --git a/test/unit/support/shared/plugin_command_context.rb b/test/unit/support/shared/plugin_command_context.rb index e5f5a5612..5a226a4c1 100644 --- a/test/unit/support/shared/plugin_command_context.rb +++ b/test/unit/support/shared/plugin_command_context.rb @@ -1,8 +1,9 @@ shared_context "command plugin helpers" do - def command_lambda(name, result) + def command_lambda(name, result, **opts) lambda do Class.new(Vagrant.plugin("2", "command")) do define_method(:execute) do + raise opts[:exception] if opts[:exception] result end end diff --git a/test/unit/vagrant/cli_test.rb b/test/unit/vagrant/cli_test.rb index 620193eda..0aa669c06 100644 --- a/test/unit/vagrant/cli_test.rb +++ b/test/unit/vagrant/cli_test.rb @@ -28,6 +28,13 @@ describe Vagrant::CLI do subject.should_not_receive(:help) expect(subject.execute).to eql(42) end + + it "returns exit code 1 if interrupted" do + commands[:destroy] = [command_lambda("destroy", 42, exception: Interrupt), {}] + + subject = described_class.new(["destroy"], env) + expect(subject.execute).to eql(1) + end end describe "#help" do