Browse files

Specs for File.fnmatch handling of Regexp specials.

  • Loading branch information...
1 parent 7c0dc7e commit 55aa5a1f10655618e45d0ec84502cc13c982227e @drbrain drbrain committed Jan 12, 2008
View
2 .gitignore
@@ -14,7 +14,7 @@ tmtags
*.rbc
.spec/bm.yml
spec/fixtures/load/load_spec_rba.rba
-spec/core/dir/fixtures/mock
+spec/ruby/1.8/core/dir/fixtures/mock
tmp
scratch
!runtime/stable/loader.rbc
View
20 kernel/core/file.rb
@@ -323,18 +323,24 @@ def self.basename(path,ext = "")
def self.expand_path(path, dir_string = nil)
Platform::File.expand_path(path, dir_string)
end
-
+
def self.fnmatch(pattern, path, flags=0)
pattern = StringValue(pattern).dup
path = StringValue(path).dup
escape = (flags & FNM_NOESCAPE) == 0
pathname = (flags & FNM_PATHNAME) != 0
nocase = (flags & FNM_CASEFOLD) != 0
period = (flags & FNM_DOTMATCH) == 0
- subs = { /\{/ => '\{', /\}/ => '\}',
- /(^|([^\\]))\*{1,2}/ => '\1(.*)', /\\\*/ => '\\\\\*',
- /(^|([^\\]))\?/ => '\1(.)', /\\\?/ => '\\\\\?' }
-
+ subs = { /\{/ => '\{',
+ /\}/ => '\}',
+ /(^|([^\\]))\*{1,2}/ => '\1(.*)',
+ /(^|([^\\]))\?/ => '\1(.)',
+ /\\\?/ => '\\\\\?',
+ /\\\*/ => '\\\\\*',
+ # HACK /\+/ => '\\\+',
+ /(\+)/ => '\\\\\\\\\+', # HACK String#sub is broken
+ }
+
return false if path[0] == ?. and pattern[0] != ?. and period
pattern.gsub!('.', '\.')
pattern = pattern.split(/(?<b>\[(?:\\[\[\]]|[^\[\]]|\g<b>)*\])/).collect do |part|
@@ -354,10 +360,6 @@ def self.fnmatch(pattern, path, flags=0)
end
end.join
- pattern = pattern.gsub(/\{(.*?)\}/) do
- "(#{$1.gsub ',', '|'})"
- end
-
re = Regexp.new("^#{pattern}$", nocase ? Regexp::IGNORECASE : 0)
m = re.match path
View
23 spec/ruby/1.8/core/dir/fixtures/common.rb
@@ -25,6 +25,7 @@ def DirSpecs.create_mock_dirs
files = %W[ #{stem}/mock/.dotfile
#{stem}/mock/.dotsubdir/.dotfile
#{stem}/mock/.dotsubdir/nondotfile
+
#{stem}/mock/deeply/.dotfile
#{stem}/mock/deeply/nested/.dotfile.ext
#{stem}/mock/deeply/nested/directory/structure/.ext
@@ -34,13 +35,33 @@ def DirSpecs.create_mock_dirs
#{stem}/mock/deeply/nested/directory/structure/file_one.ext
#{stem}/mock/deeply/nested/directory/structure/foo
#{stem}/mock/deeply/nondotfile
+
#{stem}/mock/file_one.ext
#{stem}/mock/file_two.ext
+
#{stem}/mock/nondotfile
+
#{stem}/mock/subdir_one/.dotfile
#{stem}/mock/subdir_one/nondotfile
#{stem}/mock/subdir_two/nondotfile
- #{stem}/mock/subdir_two/nondotfile.ext]
+ #{stem}/mock/subdir_two/nondotfile.ext
+
+ #{stem}/mock/special/+
+ #{stem}/mock/special/*
+ #{stem}/mock/special/?
+
+ #{stem}/mock/special/|
+
+ #{stem}/mock/special/^
+ #{stem}/mock/special/$
+
+ #{stem}/mock/special/(
+ #{stem}/mock/special/)
+ #{stem}/mock/special/[
+ #{stem}/mock/special/]
+ #{stem}/mock/special/{
+ #{stem}/mock/special/}
+ ]
FileUtils.rm_rf "#{stem}/mock"
files.each do |file|
View
35 spec/ruby/1.8/core/dir/glob_spec.rb
@@ -7,26 +7,49 @@
before(:all) do
@cwd = Dir.pwd
+
Dir.chdir DirSpecs.mock_dir
+
+ @all_dirs = %w[
+ .
+ ..
+ .dotfile
+ .dotsubdir
+ deeply
+ file_one.ext
+ file_two.ext
+ nondotfile
+ special
+ subdir_one
+ subdir_two
+ ]
end
it "matches both dot and non-dotfiles with '*' and option File::FNM_DOTMATCH" do
- Dir.glob('*', File::FNM_DOTMATCH).sort.should ==
- %w|. .. .dotfile .dotsubdir subdir_one subdir_two deeply nondotfile file_one.ext file_two.ext|.sort
+ Dir.glob('*', File::FNM_DOTMATCH).sort.should == @all_dirs
end
it "matches files with any beginning with '*<non-special characters>' and option File::FNM_DOTMATCH" do
Dir.glob('*file', File::FNM_DOTMATCH).sort.should == %w|.dotfile nondotfile|.sort
end
it "matches any files in the current directory with '**' and option File::FNM_DOTMATCH" do
- Dir.glob('**', File::FNM_DOTMATCH).sort.should == %w|. .. .dotsubdir .dotfile subdir_one subdir_two
- deeply nondotfile file_one.ext file_two.ext|.sort
+ Dir.glob('**', File::FNM_DOTMATCH).sort.should == @all_dirs
end
it "recursively matches any subdirectories except './' or '../' with '**/' and option File::FNM_DOTMATCH" do
- Dir.glob('**/', File::FNM_DOTMATCH).sort.should == %w|.dotsubdir/ subdir_one/ subdir_two/ deeply/ deeply/nested/
- deeply/nested/directory/ deeply/nested/directory/structure/|.sort
+ expected = %w[
+ .dotsubdir/
+ deeply/
+ deeply/nested/
+ deeply/nested/directory/
+ deeply/nested/directory/structure/
+ special/
+ subdir_one/
+ subdir_two/
+ ]
+
+ Dir.glob('**/', File::FNM_DOTMATCH).sort.should == expected
end
it "matches the literal character '\\' with option File::FNM_NOESCAPE" do
View
103 spec/ruby/1.8/core/dir/shared/glob.rb
@@ -7,8 +7,65 @@
end
it "matches non-dotfiles with '*'" do
- Dir.send(cmd,'*').sort.should ==
- %w|subdir_one subdir_two deeply nondotfile file_one.ext file_two.ext|.sort
+ expected = %w[
+ deeply
+ file_one.ext
+ file_two.ext
+ nondotfile
+ special
+ subdir_one
+ subdir_two
+ ]
+
+ Dir.send(cmd,'*').sort.should == expected
+ end
+
+ it "matches regexp special +" do
+ Dir.send(cmd, 'special/+').should == ['special/+']
+ end
+
+ it "matches regexp special *" do
+ Dir.send(cmd, 'special/\*').should == ['special/*']
+ end
+
+ it "matches regexp special ?" do
+ Dir.send(cmd, 'special/\?').should == ['special/?']
+ end
+
+ it "matches regexp special |" do
+ Dir.send(cmd, 'special/|').should == ['special/|']
+ end
+
+ it "matches regexp special ^" do
+ Dir.send(cmd, 'special/^').should == ['special/^']
+ end
+
+ it "matches regexp special $" do
+ Dir.send(cmd, 'special/$').should == ['special/$']
+ end
+
+ it "matches regexp special (" do
+ Dir.send(cmd, 'special/(').should == ['special/(']
+ end
+
+ it "matches regexp special )" do
+ Dir.send(cmd, 'special/)').should == ['special/)']
+ end
+
+ it "matches regexp special [" do
+ Dir.send(cmd, 'special/\[').should == ['special/[']
+ end
+
+ it "matches regexp special ]" do
+ Dir.send(cmd, 'special/]').should == ['special/]']
+ end
+
+ it "matches regexp special {" do
+ Dir.send(cmd, 'special/\{').should == ['special/{']
+ end
+
+ it "matches regexp special }" do
+ Dir.send(cmd, 'special/\}').should == ['special/}']
end
it "matches dotfiles with '.*'" do
@@ -36,16 +93,35 @@
end
it "matches non-dotfiles in the current directory with '**'" do
- Dir.send(cmd, '**').sort.should == %w|subdir_one subdir_two deeply nondotfile file_one.ext file_two.ext|.sort
+ expected = %w[
+ deeply
+ file_one.ext
+ file_two.ext
+ nondotfile
+ special
+ subdir_one
+ subdir_two
+ ]
+
+ Dir.send(cmd, '**').sort.should == expected
end
it "matches dotfiles in the current directory with '.**'" do
Dir.send(cmd, '.**').sort.should == %w|. .. .dotsubdir .dotfile|.sort
end
it "recursively matches any nondot subdirectories with '**/'" do
- Dir.send(cmd, '**/').sort.should == %w|subdir_one/ subdir_two/ deeply/ deeply/nested/
- deeply/nested/directory/ deeply/nested/directory/structure/|.sort
+ expected = %w[
+ deeply/
+ deeply/nested/
+ deeply/nested/directory/
+ deeply/nested/directory/structure/
+ special/
+ subdir_one/
+ subdir_two/
+ ]
+
+ Dir.send(cmd, '**/').sort.should == expected
end
it "recursively matches any subdirectories including ./ and ../ with '.**/'" do
@@ -89,8 +165,21 @@
end
it "matches dot or non-dotfiles with '{,.}*'" do
- Dir.send(cmd, '{,.}*').sort.should == %w|. .. .dotsubdir subdir_one subdir_two deeply
- .dotfile nondotfile file_one.ext file_two.ext|.sort
+ expected = %w[
+ .
+ ..
+ .dotfile
+ .dotsubdir
+ deeply
+ file_one.ext
+ file_two.ext
+ nondotfile
+ special
+ subdir_one
+ subdir_two
+ ]
+
+ Dir.send(cmd, '{,.}*').sort.should == expected
end
it "matches special characters by escaping with a backslash with '\\<character>'" do

0 comments on commit 55aa5a1

Please sign in to comment.