summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLamont Granquist <lamont@chef.io>2020-07-23 19:23:45 -0700
committerGitHub <noreply@github.com>2020-07-23 19:23:45 -0700
commite40d253527d2ccd58f7a05a9024e872211d7ed8b (patch)
tree396d8c545a8ef0817f4043c06bdc270c22d2cdda
parent64c833e63c845144dcf5b1bd484f1c06994f36c0 (diff)
parentdbb1c4534bd504c11e53fb19681889b959c86034 (diff)
downloadchef-e40d253527d2ccd58f7a05a9024e872211d7ed8b.tar.gz
Merge pull request #10200 from chef/lcg/path-helper-allocations
-rw-r--r--chef-config/lib/chef-config/path_helper.rb15
-rw-r--r--chef-config/spec/unit/path_helper_spec.rb4
2 files changed, 8 insertions, 11 deletions
diff --git a/chef-config/lib/chef-config/path_helper.rb b/chef-config/lib/chef-config/path_helper.rb
index 570e666e10..19e7cce26a 100644
--- a/chef-config/lib/chef-config/path_helper.rb
+++ b/chef-config/lib/chef-config/path_helper.rb
@@ -55,18 +55,15 @@ module ChefConfig
end
end
- def self.join(*args)
- path_separator_regex = Regexp.escape(File::SEPARATOR)
- unless path_separator == File::SEPARATOR
- path_separator_regex << Regexp.escape(path_separator)
- end
+ path_separator_regex = [Regexp.escape(File::SEPARATOR), Regexp.escape(path_separator)].uniq.join
- trailing_slashes = /[#{path_separator_regex}]+$/
- leading_slashes = /^[#{path_separator_regex}]+/
+ TRAILING_SLASHES_REGEX = /[#{path_separator_regex}]+$/.freeze
+ LEADING_SLASHES_REGEX = /^[#{path_separator_regex}]+/.freeze
+ def self.join(*args)
args.flatten.inject do |joined_path, component|
- joined_path = joined_path.sub(trailing_slashes, "")
- component = component.sub(leading_slashes, "")
+ joined_path = joined_path.sub(TRAILING_SLASHES_REGEX, "")
+ component = component.sub(LEADING_SLASHES_REGEX, "")
joined_path + "#{path_separator}#{component}"
end
end
diff --git a/chef-config/spec/unit/path_helper_spec.rb b/chef-config/spec/unit/path_helper_spec.rb
index aa670577d4..f416bb1826 100644
--- a/chef-config/spec/unit/path_helper_spec.rb
+++ b/chef-config/spec/unit/path_helper_spec.rb
@@ -55,7 +55,7 @@ RSpec.describe ChefConfig::PathHelper do
end
end
- context "on windows" do
+ context "on windows", :windows_only do
before(:each) do
allow(ChefUtils).to receive(:windows?).and_return(true)
@@ -103,7 +103,7 @@ RSpec.describe ChefConfig::PathHelper do
end
- context "on unix" do
+ context "on unix", :unix_only do
before(:each) do
allow(ChefUtils).to receive(:windows?).and_return(false)