From 5ae3e0e80c66e2538f622de9c0feffcff56dadaa Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 8 Aug 2012 21:52:25 -0700 Subject: [PATCH] Allow the definition of communicators in plugins --- lib/vagrant/machine.rb | 1 + lib/vagrant/plugin/v1/plugin.rb | 18 ++++++++++++++ test/unit/vagrant/plugin/v1/plugin_test.rb | 28 ++++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/lib/vagrant/machine.rb b/lib/vagrant/machine.rb index 5aa505f69..19de0cd2c 100644 --- a/lib/vagrant/machine.rb +++ b/lib/vagrant/machine.rb @@ -109,6 +109,7 @@ module Vagrant def communicate # For now, we always return SSH. In the future, we'll abstract # this and allow plugins to define new methods of communication. + end # This sets the unique ID associated with this machine. This will diff --git a/lib/vagrant/plugin/v1/plugin.rb b/lib/vagrant/plugin/v1/plugin.rb index abb503549..7d0e57b53 100644 --- a/lib/vagrant/plugin/v1/plugin.rb +++ b/lib/vagrant/plugin/v1/plugin.rb @@ -103,6 +103,24 @@ module Vagrant data[:command] 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 # Vagrantfile. The configuration class should be returned by a # block passed to this method. This is done to ensure that the class diff --git a/test/unit/vagrant/plugin/v1/plugin_test.rb b/test/unit/vagrant/plugin/v1/plugin_test.rb index 6a20f14b8..df1d5ccc2 100644 --- a/test/unit/vagrant/plugin/v1/plugin_test.rb +++ b/test/unit/vagrant/plugin/v1/plugin_test.rb @@ -100,6 +100,34 @@ describe Vagrant::Plugin::V1::Plugin do 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 it "should register configuration classes" do plugin = Class.new(described_class) do