core: Add synced_folder plugin type

This commit is contained in:
Mitchell Hashimoto 2013-11-21 15:56:37 -08:00
parent bf2b06e1be
commit ee0086ddee
6 changed files with 80 additions and 3 deletions

View File

@ -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

View File

@ -32,6 +32,11 @@ module Vagrant
# @return [Hash<Symbol, Registry>]
attr_reader :providers
# This contains all the synced folder implementations by name.
#
# @return [Registry<Symbol, Class>]
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

View File

@ -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.
#

View File

@ -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

View File

@ -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

View File

@ -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 }