Permalink
Browse files

Escape Dir glob characters in paths

  • Loading branch information...
1 parent c0b8401 commit 3b7564311f9c8c73e204d95279f9cb0c80594f67 @pixeltrix pixeltrix committed May 14, 2012
View
3 lib/thor/actions/directory.rb
@@ -71,7 +71,8 @@ def revoke!
protected
def execute!
- lookup = config[:recursive] ? File.join(source, '**') : source
+ lookup = Util.escape_globs(source)
+ lookup = config[:recursive] ? File.join(lookup, '**') : lookup
lookup = File.join(lookup, '{*,.[a-z]*}')
Dir[lookup].sort.each do |file_source|
View
0 lib/thor/core_ext/dir_escape.rb
No changes.
View
20 lib/thor/util.rb
@@ -198,7 +198,7 @@ def self.thor_root
# If we don't #gsub the \ character, Dir.glob will fail.
#
def self.thor_root_glob
- files = Dir["#{thor_root}/*"]
+ files = Dir["#{escape_globs(thor_root)}/*"]
files.map! do |file|
File.directory?(file) ? File.join(file, "main.thor") : file
@@ -208,6 +208,7 @@ def self.thor_root_glob
# Where to look for Thor files.
#
def self.globs_for(path)
+ path = escape_globs(path)
["#{path}/Thorfile", "#{path}/*.thor", "#{path}/tasks/*.thor", "#{path}/lib/tasks/*.thor"]
end
@@ -244,5 +245,22 @@ def self.ruby_command
end
end
+ # Returns a string that has had any glob characters escaped.
+ # The glob characters are `* ? { } [ ]`.
+ #
+ # ==== Examples
+ #
+ # Thor::Util.escape_globs('[apps]') # => '\[apps\]'
+ #
+ # ==== Parameters
+ # String
+ #
+ # ==== Returns
+ # String
+ #
+ def self.escape_globs(path)
+ path.to_s.gsub(/[*?{}\[\]]/, '\\\\\\&')
+ end
+
end
end
View
13 spec/actions/directory_spec.rb
@@ -121,6 +121,11 @@ def exists_and_identical?(source_path, destination_path)
end
checked.should be_true
end
+
+ it "works with glob characters in the path" do
+ content = invoke!("app{1}")
+ content.should =~ /create app\{1\}\/README/
+ end
end
describe "#revoke!" do
@@ -132,5 +137,13 @@ def exists_and_identical?(source_path, destination_path)
File.exists?(File.join(destination_root, "doc", "config.rb")).should be_false
File.exists?(File.join(destination_root, "doc", "components")).should be_false
end
+
+ it "works with glob characters in the path" do
+ invoke! "app{1}"
+ File.exists?(File.join(destination_root, "app{1}", "README")).should be_true
+
+ revoke! "app{1}"
+ File.exists?(File.join(destination_root, "app{1}", "README")).should be_false
+ end
end
end
View
3 spec/fixtures/app{1}/README
@@ -0,0 +1,3 @@
+__start__
+README
+__end__
View
33 spec/util_spec.rb
@@ -160,4 +160,37 @@ def self.clear_user_home!
Thor::Util.user_home.should == "/home/user/"
end
end
+
+ describe "#thor_root_glob" do
+ before do
+ ENV.stub!(:[])
+ Thor::Util.clear_user_home!
+ end
+
+ it "escapes globs in path" do
+ ENV.stub!(:[]).with("HOME").and_return("/home/user{1}/")
+ Dir.should_receive(:[]).with("/home/user\\{1\\}/.thor/*").and_return([])
+ Thor::Util.thor_root_glob.should == []
+ end
+ end
+
+ describe "#globs_for" do
+ it "escapes globs in path" do
+ Thor::Util.globs_for("/home/apps{1}").should == [
+ "/home/apps\\{1\\}/Thorfile",
+ "/home/apps\\{1\\}/*.thor",
+ "/home/apps\\{1\\}/tasks/*.thor",
+ "/home/apps\\{1\\}/lib/tasks/*.thor"
+ ]
+ end
+ end
+
+ describe "#escape_globs" do
+ it "escapes ? * { } [ ] glob characters" do
+ Thor::Util.escape_globs("apps?").should == "apps\\?"
+ Thor::Util.escape_globs("apps*").should == "apps\\*"
+ Thor::Util.escape_globs("apps {1}").should == "apps \\{1\\}"
+ Thor::Util.escape_globs("apps [1]").should == "apps \\[1\\]"
+ end
+ end
end

0 comments on commit 3b75643

Please sign in to comment.