diff options
author | John Kerry <john@kerryhouse.net> | 2016-11-09 04:25:11 -0500 |
---|---|---|
committer | Thom May <thom@may.lt> | 2016-11-09 09:25:11 +0000 |
commit | 94ebcc4b16d8ce554db8cb18a37dfce3436d12b0 (patch) | |
tree | e3407e66ef9ea62e4066b99a2f81e24dc58dd19f | |
parent | 975d1e68ddf8fd4103e79aa0b8d08aef9560deee (diff) | |
download | chef-94ebcc4b16d8ce554db8cb18a37dfce3436d12b0.tar.gz |
Windows: search for config on same drive as executable location (#5478)
* changing the default drive for the config file to be the drive that the executable is launched from. This should allow for an easier launch of chef from a d: drive installation
Signed-off-by: John Kerry <john@kerryhouse.net>
* adding a check and a fallback to systemdrive for the base path when __FILE__ path doesn't have a drive (appears with linux hosts running windows targeted unit tests)
Signed-off-by: John Kerry <john@kerryhouse.net>
* fixing rubocop errors
Signed-off-by: John Kerry <john@kerryhouse.net>
* adding a __FILE__ path override for cache_path tests
Signed-off-by: John Kerry <john@kerryhouse.net>
-rw-r--r-- | chef-config/lib/chef-config/config.rb | 15 | ||||
-rw-r--r-- | chef-config/spec/unit/config_spec.rb | 46 |
2 files changed, 51 insertions, 10 deletions
diff --git a/chef-config/lib/chef-config/config.rb b/chef-config/lib/chef-config/config.rb index fb8e19518f..c9c2ca77df 100644 --- a/chef-config/lib/chef-config/config.rb +++ b/chef-config/lib/chef-config/config.rb @@ -56,13 +56,24 @@ module ChefConfig path = PathHelper.cleanpath(path) if ChefConfig.windows? # turns \etc\chef\client.rb and \var\chef\client.rb into C:/chef/client.rb - if env["SYSTEMDRIVE"] && path[0] == '\\' && path.split('\\')[2] == "chef" - path = PathHelper.join(env["SYSTEMDRIVE"], path.split('\\', 3)[2]) + # Some installations will be on different drives so use the drive that + # the expanded path to __FILE__ is found. + drive = windows_installation_drive + if drive && path[0] == '\\' && path.split('\\')[2] == "chef" + path = PathHelper.join(drive, path.split('\\', 3)[2]) end end path end + def self.windows_installation_drive + if ChefConfig.windows? + drive = File.expand_path(__FILE__).split("/", 2)[0] + drive = ENV["SYSTEMDRIVE"] if drive.to_s == "" + drive + end + end + def self.add_formatter(name, file_path = nil) formatters << [name, file_path] end diff --git a/chef-config/spec/unit/config_spec.rb b/chef-config/spec/unit/config_spec.rb index 806ab7d4fa..f5e9a914c9 100644 --- a/chef-config/spec/unit/config_spec.rb +++ b/chef-config/spec/unit/config_spec.rb @@ -203,16 +203,41 @@ RSpec.describe ChefConfig::Config do before :each do allow(ChefConfig).to receive(:windows?).and_return(is_windows) end - + describe "class method: windows_installation_drive" do + before do + allow(File).to receive(:expand_path).and_return("D:/Path/To/Executable") + end + if is_windows + it "should return D: on a windows system" do + expect(ChefConfig::Config.windows_installation_drive).to eq("D:") + end + else + it "should return nil on a non-windows system" do + expect(ChefConfig::Config.windows_installation_drive).to eq(nil) + end + end + end describe "class method: platform_specific_path" do + before do + allow(ChefConfig::Config).to receive(:env).and_return({ "SYSTEMDRIVE" => "C:" }) + end if is_windows - it "should return a windows path on windows systems" do - path = "/etc/chef/cookbooks" - allow(ChefConfig::Config).to receive(:env).and_return({ "SYSTEMDRIVE" => "C:" }) - # match on a regex that looks for the base path with an optional - # system drive at the beginning (c:) - # system drive is not hardcoded b/c it can change and b/c it is not present on linux systems - expect(ChefConfig::Config.platform_specific_path(path)).to eq("C:\\chef\\cookbooks") + path = "/etc/chef/cookbooks" + context "a windows system with chef installed on C: drive" do + before do + allow(ChefConfig::Config).to receive(:windows_installation_drive).and_return("C:") + end + it "should return a windows path rooted in C:" do + expect(ChefConfig::Config.platform_specific_path(path)).to eq("C:\\chef\\cookbooks") + end + end + context "a windows system with chef installed on D: drive" do + before do + allow(ChefConfig::Config).to receive(:windows_installation_drive).and_return("D:") + end + it "should return a windows path rooted in D:" do + expect(ChefConfig::Config.platform_specific_path(path)).to eq("D:\\chef\\cookbooks") + end end else it "should return given path on non-windows systems" do @@ -345,6 +370,11 @@ RSpec.describe ChefConfig::Config do end describe "ChefConfig::Config[:cache_path]" do + before do + if is_windows + allow(File).to receive(:expand_path).and_return("#{ChefConfig::Config.env["SYSTEMDRIVE"]}/Path/To/Executable") + end + end context "when /var/chef exists and is accessible" do it "defaults to /var/chef" do allow(ChefConfig::Config).to receive(:path_accessible?).with(to_platform("/var/chef")).and_return(true) |