summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Keiser <jkeiser@opscode.com>2014-08-29 12:30:29 -0700
committerAdam Edwards <adamed@opscode.com>2014-08-30 12:31:10 -0700
commit4c54fc8719cb66db3a1314c7f6620e01dfd3462f (patch)
treee6b9ba6471549b14419406a0f14d624699847f99
parentbc3d6871086721d2049bd2e28e8801d0eed176ff (diff)
downloadchef-4c54fc8719cb66db3a1314c7f6620e01dfd3462f.tar.gz
Make join work properly with leading slashes
-rw-r--r--lib/chef/util/path_helper.rb8
-rw-r--r--spec/unit/config_spec.rb2
-rw-r--r--spec/unit/util/path_helper_spec.rb9
3 files changed, 12 insertions, 7 deletions
diff --git a/lib/chef/util/path_helper.rb b/lib/chef/util/path_helper.rb
index 1877667b9a..e9fb4e7773 100644
--- a/lib/chef/util/path_helper.rb
+++ b/lib/chef/util/path_helper.rb
@@ -53,10 +53,10 @@ class Chef
def self.join(*args)
args.flatten.inject do |joined_path, component|
- unless [ File::SEPARATOR, path_separator ].include?(joined_path[-1])
- joined_path += path_separator
- end
- joined_path += component
+ # Joined path ends with /
+ joined_path = joined_path.sub(/[#{Regexp.escape(File::SEPARATOR)}#{Regexp.escape(path_separator)}]+$/, '')
+ component = component.sub(/^[#{Regexp.escape(File::SEPARATOR)}#{Regexp.escape(path_separator)}]+/, '')
+ joined_path += "#{path_separator}#{component}"
end
end
diff --git a/spec/unit/config_spec.rb b/spec/unit/config_spec.rb
index ecc2173af5..af71c43b77 100644
--- a/spec/unit/config_spec.rb
+++ b/spec/unit/config_spec.rb
@@ -21,7 +21,7 @@ require 'spec_helper'
require 'chef/exceptions'
require 'chef/util/path_helper'
-describe Chef::Config, :focus do
+describe Chef::Config do
describe "config attribute writer: chef_server_url" do
before do
Chef::Config.chef_server_url = "https://junglist.gen.nz"
diff --git a/spec/unit/util/path_helper_spec.rb b/spec/unit/util/path_helper_spec.rb
index eb89aa083a..57774c5b53 100644
--- a/spec/unit/util/path_helper_spec.rb
+++ b/spec/unit/util/path_helper_spec.rb
@@ -35,6 +35,12 @@ describe Chef::Util::PathHelper do
PathHelper.join(is_windows ? 'C:\\foo\\' : "/foo/", "bar", "baz").should == expected
end
+ it "joins components when some end and start with separators" do
+ expected = PathHelper.cleanpath("/foo/bar/baz")
+ expected = "C:#{expected}" if is_windows
+ PathHelper.join(is_windows ? 'C:\\foo\\' : "/foo/", "bar/", "/baz").should == expected
+ end
+
it "joins components that don't end in separators" do
expected = PathHelper.cleanpath("/foo/bar/baz")
expected = "C:#{expected}" if is_windows
@@ -51,8 +57,7 @@ describe Chef::Util::PathHelper do
if is_windows
it "joins components on Windows when some end with unix separators" do
- expected = 'C:\\foo/bar\\baz'
- PathHelper.join('C:\\foo/', "bar", "baz").should == expected
+ PathHelper.join('C:\\foo/', "bar", "baz").should == 'C:\\foo\\bar\\baz'
end
end
end