diff options
author | Claire McQuin <claire@getchef.com> | 2014-09-19 11:07:29 -0700 |
---|---|---|
committer | Claire McQuin <claire@getchef.com> | 2014-09-22 11:22:29 -0700 |
commit | 655fc195e18f4bf3802718003ced9bc5ee25f163 (patch) | |
tree | 3317d5cc82a9c01768ff3f95c50217b51d869700 | |
parent | ed907ef4d64027212ceb0c082895b303844ff43f (diff) | |
download | chef-655fc195e18f4bf3802718003ced9bc5ee25f163.tar.gz |
Add method to escape glob reserved characters.
-rw-r--r-- | lib/chef/util/path_helper.rb | 8 | ||||
-rw-r--r-- | spec/functional/util/path_helper_spec.rb | 37 | ||||
-rw-r--r-- | spec/unit/util/path_helper_spec.rb | 10 |
3 files changed, 54 insertions, 1 deletions
diff --git a/lib/chef/util/path_helper.rb b/lib/chef/util/path_helper.rb index e9fb4e7773..9aea2645d7 100644 --- a/lib/chef/util/path_helper.rb +++ b/lib/chef/util/path_helper.rb @@ -133,6 +133,14 @@ class Chef def self.paths_eql?(path1, path2) canonical_path(path1) == canonical_path(path2) end + + # Paths which may contain glob-reserved characters need + # to be escaped before globbing can be done. + # http://stackoverflow.com/questions/14127343 + def self.escape_glob(*parts) + path = join(parts) + path.gsub(/[\\\{\}\[\]\*\?]/) { |x| "\\"+x } + end end end end diff --git a/spec/functional/util/path_helper_spec.rb b/spec/functional/util/path_helper_spec.rb new file mode 100644 index 0000000000..ccdf383c22 --- /dev/null +++ b/spec/functional/util/path_helper_spec.rb @@ -0,0 +1,37 @@ +# +# Copyright:: Copyright (c) 2014 Chef Software, Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require 'tmpdir' +require 'chef/util/path_helper' +require 'spec_helper' + +describe Chef::Util::PathHelper, "escape_glob" do + PathHelper = Chef::Util::PathHelper + + it "escapes the glob metacharacters so globbing succeeds" do + # make a dir + Dir.mktmpdir("\\silly[dir]") do |dir| + # add some files + files = ["some.rb", "file.txt", "names.csv"] + files.each do |file| + File.new(File.join(dir, file), 'w').close + end + + pattern = File.join(PathHelper.escape_glob(dir), "*") + Dir.glob(pattern).map { |x| File.basename(x) }.should match_array(files) + end + end +end diff --git a/spec/unit/util/path_helper_spec.rb b/spec/unit/util/path_helper_spec.rb index 66ad323c52..26bdf3ad91 100644 --- a/spec/unit/util/path_helper_spec.rb +++ b/spec/unit/util/path_helper_spec.rb @@ -214,6 +214,14 @@ describe Chef::Util::PathHelper do PathHelper.stub(:canonical_path).with("bandit").and_return("c:/Bo/Bandit") PathHelper.stub(:canonical_path).with("../bandit/bandit").and_return("c:/bandit/bandit") expect(PathHelper.paths_eql?("bandit", "../bandit/bandit")).to be_false - end + end + end + + describe "escape_glob" do + it "escapes characters reserved by glob" do + path = "C:\\this\\*path\\[needs]\\escaping?" + escaped_path = "C:\\\\this\\\\\\*path\\\\\\[needs\\]\\\\escaping\\?" + expect(PathHelper.escape_glob(path)).to eq(escaped_path) + end end end |