From 269645f63d76dab660a09d364d3fb2988d49fca9 Mon Sep 17 00:00:00 2001 From: Julian Zinn Date: Mon, 19 Feb 2018 20:33:37 -0800 Subject: [PATCH] Improve Windows drive letter removal hack --- .../chef/provisioner/chef_solo.rb | 4 +- .../chef/provisioner/chef_solo_test.rb | 75 +++++++++++++++++++ 2 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 test/unit/plugins/provisioners/chef/provisioner/chef_solo_test.rb diff --git a/plugins/provisioners/chef/provisioner/chef_solo.rb b/plugins/provisioners/chef/provisioner/chef_solo.rb index 754ace11c..676d40d7a 100644 --- a/plugins/provisioners/chef/provisioner/chef_solo.rb +++ b/plugins/provisioners/chef/provisioner/chef_solo.rb @@ -5,6 +5,7 @@ require "set" require "log4r" require "vagrant/util/counter" +require "vagrant/action/builtin/mixin_synced_folders" require_relative "base" @@ -95,13 +96,12 @@ module VagrantPlugins else # Path already exists on the virtual machine. Expand it # relative to where we're provisioning. - remote_path = File.expand_path(path, guest_provisioning_path) # Remove drive letter if running on a windows host. This is a bit # of a hack but is the most portable way I can think of at the moment # to achieve this. Otherwise, Vagrant attempts to share at some crazy # path like /home/vagrant/c:/foo/bar - remote_path = remote_path.gsub(/^[a-zA-Z]:/, "") + remote_path = File.expand_path(path.gsub(/^[a-zA-Z]:\//, "/"), guest_provisioning_path.gsub(/^[a-zA-Z]:\//, "/")) end # If we have specified a folder name to append then append it diff --git a/test/unit/plugins/provisioners/chef/provisioner/chef_solo_test.rb b/test/unit/plugins/provisioners/chef/provisioner/chef_solo_test.rb new file mode 100644 index 000000000..1282740d3 --- /dev/null +++ b/test/unit/plugins/provisioners/chef/provisioner/chef_solo_test.rb @@ -0,0 +1,75 @@ +require_relative "../../../../base" + +require Vagrant.source_root.join("plugins/provisioners/chef/provisioner/chef_solo") + +describe VagrantPlugins::Chef::Provisioner::ChefSolo do + include_context "unit" + + let(:iso_env) do + # We have to create a Vagrantfile so there is a root path + env = isolated_environment + env.vagrantfile("") + env.create_vagrant_env + end + + let(:machine) { iso_env.machine(iso_env.machine_names[0], :dummy) } + let(:config) { double("config") } + + subject { described_class.new(machine, config) } + + before do + allow(config).to receive(:node_name) + allow(config).to receive(:node_name=) + end + + describe "#expanded_folders" do + it "handles the default Windows provisioning path" do + allow(config).to receive(:provisioning_path).and_return(nil) + allow(subject).to receive(:windows?).and_return(true) + remote_path = subject.expanded_folders([[:vm, "cookbooks-1"]])[0][2] + expect(remote_path).to eq("/vagrant-chef/cookbooks-1") + end + end + + describe "#expanded_folders" do + it "expands Windows absolute provisioning path with relative path" do + provisioning_path = "C:/vagrant-chef-1" + unexpanded_path = "cookbooks-1" + + allow(config).to receive(:provisioning_path).and_return(provisioning_path) + remote_path = subject.expanded_folders([[:vm, unexpanded_path]])[0][2] + + expect(remote_path).to eq("/vagrant-chef-1/cookbooks-1") + end + + it "expands Windows absolute provisioning path with absolute path" do + provisioning_path = "C:/vagrant-chef-1" + unexpanded_path = "/cookbooks-1" + + allow(config).to receive(:provisioning_path).and_return(provisioning_path) + remote_path = subject.expanded_folders([[:vm, unexpanded_path]])[0][2] + + expect(remote_path).to eq("/cookbooks-1") + end + + it "expands Windows absolute provisioning path with Windows absolute path" do + provisioning_path = "C:/vagrant-chef-1" + unexpanded_path = "D:/cookbooks-1" + + allow(config).to receive(:provisioning_path).and_return(provisioning_path) + remote_path = subject.expanded_folders([[:vm, unexpanded_path]])[0][2] + + expect(remote_path).to eq("/cookbooks-1") + end + + it "expands absolute provisioning path with Windows absolute path" do + provisioning_path = "/vagrant-chef-1" + unexpanded_path = "D:/cookbooks-1" + + allow(config).to receive(:provisioning_path).and_return(provisioning_path) + remote_path = subject.expanded_folders([[:vm, unexpanded_path]])[0][2] + + expect(remote_path).to eq("/cookbooks-1") + end + end +end