From ee0086ddeece2f50f69428429e910fb939dfc988 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 21 Nov 2013 15:56:37 -0800 Subject: [PATCH] core: Add synced_folder plugin type --- lib/vagrant/plugin/v2.rb | 1 + lib/vagrant/plugin/v2/components.rb | 6 ++++ lib/vagrant/plugin/v2/plugin.rb | 13 +++++++ lib/vagrant/plugin/v2/synced_folder.rb | 17 +++++++++ .../unit/vagrant/plugin/v2/components_test.rb | 10 ++++-- test/unit/vagrant/plugin/v2/plugin_test.rb | 36 +++++++++++++++++++ 6 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 lib/vagrant/plugin/v2/synced_folder.rb diff --git a/lib/vagrant/plugin/v2.rb b/lib/vagrant/plugin/v2.rb index 91803e648..1539667bd 100644 --- a/lib/vagrant/plugin/v2.rb +++ b/lib/vagrant/plugin/v2.rb @@ -17,6 +17,7 @@ module Vagrant autoload :Plugin, "vagrant/plugin/v2/plugin" autoload :Provider, "vagrant/plugin/v2/provider" autoload :Provisioner, "vagrant/plugin/v2/provisioner" + autoload :SyncedFolder, "vagrant/plugin/v2/synced_folder" end end end diff --git a/lib/vagrant/plugin/v2/components.rb b/lib/vagrant/plugin/v2/components.rb index 6b585d071..8a62401af 100644 --- a/lib/vagrant/plugin/v2/components.rb +++ b/lib/vagrant/plugin/v2/components.rb @@ -32,6 +32,11 @@ module Vagrant # @return [Hash] attr_reader :providers + # This contains all the synced folder implementations by name. + # + # @return [Registry] + attr_reader :synced_folders + def initialize # The action hooks hash defaults to [] @action_hooks = Hash.new { |h, k| h[k] = [] } @@ -40,6 +45,7 @@ module Vagrant @guests = Registry.new @guest_capabilities = Hash.new { |h, k| h[k] = Registry.new } @providers = Registry.new + @synced_folders = Registry.new end end end diff --git a/lib/vagrant/plugin/v2/plugin.rb b/lib/vagrant/plugin/v2/plugin.rb index 0acc7614f..6ffb860a1 100644 --- a/lib/vagrant/plugin/v2/plugin.rb +++ b/lib/vagrant/plugin/v2/plugin.rb @@ -194,6 +194,19 @@ module Vagrant data[:provisioners] end + # Registers additional synced folder implementations. + # + # @param [String] name Name of the implementation. + # @param [Integer] priority The priority of the implementation, + # higher (big) numbers are tried before lower (small) numbers. + def self.synced_folder(name, priority=10, &block) + components.synced_folders.register(name.to_sym) do + [block.call, priority] + end + + nil + end + # Returns the internal data associated with this plugin. This # should NOT be called by the general public. # diff --git a/lib/vagrant/plugin/v2/synced_folder.rb b/lib/vagrant/plugin/v2/synced_folder.rb new file mode 100644 index 000000000..1b7a856fd --- /dev/null +++ b/lib/vagrant/plugin/v2/synced_folder.rb @@ -0,0 +1,17 @@ +module Vagrant + module Plugin + module V2 + # This is the base class for a synced folder implementation. + class SyncedFolder + def usable?(machine) + end + + def prepare(machine) + end + + def enable(machine) + end + end + end + end +end diff --git a/test/unit/vagrant/plugin/v2/components_test.rb b/test/unit/vagrant/plugin/v2/components_test.rb index bf98ca71b..b8dd1837c 100644 --- a/test/unit/vagrant/plugin/v2/components_test.rb +++ b/test/unit/vagrant/plugin/v2/components_test.rb @@ -3,15 +3,19 @@ require File.expand_path("../../../../base", __FILE__) require "vagrant/registry" describe Vagrant::Plugin::V2::Components do - let(:instance) { described_class.new } + subject { described_class.new } + + it "should have synced folders" do + subject.synced_folders.should be_kind_of(Vagrant::Registry) + end describe "configs" do it "should have configs" do - instance.configs.should be_kind_of(Hash) + subject.configs.should be_kind_of(Hash) end it "should default the values to registries" do - instance.configs[:i_probably_dont_exist].should be_kind_of(Vagrant::Registry) + subject.configs[:i_probably_dont_exist].should be_kind_of(Vagrant::Registry) end end end diff --git a/test/unit/vagrant/plugin/v2/plugin_test.rb b/test/unit/vagrant/plugin/v2/plugin_test.rb index 0295b3242..71597988c 100644 --- a/test/unit/vagrant/plugin/v2/plugin_test.rb +++ b/test/unit/vagrant/plugin/v2/plugin_test.rb @@ -278,6 +278,42 @@ describe Vagrant::Plugin::V2::Plugin do end end + describe "synced folders" do + it "should register implementations" do + plugin = Class.new(described_class) do + synced_folder("foo") { "bar" } + end + + plugin.components.synced_folders[:foo].should == ["bar", 10] + end + + it "should be able to specify priorities" do + plugin = Class.new(described_class) do + synced_folder("foo", 50) { "bar" } + end + + plugin.components.synced_folders[:foo].should == ["bar", 50] + end + + it "should lazily register implementations" do + # Below would raise an error if the value of the config 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 + synced_folder("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.components.synced_folders[:foo] + }.to raise_error(StandardError) + end + end + describe "plugin registration" do let(:manager) { described_class.manager }