diff options
author | Claire McQuin <claire@getchef.com> | 2014-09-18 15:12:30 -0700 |
---|---|---|
committer | Claire McQuin <claire@getchef.com> | 2014-09-18 15:40:45 -0700 |
commit | a5e0ab87656144943e27cde0cc1c9344b2d446f2 (patch) | |
tree | b451b48d6430a78b70fcd7fc138b7d9e67666037 | |
parent | e60d16d1cb9378d8927207080494736c4c3609c2 (diff) | |
download | chef-a5e0ab87656144943e27cde0cc1c9344b2d446f2.tar.gz |
Add functionality to escape characters reserved by glob
-rw-r--r-- | lib/chef/util/path_helper.rb | 8 | ||||
-rw-r--r-- | spec/unit/util/path_helper_spec.rb | 7 |
2 files changed, 15 insertions, 0 deletions
diff --git a/lib/chef/util/path_helper.rb b/lib/chef/util/path_helper.rb index e9fb4e7773..55457796d9 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 + + # Escape path characters that are reserved by Dir.glob + # and Dir[] + # http://stackoverflow.com/questions/14127343 + def self.glob(pattern, *flags) + globsafe_pattern = pattern.gsub(/[\\]/) { |x| "\\"+x } + Dir.glob(globsafe_pattern, *flags) + end end end end diff --git a/spec/unit/util/path_helper_spec.rb b/spec/unit/util/path_helper_spec.rb index 66ad323c52..97a67bca11 100644 --- a/spec/unit/util/path_helper_spec.rb +++ b/spec/unit/util/path_helper_spec.rb @@ -216,4 +216,11 @@ describe Chef::Util::PathHelper do expect(PathHelper.paths_eql?("bandit", "../bandit/bandit")).to be_false end end + + describe "glob" do + it "escapes backslash characters in glob pattern" do + expect(Dir).to receive(:glob).with("C:\\\\foo") + PathHelper.glob("C:\\foo") + end + end end |