core: SyncedFolders middleware saves what it synced

This commit is contained in:
Mitchell Hashimoto 2014-04-16 13:43:54 -07:00
parent 794cd4f287
commit 4557ece4df
4 changed files with 53 additions and 3 deletions

View File

@ -62,7 +62,21 @@ module Vagrant
#
# @param [Machine] machine The machine that the folders belong to
# @param [Hash] folders The result from a {#synced_folders} call.
def save_synced_folders(machine, folders)
def save_synced_folders(machine, folders, **opts)
if opts[:merge]
existing = cached_synced_folders(machine)
if existing
folders.each do |impl, fs|
existing[impl] ||= {}
fs.each do |id, data|
existing[impl][id] = data
end
end
folders = existing
end
end
machine.data_dir.join("synced_folders").open("w") do |f|
f.write(JSON.dump(folders))
end

View File

@ -18,7 +18,12 @@ module Vagrant
end
def call(env)
folders = synced_folders(env[:machine], env[:synced_folders_config])
opts = {
cached: !!env[:synced_folders_cached],
}
folders = synced_folders(
env[:machine], env[:synced_folders_config], **opts)
folders.each do |impl_name, fs|
@logger.info("Synced Folder Implementation: #{impl_name}")
@ -82,6 +87,9 @@ module Vagrant
@logger.info("Invoking synced folder enable: #{impl_name}")
impl.enable(env[:machine], fs, impl_opts(impl_name, env))
end
# Save the synced folders
save_synced_folders(env[:machine], folders, merge: true)
end
end
end

View File

@ -158,5 +158,32 @@ describe Vagrant::Action::Builtin::MixinSyncedFolders do
})
expect(result[:nfs]).to eq({ "nfs" => old_folders["nfs"] })
end
it "should be able to save and retrieve cached versions" do
folders["foo"] = { type: "default" }
result = subject.synced_folders(machine)
subject.save_synced_folders(machine, result)
# Clear the folders and set some more
folders.clear
folders["bar"] = { type: "default" }
folders["baz"] = { type: "nfs" }
result = subject.synced_folders(machine)
subject.save_synced_folders(machine, result, merge: true)
# Clear one last time
folders.clear
# Read them all back
result = subject.synced_folders(machine, nil, cached: true)
expect(result.length).to eq(2)
expect(result[:default]).to eq({
"foo" => { type: "default" },
"bar" => { type: "default" },
})
expect(result[:nfs]).to eq({
"baz" => { type: "nfs" }
})
end
end
end

View File

@ -44,6 +44,7 @@ describe Vagrant::Action::Builtin::SyncedFolders do
env[:root_path] = Pathname.new(Dir.mktmpdir)
subject.stub(:plugins => plugins)
subject.stub(:synced_folders => synced_folders)
allow(subject).to receive(:save_synced_folders)
end
it "should create on the host if specified" do
@ -189,7 +190,7 @@ describe Vagrant::Action::Builtin::SyncedFolders do
env[:synced_folders_config] = new_config
expect(subject).to receive(:synced_folders).
with(machine, new_config).
with(machine, new_config, cached: false).
and_return(synced_folders)
subject.call(env)