diff options
author | Lamont Granquist <454857+lamont-granquist@users.noreply.github.com> | 2022-02-15 12:25:02 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-15 12:25:02 -0800 |
commit | 70052630348ab0a4597a1a27b6d6b5ae16cf422d (patch) | |
tree | 0759902ecf600daff6db811d52d43cfdf82b312c /chef-config | |
parent | 1d5166f3e7c68ef8d246c54897f87dc19e526853 (diff) | |
parent | 5b26f94d92700ddffe12b93f0833d3c8fc664393 (diff) | |
download | chef-70052630348ab0a4597a1a27b6d6b5ae16cf422d.tar.gz |
Merge pull request #12581 from neha-p6/avoid_multiple_config_calls
Diffstat (limited to 'chef-config')
-rw-r--r-- | chef-config/lib/chef-config/config.rb | 7 | ||||
-rw-r--r-- | chef-config/spec/unit/config_spec.rb | 8 |
2 files changed, 13 insertions, 2 deletions
diff --git a/chef-config/lib/chef-config/config.rb b/chef-config/lib/chef-config/config.rb index aac18505d3..7d1937ed3a 100644 --- a/chef-config/lib/chef-config/config.rb +++ b/chef-config/lib/chef-config/config.rb @@ -84,8 +84,11 @@ module ChefConfig # @return [String] the platform-specific path # def self.etc_chef_dir(windows: ChefUtils.windows?) - path = windows ? c_chef_dir : PathHelper.join("/etc", ChefUtils::Dist::Infra::DIR_SUFFIX, windows: windows) - PathHelper.cleanpath(path, windows: windows) + @etc_chef_dir ||= {} + @etc_chef_dir[windows] ||= begin + path = windows ? c_chef_dir : PathHelper.join("/etc", ChefUtils::Dist::Infra::DIR_SUFFIX, windows: windows) + PathHelper.cleanpath(path, windows: windows) + end end # On *nix, /var/chef, on Windows C:\chef diff --git a/chef-config/spec/unit/config_spec.rb b/chef-config/spec/unit/config_spec.rb index 658acca615..d4d5844d3d 100644 --- a/chef-config/spec/unit/config_spec.rb +++ b/chef-config/spec/unit/config_spec.rb @@ -282,6 +282,14 @@ RSpec.describe ChefConfig::Config do expect(ChefConfig::Config.etc_chef_dir(windows: true)).to eql("C:\\#{dirname}") end end + + context "when calling etc_chef_dir multiple times" do + it "should not recalculate path for every call" do + expect(ChefConfig::Config.etc_chef_dir(windows: false)).to eql("/etc/#{dirname}") + expect(ChefConfig::PathHelper).not_to receive(:cleanpath) + expect(ChefConfig::Config.etc_chef_dir(windows: false)).to eql("/etc/#{dirname}") + end + end end [ false, true ].each do |is_windows| |