diff --git a/plugins/commands/snapshot/command/root.rb b/plugins/commands/snapshot/command/root.rb index 4ced72277..50fcfb2fe 100644 --- a/plugins/commands/snapshot/command/root.rb +++ b/plugins/commands/snapshot/command/root.rb @@ -48,13 +48,17 @@ module VagrantPlugins def execute if @main_args.include?("-h") || @main_args.include?("--help") # Print the help for all the commands. - return help + @env.ui.info(help, prefix: false) + return 0 end # If we reached this far then we must have a subcommand. If not, # then we also just print the help and exit. command_class = @subcommands.get(@sub_command.to_sym) if @sub_command - return help if !command_class || !@sub_command + if !command_class || !@sub_command + raise Vagrant::Errors::CLIInvalidUsage, + help: help() + end @logger.debug("Invoking command class: #{command_class} #{@sub_args.inspect}") # Initialize and execute the command class @@ -81,7 +85,7 @@ module VagrantPlugins opts.separator "For help on any individual subcommand run `vagrant snapshot -h`" end - @env.ui.info(opts.help, prefix: false) + opts.help end end end diff --git a/test/unit/plugins/commands/snapshot/command/root_test.rb b/test/unit/plugins/commands/snapshot/command/root_test.rb new file mode 100644 index 000000000..426015f18 --- /dev/null +++ b/test/unit/plugins/commands/snapshot/command/root_test.rb @@ -0,0 +1,45 @@ +require File.expand_path("../../../../../base", __FILE__) + +require Vagrant.source_root.join("plugins/commands/snapshot/command/root") + +describe VagrantPlugins::CommandSnapshot::Command::Root do + include_context "unit" + + let(:iso_env) do + # We have to create a Vagrantfile so there is a root path + env = isolated_environment + env.vagrantfile("") + env.create_vagrant_env + end + + let(:argv) { [] } + + subject { described_class.new(argv, iso_env) } + + describe "execute" do + context "--help" do + let(:argv) { ["--help"] } + it "shows help" do + expect(iso_env.ui). + to receive(:info).with(/Usage: vagrant snapshot /, anything) + expect(subject.execute).to eq(0) + end + end + + context "with no subcommand" do + let(:argv) { [] } + it "shows help and fails" do + expect { subject.execute }. + to raise_error(Vagrant::Errors::CLIInvalidUsage) + end + end + + context "with invalid subcommand" do + let(:argv) { ["invalid"] } + it "shows help and fails" do + expect { subject.execute }. + to raise_error(Vagrant::Errors::CLIInvalidUsage) + end + end + end +end