Allow the definition of communicators in plugins

This commit is contained in:
Mitchell Hashimoto 2012-08-08 21:52:25 -07:00
parent 595e7cee0e
commit 5ae3e0e80c
3 changed files with 47 additions and 0 deletions

View File

@ -109,6 +109,7 @@ module Vagrant
def communicate def communicate
# For now, we always return SSH. In the future, we'll abstract # For now, we always return SSH. In the future, we'll abstract
# this and allow plugins to define new methods of communication. # this and allow plugins to define new methods of communication.
end end
# This sets the unique ID associated with this machine. This will # This sets the unique ID associated with this machine. This will

View File

@ -103,6 +103,24 @@ module Vagrant
data[:command] data[:command]
end end
# Defines additional communicators to be available. Communicators
# should be returned by a block passed to this method. This is done
# to ensure that the class is lazy loaded, so if your class inherits
# from or uses any Vagrant internals specific to Vagrant 1.0, then
# the plugin can still be defined without breaking anything in future
# versions of Vagrant.
#
# @param [String] name Communicator name.
def self.communicator(name=UNSET_VALUE, &block)
data[:communicator] ||= Registry.new
# Register a new communicator class only if a name was given.
data[:communicator].register(name.to_sym, &block) if name != UNSET_VALUE
# Return the registry
data[:communicator]
end
# Defines additional configuration keys to be available in the # Defines additional configuration keys to be available in the
# Vagrantfile. The configuration class should be returned by a # Vagrantfile. The configuration class should be returned by a
# block passed to this method. This is done to ensure that the class # block passed to this method. This is done to ensure that the class

View File

@ -100,6 +100,34 @@ describe Vagrant::Plugin::V1::Plugin do
end end
end end
describe "communicators" do
it "should register communicator classes" do
plugin = Class.new(described_class) do
communicator("foo") { "bar" }
end
plugin.communicator[:foo].should == "bar"
end
it "should lazily register communicator classes" do
# Below would raise an error if the value of the class was
# evaluated immediately. By asserting that this does not raise an
# error, we verify that the value is actually lazily loaded
plugin = nil
expect {
plugin = Class.new(described_class) do
communicator("foo") { raise StandardError, "FAIL!" }
end
}.to_not raise_error
# Now verify when we actually get the configuration key that
# a proper error is raised.
expect {
plugin.communicator[:foo]
}.to raise_error(StandardError)
end
end
describe "configuration" do describe "configuration" do
it "should register configuration classes" do it "should register configuration classes" do
plugin = Class.new(described_class) do plugin = Class.new(described_class) do