From 79df69392fd2cb603b6fa698be75c240ab8afb6f Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 6 Mar 2014 08:35:21 -0800 Subject: [PATCH] core: initialize synced folder class only once [GH-3067] --- lib/vagrant/action/builtin/synced_folders.rb | 15 +++++++++++---- .../vagrant/action/builtin/synced_folders_test.rb | 5 +++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/vagrant/action/builtin/synced_folders.rb b/lib/vagrant/action/builtin/synced_folders.rb index ce6a1cc85..7b34bdfe2 100644 --- a/lib/vagrant/action/builtin/synced_folders.rb +++ b/lib/vagrant/action/builtin/synced_folders.rb @@ -53,19 +53,26 @@ module Vagrant end end + # Build up the instances of the synced folders. We do this once + # so that they can store state. + folders = folders.map do |impl_name, fs| + instance = plugins[impl_name.to_sym][0].new + [instance, impl_name, fs] + end + # Go through each folder and prepare the folders - folders.each do |impl_name, fs| + folders.each do |impl, impl_name, fs| @logger.info("Invoking synced folder prepare for: #{impl_name}") - plugins[impl_name.to_sym][0].new.prepare(env[:machine], fs, impl_opts(impl_name, env)) + impl.prepare(env[:machine], fs, impl_opts(impl_name, env)) end # Continue, we need the VM to be booted. @app.call(env) # Once booted, setup the folder contents - folders.each do |impl_name, fs| + folders.each do |impl, impl_name, fs| @logger.info("Invoking synced folder enable: #{impl_name}") - plugins[impl_name.to_sym][0].new.enable(env[:machine], fs, impl_opts(impl_name, env)) + impl.enable(env[:machine], fs, impl_opts(impl_name, env)) end end end diff --git a/test/unit/vagrant/action/builtin/synced_folders_test.rb b/test/unit/vagrant/action/builtin/synced_folders_test.rb index 23c77f2c9..cf5bb0434 100644 --- a/test/unit/vagrant/action/builtin/synced_folders_test.rb +++ b/test/unit/vagrant/action/builtin/synced_folders_test.rb @@ -62,13 +62,16 @@ describe Vagrant::Action::Builtin::SyncedFolders do end it "should invoke prepare then enable" do + ids = [] order = [] tracker = Class.new(impl(true, "good")) do define_method(:prepare) do |machine, folders, opts| + ids << self.object_id order << :prepare end define_method(:enable) do |machine, folders, opts| + ids << self.object_id order << :enable end end @@ -89,6 +92,8 @@ describe Vagrant::Action::Builtin::SyncedFolders do subject.call(env) order.should == [:prepare, :enable] + expect(ids.length).to eq(2) + expect(ids[0]).to eq(ids[1]) end end end