From 375d70c78aae5184c976dfdca0213c86e9ed186d Mon Sep 17 00:00:00 2001 From: Lamont Granquist Date: Fri, 14 Aug 2020 16:32:46 -0700 Subject: more tests and docs Signed-off-by: Lamont Granquist --- chef-config/lib/chef-config/config.rb | 34 +++++++++++++++-- chef-config/lib/chef-config/path_helper.rb | 4 ++ chef-config/spec/unit/config_spec.rb | 61 +++++++++++++++++++++++++++++- 3 files changed, 93 insertions(+), 6 deletions(-) diff --git a/chef-config/lib/chef-config/config.rb b/chef-config/lib/chef-config/config.rb index 25a7e7ba2b..a767d072ef 100644 --- a/chef-config/lib/chef-config/config.rb +++ b/chef-config/lib/chef-config/config.rb @@ -74,30 +74,53 @@ module ChefConfig path end - # On *nix, /etc/chef + # On *nix, /etc/chef, on Windows C:\chef + # + # @param windows [Boolean] optional flag to force to windows or unix-style + # @return [String] the platform-specific path + # def self.etc_chef_dir(windows: ChefUtils.windows?) path = windows ? c_chef_dir : PathHelper.join("/etc", ChefConfig::Dist::DIR_SUFFIX, windows: windows) PathHelper.cleanpath(path, windows: windows) end - # On *nix, /var/chef + # On *nix, /var/chef, on Windows C:\chef + # + # @param windows [Boolean] optional flag to force to windows or unix-style + # @return [String] the platform-specific path + # def self.var_chef_dir(windows: ChefUtils.windows?) path = windows ? c_chef_dir : PathHelper.join("/var", ChefConfig::Dist::DIR_SUFFIX, windows: windows) PathHelper.cleanpath(path, windows: windows) end - # On *nix, the root of /var/, used to test if we can create and write in /var/chef + # On *nix, /var, on Windows C:\ + # + # @param windows [Boolean] optional flag to force to windows or unix-style + # @return [String] the platform-specific path + # def self.var_root_dir(windows: ChefUtils.windows?) - path = windows ? c_chef_dir : "/var" + path = windows ? "C:\\" : "/var" PathHelper.cleanpath(path, windows: windows) end # On windows, C:/chef/ + # + # (should only be called in a windows-context) + # + # @return [String] the platform-specific path + # def self.c_chef_dir(windows: ChefUtils.windows?) drive = windows_installation_drive || "C:" PathHelper.join(drive, ChefConfig::Dist::DIR_SUFFIX, windows: windows) end + # On windows, C:/opscode + # + # (should only be called in a windows-context) + # + # @return [String] the platform-specific path + # def self.c_opscode_dir(windows: ChefUtils.windows?) drive = windows_installation_drive || "C:" PathHelper.join(drive, ChefConfig::Dist::LEGACY_CONF_DIR, ChefConfig::Dist::DIR_SUFFIX, windows: windows) @@ -107,7 +130,10 @@ module ChefConfig # either by the drive containing the current file or by the SYSTEMDRIVE ENV # variable # + # (should only be called in a windows-context) + # # @return [String] the drive letter + # def self.windows_installation_drive if ChefUtils.windows? drive = File.expand_path(__FILE__).split("/", 2)[0] diff --git a/chef-config/lib/chef-config/path_helper.rb b/chef-config/lib/chef-config/path_helper.rb index d22858485e..9d3db7621d 100644 --- a/chef-config/lib/chef-config/path_helper.rb +++ b/chef-config/lib/chef-config/path_helper.rb @@ -141,6 +141,10 @@ module ChefConfig # within chef/ruby itself is not to care. Only use it to force windows or unix style # when it really matters. # + # @param path [String] the path to clean + # @param windows [Boolean] optional flag to force to windows or unix-style + # @return [String] cleaned path + # def self.cleanpath(path, windows: ChefUtils.windows?) path = Pathname.new(path).cleanpath.to_s if windows diff --git a/chef-config/spec/unit/config_spec.rb b/chef-config/spec/unit/config_spec.rb index c080734ff3..8daee3fa19 100644 --- a/chef-config/spec/unit/config_spec.rb +++ b/chef-config/spec/unit/config_spec.rb @@ -222,13 +222,70 @@ RSpec.describe ChefConfig::Config do ChefConfig::Config.add_formatter(:doc, "/var/log/formatter.log") expect(ChefConfig::Config.formatters).to eq([[:doc, "/var/log/formatter.log"]]) end + end + + describe "#var_chef_path" do + let (:dirname) { ChefConfig::Dist::DIR_SUFFIX } + + context "on unix", :unix_only do + it "var_chef_dir is /var/chef" do + expect(ChefConfig::Config.var_chef_dir).to eql("/var/#{dirname}") + end + + it "var_root_dir is /var" do + expect(ChefConfig::Config.var_root_dir).to eql("/var") + end + + it "etc_chef_dir is /etc/chef" do + expect(ChefConfig::Config.etc_chef_dir).to eql("/etc/#{dirname}") + end + end + + context "on windows", :windows_only do + it "var_chef_dir is C:\\chef" do + expect(ChefConfig::Config.var_chef_dir).to eql("C:\\#{dirname}") + end + + it "var_root_dir is C:\\" do + expect(ChefConfig::Config.var_root_dir).to eql("C:\\") + end + + it "etc_chef_dir is C:\\chef" do + expect(ChefConfig::Config.etc_chef_dir).to eql("C:\\#{dirname}") + end + end + context "when forced to unix" do + it "var_chef_dir is /var/chef" do + expect(ChefConfig::Config.var_chef_dir(windows: false)).to eql("/var/#{dirname}") + end + + it "var_root_dir is /var" do + expect(ChefConfig::Config.var_root_dir(windows: false)).to eql("/var") + end + + it "etc_chef_dir is /etc/chef" do + expect(ChefConfig::Config.etc_chef_dir(windows: false)).to eql("/etc/#{dirname}") + end + end + + context "when forced to windows" do + it "var_chef_dir is C:\\chef" do + expect(ChefConfig::Config.var_chef_dir(windows: true)).to eql("C:\\#{dirname}") + end + + it "var_root_dir is C:\\" do + expect(ChefConfig::Config.var_root_dir(windows: true)).to eql("C:\\") + end + + it "etc_chef_dir is C:\\chef" do + expect(ChefConfig::Config.etc_chef_dir(windows: true)).to eql("C:\\#{dirname}") + end + end end [ false, true ].each do |is_windows| - context "On #{is_windows ? "Windows" : "Unix"}" do - before :each do allow(ChefUtils).to receive(:windows?).and_return(is_windows) end -- cgit v1.2.1