New files for the new command structure. Ignore the `vagrant-temp` binary for now.

This commit is contained in:
Mitchell Hashimoto 2010-04-13 01:22:53 -07:00
parent 46956d8caa
commit d8387f8280
10 changed files with 143 additions and 4 deletions

9
bin/vagrant-temp Executable file
View File

@ -0,0 +1,9 @@
#!/usr/bin/env ruby
require 'rubygems'
# Get library
libdir = File.join(File.dirname(__FILE__), '..', 'lib')
require File.expand_path('vagrant', libdir)
# Call the command
Vagrant::Command.execute(*ARGV)

View File

@ -9,7 +9,7 @@ end
# The vagrant specific files which must be loaded prior to the rest
%w{vagrant/util vagrant/util/stacked_proc_runner vagrant/util/progress_meter vagrant/actions/base vagrant/downloaders/base vagrant/actions/collection
vagrant/actions/runner vagrant/config vagrant/provisioners/base vagrant/provisioners/chef}.each do |f|
vagrant/actions/runner vagrant/config vagrant/provisioners/base vagrant/provisioners/chef vagrant/commands/base}.each do |f|
require File.expand_path(f, libdir)
end

33
lib/vagrant/command.rb Normal file
View File

@ -0,0 +1,33 @@
module Vagrant
# This class handles commands from the command line program `vagrant`
# and redirects them to the proper sub-command, setting up the environment
# and executing.
class Command
attr_reader :env
class <<self
def execute(*args)
env = Environment.load!
env.commands.subcommand(*args)
end
end
def initialize(env)
@env = env
end
def subcommand(name, *args)
command_klass = Commands.const_get(camelize(name))
command = command_klass.new(env)
command.execute(args)
end
def camelize(string)
parts = string.to_s.split(/[^a-z0-9]/).collect do |part|
part.capitalize
end
parts.join("")
end
end
end

View File

@ -0,0 +1,17 @@
module Vagrant
class Commands
# This is the base command class which all sub-commands must
# inherit from.
class Base
attr_reader :env
def initialize(env)
@env = env
end
def execute(args)
end
end
end
end

View File

@ -0,0 +1,9 @@
module Vagrant
class Commands
class Init < Base
def execute(args)
p args
end
end
end
end

View File

@ -189,11 +189,11 @@ module Vagrant
@active_list = ActiveList.new(self)
end
# Loads the instance of {Commands} for this environment. This allows
# Loads the instance of {Command} for this environment. This allows
# users of the instance to run commands such as "up" "down" etc. in
# the context of this environment.
def load_commands!
@commands = Commands.new(self)
@commands = Command.new(self)
end
#---------------------------------------------------------------

View File

@ -0,0 +1,71 @@
require File.join(File.dirname(__FILE__), '..', 'test_helper')
class CommandTest < Test::Unit::TestCase
setup do
@klass = Vagrant::Command
end
context "initializing" do
setup do
@env = mock("environment")
end
should "set the env attribute" do
@instance = @klass.new(@env)
assert_equal @env, @instance.env
end
end
context "class methods" do
context "executing" do
should "load the environment then send the command on commands" do
env = mock("env")
commands = mock("commands")
env.stubs(:commands).returns(commands)
Vagrant::Environment.expects(:load!).returns(env)
commands.expects(:subcommand).with(1,2,3).once
@klass.execute(1,2,3)
end
end
end
context "with an instance" do
setup do
@env = mock_environment
@instance = @klass.new(@env)
end
context "subcommands" do
setup do
@raw_name = :bar
@name = :foo
@instance.stubs(:camelize).with(@raw_name).returns(@name)
end
should "send the command to the proper class" do
klass = mock("klass")
instance = mock("instance")
args = [1,2,3]
Vagrant::Commands.expects(:const_get).with(@name).returns(klass)
klass.expects(:new).with(@env).returns(instance)
instance.expects(:execute).with(args)
@instance.subcommand(@raw_name, *args)
end
end
context "camelizing" do
should "camel case a string" do
tests = {
"foo_bar_baz" => "FooBarBaz",
"ssh-config" => "SshConfig"
}
tests.each do |test, expected|
assert_equal expected, @instance.camelize(test)
end
end
end
end
end

View File

@ -424,7 +424,7 @@ class EnvironmentTest < Test::Unit::TestCase
should "initialize the Commands object with the given environment" do
commands = mock("commands")
Vagrant::Commands.expects(:new).with(@env).returns(commands)
Vagrant::Command.expects(:new).with(@env).returns(commands)
@env.load_commands!
assert_equal commands, @env.commands
end