Skip to content
This repository

Escape Dir glob characters in paths #231

Merged
merged 1 commit into from almost 2 years ago

2 participants

Andrew White Erik Michaels-Ober
Andrew White
Collaborator

If you have glob characters in your projects path then they need to be escaped before passing them to Dir[].

Erik Michaels-Ober sferik merged commit 2373c1f into from
Erik Michaels-Ober sferik closed this
Andrew White pixeltrix deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

May 14, 2012
Andrew White Escape Dir glob characters in paths 3b75643
This page is out of date. Refresh to see the latest.
3  lib/thor/actions/directory.rb
@@ -71,7 +71,8 @@ def revoke!
71 71
       protected
72 72
 
73 73
         def execute!
74  
-          lookup = config[:recursive] ? File.join(source, '**') : source
  74
+          lookup = Util.escape_globs(source)
  75
+          lookup = config[:recursive] ? File.join(lookup, '**') : lookup
75 76
           lookup = File.join(lookup, '{*,.[a-z]*}')
76 77
 
77 78
           Dir[lookup].sort.each do |file_source|
20  lib/thor/util.rb
@@ -198,7 +198,7 @@ def self.thor_root
198 198
     # If we don't #gsub the \ character, Dir.glob will fail.
199 199
     #
200 200
     def self.thor_root_glob
201  
-      files = Dir["#{thor_root}/*"]
  201
+      files = Dir["#{escape_globs(thor_root)}/*"]
202 202
 
203 203
       files.map! do |file|
204 204
         File.directory?(file) ? File.join(file, "main.thor") : file
@@ -208,6 +208,7 @@ def self.thor_root_glob
208 208
     # Where to look for Thor files.
209 209
     #
210 210
     def self.globs_for(path)
  211
+      path = escape_globs(path)
211 212
       ["#{path}/Thorfile", "#{path}/*.thor", "#{path}/tasks/*.thor", "#{path}/lib/tasks/*.thor"]
212 213
     end
213 214
 
@@ -244,5 +245,22 @@ def self.ruby_command
244 245
       end
245 246
     end
246 247
 
  248
+    # Returns a string that has had any glob characters escaped.
  249
+    # The glob characters are `* ? { } [ ]`.
  250
+    #
  251
+    # ==== Examples
  252
+    #
  253
+    #   Thor::Util.escape_globs('[apps]')   # => '\[apps\]'
  254
+    #
  255
+    # ==== Parameters
  256
+    # String
  257
+    #
  258
+    # ==== Returns
  259
+    # String
  260
+    #
  261
+    def self.escape_globs(path)
  262
+      path.to_s.gsub(/[*?{}\[\]]/, '\\\\\\&')
  263
+    end
  264
+
247 265
   end
248 266
 end
13  spec/actions/directory_spec.rb
@@ -121,6 +121,11 @@ def exists_and_identical?(source_path, destination_path)
121 121
       end
122 122
       checked.should be_true
123 123
     end
  124
+
  125
+    it "works with glob characters in the path" do
  126
+      content = invoke!("app{1}")
  127
+      content.should =~ /create  app\{1\}\/README/
  128
+    end
124 129
   end
125 130
 
126 131
   describe "#revoke!" do
@@ -132,5 +137,13 @@ def exists_and_identical?(source_path, destination_path)
132 137
       File.exists?(File.join(destination_root, "doc", "config.rb")).should be_false
133 138
       File.exists?(File.join(destination_root, "doc", "components")).should be_false
134 139
     end
  140
+
  141
+    it "works with glob characters in the path" do
  142
+      invoke! "app{1}"
  143
+      File.exists?(File.join(destination_root, "app{1}", "README")).should be_true
  144
+
  145
+      revoke! "app{1}"
  146
+      File.exists?(File.join(destination_root, "app{1}", "README")).should be_false
  147
+    end
135 148
   end
136 149
 end
3  spec/fixtures/app{1}/README
... ...
@@ -0,0 +1,3 @@
  1
+__start__
  2
+README
  3
+__end__
33  spec/util_spec.rb
@@ -160,4 +160,37 @@ def self.clear_user_home!
160 160
       Thor::Util.user_home.should == "/home/user/"
161 161
     end
162 162
   end
  163
+
  164
+  describe "#thor_root_glob" do
  165
+    before do
  166
+      ENV.stub!(:[])
  167
+      Thor::Util.clear_user_home!
  168
+    end
  169
+
  170
+    it "escapes globs in path" do
  171
+      ENV.stub!(:[]).with("HOME").and_return("/home/user{1}/")
  172
+      Dir.should_receive(:[]).with("/home/user\\{1\\}/.thor/*").and_return([])
  173
+      Thor::Util.thor_root_glob.should == []
  174
+    end
  175
+  end
  176
+
  177
+  describe "#globs_for" do
  178
+    it "escapes globs in path" do
  179
+      Thor::Util.globs_for("/home/apps{1}").should == [
  180
+        "/home/apps\\{1\\}/Thorfile",
  181
+        "/home/apps\\{1\\}/*.thor",
  182
+        "/home/apps\\{1\\}/tasks/*.thor",
  183
+        "/home/apps\\{1\\}/lib/tasks/*.thor"
  184
+      ]
  185
+    end
  186
+  end
  187
+
  188
+  describe "#escape_globs" do
  189
+    it "escapes ? * { } [ ] glob characters" do
  190
+      Thor::Util.escape_globs("apps?").should == "apps\\?"
  191
+      Thor::Util.escape_globs("apps*").should == "apps\\*"
  192
+      Thor::Util.escape_globs("apps {1}").should == "apps \\{1\\}"
  193
+      Thor::Util.escape_globs("apps [1]").should == "apps \\[1\\]"
  194
+    end
  195
+  end
163 196
 end
0  thor/core_ext/dir_escape.rb b/lib/thor/core_ext/dir_escape.rb
No changes.
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.