From 0d6248394ce3ee0b6a72a4b3ec19b87b3ee61e4b Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 6 May 2012 10:01:50 -0700 Subject: [PATCH] Tests for the Easy command base --- lib/vagrant/easy/command_base.rb | 17 +++++++++++++++++ test/unit/vagrant/easy/command_base_test.rb | 20 ++++++++++++++++++++ test/unit/vagrant/plugin/v1_test.rb | 10 ---------- 3 files changed, 37 insertions(+), 10 deletions(-) create mode 100644 test/unit/vagrant/easy/command_base_test.rb diff --git a/lib/vagrant/easy/command_base.rb b/lib/vagrant/easy/command_base.rb index 7e1a40ccd..ab558afda 100644 --- a/lib/vagrant/easy/command_base.rb +++ b/lib/vagrant/easy/command_base.rb @@ -5,18 +5,35 @@ module Vagrant # Base class for all easy commands. This contains the basic code # that knows how to run the easy commands. 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 # an easy command to set the invocation command. 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 @runner = block end 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 + # 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) @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}") end diff --git a/test/unit/vagrant/easy/command_base_test.rb b/test/unit/vagrant/easy/command_base_test.rb new file mode 100644 index 000000000..335b43f18 --- /dev/null +++ b/test/unit/vagrant/easy/command_base_test.rb @@ -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 diff --git a/test/unit/vagrant/plugin/v1_test.rb b/test/unit/vagrant/plugin/v1_test.rb index 14cfacd9c..c187b3ab5 100644 --- a/test/unit/vagrant/plugin/v1_test.rb +++ b/test/unit/vagrant/plugin/v1_test.rb @@ -109,16 +109,6 @@ describe Vagrant::Plugin::V1 do # Check that the command class subclasses the easy command base plugin.command[:foo].should < Vagrant::Easy::CommandBase 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 describe "guests" do