Tests for the Easy command base

This commit is contained in:
Mitchell Hashimoto 2012-05-06 10:01:50 -07:00
parent dc4f6e1939
commit 0d6248394c
3 changed files with 37 additions and 10 deletions

View File

@ -5,18 +5,35 @@ module Vagrant
# Base class for all easy commands. This contains the basic code # Base class for all easy commands. This contains the basic code
# that knows how to run the easy commands. # that knows how to run the easy commands.
class CommandBase < Vagrant::Command::Base class CommandBase < Vagrant::Command::Base
# This is the command that this easy command responds to
attr_reader :command
# This is called by the {EasyCommand.create} method when creating # This is called by the {EasyCommand.create} method when creating
# an easy command to set the invocation command. # an easy command to set the invocation command.
def self.configure(name, &block) def self.configure(name, &block)
# We use class-level instance variables so that each class has
# its own single command/runner. If we use class variables then this
# whole base sharse a single one.
@command = name @command = name
@runner = block @runner = block
end end
def initialize(*args, &block) def initialize(*args, &block)
if self.class == CommandBase
raise "CommandBase must not be instantiated directly. Please subclass."
end
# Let the regular command state setup
super super
# Get the command we're listening to and the block we're invoking
# when we get that command, do some basic validation.
@command = self.class.instance_variable_get(:@command) @command = self.class.instance_variable_get(:@command)
@runner = self.class.instance_variable_get(:@runner) @runner = self.class.instance_variable_get(:@runner)
if !@command || !@runner
raise ArgumentError, "CommandBase requires both a command and a runner"
end
@logger = Log4r::Logger.new("vagrant::easy_command::#{@command}") @logger = Log4r::Logger.new("vagrant::easy_command::#{@command}")
end end

View File

@ -0,0 +1,20 @@
require File.expand_path("../../../base", __FILE__)
describe Vagrant::Easy::CommandBase do
let(:klass) { Class.new(described_class) }
it "should raise an error if instantiated directly" do
expect { described_class.new(nil, nil) }.to raise_error(RuntimeError)
end
it "should raise an error if command/runner are not set" do
expect { klass.new(nil, nil) }.to raise_error(ArgumentError)
end
it "should inherit the configured name" do
klass.configure("name") {}
instance = klass.new(nil, nil)
instance.command.should == "name"
end
end

View File

@ -109,16 +109,6 @@ describe Vagrant::Plugin::V1 do
# Check that the command class subclasses the easy command base # Check that the command class subclasses the easy command base
plugin.command[:foo].should < Vagrant::Easy::CommandBase plugin.command[:foo].should < Vagrant::Easy::CommandBase
end end
it "should support registering multiple unique commands" do
plugins = %w(foo bar baz).map do |cmd|
[cmd, Vagrant::Easy.create_command(cmd)]
end
plugins.each do |cmd, plugin|
plugin.instance_variable_get(:@command).should == cmd
end
end
end end
describe "guests" do describe "guests" do