From aec05eff0d0dfb3ad706970277f789c3e5727603 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 13 Apr 2010 13:13:55 -0700 Subject: [PATCH] Commands can now be registered as subcommands (similar to how config works) --- lib/vagrant/command.rb | 13 +------ lib/vagrant/commands/base.rb | 55 ++++++++++++++++++++++++++++++ lib/vagrant/commands/init.rb | 5 +++ test/vagrant/command_test.rb | 24 ++----------- test/vagrant/commands/base_test.rb | 45 ++++++++++++++++++++++++ 5 files changed, 109 insertions(+), 33 deletions(-) diff --git a/lib/vagrant/command.rb b/lib/vagrant/command.rb index 716be9ae2..ec673b810 100644 --- a/lib/vagrant/command.rb +++ b/lib/vagrant/command.rb @@ -21,18 +21,7 @@ module Vagrant # Execute a subcommand with the given name and args. This method properly # finds the subcommand, instantiates it, and executes. def subcommand(name, *args) - command_klass = Commands.const_get(camelize(name)) - command = command_klass.new(env) - command.execute(args) - end - - # Camel-case a string. - def camelize(string) - parts = string.to_s.split(/[^a-z0-9]/).collect do |part| - part.capitalize - end - - parts.join("") + Commands::Base.dispatch(env, name, *args) end end end \ No newline at end of file diff --git a/lib/vagrant/commands/base.rb b/lib/vagrant/commands/base.rb index 51a9a24da..d5c5c806a 100644 --- a/lib/vagrant/commands/base.rb +++ b/lib/vagrant/commands/base.rb @@ -7,6 +7,61 @@ module Vagrant class Base attr_reader :env + class < "FooBarBaz", - "ssh-config" => "SshConfig" - } - - tests.each do |test, expected| - assert_equal expected, @instance.camelize(test) - end + Vagrant::Commands::Base.expects(:dispatch).with(@env, @name, *args) + @instance.subcommand(@name, *args) end end end diff --git a/test/vagrant/commands/base_test.rb b/test/vagrant/commands/base_test.rb index 347a3d958..9adb08347 100644 --- a/test/vagrant/commands/base_test.rb +++ b/test/vagrant/commands/base_test.rb @@ -13,6 +13,51 @@ class CommandsBastTest < Test::Unit::TestCase end end + context "class methods" do + setup do + @klass.subcommands.clear + end + + context "registering commands" do + should "register commands" do + klass = mock("klass") + @klass.subcommand("init", klass) + assert_equal klass, @klass.subcommands["init"] + end + end + + context "dispatching to subcommands" do + setup do + @command_klass = mock("klass") + @name = "init" + @klass.subcommand(@name, @command_klass) + + @args = [1,2,3] + end + + should "instantiate and execute on registered subcommands" do + instance = mock("instance") + @command_klass.expects(:new).with(@env).returns(instance) + instance.expects(:execute).with(@args) + + @klass.dispatch(@env, @name, *@args) + end + + should "print help if command doesn't exist" do + @klass.expects(:puts_help).once + @klass.dispatch(@env, "#{@name}foo") + end + end + + context "descriptions" do + should "be able to set description" do + description = "The lazy fox yada yada" + @klass.description(description) + assert_equal description, @klass.description + end + end + end + context "instance methods" do setup do @env = mock_environment