diff options
Diffstat (limited to 'spec/ruby/core/dir/shared/glob.rb')
| -rw-r--r-- | spec/ruby/core/dir/shared/glob.rb | 182 |
1 files changed, 104 insertions, 78 deletions
diff --git a/spec/ruby/core/dir/shared/glob.rb b/spec/ruby/core/dir/shared/glob.rb index b47e23b41c..86aa105259 100644 --- a/spec/ruby/core/dir/shared/glob.rb +++ b/spec/ruby/core/dir/shared/glob.rb @@ -12,8 +12,8 @@ describe :dir_glob, shared: true do end it "raises an Encoding::CompatibilityError if the argument encoding is not compatible with US-ASCII" do - pattern = "file*".force_encoding Encoding::UTF_16BE - -> { Dir.send(@method, pattern) }.should raise_error(Encoding::CompatibilityError) + pattern = "files*".dup.force_encoding Encoding::UTF_16BE + -> { Dir.send(@method, pattern) }.should.raise(Encoding::CompatibilityError) end it "calls #to_path to convert a pattern" do @@ -23,26 +23,29 @@ describe :dir_glob, shared: true do Dir.send(@method, obj).should == %w[file_one.ext] end - ruby_version_is ""..."2.6" do - it "splits the string on \\0 if there is only one string given" do - Dir.send(@method, "file_o*\0file_t*").should == - %w!file_one.ext file_two.ext! - end + it "raises an ArgumentError if the string contains \\0" do + -> {Dir.send(@method, "file_o*\0file_t*")}.should.raise ArgumentError, /nul-separated/ end - ruby_version_is "2.6"..."2.7" do - it "splits the string on \\0 if there is only one string given and warns" do - -> { - Dir.send(@method, "file_o*\0file_t*").should == - %w!file_one.ext file_two.ext! - }.should complain(/warning: use glob patterns list instead of nul-separated patterns/) - end + it "result is sorted by default" do + result = Dir.send(@method, '*') + result.should == result.sort end - ruby_version_is "2.7" do - it "raises an ArgumentError if the string contains \\0" do - -> {Dir.send(@method, "file_o*\0file_t*")}.should raise_error ArgumentError, /nul-separated/ - end + it "result is sorted with sort: true" do + result = Dir.send(@method, '*', sort: true) + result.should == result.sort + end + + it "sort: false returns same files" do + result = Dir.send(@method,'*', sort: false) + result.sort.should == Dir.send(@method, '*').sort + end + + it "raises an ArgumentError if sort: is not true or false" do + -> { Dir.send(@method, '*', sort: 0) }.should.raise ArgumentError, /expected true or false/ + -> { Dir.send(@method, '*', sort: nil) }.should.raise ArgumentError, /expected true or false/ + -> { Dir.send(@method, '*', sort: 'false') }.should.raise ArgumentError, /expected true or false/ end it "matches non-dotfiles with '*'" do @@ -53,6 +56,7 @@ describe :dir_glob, shared: true do dir_filename_ordering file_one.ext file_two.ext + nested nondotfile special subdir_one @@ -70,6 +74,10 @@ describe :dir_glob, shared: true do Dir.send(@method, 'special/+').should == ['special/+'] end + it "matches directories with special characters when escaped" do + Dir.send(@method, 'special/\{}/special').should == ["special/{}/special"] + end + platform_is_not :windows do it "matches regexp special *" do Dir.send(@method, 'special/\*').should == ['special/*'] @@ -82,6 +90,14 @@ describe :dir_glob, shared: true do it "matches regexp special |" do Dir.send(@method, 'special/|').should == ['special/|'] end + + it "matches files with backslashes in their name" do + Dir.glob('special/\\\\{a,b}').should == ['special/\a'] + end + + it "matches directory with special characters in their name in complex patterns" do + Dir.glob("special/test +()\\[\\]\\{\\}/hello_world{.{en},}{.{html},}{+{phone},}{.{erb},}").should == ['special/test +()[]{}/hello_world.erb'] + end end it "matches regexp special ^" do @@ -120,8 +136,8 @@ describe :dir_glob, shared: true do Dir.send(@method, 'special/test\{1\}/*').should == ['special/test{1}/file[1]'] end - it "matches dotfiles with '.*'" do - Dir.send(@method, '.*').sort.should == %w|. .. .dotfile .dotsubdir|.sort + it "matches dotfiles except .. with '.*'" do + Dir.send(@method, '.*').sort.should == %w|. .dotfile .dotsubdir|.sort end it "matches non-dotfiles with '*<non-special characters>'" do @@ -156,6 +172,7 @@ describe :dir_glob, shared: true do dir_filename_ordering file_one.ext file_two.ext + nested nondotfile special subdir_one @@ -165,8 +182,8 @@ describe :dir_glob, shared: true do Dir.send(@method, '**').sort.should == expected end - it "matches dotfiles in the current directory with '.**'" do - Dir.send(@method, '.**').sort.should == %w|. .. .dotsubdir .dotfile|.sort + it "matches dotfiles in the current directory except .. with '.**'" do + Dir.send(@method, '.**').sort.should == %w|. .dotsubdir .dotfile|.sort end it "recursively matches any nondot subdirectories with '**/'" do @@ -177,8 +194,11 @@ describe :dir_glob, shared: true do deeply/nested/directory/ deeply/nested/directory/structure/ dir/ + nested/ special/ + special/test\ +()[]{}/ special/test{1}/ + special/{}/ subdir_one/ subdir_two/ ] @@ -186,9 +206,17 @@ describe :dir_glob, shared: true do Dir.send(@method, '**/').sort.should == expected end - it "recursively matches any subdirectories including ./ and ../ with '.**/'" do + it "recursively matches any subdirectories except './' or '../' with '**/' from the base directory if that is specified" do + expected = %w[ + nested/directory + ] + + Dir.send(@method, '**/*ory', base: 'deeply').sort.should == expected + end + + it "recursively matches any subdirectories including ./ with '.**/'" do Dir.chdir("#{DirSpecs.mock_dir}/subdir_one") do - Dir.send(@method, '.**/').sort.should == %w|./ ../|.sort + Dir.send(@method, '.**/').should == ['./'] end end @@ -231,7 +259,7 @@ describe :dir_glob, shared: true do end it "matches dot or non-dotfiles with '{,.}*'" do - Dir.send(@method, '{,.}*').sort.should == DirSpecs.expected_paths + Dir.send(@method, '{,.}*').sort.should == DirSpecs.expected_glob_paths end it "respects the order of {} expressions, expanding left most first" do @@ -291,74 +319,72 @@ describe :dir_glob, shared: true do end end - ruby_version_is "2.5" do - context ":base option passed" do - before :each do - @mock_dir = File.expand_path tmp('dir_glob_mock') - - %w[ - a/b/x - a/b/c/y - a/b/c/d/z - ].each do |path| - file = File.join @mock_dir, path - mkdir_p File.dirname(file) - touch file - end - end + context ":base option passed" do + before :each do + @mock_dir = File.expand_path tmp('dir_glob_mock') - after :each do - rm_r @mock_dir + %w[ + a/b/x + a/b/c/y + a/b/c/d/z + ].each do |path| + file = File.join @mock_dir, path + mkdir_p File.dirname(file) + touch file end + end - it "matches entries only from within the specified directory" do - path = File.join(@mock_dir, "a/b/c") - Dir.send(@method, "*", base: path).sort.should == %w( d y ) - end + after :each do + rm_r @mock_dir + end - it "accepts both relative and absolute paths" do - require 'pathname' + it "matches entries only from within the specified directory" do + path = File.join(@mock_dir, "a/b/c") + Dir.send(@method, "*", base: path).sort.should == %w( d y ) + end - path_abs = File.join(@mock_dir, "a/b/c") - path_rel = Pathname.new(path_abs).relative_path_from(Pathname.new(Dir.pwd)) + it "accepts both relative and absolute paths" do + require 'pathname' - result_abs = Dir.send(@method, "*", base: path_abs).sort - result_rel = Dir.send(@method, "*", base: path_rel).sort + path_abs = File.join(@mock_dir, "a/b/c") + path_rel = Pathname.new(path_abs).relative_path_from(Pathname.new(Dir.pwd)) - result_abs.should == %w( d y ) - result_rel.should == %w( d y ) - end + result_abs = Dir.send(@method, "*", base: path_abs).sort + result_rel = Dir.send(@method, "*", base: path_rel).sort - it "returns [] if specified path does not exist" do - path = File.join(@mock_dir, "fake-name") - File.should_not.exist?(path) + result_abs.should == %w( d y ) + result_rel.should == %w( d y ) + end - Dir.send(@method, "*", base: path).should == [] - end + it "returns [] if specified path does not exist" do + path = File.join(@mock_dir, "fake-name") + File.should_not.exist?(path) - it "returns [] if specified path is a file" do - path = File.join(@mock_dir, "a/b/x") - File.should.exist?(path) + Dir.send(@method, "*", base: path).should == [] + end - Dir.send(@method, "*", base: path).should == [] - end + it "returns [] if specified path is a file" do + path = File.join(@mock_dir, "a/b/x") + File.should.exist?(path) - it "raises TypeError when cannot convert value to string" do - -> { - Dir.send(@method, "*", base: []) - }.should raise_error(TypeError) - end + Dir.send(@method, "*", base: path).should == [] + end - it "handles '' as current directory path" do - Dir.chdir @mock_dir do - Dir.send(@method, "*", base: "").should == %w( a ) - end + it "raises TypeError when cannot convert value to string" do + -> { + Dir.send(@method, "*", base: []) + }.should.raise(TypeError) + end + + it "handles '' as current directory path" do + Dir.chdir @mock_dir do + Dir.send(@method, "*", base: "").should == %w( a ) end + end - it "handles nil as current directory path" do - Dir.chdir @mock_dir do - Dir.send(@method, "*", base: nil).should == %w( a ) - end + it "handles nil as current directory path" do + Dir.chdir @mock_dir do + Dir.send(@method, "*", base: nil).should == %w( a ) end end end |
