Commit f4df7a61 authored by Fabio Pelosin's avatar Fabio Pelosin

[PathList] Escape glob metacharacters

Closes #862
parent 2aa73d65
......@@ -47,7 +47,8 @@ module Pod
raise Informative, "Attempt to read non existent folder `#{root}`."
end
root_length = root.to_s.length+1
paths = Dir.glob(root + "**/*", File::FNM_DOTMATCH)
escaped_root = escape_path_for_glob(root)
paths = Dir.glob(escaped_root + "**/*", File::FNM_DOTMATCH)
absolute_dirs = paths.select { |path| File.directory?(path) }
relative_dirs = absolute_dirs.map { |p| p[root_length..-1] }
absolute_paths = paths.reject { |p| p == "#{root}/." || p == "#{root}/.." }
......@@ -174,6 +175,25 @@ module Pod
end
end
# Escapes the glob metacharacters from a given path so it can used in
# Dir#glob and similar methods.
#
# @note See CocoaPods/CocoaPods#862.
#
# @param [String, Pathname] path
# The path to escape.
#
# @return [Pathname] The escaped path.
#
def escape_path_for_glob(path)
result = path.to_s
characters_to_escape = ['[', ']', '{', '}', '?', '*']
characters_to_escape.each do |character|
result.gsub!(character, "\\#{character}" )
end
Pathname.new(result)
end
#-----------------------------------------------------------------------#
end
......
......@@ -38,6 +38,14 @@ module Pod
dirs.sort.should == %w| Classes Resources Resources/sub_dir sub-dir sub-dir/sub-dir-2 |
end
it "handles directories with glob metacharacters" do
root = temporary_directory + '[CP] Test'
root.mkpath
FileUtils.touch(root + 'Class.h')
@path_list = Sandbox::PathList.new(root)
@path_list.files.should == ["Class.h"]
end
end
#-------------------------------------------------------------------------#
......@@ -122,6 +130,7 @@ module Pod
end
end
#--------------------------------------#
describe "#directory?" do
it "expands a pattern into all the combinations of Dir#glob literals" do
......@@ -148,7 +157,22 @@ module Pod
Classes/file.m
]
end
end
#--------------------------------------#
describe "#escape_path_for_glob" do
it "escapes metacharacters" do
escaped = @path_list.send(:escape_path_for_glob, '[]{}?**')
escaped.to_s.should == '\[\]\{\}\?\*\*'
end
end
#--------------------------------------#
end
#-------------------------------------------------------------------------#
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment