Merge pull request #10470 from wolfgang42/snapshot-error

snapshot plugin: Raise error for bad subcommand.
This commit is contained in:
Brian Cain 2018-12-04 14:32:22 -08:00 committed by GitHub
commit 2038f2878c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 3 deletions

View File

@ -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 <subcommand> -h`"
end
@env.ui.info(opts.help, prefix: false)
opts.help
end
end
end

View File

@ -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 <subcommand>/, 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