Skip to content

Commit

Permalink
[PathList] Escape glob metacharacters
Browse files Browse the repository at this point in the history
Closes #862
  • Loading branch information
fabiopelosin committed Mar 20, 2013
1 parent 2aa73d6 commit f4df7a6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
22 changes: 21 additions & 1 deletion lib/cocoapods/sandbox/path_list.rb
Expand Up @@ -47,7 +47,8 @@ def read_file_system
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}/.." }
Expand Down Expand Up @@ -174,6 +175,25 @@ def dir_glob_equivalent_patterns(pattern)
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
Expand Down
24 changes: 24 additions & 0 deletions spec/unit/sandbox/path_list_spec.rb
Expand Up @@ -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

#-------------------------------------------------------------------------#
Expand Down Expand Up @@ -122,6 +130,7 @@ module Pod
end
end

#--------------------------------------#

describe "#directory?" do
it "expands a pattern into all the combinations of Dir#glob literals" do
Expand All @@ -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

#-------------------------------------------------------------------------#
Expand Down

1 comment on commit f4df7a6

@alloy
Copy link
Member

@alloy alloy commented on f4df7a6 Mar 20, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wow, good catch! :D

Please sign in to comment.