From ad99b4f250f0e02d135d7300235df0ed7e167ee7 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 11 Jan 2014 08:53:25 -0800 Subject: [PATCH] core: CLI understands new plugin definition format --- lib/vagrant/cli.rb | 15 ++++--- lib/vagrant/plugin/v2/manager.rb | 4 +- test/unit/vagrant/cli_test.rb | 70 +++++++++++++++++++++++--------- 3 files changed, 61 insertions(+), 28 deletions(-) diff --git a/lib/vagrant/cli.rb b/lib/vagrant/cli.rb index f98c045b3..1f2f04514 100644 --- a/lib/vagrant/cli.rb +++ b/lib/vagrant/cli.rb @@ -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,10 +59,11 @@ module Vagrant # out as well. commands = {} longest = 0 - Vagrant.plugin("2").manager.commands.each do |key, klass| - key = key.to_s + 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 + longest = key.length if key.length > longest end commands.keys.sort.each do |key| diff --git a/lib/vagrant/plugin/v2/manager.rb b/lib/vagrant/plugin/v2/manager.rb index 17e4b7a8c..dd4788dd3 100644 --- a/lib/vagrant/plugin/v2/manager.rb +++ b/lib/vagrant/plugin/v2/manager.rb @@ -30,11 +30,11 @@ module Vagrant # This returns all the registered commands. # - # @return [Hash] + # @return [Registry>] def commands Registry.new.tap do |result| @registered.each do |plugin| - result.merge!(plugin.command) + result.merge!(plugin.components.commands) end end end diff --git a/test/unit/vagrant/cli_test.rb b/test/unit/vagrant/cli_test.rb index 2945cd978..6b2d12c35 100644 --- a/test/unit/vagrant/cli_test.rb +++ b/test/unit/vagrant/cli_test.rb @@ -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 + + 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 - 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 - end + it "invokes command and returns its exit status if the command is valid" do + commands[:destroy] = [command_lambda("destroy", 42), {}] - it "returns a non-zero exit status if an invalid command is given" do - result = klass.new(["destroypp"], environment).execute - result.should_not == 0 + 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