core: CLI understands new plugin definition format

This commit is contained in:
Mitchell Hashimoto 2014-01-11 08:53:25 -08:00
parent 4a99cdccdf
commit ad99b4f250
3 changed files with 61 additions and 28 deletions

View File

@ -23,15 +23,17 @@ module Vagrant
# If we reached this far then we must have a subcommand. If not,
# then we also just print the help and exit.
command_class = nil
command_plugin = nil
if @sub_command
command_class = Vagrant.plugin("2").manager.commands[@sub_command.to_sym]
command_plugin = Vagrant.plugin("2").manager.commands[@sub_command.to_sym]
end
if !command_class || !@sub_command
if !command_plugin || !@sub_command
help
return 1
end
command_class = command_plugin[0].call
@logger.debug("Invoking command class: #{command_class} #{@sub_args.inspect}")
# Initialize and execute the command class, returning the exit status.
@ -57,8 +59,9 @@ module Vagrant
# out as well.
commands = {}
longest = 0
Vagrant.plugin("2").manager.commands.each do |key, klass|
Vagrant.plugin("2").manager.commands.each do |key, data|
key = key.to_s
klass = data[0].call
commands[key] = klass.synopsis
longest = key.length if key.length > longest
end

View File

@ -30,11 +30,11 @@ module Vagrant
# This returns all the registered commands.
#
# @return [Hash]
# @return [Registry<Symbol, Array<Proc, Hash>>]
def commands
Registry.new.tap do |result|
@registered.each do |plugin|
result.merge!(plugin.command)
result.merge!(plugin.components.commands)
end
end
end

View File

@ -1,29 +1,59 @@
require_relative "../base"
require "vagrant/cli"
describe Vagrant::CLI do
describe "parsing options" do
let(:klass) do
Class.new(described_class)
include_context "unit"
let(:commands) { {} }
let(:iso_env) { isolated_environment }
let(:env) { iso_env.create_vagrant_env }
before do
Vagrant.plugin("2").manager.stub(commands: commands)
end
let(:environment) do
ui = double("UI::Silent")
ui.stub(:machine => "bar")
ui.stub(:info => "bar")
env = double("Vagrant::Environment")
env.stub(:active_machines => [])
env.stub(:ui => ui)
env.stub(:root_path => "foo")
env.stub(:machine_names => [])
env
def command_lambda(name, result)
lambda do
Class.new(Vagrant.plugin("2", "command")) do
define_method(:execute) do
result
end
end
end
end
describe "#execute" do
it "invokes help and exits with 1 if invalid command" do
subject = described_class.new(["i-dont-exist"], env)
subject.should_receive(:help).once
expect(subject.execute).to eql(1)
end
it "returns a non-zero exit status if an invalid command is given" do
result = klass.new(["destroypp"], environment).execute
result.should_not == 0
it "invokes command and returns its exit status if the command is valid" do
commands[:destroy] = [command_lambda("destroy", 42), {}]
subject = described_class.new(["destroy"], env)
subject.should_not_receive(:help)
expect(subject.execute).to eql(42)
end
end
it "returns an exit status of zero if a valid command is given" do
result = klass.new(["destroy"], environment).execute
result.should == 0
describe "#help" do
subject { described_class.new([], env) }
it "includes all available subcommands" do
commands[:foo] = [command_lambda("foo", 0), {}]
commands[:bar] = [command_lambda("bar", 0), {}]
commands[:baz] = [command_lambda("baz", 0), {}]
env.ui.should_receive(:info).with do |message, opts|
expect(message).to include("foo")
expect(message).to include("bar")
expect(message).to include("baz")
end
subject.help
end
end
end