diff options
Diffstat (limited to 'spec/ruby/core/dir')
43 files changed, 1266 insertions, 379 deletions
diff --git a/spec/ruby/core/dir/chdir_spec.rb b/spec/ruby/core/dir/chdir_spec.rb index f5b0b80d1c..015386a902 100644 --- a/spec/ruby/core/dir/chdir_spec.rb +++ b/spec/ruby/core/dir/chdir_spec.rb @@ -1,5 +1,5 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/common', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/common' describe "Dir.chdir" do before :all do @@ -19,14 +19,14 @@ describe "Dir.chdir" do end it "defaults to $HOME with no arguments" do - if ENV['HOME'] - Dir.chdir - current_dir = Dir.pwd + skip "$HOME not valid directory" unless ENV['HOME'] && File.directory?(ENV['HOME']) - Dir.chdir(ENV['HOME']) - home = Dir.pwd - current_dir.should == home - end + Dir.chdir + current_dir = Dir.pwd + + Dir.chdir(ENV['HOME']) + home = Dir.pwd + current_dir.should == home end it "changes to the specified directory" do @@ -70,6 +70,8 @@ describe "Dir.chdir" do end it "defaults to the home directory when given a block but no argument" do + skip "$HOME not valid directory" unless ENV['HOME'] && File.directory?(ENV['HOME']) + # Windows will return a path with forward slashes for ENV["HOME"] so we have # to compare the route representations returned by Dir.chdir. current_dir = "" @@ -88,26 +90,26 @@ describe "Dir.chdir" do end it "raises an Errno::ENOENT if the directory does not exist" do - lambda { Dir.chdir DirSpecs.nonexistent }.should raise_error(Errno::ENOENT) - lambda { Dir.chdir(DirSpecs.nonexistent) { } }.should raise_error(Errno::ENOENT) + -> { Dir.chdir DirSpecs.nonexistent }.should raise_error(Errno::ENOENT) + -> { Dir.chdir(DirSpecs.nonexistent) { } }.should raise_error(Errno::ENOENT) end it "raises an Errno::ENOENT if the original directory no longer exists" do - dir1 = tmp('/testdir1') - dir2 = tmp('/testdir2') - File.exist?(dir1).should == false - File.exist?(dir2).should == false + dir1 = tmp('testdir1') + dir2 = tmp('testdir2') + Dir.should_not.exist?(dir1) + Dir.should_not.exist?(dir2) Dir.mkdir dir1 Dir.mkdir dir2 begin - lambda { + -> { Dir.chdir dir1 do Dir.chdir(dir2) { Dir.unlink dir1 } end }.should raise_error(Errno::ENOENT) ensure - Dir.unlink dir1 if File.exist?(dir1) - Dir.unlink dir2 if File.exist?(dir2) + Dir.unlink dir1 if Dir.exist?(dir1) + Dir.unlink dir2 if Dir.exist?(dir2) end end @@ -122,3 +124,97 @@ describe "Dir.chdir" do Dir.pwd.should == @original end end + +ruby_version_is '3.3' do + describe "Dir#chdir" do + before :all do + DirSpecs.create_mock_dirs + end + + after :all do + DirSpecs.delete_mock_dirs + end + + before :each do + @original = Dir.pwd + end + + after :each do + Dir.chdir(@original) + end + + it "changes the current working directory to self" do + dir = Dir.new(DirSpecs.mock_dir) + dir.chdir + Dir.pwd.should == DirSpecs.mock_dir + ensure + dir.close + end + + it "changes the current working directory to self for duration of the block when a block is given" do + dir = Dir.new(DirSpecs.mock_dir) + pwd_in_block = nil + + dir.chdir { pwd_in_block = Dir.pwd } + + pwd_in_block.should == DirSpecs.mock_dir + Dir.pwd.should == @original + ensure + dir.close + end + + it "returns 0 when successfully changing directory" do + dir = Dir.new(DirSpecs.mock_dir) + dir.chdir.should == 0 + ensure + dir.close + end + + it "returns the value of the block when a block is given" do + dir = Dir.new(DirSpecs.mock_dir) + dir.chdir { :block_value }.should == :block_value + ensure + dir.close + end + + platform_is_not :windows do + it "does not raise an Errno::ENOENT if the original directory no longer exists" do + dir_name1 = tmp('testdir1') + dir_name2 = tmp('testdir2') + Dir.should_not.exist?(dir_name1) + Dir.should_not.exist?(dir_name2) + Dir.mkdir dir_name1 + Dir.mkdir dir_name2 + + dir2 = Dir.new(dir_name2) + + begin + Dir.chdir(dir_name1) do + dir2.chdir { Dir.unlink dir_name1 } + end + Dir.pwd.should == @original + ensure + Dir.unlink dir_name1 if Dir.exist?(dir_name1) + Dir.unlink dir_name2 if Dir.exist?(dir_name2) + end + ensure + dir2.close + end + end + + it "always returns to the original directory when given a block" do + dir = Dir.new(DirSpecs.mock_dir) + + begin + dir.chdir do + raise StandardError, "something bad happened" + end + rescue StandardError + end + + Dir.pwd.should == @original + ensure + dir.close + end + end +end diff --git a/spec/ruby/core/dir/children_spec.rb b/spec/ruby/core/dir/children_spec.rb index a80e685996..0ad3df4669 100644 --- a/spec/ruby/core/dir/children_spec.rb +++ b/spec/ruby/core/dir/children_spec.rb @@ -1,72 +1,147 @@ # encoding: utf-8 -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/common', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/common' -ruby_version_is "2.5" do - describe "Dir.children" do - before :all do - DirSpecs.create_mock_dirs - end +describe "Dir.children" do + before :all do + DirSpecs.create_mock_dirs + end - before :each do - @internal = Encoding.default_internal - end + before :each do + @internal = Encoding.default_internal + end - after :all do - DirSpecs.delete_mock_dirs - end + after :all do + DirSpecs.delete_mock_dirs + end - after :each do - Encoding.default_internal = @internal - end + after :each do + Encoding.default_internal = @internal + end - it "returns an Array of filenames in an existing directory including dotfiles" do - a = Dir.children(DirSpecs.mock_dir).sort + it "returns an Array of filenames in an existing directory including dotfiles" do + a = Dir.children(DirSpecs.mock_dir).sort - a.should == DirSpecs.expected_paths - %w[. ..] + a.should == DirSpecs.expected_paths - %w[. ..] - a = Dir.children("#{DirSpecs.mock_dir}/deeply/nested").sort - a.should == %w|.dotfile.ext directory| - end + a = Dir.children("#{DirSpecs.mock_dir}/deeply/nested").sort + a.should == %w|.dotfile.ext directory| + end - it "calls #to_path on non-String arguments" do - p = mock('path') - p.should_receive(:to_path).and_return(DirSpecs.mock_dir) - Dir.children(p) - end + it "calls #to_path on non-String arguments" do + p = mock('path') + p.should_receive(:to_path).and_return(DirSpecs.mock_dir) + Dir.children(p) + end - it "accepts an options Hash" do - a = Dir.children("#{DirSpecs.mock_dir}/deeply/nested", encoding: "utf-8").sort - a.should == %w|.dotfile.ext directory| - end + it "accepts an options Hash" do + a = Dir.children("#{DirSpecs.mock_dir}/deeply/nested", encoding: "utf-8").sort + a.should == %w|.dotfile.ext directory| + end - it "returns children encoded with the filesystem encoding by default" do - # This spec depends on the locale not being US-ASCII because if it is, the - # children that are not ascii_only? will be ASCII-8BIT encoded. - children = Dir.children(File.join(DirSpecs.mock_dir, 'special')).sort - encoding = Encoding.find("filesystem") - encoding = Encoding::ASCII_8BIT if encoding == Encoding::US_ASCII - platform_is_not :windows do - children.should include("こんにちは.txt".force_encoding(encoding)) - end - children.first.encoding.should equal(Encoding.find("filesystem")) + it "returns children encoded with the filesystem encoding by default" do + # This spec depends on the locale not being US-ASCII because if it is, the + # children that are not ascii_only? will be BINARY encoded. + children = Dir.children(File.join(DirSpecs.mock_dir, 'special')).sort + encoding = Encoding.find("filesystem") + encoding = Encoding::BINARY if encoding == Encoding::US_ASCII + platform_is_not :windows do + children.should include("こんにちは.txt".dup.force_encoding(encoding)) end + children.first.encoding.should equal(Encoding.find("filesystem")) + end - it "returns children encoded with the specified encoding" do - dir = File.join(DirSpecs.mock_dir, 'special') - children = Dir.children(dir, encoding: "euc-jp").sort - children.first.encoding.should equal(Encoding::EUC_JP) - end + it "returns children encoded with the specified encoding" do + dir = File.join(DirSpecs.mock_dir, 'special') + children = Dir.children(dir, encoding: "euc-jp").sort + children.first.encoding.should equal(Encoding::EUC_JP) + end - it "returns children transcoded to the default internal encoding" do - Encoding.default_internal = Encoding::EUC_KR - children = Dir.children(File.join(DirSpecs.mock_dir, 'special')).sort - children.first.encoding.should equal(Encoding::EUC_KR) - end + it "returns children transcoded to the default internal encoding" do + Encoding.default_internal = Encoding::EUC_KR + children = Dir.children(File.join(DirSpecs.mock_dir, 'special')).sort + children.first.encoding.should equal(Encoding::EUC_KR) + end - it "raises a SystemCallError if called with a nonexistent diretory" do - lambda { Dir.children DirSpecs.nonexistent }.should raise_error(SystemCallError) + it "raises a SystemCallError if called with a nonexistent directory" do + -> { Dir.children DirSpecs.nonexistent }.should raise_error(SystemCallError) + end +end + +describe "Dir#children" do + before :all do + DirSpecs.create_mock_dirs + end + + before :each do + @internal = Encoding.default_internal + end + + after :all do + DirSpecs.delete_mock_dirs + end + + after :each do + Encoding.default_internal = @internal + @dir.close if @dir + end + + it "returns an Array of filenames in an existing directory including dotfiles" do + @dir = Dir.new(DirSpecs.mock_dir) + a = @dir.children.sort + @dir.close + + a.should == DirSpecs.expected_paths - %w[. ..] + + @dir = Dir.new("#{DirSpecs.mock_dir}/deeply/nested") + a = @dir.children.sort + a.should == %w|.dotfile.ext directory| + end + + it "accepts an encoding keyword for the encoding of the entries" do + @dir = Dir.new("#{DirSpecs.mock_dir}/deeply/nested", encoding: "utf-8") + dirs = @dir.to_a.sort + dirs.each { |d| d.encoding.should == Encoding::UTF_8 } + end + + it "returns children encoded with the filesystem encoding by default" do + # This spec depends on the locale not being US-ASCII because if it is, the + # children that are not ascii_only? will be BINARY encoded. + @dir = Dir.new(File.join(DirSpecs.mock_dir, 'special')) + children = @dir.children.sort + encoding = Encoding.find("filesystem") + encoding = Encoding::BINARY if encoding == Encoding::US_ASCII + platform_is_not :windows do + children.should include("こんにちは.txt".dup.force_encoding(encoding)) end + children.first.encoding.should equal(Encoding.find("filesystem")) + end + + it "returns children encoded with the specified encoding" do + path = File.join(DirSpecs.mock_dir, 'special') + @dir = Dir.new(path, encoding: "euc-jp") + children = @dir.children.sort + children.first.encoding.should equal(Encoding::EUC_JP) + end + + it "returns children transcoded to the default internal encoding" do + Encoding.default_internal = Encoding::EUC_KR + @dir = Dir.new(File.join(DirSpecs.mock_dir, 'special')) + children = @dir.children.sort + children.first.encoding.should equal(Encoding::EUC_KR) + end + + it "returns the same result when called repeatedly" do + @dir = Dir.open DirSpecs.mock_dir + + a = [] + @dir.each {|dir| a << dir} + + b = [] + @dir.each {|dir| b << dir} + + a.sort.should == b.sort + a.sort.should == DirSpecs.expected_paths end end diff --git a/spec/ruby/core/dir/chroot_spec.rb b/spec/ruby/core/dir/chroot_spec.rb index 23d790c83b..a5ca8943fc 100644 --- a/spec/ruby/core/dir/chroot_spec.rb +++ b/spec/ruby/core/dir/chroot_spec.rb @@ -1,6 +1,6 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/common', __FILE__) -require File.expand_path('../shared/chroot', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/common' +require_relative 'shared/chroot' platform_is_not :windows do as_superuser do @@ -9,7 +9,7 @@ platform_is_not :windows do end end - platform_is_not :cygwin do + platform_is_not :cygwin, :android do as_user do describe "Dir.chroot as regular user" do before :all do @@ -21,17 +21,17 @@ platform_is_not :windows do end it "raises an Errno::EPERM exception if the directory exists" do - lambda { Dir.chroot('.') }.should raise_error(Errno::EPERM) + -> { Dir.chroot('.') }.should raise_error(Errno::EPERM) end it "raises a SystemCallError if the directory doesn't exist" do - lambda { Dir.chroot('xgwhwhsjai2222jg') }.should raise_error(SystemCallError) + -> { Dir.chroot('xgwhwhsjai2222jg') }.should raise_error(SystemCallError) end it "calls #to_path on non-String argument" do p = mock('path') p.should_receive(:to_path).and_return('.') - lambda { Dir.chroot(p) }.should raise_error(Errno::EPERM) + -> { Dir.chroot(p) }.should raise_error(Errno::EPERM) end end end diff --git a/spec/ruby/core/dir/close_spec.rb b/spec/ruby/core/dir/close_spec.rb index 7b08ec5ee8..f7cce318b8 100644 --- a/spec/ruby/core/dir/close_spec.rb +++ b/spec/ruby/core/dir/close_spec.rb @@ -1,9 +1,5 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/common', __FILE__) -ruby_version_is ''...'2.3' do - require File.expand_path('../shared/closed', __FILE__) -end - +require_relative '../../spec_helper' +require_relative 'fixtures/common' describe "Dir#close" do before :all do DirSpecs.create_mock_dirs @@ -13,17 +9,45 @@ describe "Dir#close" do DirSpecs.delete_mock_dirs end - ruby_version_is ''...'2.3' do - it_behaves_like :dir_closed, :close + it "does not raise an IOError even if the Dir instance is closed" do + dir = Dir.open DirSpecs.mock_dir + dir.close.should == nil + dir.close.should == nil + + platform_is_not :windows do + -> { dir.fileno }.should raise_error(IOError, /closed directory/) + end end - ruby_version_is '2.3' do - it "does not raise an IOError even if the Dir instance is closed" do - dir = Dir.open DirSpecs.mock_dir - dir.close - lambda { + it "returns nil" do + dir = Dir.open DirSpecs.mock_dir + dir.close.should == nil + end + + ruby_version_is '3.3'...'3.4' do + platform_is_not :windows do + it "does not raise an error even if the file descriptor is closed with another Dir instance" do + dir = Dir.open DirSpecs.mock_dir + dir_new = Dir.for_fd(dir.fileno) + dir.close - }.should_not raise_error(IOError) + dir_new.close + + -> { dir.fileno }.should raise_error(IOError, /closed directory/) + -> { dir_new.fileno }.should raise_error(IOError, /closed directory/) + end + end + end + + ruby_version_is '3.4' do + platform_is_not :windows do + it "raises an error if the file descriptor is closed with another Dir instance" do + dir = Dir.open DirSpecs.mock_dir + dir_new = Dir.for_fd(dir.fileno) + dir.close + + -> { dir_new.close }.should raise_error(Errno::EBADF, 'Bad file descriptor - closedir') + end end end end diff --git a/spec/ruby/core/dir/delete_spec.rb b/spec/ruby/core/dir/delete_spec.rb index 5f36956839..a0020788ca 100644 --- a/spec/ruby/core/dir/delete_spec.rb +++ b/spec/ruby/core/dir/delete_spec.rb @@ -1,6 +1,6 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/common', __FILE__) -require File.expand_path('../shared/delete', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/common' +require_relative 'shared/delete' describe "Dir.delete" do before :all do diff --git a/spec/ruby/core/dir/dir_spec.rb b/spec/ruby/core/dir/dir_spec.rb index 4923445bed..7d55ea26d4 100644 --- a/spec/ruby/core/dir/dir_spec.rb +++ b/spec/ruby/core/dir/dir_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' describe "Dir" do it "includes Enumerable" do diff --git a/spec/ruby/core/dir/each_child_spec.rb b/spec/ruby/core/dir/each_child_spec.rb index 70f6f63333..7194273b95 100644 --- a/spec/ruby/core/dir/each_child_spec.rb +++ b/spec/ruby/core/dir/each_child_spec.rb @@ -1,51 +1,117 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/common', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/common' -ruby_version_is "2.5" do - describe "Dir.each_child" do - before :all do - DirSpecs.create_mock_dirs - end +describe "Dir.each_child" do + before :all do + DirSpecs.create_mock_dirs + end - after :all do - DirSpecs.delete_mock_dirs - end + after :all do + DirSpecs.delete_mock_dirs + end - it "yields all names in an existing directory to the provided block" do - a, b = [], [] + it "accepts an encoding keyword for the encoding of the entries" do + dirs = Dir.each_child("#{DirSpecs.mock_dir}/deeply/nested", encoding: "utf-8").to_a.sort + dirs.each {|dir| dir.encoding.should == Encoding::UTF_8} + end - Dir.each_child(DirSpecs.mock_dir) {|f| a << f} - Dir.each_child("#{DirSpecs.mock_dir}/deeply/nested") {|f| b << f} + it "yields all names in an existing directory to the provided block" do + a, b = [], [] - a.sort.should == DirSpecs.expected_paths - %w[. ..] - b.sort.should == %w|.dotfile.ext directory| - end + Dir.each_child(DirSpecs.mock_dir) {|f| a << f} + Dir.each_child("#{DirSpecs.mock_dir}/deeply/nested") {|f| b << f} - it "returns nil when successful" do - Dir.each_child(DirSpecs.mock_dir) {|f| f}.should == nil - end + a.sort.should == DirSpecs.expected_paths - %w[. ..] + b.sort.should == %w|.dotfile.ext directory| + end - it "calls #to_path on non-String arguments" do - p = mock('path') - p.should_receive(:to_path).and_return(DirSpecs.mock_dir) - Dir.each_child(p).to_a - end + it "returns nil when successful" do + Dir.each_child(DirSpecs.mock_dir) {|f| f}.should == nil + end - it "raises a SystemCallError if passed a nonexistent directory" do - lambda { Dir.each_child(DirSpecs.nonexistent) {} }.should raise_error(SystemCallError) + it "calls #to_path on non-String arguments" do + p = mock('path') + p.should_receive(:to_path).and_return(DirSpecs.mock_dir) + Dir.each_child(p).to_a + end + + it "raises a SystemCallError if passed a nonexistent directory" do + -> { Dir.each_child(DirSpecs.nonexistent) {} }.should raise_error(SystemCallError) + end + + describe "when no block is given" do + it "returns an Enumerator" do + Dir.each_child(DirSpecs.mock_dir).should be_an_instance_of(Enumerator) + Dir.each_child(DirSpecs.mock_dir).to_a.sort.should == DirSpecs.expected_paths - %w[. ..] end - describe "when no block is given" do - it "returns an Enumerator" do - Dir.each_child(DirSpecs.mock_dir).should be_an_instance_of(Enumerator) - Dir.each_child(DirSpecs.mock_dir).to_a.sort.should == DirSpecs.expected_paths - %w[. ..] + describe "returned Enumerator" do + describe "size" do + it "should return nil" do + Dir.each_child(DirSpecs.mock_dir).size.should == nil + end end + end + end +end + +describe "Dir#each_child" do + before :all do + DirSpecs.create_mock_dirs + end + + after :all do + DirSpecs.delete_mock_dirs + end + + after :each do + @dir.close if @dir + end + + it "yields all names in an existing directory to the provided block" do + a, b = [], [] + @dir = Dir.new(DirSpecs.mock_dir) + @dir2 = Dir.new("#{DirSpecs.mock_dir}/deeply/nested") + + @dir.each_child { |f| a << f } + @dir2.each_child { |f| b << f } + @dir2.close + + a.sort.should == DirSpecs.expected_paths - %w|. ..| + b.sort.should == %w|.dotfile.ext directory| + end + + it "returns self when successful" do + @dir = Dir.new(DirSpecs.mock_dir) + @dir.each_child { |f| f }.should == @dir + end + + it "returns the same result when called repeatedly" do + @dir = Dir.open DirSpecs.mock_dir + + a = [] + @dir.each {|dir| a << dir} + + b = [] + @dir.each {|dir| b << dir} + + a.sort.should == b.sort + a.sort.should == DirSpecs.expected_paths + end + + describe "when no block is given" do + it "returns an Enumerator" do + @dir = Dir.new(DirSpecs.mock_dir) + + @dir.each_child.should be_an_instance_of(Enumerator) + @dir.each_child.to_a.sort.should == DirSpecs.expected_paths - %w|. ..| + end - describe "returned Enumerator" do - describe "size" do - it "should return nil" do - Dir.each_child(DirSpecs.mock_dir).size.should == nil - end + describe "returned Enumerator" do + describe "size" do + it "should return nil" do + @dir = Dir.new(DirSpecs.mock_dir) + @dir.each_child.size.should == nil end end end diff --git a/spec/ruby/core/dir/each_spec.rb b/spec/ruby/core/dir/each_spec.rb index 534691ff58..7674663d82 100644 --- a/spec/ruby/core/dir/each_spec.rb +++ b/spec/ruby/core/dir/each_spec.rb @@ -1,6 +1,6 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/common', __FILE__) -require File.expand_path('../shared/closed', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/common' +require_relative 'shared/closed' describe "Dir#each" do before :all do @@ -35,6 +35,17 @@ describe "Dir#each" do ls.should include(@dir.read) end + it "returns the same result when called repeatedly" do + a = [] + @dir.each {|dir| a << dir} + + b = [] + @dir.each {|dir| b << dir} + + a.sort.should == b.sort + a.sort.should == DirSpecs.expected_paths + end + describe "when no block is given" do it "returns an Enumerator" do @dir.each.should be_an_instance_of(Enumerator) diff --git a/spec/ruby/core/dir/element_reference_spec.rb b/spec/ruby/core/dir/element_reference_spec.rb index de379d75ac..092114bed4 100644 --- a/spec/ruby/core/dir/element_reference_spec.rb +++ b/spec/ruby/core/dir/element_reference_spec.rb @@ -1,6 +1,6 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/common', __FILE__) -require File.expand_path('../shared/glob', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/common' +require_relative 'shared/glob' describe "Dir.[]" do it_behaves_like :dir_glob, :[] diff --git a/spec/ruby/core/dir/empty_spec.rb b/spec/ruby/core/dir/empty_spec.rb index 861a538f84..8cc8757798 100644 --- a/spec/ruby/core/dir/empty_spec.rb +++ b/spec/ruby/core/dir/empty_spec.rb @@ -1,33 +1,31 @@ -require File.expand_path('../../../spec_helper', __FILE__) +require_relative '../../spec_helper' -ruby_version_is "2.4" do - describe "Dir.empty?" do - before :all do - @empty_dir = tmp("empty_dir") - mkdir_p @empty_dir - end +describe "Dir.empty?" do + before :all do + @empty_dir = tmp("empty_dir") + mkdir_p @empty_dir + end - after :all do - rm_r @empty_dir - end + after :all do + rm_r @empty_dir + end - it "returns true for empty directories" do - result = Dir.empty? @empty_dir - result.should be_true - end + it "returns true for empty directories" do + result = Dir.empty? @empty_dir + result.should be_true + end - it "returns false for non-empty directories" do - result = Dir.empty? __dir__ - result.should be_false - end + it "returns false for non-empty directories" do + result = Dir.empty? __dir__ + result.should be_false + end - it "returns false for a non-directory" do - result = Dir.empty? __FILE__ - result.should be_false - end + it "returns false for a non-directory" do + result = Dir.empty? __FILE__ + result.should be_false + end - it "raises ENOENT for nonexistent directories" do - lambda { Dir.empty? tmp("nonexistent") }.should raise_error(Errno::ENOENT) - end + it "raises ENOENT for nonexistent directories" do + -> { Dir.empty? tmp("nonexistent") }.should raise_error(Errno::ENOENT) end end diff --git a/spec/ruby/core/dir/entries_spec.rb b/spec/ruby/core/dir/entries_spec.rb index 8a31ab4b4a..7462542acf 100644 --- a/spec/ruby/core/dir/entries_spec.rb +++ b/spec/ruby/core/dir/entries_spec.rb @@ -1,7 +1,7 @@ # encoding: utf-8 -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/common', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/common' describe "Dir.entries" do before :all do @@ -35,19 +35,19 @@ describe "Dir.entries" do Dir.entries(p) end - it "accepts an options Hash" do - a = Dir.entries("#{DirSpecs.mock_dir}/deeply/nested", encoding: "utf-8").sort - a.should == %w|. .. .dotfile.ext directory| + it "accepts an encoding keyword for the encoding of the entries" do + dirs = Dir.entries("#{DirSpecs.mock_dir}/deeply/nested", encoding: "utf-8").to_a.sort + dirs.each {|dir| dir.encoding.should == Encoding::UTF_8} end it "returns entries encoded with the filesystem encoding by default" do # This spec depends on the locale not being US-ASCII because if it is, the - # entries that are not ascii_only? will be ASCII-8BIT encoded. + # entries that are not ascii_only? will be BINARY encoded. entries = Dir.entries(File.join(DirSpecs.mock_dir, 'special')).sort encoding = Encoding.find("filesystem") - encoding = Encoding::ASCII_8BIT if encoding == Encoding::US_ASCII + encoding = Encoding::BINARY if encoding == Encoding::US_ASCII platform_is_not :windows do - entries.should include("こんにちは.txt".force_encoding(encoding)) + entries.should include("こんにちは.txt".dup.force_encoding(encoding)) end entries.first.encoding.should equal(Encoding.find("filesystem")) end @@ -64,7 +64,7 @@ describe "Dir.entries" do entries.first.encoding.should equal(Encoding::EUC_KR) end - it "raises a SystemCallError if called with a nonexistent diretory" do - lambda { Dir.entries DirSpecs.nonexistent }.should raise_error(SystemCallError) + it "raises a SystemCallError if called with a nonexistent directory" do + -> { Dir.entries DirSpecs.nonexistent }.should raise_error(SystemCallError) end end diff --git a/spec/ruby/core/dir/exist_spec.rb b/spec/ruby/core/dir/exist_spec.rb index 194284b5a0..0b8e521894 100644 --- a/spec/ruby/core/dir/exist_spec.rb +++ b/spec/ruby/core/dir/exist_spec.rb @@ -1,6 +1,6 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/common', __FILE__) -require File.expand_path('../shared/exist', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/common' +require_relative 'shared/exist' describe "Dir.exist?" do before :all do @@ -11,5 +11,11 @@ describe "Dir.exist?" do DirSpecs.delete_mock_dirs end - it_behaves_like(:dir_exist, :exist?) + it_behaves_like :dir_exist, :exist? +end + +describe "Dir.exists?" do + it "has been removed" do + Dir.should_not.respond_to?(:exists?) + end end diff --git a/spec/ruby/core/dir/exists_spec.rb b/spec/ruby/core/dir/exists_spec.rb deleted file mode 100644 index 002506a22f..0000000000 --- a/spec/ruby/core/dir/exists_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/common', __FILE__) -require File.expand_path('../shared/exist', __FILE__) - -describe "Dir.exists?" do - before :all do - DirSpecs.create_mock_dirs - end - - after :all do - DirSpecs.delete_mock_dirs - end - - it_behaves_like(:dir_exist, :exists?) -end diff --git a/spec/ruby/core/dir/fchdir_spec.rb b/spec/ruby/core/dir/fchdir_spec.rb new file mode 100644 index 0000000000..52600a95f2 --- /dev/null +++ b/spec/ruby/core/dir/fchdir_spec.rb @@ -0,0 +1,73 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/common' + +ruby_version_is '3.3' do + platform_is_not :windows do + describe "Dir.fchdir" do + before :all do + DirSpecs.create_mock_dirs + end + + after :all do + DirSpecs.delete_mock_dirs + end + + before :each do + @original = Dir.pwd + end + + after :each do + Dir.chdir(@original) + end + + it "changes the current working directory to the directory specified by the integer file descriptor" do + dir = Dir.new(DirSpecs.mock_dir) + Dir.fchdir dir.fileno + Dir.pwd.should == DirSpecs.mock_dir + ensure + dir.close + end + + it "returns 0 when successfully changing directory" do + dir = Dir.new(DirSpecs.mock_dir) + Dir.fchdir(dir.fileno).should == 0 + ensure + dir.close + end + + it "returns the value of the block when a block is given" do + dir = Dir.new(DirSpecs.mock_dir) + Dir.fchdir(dir.fileno) { :block_value }.should == :block_value + ensure + dir.close + end + + it "changes to the specified directory for the duration of the block" do + dir = Dir.new(DirSpecs.mock_dir) + Dir.fchdir(dir.fileno) { Dir.pwd }.should == DirSpecs.mock_dir + Dir.pwd.should == @original + ensure + dir.close + end + + it "raises a SystemCallError if the file descriptor given is not valid" do + -> { Dir.fchdir(-1) }.should raise_error(SystemCallError, "Bad file descriptor - fchdir") + -> { Dir.fchdir(-1) { } }.should raise_error(SystemCallError, "Bad file descriptor - fchdir") + end + + it "raises a SystemCallError if the file descriptor given is not for a directory" do + -> { Dir.fchdir $stdout.fileno }.should raise_error(SystemCallError, /(Not a directory|Invalid argument) - fchdir/) + -> { Dir.fchdir($stdout.fileno) { } }.should raise_error(SystemCallError, /(Not a directory|Invalid argument) - fchdir/) + end + end + end + + platform_is :windows do + describe "Dir.fchdir" do + it "raises NotImplementedError" do + -> { Dir.fchdir 1 }.should raise_error(NotImplementedError) + -> { Dir.fchdir(1) { } }.should raise_error(NotImplementedError) + end + end + end +end diff --git a/spec/ruby/core/dir/fileno_spec.rb b/spec/ruby/core/dir/fileno_spec.rb index cf8b811e3b..bb84ef5378 100644 --- a/spec/ruby/core/dir/fileno_spec.rb +++ b/spec/ruby/core/dir/fileno_spec.rb @@ -1,5 +1,5 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/common', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/common' has_dir_fileno = begin dir = Dir.new('.') @@ -27,11 +27,11 @@ describe "Dir#fileno" do if has_dir_fileno it "returns the file descriptor of the dir" do - @dir.fileno.should be_kind_of(Fixnum) + @dir.fileno.should be_kind_of(Integer) end else it "raises an error when not implemented on the platform" do - lambda { @dir.fileno }.should raise_error(NotImplementedError) + -> { @dir.fileno }.should raise_error(NotImplementedError) end end end diff --git a/spec/ruby/core/dir/fixtures/common.rb b/spec/ruby/core/dir/fixtures/common.rb index f6708b04f7..848656c9b9 100644 --- a/spec/ruby/core/dir/fixtures/common.rb +++ b/spec/ruby/core/dir/fixtures/common.rb @@ -22,7 +22,7 @@ module DirSpecs [0xe9].pack('U') ].each do |dir| begin - Dir.rmdir dir + Dir.rmdir mock_dir(dir) rescue end end @@ -36,6 +36,8 @@ module DirSpecs .dotfile .dotsubdir/.dotfile .dotsubdir/nondotfile + nested/.dotsubir/.dotfile + nested/.dotsubir/nondotfile deeply/.dotfile deeply/nested/.dotfile.ext @@ -79,6 +81,8 @@ module DirSpecs special/} special/test{1}/file[1] + special/{}/special + special/test\ +()[]{}/hello_world.erb ] platform_is_not :windows do @@ -89,19 +93,39 @@ module DirSpecs special/| special/こんにちは.txt + special/\a ] + @mock_dir_files << "special/_\u{1f60e}.erb" end end @mock_dir_files end + def self.mock_dir_links + unless @mock_dir_links + @mock_dir_links = [] + platform_is_not :windows do + @mock_dir_links += [ + ['special/ln', 'subdir_one'] + ] + end + end + @mock_dir_links + end + def self.create_mock_dirs mock_dir_files.each do |name| file = File.join mock_dir, name mkdir_p File.dirname(file) touch file end + mock_dir_links.each do |link, target| + full_link = File.join mock_dir, link + full_target = File.join mock_dir, target + + File.symlink full_target, full_link + end end def self.delete_mock_dirs @@ -160,10 +184,15 @@ module DirSpecs dir_filename_ordering file_one.ext file_two.ext + nested nondotfile special subdir_one subdir_two ] end + + def self.expected_glob_paths + expected_paths - ['..'] + end end diff --git a/spec/ruby/core/dir/for_fd_spec.rb b/spec/ruby/core/dir/for_fd_spec.rb new file mode 100644 index 0000000000..ba467f2f86 --- /dev/null +++ b/spec/ruby/core/dir/for_fd_spec.rb @@ -0,0 +1,79 @@ +require_relative '../../spec_helper' +require_relative 'fixtures/common' + +quarantine! do # leads to "Errno::EBADF: Bad file descriptor - closedir" in DirSpecs.delete_mock_dirs +ruby_version_is '3.3' do + platform_is_not :windows do + describe "Dir.for_fd" do + before :all do + DirSpecs.create_mock_dirs + end + + after :all do + DirSpecs.delete_mock_dirs + end + + before :each do + @original = Dir.pwd + end + + after :each do + Dir.chdir(@original) + end + + it "returns a new Dir object representing the directory specified by the given integer directory file descriptor" do + dir = Dir.new(DirSpecs.mock_dir) + dir_new = Dir.for_fd(dir.fileno) + + dir_new.should.instance_of?(Dir) + dir_new.children.should == dir.children + dir_new.fileno.should == dir.fileno + ensure + dir.close + end + + it "returns a new Dir object without associated path" do + dir = Dir.new(DirSpecs.mock_dir) + dir_new = Dir.for_fd(dir.fileno) + + dir_new.path.should == nil + ensure + dir.close + end + + it "calls #to_int to convert a value to an Integer" do + dir = Dir.new(DirSpecs.mock_dir) + obj = mock("fd") + obj.should_receive(:to_int).and_return(dir.fileno) + + dir_new = Dir.for_fd(obj) + dir_new.fileno.should == dir.fileno + ensure + dir.close + end + + it "raises TypeError when value cannot be converted to Integer" do + -> { + Dir.for_fd(nil) + }.should raise_error(TypeError, "no implicit conversion from nil to integer") + end + + it "raises a SystemCallError if the file descriptor given is not valid" do + -> { Dir.for_fd(-1) }.should raise_error(SystemCallError, "Bad file descriptor - fdopendir") + end + + it "raises a SystemCallError if the file descriptor given is not for a directory" do + -> { Dir.for_fd $stdout.fileno }.should raise_error(SystemCallError, "Not a directory - fdopendir") + end + end + end + + platform_is :windows do + describe "Dir.for_fd" do + it "raises NotImplementedError" do + -> { Dir.for_fd 1 }.should raise_error(NotImplementedError) + end + end + end +end +end diff --git a/spec/ruby/core/dir/foreach_spec.rb b/spec/ruby/core/dir/foreach_spec.rb index e606b4f65c..9cf34b1d71 100644 --- a/spec/ruby/core/dir/foreach_spec.rb +++ b/spec/ruby/core/dir/foreach_spec.rb @@ -1,5 +1,5 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/common', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/common' describe "Dir.foreach" do before :all do @@ -31,7 +31,7 @@ describe "Dir.foreach" do end it "raises a SystemCallError if passed a nonexistent directory" do - lambda { Dir.foreach(DirSpecs.nonexistent) {} }.should raise_error(SystemCallError) + -> { Dir.foreach(DirSpecs.nonexistent) {} }.should raise_error(SystemCallError) end it "returns an Enumerator if no block given" do @@ -39,6 +39,18 @@ describe "Dir.foreach" do Dir.foreach(DirSpecs.mock_dir).to_a.sort.should == DirSpecs.expected_paths end + it "accepts an encoding keyword for the encoding of the entries" do + dirs = Dir.foreach("#{DirSpecs.mock_dir}/deeply/nested", encoding: "utf-8").to_a.sort + dirs.each { |dir| dir.encoding.should == Encoding::UTF_8 } + + dirs = Dir.foreach("#{DirSpecs.mock_dir}/deeply/nested", encoding: Encoding::ISO_8859_1).to_a.sort + dirs.each { |dir| dir.encoding.should == Encoding::ISO_8859_1 } + + Dir.foreach("#{DirSpecs.mock_dir}/deeply/nested", encoding: Encoding::ISO_8859_1) do |f| + f.encoding.should == Encoding::ISO_8859_1 + end + end + describe "when no block is given" do it "returns an Enumerator" do Dir.foreach(DirSpecs.mock_dir).should be_an_instance_of(Enumerator) diff --git a/spec/ruby/core/dir/getwd_spec.rb b/spec/ruby/core/dir/getwd_spec.rb index 26659ddec7..132634347c 100644 --- a/spec/ruby/core/dir/getwd_spec.rb +++ b/spec/ruby/core/dir/getwd_spec.rb @@ -1,6 +1,6 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/common', __FILE__) -require File.expand_path('../shared/pwd', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/common' +require_relative 'shared/pwd' describe "Dir.getwd" do before :all do diff --git a/spec/ruby/core/dir/glob_spec.rb b/spec/ruby/core/dir/glob_spec.rb index 4a9230f8b3..a60b233bc0 100644 --- a/spec/ruby/core/dir/glob_spec.rb +++ b/spec/ruby/core/dir/glob_spec.rb @@ -1,6 +1,6 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/common', __FILE__) -require File.expand_path('../shared/glob', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/common' +require_relative 'shared/glob' describe "Dir.glob" do it_behaves_like :dir_glob, :glob @@ -29,6 +29,23 @@ describe "Dir.glob" do %w!file_one.ext file_two.ext! end + it 'returns matching file paths when supplied :base keyword argument' do + dir = tmp('dir_glob_base') + file_1 = "#{dir}/lib/bloop.rb" + file_2 = "#{dir}/lib/soup.rb" + file_3 = "#{dir}/lib/mismatched_file_type.txt" + file_4 = "#{dir}/mismatched_directory.rb" + + touch file_1 + touch file_2 + touch file_3 + touch file_4 + + Dir.glob('**/*.rb', base: "#{dir}/lib").sort.should == ["bloop.rb", "soup.rb"].sort + ensure + rm_r dir + end + it "calls #to_path to convert multiple patterns" do pat1 = mock('file_one.ext') pat1.should_receive(:to_path).and_return('file_one.ext') @@ -39,7 +56,7 @@ describe "Dir.glob" do end it "matches both dot and non-dotfiles with '*' and option File::FNM_DOTMATCH" do - Dir.glob('*', File::FNM_DOTMATCH).sort.should == DirSpecs.expected_paths + Dir.glob('*', File::FNM_DOTMATCH).sort.should == DirSpecs.expected_glob_paths end it "matches files with any beginning with '*<non-special characters>' and option File::FNM_DOTMATCH" do @@ -47,7 +64,7 @@ describe "Dir.glob" do end it "matches any files in the current directory with '**' and option File::FNM_DOTMATCH" do - Dir.glob('**', File::FNM_DOTMATCH).sort.should == DirSpecs.expected_paths + Dir.glob('**', File::FNM_DOTMATCH).sort.should == DirSpecs.expected_glob_paths end it "recursively matches any subdirectories except './' or '../' with '**/' from the current directory and option File::FNM_DOTMATCH" do @@ -59,8 +76,12 @@ describe "Dir.glob" do deeply/nested/directory/ deeply/nested/directory/structure/ dir/ + nested/ + nested/.dotsubir/ special/ + special/test\ +()[]{}/ special/test{1}/ + special/{}/ subdir_one/ subdir_two/ ] @@ -68,6 +89,17 @@ describe "Dir.glob" do Dir.glob('**/', File::FNM_DOTMATCH).sort.should == expected end + it "recursively matches files and directories in nested dot subdirectory except . with 'nested/**/*' from the current directory and option File::FNM_DOTMATCH" do + expected = %w[ + nested/. + nested/.dotsubir + nested/.dotsubir/.dotfile + nested/.dotsubir/nondotfile + ] + + Dir.glob('nested/**/*', File::FNM_DOTMATCH).sort.should == expected.sort + end + # This is a separate case to check **/ coming after a constant # directory as well. it "recursively matches any subdirectories except './' or '../' with '**/' and option File::FNM_DOTMATCH" do @@ -80,8 +112,12 @@ describe "Dir.glob" do ./deeply/nested/directory/ ./deeply/nested/directory/structure/ ./dir/ + ./nested/ + ./nested/.dotsubir/ ./special/ + ./special/test\ +()[]{}/ ./special/test{1}/ + ./special/{}/ ./subdir_one/ ./subdir_two/ ] @@ -102,6 +138,14 @@ describe "Dir.glob" do Dir.glob('{deeply/**/,subdir_two/*}').sort.should == expected end + it "preserves multiple /s before a **" do + expected = %w[ + deeply//nested/directory/structure + ] + + Dir.glob('{deeply//**/structure}').sort.should == expected + end + it "accepts a block and yields it with each elements" do ary = [] ret = Dir.glob(["file_o*", "file_t*"]) { |t| ary << t } @@ -119,7 +163,144 @@ describe "Dir.glob" do end it "handles infinite directory wildcards" do - Dir.glob('**/**/**').empty?.should == false + Dir.glob('**/**/**').should_not.empty? + end + + it "handles **/** with base keyword argument" do + Dir.glob('**/**', base: "dir").should == ["filename_ordering"] + + expected = %w[ + nested + nested/directory + nested/directory/structure + nested/directory/structure/bar + nested/directory/structure/baz + nested/directory/structure/file_one + nested/directory/structure/file_one.ext + nested/directory/structure/foo + nondotfile + ].sort + + Dir.glob('**/**', base: "deeply").sort.should == expected + end + + it "handles **/ with base keyword argument" do + expected = %w[ + / + directory/ + directory/structure/ + ] + Dir.glob('**/', base: "deeply/nested").sort.should == expected + end + + it "handles **/nondotfile with base keyword argument" do + expected = %w[ + deeply/nondotfile + nondotfile + subdir_one/nondotfile + subdir_two/nondotfile + ] + Dir.glob('**/nondotfile', base: ".").sort.should == expected + end + + it "handles **/nondotfile with base keyword argument and FNM_DOTMATCH" do + expected = %w[ + .dotsubdir/nondotfile + deeply/nondotfile + nested/.dotsubir/nondotfile + nondotfile + subdir_one/nondotfile + subdir_two/nondotfile + ] + Dir.glob('**/nondotfile', File::FNM_DOTMATCH, base: ".").sort.should == expected + end + + it "handles **/.dotfile with base keyword argument" do + expected = %w[ + .dotfile + deeply/.dotfile + subdir_one/.dotfile + ] + Dir.glob('**/.dotfile', base: ".").sort.should == expected + end + + it "handles **/.dotfile with base keyword argument and FNM_DOTMATCH" do + expected = %w[ + .dotfile + .dotsubdir/.dotfile + deeply/.dotfile + nested/.dotsubir/.dotfile + subdir_one/.dotfile + ] + Dir.glob('**/.dotfile', File::FNM_DOTMATCH, base: ".").sort.should == expected + end + + it "handles **/.* with base keyword argument" do + expected = %w[ + .dotfile.ext + directory/structure/.ext + ].sort + + Dir.glob('**/.*', base: "deeply/nested").sort.should == expected + end + + it "handles **/.* with base keyword argument and FNM_DOTMATCH" do + expected = %w[ + . + .dotfile.ext + directory/structure/.ext + ].sort + + Dir.glob('**/.*', File::FNM_DOTMATCH, base: "deeply/nested").sort.should == expected + end + + it "handles **/** with base keyword argument and FNM_DOTMATCH" do + expected = %w[ + . + .dotfile.ext + directory + directory/structure + directory/structure/.ext + directory/structure/bar + directory/structure/baz + directory/structure/file_one + directory/structure/file_one.ext + directory/structure/foo + ].sort + + Dir.glob('**/**', File::FNM_DOTMATCH, base: "deeply/nested").sort.should == expected + end + + it "handles **/*pattern* with base keyword argument and FNM_DOTMATCH" do + expected = %w[ + .dotfile.ext + directory/structure/file_one + directory/structure/file_one.ext + ] + + Dir.glob('**/*file*', File::FNM_DOTMATCH, base: "deeply/nested").sort.should == expected + end + + it "handles **/glob with base keyword argument and FNM_EXTGLOB" do + expected = %w[ + directory/structure/bar + directory/structure/file_one + directory/structure/file_one.ext + ] + + Dir.glob('**/*{file,bar}*', File::FNM_EXTGLOB, base: "deeply/nested").sort.should == expected + end + + it "handles simple filename patterns" do + Dir.glob('.dotfile').should == ['.dotfile'] + end + + it "handles simple directory patterns" do + Dir.glob('.dotsubdir/').should == ['.dotsubdir/'] + end + + it "handles simple directory patterns applied to non-directories" do + Dir.glob('nondotfile/').should == [] end platform_is_not(:windows) do @@ -152,5 +333,30 @@ describe "Dir.glob" do Dir.rmdir('no_permission') end end + + it "will follow symlinks when processing a `*/` pattern." do + expected = ['special/ln/nondotfile'] + Dir.glob('special/*/nondotfile').should == expected + end + + it "will not follow symlinks when recursively traversing directories" do + expected = %w[ + deeply/nondotfile + nondotfile + subdir_one/nondotfile + subdir_two/nondotfile + ] + Dir.glob('**/nondotfile').sort.should == expected + end + + it "will follow symlinks when testing directory after recursive directory in pattern" do + expected = %w[ + deeply/nondotfile + special/ln/nondotfile + subdir_one/nondotfile + subdir_two/nondotfile + ] + Dir.glob('**/*/nondotfile').sort.should == expected + end end end diff --git a/spec/ruby/core/dir/home_spec.rb b/spec/ruby/core/dir/home_spec.rb index 6d99678034..966ac38af3 100644 --- a/spec/ruby/core/dir/home_spec.rb +++ b/spec/ruby/core/dir/home_spec.rb @@ -1,26 +1,85 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/common', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/common' describe "Dir.home" do - it "returns the current user's home directory as a string if called without arguments" do - home_directory = ENV['HOME'] - platform_is :windows do - unless home_directory - home_directory = ENV['HOMEDRIVE'] + ENV['HOMEPATH'] + before :each do + @home = ENV['HOME'] + ENV['HOME'] = "/rubyspec_home" + end + + after :each do + ENV['HOME'] = @home + end + + describe "when called without arguments" do + it "returns the current user's home directory, reading $HOME first" do + Dir.home.should == "/rubyspec_home" + end + + it "returns a non-frozen string" do + Dir.home.should_not.frozen? + end + + it "returns a string with the filesystem encoding" do + Dir.home.encoding.should == Encoding.find("filesystem") + end + + platform_is_not :windows do + it "works even if HOME is unset" do + ENV.delete('HOME') + Dir.home.should.start_with?('/') + Dir.home.encoding.should == Encoding.find("filesystem") end - home_directory = home_directory.tr('\\', '/').chomp('/') end - Dir.home.should == home_directory + platform_is :windows do + it "returns the home directory with forward slashs and as UTF-8" do + ENV['HOME'] = "C:\\rubyspäc\\home" + home = Dir.home + home.should == "C:/rubyspäc/home" + home.encoding.should == Encoding::UTF_8 + end + + it "retrieves the directory from HOME, USERPROFILE, HOMEDRIVE/HOMEPATH and the WinAPI in that order" do + old_dirs = [ENV.delete('HOME'), ENV.delete('USERPROFILE'), ENV.delete('HOMEDRIVE'), ENV.delete('HOMEPATH')] + + Dir.home.should == old_dirs[1].gsub("\\", "/") + ENV['HOMEDRIVE'] = "C:" + ENV['HOMEPATH'] = "\\rubyspec\\home1" + Dir.home.should == "C:/rubyspec/home1" + ENV['USERPROFILE'] = "C:\\rubyspec\\home2" + Dir.home.should == "C:/rubyspec/home2" + ENV['HOME'] = "C:\\rubyspec\\home3" + Dir.home.should == "C:/rubyspec/home3" + ensure + ENV['HOME'], ENV['USERPROFILE'], ENV['HOMEDRIVE'], ENV['HOMEPATH'] = *old_dirs + end + end end - platform_is_not :windows do - it "returns the named user's home directory as a string if called with an argument" do - Dir.home(ENV['USER']).should == ENV['HOME'] + describe "when called with the current user name" do + platform_is_not :windows, :android, :wasi do + it "returns the named user's home directory, from the user database" do + Dir.home(ENV['USER']).should == `echo ~#{ENV['USER']}`.chomp + end + end + + it "returns a non-frozen string" do + Dir.home(ENV['USER']).should_not.frozen? + end + + it "returns a string with the filesystem encoding" do + Dir.home(ENV['USER']).encoding.should == Encoding.find("filesystem") end end it "raises an ArgumentError if the named user doesn't exist" do - lambda { Dir.home('geuw2n288dh2k') }.should raise_error(ArgumentError) + -> { Dir.home('geuw2n288dh2k') }.should raise_error(ArgumentError) + end + + describe "when called with a nil user name" do + it "returns the current user's home directory, reading $HOME first" do + Dir.home(nil).should == "/rubyspec_home" + end end end diff --git a/spec/ruby/core/dir/initialize_spec.rb b/spec/ruby/core/dir/initialize_spec.rb index b9420647d1..547b7dc18e 100644 --- a/spec/ruby/core/dir/initialize_spec.rb +++ b/spec/ruby/core/dir/initialize_spec.rb @@ -1,5 +1,5 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/common', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/common' describe "Dir#initialize" do before :each do diff --git a/spec/ruby/core/dir/inspect_spec.rb b/spec/ruby/core/dir/inspect_spec.rb index 01bde8a862..37338a97d4 100644 --- a/spec/ruby/core/dir/inspect_spec.rb +++ b/spec/ruby/core/dir/inspect_spec.rb @@ -1,5 +1,5 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/common', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/common' describe "Dir#inspect" do before :each do diff --git a/spec/ruby/core/dir/mkdir_spec.rb b/spec/ruby/core/dir/mkdir_spec.rb index 7eb8a8fe6c..076ec19dd9 100644 --- a/spec/ruby/core/dir/mkdir_spec.rb +++ b/spec/ruby/core/dir/mkdir_spec.rb @@ -1,5 +1,5 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/common', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/common' describe "Dir.mkdir" do before :all do @@ -13,73 +13,95 @@ describe "Dir.mkdir" do it "creates the named directory with the given permissions" do DirSpecs.clear_dirs + nonexisting = DirSpecs.mock_dir('nonexisting') + default_perms = DirSpecs.mock_dir('default_perms') + reduced = DirSpecs.mock_dir('reduced') begin - File.exist?('nonexisting').should == false - Dir.mkdir 'nonexisting' - File.exist?('nonexisting').should == true + File.should_not.exist?(nonexisting) + Dir.mkdir nonexisting + File.should.exist?(nonexisting) platform_is_not :windows do - Dir.mkdir 'default_perms' - a = File.stat('default_perms').mode - Dir.mkdir 'reduced', (a - 1) - File.stat('reduced').mode.should_not == a + Dir.mkdir default_perms + a = File.stat(default_perms).mode + Dir.mkdir reduced, (a - 1) + File.stat(reduced).mode.should_not == a end platform_is :windows do - Dir.mkdir 'default_perms', 0666 - a = File.stat('default_perms').mode - Dir.mkdir 'reduced', 0444 - File.stat('reduced').mode.should_not == a + Dir.mkdir default_perms, 0666 + a = File.stat(default_perms).mode + Dir.mkdir reduced, 0444 + File.stat(reduced).mode.should_not == a end - Dir.mkdir('always_returns_0').should == 0 + always_returns_0 = DirSpecs.mock_dir('always_returns_0') + Dir.mkdir(always_returns_0).should == 0 platform_is_not(:windows) do - File.chmod(0777, "nonexisting","default_perms","reduced","always_returns_0") + File.chmod(0777, nonexisting, default_perms, reduced, always_returns_0) end platform_is_not(:windows) do - File.chmod(0644, "nonexisting","default_perms","reduced","always_returns_0") + File.chmod(0644, nonexisting, default_perms, reduced, always_returns_0) end ensure DirSpecs.clear_dirs end end - it "calls #to_path on non-String arguments" do + it "calls #to_path on non-String path arguments" do DirSpecs.clear_dirs p = mock('path') - p.should_receive(:to_path).and_return('nonexisting') + p.should_receive(:to_path).and_return(DirSpecs.mock_dir('nonexisting')) Dir.mkdir(p) DirSpecs.clear_dirs end + it "calls #to_int on non-Integer permissions argument" do + DirSpecs.clear_dirs + path = DirSpecs.mock_dir('nonexisting') + permissions = mock('permissions') + permissions.should_receive(:to_int).and_return(0666) + Dir.mkdir(path, permissions) + DirSpecs.clear_dirs + end + + it "raises TypeError if non-Integer permissions argument does not have #to_int method" do + path = DirSpecs.mock_dir('nonexisting') + permissions = Object.new + + -> { Dir.mkdir(path, permissions) }.should raise_error(TypeError, 'no implicit conversion of Object into Integer') + end + it "raises a SystemCallError if any of the directories in the path before the last does not exist" do - lambda { Dir.mkdir "#{DirSpecs.nonexistent}/subdir" }.should raise_error(SystemCallError) + -> { Dir.mkdir "#{DirSpecs.nonexistent}/subdir" }.should raise_error(SystemCallError) end it "raises Errno::EEXIST if the specified directory already exists" do - lambda { Dir.mkdir("#{DirSpecs.mock_dir}/dir") }.should raise_error(Errno::EEXIST) + -> { Dir.mkdir("#{DirSpecs.mock_dir}/dir") }.should raise_error(Errno::EEXIST) end it "raises Errno::EEXIST if the argument points to the existing file" do - lambda { Dir.mkdir("#{DirSpecs.mock_dir}/file_one.ext") }.should raise_error(Errno::EEXIST) + -> { Dir.mkdir("#{DirSpecs.mock_dir}/file_one.ext") }.should raise_error(Errno::EEXIST) end end # The permissions flag are not supported on Windows as stated in documentation: # The permissions may be modified by the value of File.umask, and are ignored on NT. platform_is_not :windows do - describe "Dir.mkdir" do - before :each do - @dir = tmp "noperms" - end + as_user do + describe "Dir.mkdir" do + before :each do + @dir = tmp "noperms" + end - after :each do - File.chmod 0777, @dir - rm_r @dir - end + after :each do + File.chmod 0777, @dir + rm_r @dir + end - it "raises a SystemCallError when lacking adequate permissions in the parent dir" do - Dir.mkdir @dir, 0000 + it "raises a SystemCallError when lacking adequate permissions in the parent dir" do + Dir.mkdir @dir, 0000 - lambda { Dir.mkdir "#{@dir}/subdir" }.should raise_error(SystemCallError) + -> { Dir.mkdir "#{@dir}/subdir" }.should raise_error(SystemCallError) + end end end end diff --git a/spec/ruby/core/dir/open_spec.rb b/spec/ruby/core/dir/open_spec.rb index b3deed47b7..27f362320b 100644 --- a/spec/ruby/core/dir/open_spec.rb +++ b/spec/ruby/core/dir/open_spec.rb @@ -1,6 +1,6 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/common', __FILE__) -require File.expand_path('../shared/open', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/common' +require_relative 'shared/open' describe "Dir.open" do before :all do diff --git a/spec/ruby/core/dir/path_spec.rb b/spec/ruby/core/dir/path_spec.rb index 1601220636..b1c24c406b 100644 --- a/spec/ruby/core/dir/path_spec.rb +++ b/spec/ruby/core/dir/path_spec.rb @@ -1,6 +1,6 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/common', __FILE__) -require File.expand_path('../shared/path', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/common' +require_relative 'shared/path' describe "Dir#path" do before :all do @@ -11,5 +11,5 @@ describe "Dir#path" do DirSpecs.delete_mock_dirs end - it_behaves_like(:dir_path, :path) + it_behaves_like :dir_path, :path end diff --git a/spec/ruby/core/dir/pos_spec.rb b/spec/ruby/core/dir/pos_spec.rb index 9f05fab250..b382bff81f 100644 --- a/spec/ruby/core/dir/pos_spec.rb +++ b/spec/ruby/core/dir/pos_spec.rb @@ -1,7 +1,7 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/common', __FILE__) -require File.expand_path('../shared/closed', __FILE__) -require File.expand_path('../shared/pos', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/common' +require_relative 'shared/closed' +require_relative 'shared/pos' describe "Dir#pos" do before :all do diff --git a/spec/ruby/core/dir/pwd_spec.rb b/spec/ruby/core/dir/pwd_spec.rb index 4fa86dd6b9..ad01286c90 100644 --- a/spec/ruby/core/dir/pwd_spec.rb +++ b/spec/ruby/core/dir/pwd_spec.rb @@ -1,7 +1,7 @@ # -*- encoding: utf-8 -*- -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/common', __FILE__) -require File.expand_path('../shared/pwd', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/common' +require_relative 'shared/pwd' describe "Dir.pwd" do before :all do diff --git a/spec/ruby/core/dir/read_spec.rb b/spec/ruby/core/dir/read_spec.rb index 79ed9b8058..276930c6b7 100644 --- a/spec/ruby/core/dir/read_spec.rb +++ b/spec/ruby/core/dir/read_spec.rb @@ -1,6 +1,6 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/common', __FILE__) -require File.expand_path('../shared/closed', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/common' +require_relative 'shared/closed' describe "Dir#read" do before :all do @@ -39,5 +39,38 @@ describe "Dir#read" do entries.sort.should == DirSpecs.expected_paths end + platform_is_not :windows do + it "returns all directory entries even when encoding conversion will fail" do + dir = Dir.open(File.join(DirSpecs.mock_dir, 'special')) + utf8_entries = [] + begin + while entry = dir.read + utf8_entries << entry + end + ensure + dir.close + end + old_internal_encoding = Encoding::default_internal + old_external_encoding = Encoding::default_external + Encoding.default_internal = Encoding::UTF_8 + Encoding.default_external = Encoding::SHIFT_JIS + shift_jis_entries = [] + begin + Dir.open(File.join(DirSpecs.mock_dir, 'special')) do |d| + -> { + while entry = d.read + shift_jis_entries << entry + end + }.should_not raise_error + end + ensure + Encoding.default_internal = old_internal_encoding + Encoding.default_external = old_external_encoding + end + shift_jis_entries.size.should == utf8_entries.size + shift_jis_entries.filter { |f| f.encoding == Encoding::SHIFT_JIS }.size.should == 1 + end + end + it_behaves_like :dir_closed, :read end diff --git a/spec/ruby/core/dir/rewind_spec.rb b/spec/ruby/core/dir/rewind_spec.rb index 65ffcdf1c3..220d7f5372 100644 --- a/spec/ruby/core/dir/rewind_spec.rb +++ b/spec/ruby/core/dir/rewind_spec.rb @@ -1,6 +1,6 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/common', __FILE__) -require File.expand_path('../shared/closed', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/common' +require_relative 'shared/closed' describe "Dir#rewind" do before :all do diff --git a/spec/ruby/core/dir/rmdir_spec.rb b/spec/ruby/core/dir/rmdir_spec.rb index 09499572c0..08cd1a5bc6 100644 --- a/spec/ruby/core/dir/rmdir_spec.rb +++ b/spec/ruby/core/dir/rmdir_spec.rb @@ -1,6 +1,6 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/common', __FILE__) -require File.expand_path('../shared/delete', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/common' +require_relative 'shared/delete' describe "Dir.rmdir" do before :all do diff --git a/spec/ruby/core/dir/seek_spec.rb b/spec/ruby/core/dir/seek_spec.rb index b51e554441..ed409897cd 100644 --- a/spec/ruby/core/dir/seek_spec.rb +++ b/spec/ruby/core/dir/seek_spec.rb @@ -1,6 +1,6 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/common', __FILE__) -require File.expand_path('../shared/pos', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/common' +require_relative 'shared/pos' describe "Dir#seek" do before :all do diff --git a/spec/ruby/core/dir/shared/chroot.rb b/spec/ruby/core/dir/shared/chroot.rb index 2ed033dfed..a8f7c10a19 100644 --- a/spec/ruby/core/dir/shared/chroot.rb +++ b/spec/ruby/core/dir/shared/chroot.rb @@ -2,8 +2,8 @@ describe :dir_chroot_as_root, shared: true do before :all do DirSpecs.create_mock_dirs - @real_root = "../" * (File.dirname(__FILE__).count('/') - 1) - @ref_dir = File.join("/", Dir.new('/').entries.first) + @real_root = "../" * (__dir__.count('/') - 1) + @ref_dir = File.join("/", File.basename(Dir["/*"].first)) end after :all do @@ -14,24 +14,27 @@ describe :dir_chroot_as_root, shared: true do DirSpecs.delete_mock_dirs end + # Pending until https://github.com/ruby/ruby/runs/8075149420 is fixed + compilations_ci = ENV["GITHUB_WORKFLOW"] == "Compilations" + it "can be used to change the process' root directory" do - lambda { Dir.send(@method, File.dirname(__FILE__)) }.should_not raise_error - File.exist?("/#{File.basename(__FILE__)}").should be_true - end + -> { Dir.send(@method, __dir__) }.should_not raise_error + File.should.exist?("/#{File.basename(__FILE__)}") + end unless compilations_ci it "returns 0 if successful" do Dir.send(@method, '/').should == 0 end it "raises an Errno::ENOENT exception if the directory doesn't exist" do - lambda { Dir.send(@method, 'xgwhwhsjai2222jg') }.should raise_error(Errno::ENOENT) + -> { Dir.send(@method, 'xgwhwhsjai2222jg') }.should raise_error(Errno::ENOENT) end it "can be escaped from with ../" do Dir.send(@method, @real_root) - File.exist?(@ref_dir).should be_true - File.exist?("/#{File.basename(__FILE__)}").should be_false - end + File.should.exist?(@ref_dir) + File.should_not.exist?("/#{File.basename(__FILE__)}") + end unless compilations_ci it "calls #to_path on non-String argument" do p = mock('path') diff --git a/spec/ruby/core/dir/shared/closed.rb b/spec/ruby/core/dir/shared/closed.rb index a1bce06a08..17d8332c2a 100644 --- a/spec/ruby/core/dir/shared/closed.rb +++ b/spec/ruby/core/dir/shared/closed.rb @@ -1,6 +1,6 @@ describe :dir_closed, shared: true do it "raises an IOError when called on a closed Dir instance" do - lambda { + -> { dir = Dir.open DirSpecs.mock_dir dir.close dir.send(@method) {} diff --git a/spec/ruby/core/dir/shared/delete.rb b/spec/ruby/core/dir/shared/delete.rb index 8db17d985f..a81b059759 100644 --- a/spec/ruby/core/dir/shared/delete.rb +++ b/spec/ruby/core/dir/shared/delete.rb @@ -17,24 +17,14 @@ describe :dir_delete, shared: true do Dir.send(@method, p) end - platform_is_not :solaris do - it "raises an Errno::ENOTEMPTY when trying to remove a nonempty directory" do - lambda do - Dir.send @method, DirSpecs.mock_rmdir("nonempty") - end.should raise_error(Errno::ENOTEMPTY) - end - end - - platform_is :solaris do - it "raises an Errno::EEXIST when trying to remove a nonempty directory" do - lambda do - Dir.send @method, DirSpecs.mock_rmdir("nonempty") - end.should raise_error(Errno::EEXIST) - end + it "raises an Errno::ENOTEMPTY when trying to remove a nonempty directory" do + -> do + Dir.send @method, DirSpecs.mock_rmdir("nonempty") + end.should raise_error(Errno::ENOTEMPTY) end it "raises an Errno::ENOENT when trying to remove a non-existing directory" do - lambda do + -> do Dir.send @method, DirSpecs.nonexistent end.should raise_error(Errno::ENOENT) end @@ -42,20 +32,22 @@ describe :dir_delete, shared: true do it "raises an Errno::ENOTDIR when trying to remove a non-directory" do file = DirSpecs.mock_rmdir("nonempty/regular") touch(file) - lambda do + -> do Dir.send @method, file end.should raise_error(Errno::ENOTDIR) end # this won't work on Windows, since chmod(0000) does not remove all permissions platform_is_not :windows do - it "raises an Errno::EACCES if lacking adequate permissions to remove the directory" do - parent = DirSpecs.mock_rmdir("noperm") - child = DirSpecs.mock_rmdir("noperm", "child") - File.chmod(0000, parent) - lambda do - Dir.send @method, child - end.should raise_error(Errno::EACCES) + as_user do + it "raises an Errno::EACCES if lacking adequate permissions to remove the directory" do + parent = DirSpecs.mock_rmdir("noperm") + child = DirSpecs.mock_rmdir("noperm", "child") + File.chmod(0000, parent) + -> do + Dir.send @method, child + end.should raise_error(Errno::EACCES) + end end end end diff --git a/spec/ruby/core/dir/shared/exist.rb b/spec/ruby/core/dir/shared/exist.rb index fbd2c9862d..3097f57715 100644 --- a/spec/ruby/core/dir/shared/exist.rb +++ b/spec/ruby/core/dir/shared/exist.rb @@ -1,6 +1,6 @@ describe :dir_exist, shared: true do it "returns true if the given directory exists" do - Dir.send(@method, File.dirname(__FILE__)).should be_true + Dir.send(@method, __dir__).should be_true end it "returns true for '.'" do @@ -20,7 +20,7 @@ describe :dir_exist, shared: true do end it "understands relative paths" do - Dir.send(@method, File.dirname(__FILE__) + '/../').should be_true + Dir.send(@method, __dir__ + '/../').should be_true end it "returns false if the given directory doesn't exist" do @@ -28,18 +28,19 @@ describe :dir_exist, shared: true do end it "doesn't require the name to have a trailing slash" do - dir = File.dirname(__FILE__) + dir = __dir__ dir.sub!(/\/$/,'') Dir.send(@method, dir).should be_true end it "doesn't expand paths" do + skip "$HOME not valid directory" unless ENV['HOME'] && File.directory?(ENV['HOME']) Dir.send(@method, File.expand_path('~')).should be_true Dir.send(@method, '~').should be_false end it "returns false if the argument exists but is a file" do - File.exist?(__FILE__).should be_true + File.should.exist?(__FILE__) Dir.send(@method, __FILE__).should be_false end @@ -50,7 +51,7 @@ describe :dir_exist, shared: true do it "calls #to_path on non String arguments" do p = mock('path') - p.should_receive(:to_path).and_return(File.dirname(__FILE__)) + p.should_receive(:to_path).and_return(__dir__) Dir.send(@method, p) end end diff --git a/spec/ruby/core/dir/shared/glob.rb b/spec/ruby/core/dir/shared/glob.rb index d2201cd6cd..b1fc924f08 100644 --- a/spec/ruby/core/dir/shared/glob.rb +++ b/spec/ruby/core/dir/shared/glob.rb @@ -11,11 +11,9 @@ describe :dir_glob, shared: true do DirSpecs.delete_mock_dirs end - with_feature :encoding do - it "raises an Encoding::CompatibilityError if the argument encoding is not compatible with US-ASCII" do - pattern = "file*".force_encoding Encoding::UTF_16BE - lambda { Dir.send(@method, pattern) }.should raise_error(Encoding::CompatibilityError) - end + it "raises an Encoding::CompatibilityError if the argument encoding is not compatible with US-ASCII" do + pattern = "files*".dup.force_encoding Encoding::UTF_16BE + -> { Dir.send(@method, pattern) }.should raise_error(Encoding::CompatibilityError) end it "calls #to_path to convert a pattern" do @@ -25,9 +23,29 @@ describe :dir_glob, shared: true do Dir.send(@method, obj).should == %w[file_one.ext] end - 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! + 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 by default" do + result = Dir.send(@method, '*') + result.should == result.sort + 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_error ArgumentError, /expected true or false/ + -> { Dir.send(@method, '*', sort: nil) }.should raise_error ArgumentError, /expected true or false/ + -> { Dir.send(@method, '*', sort: 'false') }.should raise_error ArgumentError, /expected true or false/ end it "matches non-dotfiles with '*'" do @@ -38,6 +56,7 @@ describe :dir_glob, shared: true do dir_filename_ordering file_one.ext file_two.ext + nested nondotfile special subdir_one @@ -55,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/*'] @@ -67,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 @@ -105,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 @@ -141,6 +172,7 @@ describe :dir_glob, shared: true do dir_filename_ordering file_one.ext file_two.ext + nested nondotfile special subdir_one @@ -150,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 @@ -162,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/ ] @@ -171,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 @@ -216,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 @@ -262,11 +305,11 @@ describe :dir_glob, shared: true do subdir_two/nondotfile.ext] end - it "ignores matching through directories that doen't exist" do + it "ignores matching through directories that doesn't exist" do Dir.send(@method, "deeply/notthere/blah*/whatever").should == [] end - it "ignores matching only directories under an nonexistant path" do + it "ignores matching only directories under an nonexistent path" do Dir.send(@method, "deeply/notthere/blah/").should == [] end @@ -275,6 +318,76 @@ describe :dir_glob, shared: true do Dir.send(@method, "special/こんにちは{,.txt}").should == ["special/こんにちは.txt"] end end + + 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 + + after :each do + rm_r @mock_dir + 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 + + it "accepts both relative and absolute paths" do + require 'pathname' + + path_abs = File.join(@mock_dir, "a/b/c") + path_rel = Pathname.new(path_abs).relative_path_from(Pathname.new(Dir.pwd)) + + result_abs = Dir.send(@method, "*", base: path_abs).sort + result_rel = Dir.send(@method, "*", base: path_rel).sort + + result_abs.should == %w( d y ) + result_rel.should == %w( d y ) + end + + it "returns [] if specified path does not exist" do + path = File.join(@mock_dir, "fake-name") + File.should_not.exist?(path) + + 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) + + Dir.send(@method, "*", base: path).should == [] + end + + it "raises TypeError when cannot convert value to string" do + -> { + Dir.send(@method, "*", base: []) + }.should raise_error(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 + end + end end describe :dir_glob_recursive, shared: true do diff --git a/spec/ruby/core/dir/shared/open.rb b/spec/ruby/core/dir/shared/open.rb index 7f4fe5c2a6..920845cba1 100644 --- a/spec/ruby/core/dir/shared/open.rb +++ b/spec/ruby/core/dir/shared/open.rb @@ -6,7 +6,7 @@ describe :dir_open, shared: true do end it "raises a SystemCallError if the directory does not exist" do - lambda do + -> do Dir.send @method, DirSpecs.nonexistent end.should raise_error(SystemCallError) end @@ -21,20 +21,20 @@ describe :dir_open, shared: true do it "closes the Dir instance when the block exits if given a block" do closed_dir = Dir.send(@method, DirSpecs.mock_dir) { |dir| dir } - lambda { closed_dir.read }.should raise_error(IOError) + -> { closed_dir.read }.should raise_error(IOError) end it "closes the Dir instance when the block exits the block even due to an exception" do closed_dir = nil - lambda do + -> do Dir.send(@method, DirSpecs.mock_dir) do |dir| closed_dir = dir raise "dir specs" end end.should raise_error(RuntimeError, "dir specs") - lambda { closed_dir.read }.should raise_error(IOError) + -> { closed_dir.read }.should raise_error(IOError) end it "calls #to_path on non-String arguments" do @@ -52,7 +52,7 @@ describe :dir_open, shared: true do options = mock("dir_open") options.should_receive(:to_hash).and_return({ encoding: Encoding::UTF_8 }) - dir = Dir.send(@method, DirSpecs.mock_dir, options) {|d| d } + dir = Dir.send(@method, DirSpecs.mock_dir, **options) {|d| d } dir.should be_kind_of(Dir) end @@ -60,4 +60,14 @@ describe :dir_open, shared: true do dir = Dir.send(@method, DirSpecs.mock_dir, encoding: nil) {|d| d } dir.should be_kind_of(Dir) end + + platform_is_not :windows do + it 'sets the close-on-exec flag for the directory file descriptor' do + Dir.send(@method, DirSpecs.mock_dir) do |dir| + io = IO.for_fd(dir.fileno) + io.autoclose = false + io.should.close_on_exec? + end + end + end end diff --git a/spec/ruby/core/dir/shared/path.rb b/spec/ruby/core/dir/shared/path.rb index 829eeb04c2..494dcca775 100644 --- a/spec/ruby/core/dir/shared/path.rb +++ b/spec/ruby/core/dir/shared/path.rb @@ -1,6 +1,6 @@ -require File.expand_path('../../../../spec_helper', __FILE__) -require File.expand_path('../../fixtures/common', __FILE__) -require File.expand_path('../closed', __FILE__) +require_relative '../../../spec_helper' +require_relative '../fixtures/common' +require_relative 'closed' describe :dir_path, shared: true do it "returns the path that was supplied to .new or .open" do @@ -18,15 +18,13 @@ describe :dir_path, shared: true do dir.send(@method).should == DirSpecs.mock_dir end - with_feature :encoding do - it "returns a String with the same encoding as the argument to .open" do - path = DirSpecs.mock_dir.force_encoding Encoding::IBM866 - dir = Dir.open path - begin - dir.send(@method).encoding.should equal(Encoding::IBM866) - ensure - dir.close - end + it "returns a String with the same encoding as the argument to .open" do + path = DirSpecs.mock_dir.force_encoding Encoding::IBM866 + dir = Dir.open path + begin + dir.send(@method).encoding.should equal(Encoding::IBM866) + ensure + dir.close end end end diff --git a/spec/ruby/core/dir/shared/pwd.rb b/spec/ruby/core/dir/shared/pwd.rb index 5f041a9d41..2a8d7fe790 100644 --- a/spec/ruby/core/dir/shared/pwd.rb +++ b/spec/ruby/core/dir/shared/pwd.rb @@ -1,8 +1,6 @@ describe :dir_pwd, shared: true do - with_feature :encoding do - before :each do - @fs_encoding = Encoding.find('filesystem') - end + before :each do + @fs_encoding = Encoding.find('filesystem') end it "returns the current working directory" do @@ -36,14 +34,12 @@ describe :dir_pwd, shared: true do end end - with_feature :encoding do - it "returns a String with the filesystem encoding" do - enc = Dir.send(@method).encoding - if @fs_encoding == Encoding::US_ASCII - [Encoding::US_ASCII, Encoding::ASCII_8BIT].should include(enc) - else - enc.should equal(@fs_encoding) - end + it "returns a String with the filesystem encoding" do + enc = Dir.send(@method).encoding + if @fs_encoding == Encoding::US_ASCII + [Encoding::US_ASCII, Encoding::BINARY].should include(enc) + else + enc.should equal(@fs_encoding) end end end diff --git a/spec/ruby/core/dir/tell_spec.rb b/spec/ruby/core/dir/tell_spec.rb index fb9848153d..af86dc1598 100644 --- a/spec/ruby/core/dir/tell_spec.rb +++ b/spec/ruby/core/dir/tell_spec.rb @@ -1,7 +1,7 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/common', __FILE__) -require File.expand_path('../shared/closed', __FILE__) -require File.expand_path('../shared/pos', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/common' +require_relative 'shared/closed' +require_relative 'shared/pos' describe "Dir#tell" do before :all do diff --git a/spec/ruby/core/dir/to_path_spec.rb b/spec/ruby/core/dir/to_path_spec.rb index 85609fbfff..77404a3dc8 100644 --- a/spec/ruby/core/dir/to_path_spec.rb +++ b/spec/ruby/core/dir/to_path_spec.rb @@ -1,6 +1,6 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/common', __FILE__) -require File.expand_path('../shared/path', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/common' +require_relative 'shared/path' describe "Dir#to_path" do before :all do @@ -11,5 +11,5 @@ describe "Dir#to_path" do DirSpecs.delete_mock_dirs end - it_behaves_like(:dir_path, :to_path) + it_behaves_like :dir_path, :to_path end diff --git a/spec/ruby/core/dir/unlink_spec.rb b/spec/ruby/core/dir/unlink_spec.rb index 4459bef56c..79027e020c 100644 --- a/spec/ruby/core/dir/unlink_spec.rb +++ b/spec/ruby/core/dir/unlink_spec.rb @@ -1,6 +1,6 @@ -require File.expand_path('../../../spec_helper', __FILE__) -require File.expand_path('../fixtures/common', __FILE__) -require File.expand_path('../shared/delete', __FILE__) +require_relative '../../spec_helper' +require_relative 'fixtures/common' +require_relative 'shared/delete' describe "Dir.unlink" do before :all do |
