From 1d15d5f08032acf1b7bceacbb450d617ff6e0931 Mon Sep 17 00:00:00 2001 From: eregon Date: Wed, 20 Sep 2017 20:18:52 +0000 Subject: Move spec/rubyspec to spec/ruby for consistency * Other ruby implementations use the spec/ruby directory. [Misc #13792] [ruby-core:82287] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59979 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- spec/ruby/core/dir/chdir_spec.rb | 124 ++++++++++ spec/ruby/core/dir/chroot_spec.rb | 47 ++++ spec/ruby/core/dir/close_spec.rb | 29 +++ spec/ruby/core/dir/delete_spec.rb | 15 ++ spec/ruby/core/dir/dir_spec.rb | 7 + spec/ruby/core/dir/each_spec.rb | 64 ++++++ spec/ruby/core/dir/element_reference_spec.rb | 33 +++ spec/ruby/core/dir/entries_spec.rb | 70 ++++++ spec/ruby/core/dir/exist_spec.rb | 15 ++ spec/ruby/core/dir/exists_spec.rb | 15 ++ spec/ruby/core/dir/fileno_spec.rb | 37 +++ spec/ruby/core/dir/fixtures/common.rb | 169 ++++++++++++++ spec/ruby/core/dir/foreach_spec.rb | 56 +++++ spec/ruby/core/dir/getwd_spec.rb | 15 ++ spec/ruby/core/dir/glob_spec.rb | 156 +++++++++++++ spec/ruby/core/dir/home_spec.rb | 26 +++ spec/ruby/core/dir/initialize_spec.rb | 23 ++ spec/ruby/core/dir/inspect_spec.rb | 24 ++ spec/ruby/core/dir/mkdir_spec.rb | 85 +++++++ spec/ruby/core/dir/open_spec.rb | 15 ++ spec/ruby/core/dir/path_spec.rb | 15 ++ spec/ruby/core/dir/pos_spec.rb | 40 ++++ spec/ruby/core/dir/pwd_spec.rb | 39 ++++ spec/ruby/core/dir/read_spec.rb | 43 ++++ spec/ruby/core/dir/rewind_spec.rb | 36 +++ spec/ruby/core/dir/rmdir_spec.rb | 15 ++ spec/ruby/core/dir/seek_spec.rb | 19 ++ spec/ruby/core/dir/shared/chroot.rb | 41 ++++ spec/ruby/core/dir/shared/closed.rb | 9 + spec/ruby/core/dir/shared/delete.rb | 61 +++++ spec/ruby/core/dir/shared/exist.rb | 56 +++++ spec/ruby/core/dir/shared/glob.rb | 328 +++++++++++++++++++++++++++ spec/ruby/core/dir/shared/open.rb | 63 +++++ spec/ruby/core/dir/shared/path.rb | 32 +++ spec/ruby/core/dir/shared/pos.rb | 51 +++++ spec/ruby/core/dir/shared/pwd.rb | 49 ++++ spec/ruby/core/dir/tell_spec.rb | 18 ++ spec/ruby/core/dir/to_path_spec.rb | 15 ++ spec/ruby/core/dir/unlink_spec.rb | 15 ++ 39 files changed, 1970 insertions(+) create mode 100644 spec/ruby/core/dir/chdir_spec.rb create mode 100644 spec/ruby/core/dir/chroot_spec.rb create mode 100644 spec/ruby/core/dir/close_spec.rb create mode 100644 spec/ruby/core/dir/delete_spec.rb create mode 100644 spec/ruby/core/dir/dir_spec.rb create mode 100644 spec/ruby/core/dir/each_spec.rb create mode 100644 spec/ruby/core/dir/element_reference_spec.rb create mode 100644 spec/ruby/core/dir/entries_spec.rb create mode 100644 spec/ruby/core/dir/exist_spec.rb create mode 100644 spec/ruby/core/dir/exists_spec.rb create mode 100644 spec/ruby/core/dir/fileno_spec.rb create mode 100644 spec/ruby/core/dir/fixtures/common.rb create mode 100644 spec/ruby/core/dir/foreach_spec.rb create mode 100644 spec/ruby/core/dir/getwd_spec.rb create mode 100644 spec/ruby/core/dir/glob_spec.rb create mode 100644 spec/ruby/core/dir/home_spec.rb create mode 100644 spec/ruby/core/dir/initialize_spec.rb create mode 100644 spec/ruby/core/dir/inspect_spec.rb create mode 100644 spec/ruby/core/dir/mkdir_spec.rb create mode 100644 spec/ruby/core/dir/open_spec.rb create mode 100644 spec/ruby/core/dir/path_spec.rb create mode 100644 spec/ruby/core/dir/pos_spec.rb create mode 100644 spec/ruby/core/dir/pwd_spec.rb create mode 100644 spec/ruby/core/dir/read_spec.rb create mode 100644 spec/ruby/core/dir/rewind_spec.rb create mode 100644 spec/ruby/core/dir/rmdir_spec.rb create mode 100644 spec/ruby/core/dir/seek_spec.rb create mode 100644 spec/ruby/core/dir/shared/chroot.rb create mode 100644 spec/ruby/core/dir/shared/closed.rb create mode 100644 spec/ruby/core/dir/shared/delete.rb create mode 100644 spec/ruby/core/dir/shared/exist.rb create mode 100644 spec/ruby/core/dir/shared/glob.rb create mode 100644 spec/ruby/core/dir/shared/open.rb create mode 100644 spec/ruby/core/dir/shared/path.rb create mode 100644 spec/ruby/core/dir/shared/pos.rb create mode 100644 spec/ruby/core/dir/shared/pwd.rb create mode 100644 spec/ruby/core/dir/tell_spec.rb create mode 100644 spec/ruby/core/dir/to_path_spec.rb create mode 100644 spec/ruby/core/dir/unlink_spec.rb (limited to 'spec/ruby/core/dir') diff --git a/spec/ruby/core/dir/chdir_spec.rb b/spec/ruby/core/dir/chdir_spec.rb new file mode 100644 index 0000000000..f5b0b80d1c --- /dev/null +++ b/spec/ruby/core/dir/chdir_spec.rb @@ -0,0 +1,124 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/common', __FILE__) + +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 "defaults to $HOME with no arguments" do + if ENV['HOME'] + Dir.chdir + current_dir = Dir.pwd + + Dir.chdir(ENV['HOME']) + home = Dir.pwd + current_dir.should == home + end + end + + it "changes to the specified directory" do + Dir.chdir DirSpecs.mock_dir + Dir.pwd.should == DirSpecs.mock_dir + end + + it "returns 0 when successfully changing directory" do + Dir.chdir(@original).should == 0 + end + + it "calls #to_str on the argument if it's not a String" do + obj = mock('path') + obj.should_receive(:to_str).and_return(Dir.pwd) + Dir.chdir(obj) + end + + it "calls #to_str on the argument if it's not a String and a block is given" do + obj = mock('path') + obj.should_receive(:to_str).and_return(Dir.pwd) + Dir.chdir(obj) { } + end + + it "calls #to_path on the argument if it's not a String" do + obj = mock('path') + obj.should_receive(:to_path).and_return(Dir.pwd) + Dir.chdir(obj) + end + + it "prefers #to_path over #to_str" do + obj = Class.new do + def to_path; Dir.pwd; end + def to_str; DirSpecs.mock_dir; end + end + Dir.chdir(obj.new) + Dir.pwd.should == @original + end + + it "returns the value of the block when a block is given" do + Dir.chdir(@original) { :block_value }.should == :block_value + end + + it "defaults to the home directory when given a block but no argument" do + # 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 = "" + Dir.chdir { current_dir = Dir.pwd } + + Dir.chdir(ENV['HOME']) + home = Dir.pwd + current_dir.should == home + end + + it "changes to the specified directory for the duration of the block" do + ar = Dir.chdir(DirSpecs.mock_dir) { |dir| [dir, Dir.pwd] } + ar.should == [DirSpecs.mock_dir, DirSpecs.mock_dir] + + Dir.pwd.should == @original + 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) + 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 + 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) + end + end + + it "always returns to the original directory when given a block" do + begin + Dir.chdir(DirSpecs.mock_dir) do + raise StandardError, "something bad happened" + end + rescue StandardError + end + + Dir.pwd.should == @original + end +end diff --git a/spec/ruby/core/dir/chroot_spec.rb b/spec/ruby/core/dir/chroot_spec.rb new file mode 100644 index 0000000000..1afe254957 --- /dev/null +++ b/spec/ruby/core/dir/chroot_spec.rb @@ -0,0 +1,47 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/common', __FILE__) +require File.expand_path('../shared/chroot', __FILE__) + +platform_is_not :windows do + as_superuser do + describe "Dir.chroot as root" do + it_behaves_like :dir_chroot_as_root, :chroot + end + end + + platform_is_not :cygwin do + as_user do + describe "Dir.chroot as regular user" do + before :all do + DirSpecs.create_mock_dirs + end + + after :all do + DirSpecs.delete_mock_dirs + end + + it "raises an Errno::EPERM exception if the directory exists" do + lambda { 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) + 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 + end + end + end + end + + platform_is :cygwin do + as_user do + describe "Dir.chroot as regular user" do + it_behaves_like :dir_chroot_as_root, :chroot + end + end + end +end diff --git a/spec/ruby/core/dir/close_spec.rb b/spec/ruby/core/dir/close_spec.rb new file mode 100644 index 0000000000..7b08ec5ee8 --- /dev/null +++ b/spec/ruby/core/dir/close_spec.rb @@ -0,0 +1,29 @@ +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 + +describe "Dir#close" do + before :all do + DirSpecs.create_mock_dirs + end + + after :all do + DirSpecs.delete_mock_dirs + end + + ruby_version_is ''...'2.3' do + it_behaves_like :dir_closed, :close + 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 { + dir.close + }.should_not raise_error(IOError) + end + end +end diff --git a/spec/ruby/core/dir/delete_spec.rb b/spec/ruby/core/dir/delete_spec.rb new file mode 100644 index 0000000000..5f36956839 --- /dev/null +++ b/spec/ruby/core/dir/delete_spec.rb @@ -0,0 +1,15 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/common', __FILE__) +require File.expand_path('../shared/delete', __FILE__) + +describe "Dir.delete" do + before :all do + DirSpecs.create_mock_dirs + end + + after :all do + DirSpecs.delete_mock_dirs + end + + it_behaves_like :dir_delete, :delete +end diff --git a/spec/ruby/core/dir/dir_spec.rb b/spec/ruby/core/dir/dir_spec.rb new file mode 100644 index 0000000000..4923445bed --- /dev/null +++ b/spec/ruby/core/dir/dir_spec.rb @@ -0,0 +1,7 @@ +require File.expand_path('../../../spec_helper', __FILE__) + +describe "Dir" do + it "includes Enumerable" do + Dir.include?(Enumerable).should == true + end +end diff --git a/spec/ruby/core/dir/each_spec.rb b/spec/ruby/core/dir/each_spec.rb new file mode 100644 index 0000000000..534691ff58 --- /dev/null +++ b/spec/ruby/core/dir/each_spec.rb @@ -0,0 +1,64 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/common', __FILE__) +require File.expand_path('../shared/closed', __FILE__) + +describe "Dir#each" do + before :all do + DirSpecs.create_mock_dirs + end + + after :all do + DirSpecs.delete_mock_dirs + end + + before :each do + @dir = Dir.open DirSpecs.mock_dir + end + + after :each do + @dir.close + end + + it "yields each directory entry in succession" do + a = [] + @dir.each {|dir| a << dir} + + a.sort.should == DirSpecs.expected_paths + end + + it "returns the directory which remains open" do + # an FS does not necessarily impose order + ls = Dir.entries(DirSpecs.mock_dir) + @dir.each {}.should == @dir + @dir.read.should == nil + @dir.rewind + ls.should include(@dir.read) + end + + describe "when no block is given" do + it "returns an Enumerator" do + @dir.each.should be_an_instance_of(Enumerator) + @dir.each.to_a.sort.should == DirSpecs.expected_paths + end + + describe "returned Enumerator" do + describe "size" do + it "should return nil" do + @dir.each.size.should == nil + end + end + end + end +end + +describe "Dir#each" do + before :all do + DirSpecs.create_mock_dirs + end + + after :all do + DirSpecs.delete_mock_dirs + end + + it_behaves_like :dir_closed, :each +end diff --git a/spec/ruby/core/dir/element_reference_spec.rb b/spec/ruby/core/dir/element_reference_spec.rb new file mode 100644 index 0000000000..de379d75ac --- /dev/null +++ b/spec/ruby/core/dir/element_reference_spec.rb @@ -0,0 +1,33 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/common', __FILE__) +require File.expand_path('../shared/glob', __FILE__) + +describe "Dir.[]" do + it_behaves_like :dir_glob, :[] +end + +describe "Dir.[]" do + it_behaves_like :dir_glob_recursive, :[] +end + +describe "Dir.[]" do + before :all do + DirSpecs.create_mock_dirs + @cwd = Dir.pwd + Dir.chdir DirSpecs.mock_dir + end + + after :all do + Dir.chdir @cwd + DirSpecs.delete_mock_dirs + 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') + pat2 = mock('file_two.ext') + pat2.should_receive(:to_path).and_return('file_two.ext') + + Dir[pat1, pat2].should == %w[file_one.ext file_two.ext] + end +end diff --git a/spec/ruby/core/dir/entries_spec.rb b/spec/ruby/core/dir/entries_spec.rb new file mode 100644 index 0000000000..8a31ab4b4a --- /dev/null +++ b/spec/ruby/core/dir/entries_spec.rb @@ -0,0 +1,70 @@ +# encoding: utf-8 + +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/common', __FILE__) + +describe "Dir.entries" 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 + end + + it "returns an Array of filenames in an existing directory including dotfiles" do + a = Dir.entries(DirSpecs.mock_dir).sort + + a.should == DirSpecs.expected_paths + + a = Dir.entries("#{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.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| + 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 = Dir.entries(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 + entries.should include("こんにちは.txt".force_encoding(encoding)) + end + entries.first.encoding.should equal(Encoding.find("filesystem")) + end + + it "returns entries encoded with the specified encoding" do + dir = File.join(DirSpecs.mock_dir, 'special') + entries = Dir.entries(dir, encoding: "euc-jp").sort + entries.first.encoding.should equal(Encoding::EUC_JP) + end + + it "returns entries transcoded to the default internal encoding" do + Encoding.default_internal = Encoding::EUC_KR + entries = Dir.entries(File.join(DirSpecs.mock_dir, 'special')).sort + 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) + end +end diff --git a/spec/ruby/core/dir/exist_spec.rb b/spec/ruby/core/dir/exist_spec.rb new file mode 100644 index 0000000000..194284b5a0 --- /dev/null +++ b/spec/ruby/core/dir/exist_spec.rb @@ -0,0 +1,15 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/common', __FILE__) +require File.expand_path('../shared/exist', __FILE__) + +describe "Dir.exist?" do + before :all do + DirSpecs.create_mock_dirs + end + + after :all do + DirSpecs.delete_mock_dirs + end + + it_behaves_like(:dir_exist, :exist?) +end diff --git a/spec/ruby/core/dir/exists_spec.rb b/spec/ruby/core/dir/exists_spec.rb new file mode 100644 index 0000000000..002506a22f --- /dev/null +++ b/spec/ruby/core/dir/exists_spec.rb @@ -0,0 +1,15 @@ +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/fileno_spec.rb b/spec/ruby/core/dir/fileno_spec.rb new file mode 100644 index 0000000000..cf8b811e3b --- /dev/null +++ b/spec/ruby/core/dir/fileno_spec.rb @@ -0,0 +1,37 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/common', __FILE__) + +has_dir_fileno = begin + dir = Dir.new('.') + dir.fileno + true +rescue NotImplementedError + false +rescue Exception + true +ensure + dir.close +end + +describe "Dir#fileno" do + before :each do + @name = tmp("fileno") + mkdir_p @name + @dir = Dir.new(@name) + end + + after :each do + @dir.close + rm_r @name + end + + if has_dir_fileno + it "returns the file descriptor of the dir" do + @dir.fileno.should be_kind_of(Fixnum) + end + else + it "raises an error when not implemented on the platform" do + lambda { @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 new file mode 100644 index 0000000000..f6708b04f7 --- /dev/null +++ b/spec/ruby/core/dir/fixtures/common.rb @@ -0,0 +1,169 @@ +# encoding: utf-8 + +module DirSpecs + def self.mock_dir(dirs = ['dir_specs_mock']) + @mock_dir ||= tmp("") + File.join @mock_dir, dirs + end + + def self.nonexistent + name = File.join mock_dir, "nonexistent00" + name = name.next while File.exist? name + name + end + + # TODO: make these relative to the mock_dir + def self.clear_dirs + [ 'nonexisting', + 'default_perms', + 'reduced', + 'always_returns_0', + '???', + [0xe9].pack('U') + ].each do |dir| + begin + Dir.rmdir dir + rescue + end + end + end + + # The names of the fixture directories and files used by + # various Dir specs. + def self.mock_dir_files + unless @mock_dir_files + @mock_dir_files = %w[ + .dotfile + .dotsubdir/.dotfile + .dotsubdir/nondotfile + + deeply/.dotfile + deeply/nested/.dotfile.ext + deeply/nested/directory/structure/.ext + deeply/nested/directory/structure/bar + deeply/nested/directory/structure/baz + deeply/nested/directory/structure/file_one + deeply/nested/directory/structure/file_one.ext + deeply/nested/directory/structure/foo + deeply/nondotfile + + file_one.ext + file_two.ext + + dir_filename_ordering + dir/filename_ordering + + nondotfile + + subdir_one/.dotfile + subdir_one/nondotfile + subdir_two/nondotfile + subdir_two/nondotfile.ext + + brace/a + brace/a.js + brace/a.erb + brace/a.js.rjs + brace/a.html.erb + + special/+ + + special/^ + special/$ + + special/( + special/) + special/[ + special/] + special/{ + special/} + + special/test{1}/file[1] + ] + + platform_is_not :windows do + @mock_dir_files += %w[ + special/* + special/? + + special/| + + special/こんにちは.txt + ] + end + end + + @mock_dir_files + 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 + end + + def self.delete_mock_dirs + begin + rm_r mock_dir + rescue Errno::ENOTEMPTY => e + puts Dir["#{mock_dir}/**/*"] + raise e + end + end + + def self.mock_rmdir(*dirs) + mock_dir ['rmdir_dirs'].concat(dirs) + end + + def self.rmdir_dirs(create = true) + dirs = %w[ + empty + nonempty + nonempty/child + noperm + noperm/child + ] + + base_dir = mock_dir ['rmdir_dirs'] + + dirs.reverse_each do |d| + dir = File.join base_dir, d + if File.exist? dir + File.chmod 0777, dir + rm_r dir + end + end + rm_r base_dir + + if create + dirs.each do |d| + dir = File.join base_dir, d + unless File.exist? dir + mkdir_p dir + File.chmod 0777, dir + end + end + end + end + + def self.expected_paths + %w[ + . + .. + .dotfile + .dotsubdir + brace + deeply + dir + dir_filename_ordering + file_one.ext + file_two.ext + nondotfile + special + subdir_one + subdir_two + ] + end +end diff --git a/spec/ruby/core/dir/foreach_spec.rb b/spec/ruby/core/dir/foreach_spec.rb new file mode 100644 index 0000000000..e606b4f65c --- /dev/null +++ b/spec/ruby/core/dir/foreach_spec.rb @@ -0,0 +1,56 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/common', __FILE__) + +describe "Dir.foreach" do + before :all do + DirSpecs.create_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 = [], [] + + Dir.foreach(DirSpecs.mock_dir) {|f| a << f} + Dir.foreach("#{DirSpecs.mock_dir}/deeply/nested") {|f| b << f} + + a.sort.should == DirSpecs.expected_paths + b.sort.should == %w|. .. .dotfile.ext directory| + end + + it "returns nil when successful" do + Dir.foreach(DirSpecs.mock_dir) {|f| f}.should == nil + end + + it "calls #to_path on non-String arguments" do + p = mock('path') + p.should_receive(:to_path).and_return(DirSpecs.mock_dir) + Dir.foreach(p).to_a + end + + it "raises a SystemCallError if passed a nonexistent directory" do + lambda { Dir.foreach(DirSpecs.nonexistent) {} }.should raise_error(SystemCallError) + end + + it "returns an Enumerator if no block given" do + Dir.foreach(DirSpecs.mock_dir).should be_an_instance_of(Enumerator) + Dir.foreach(DirSpecs.mock_dir).to_a.sort.should == DirSpecs.expected_paths + end + + describe "when no block is given" do + it "returns an Enumerator" do + Dir.foreach(DirSpecs.mock_dir).should be_an_instance_of(Enumerator) + Dir.foreach(DirSpecs.mock_dir).to_a.sort.should == DirSpecs.expected_paths + end + + describe "returned Enumerator" do + describe "size" do + it "should return nil" do + Dir.foreach(DirSpecs.mock_dir).size.should == nil + end + end + end + end +end diff --git a/spec/ruby/core/dir/getwd_spec.rb b/spec/ruby/core/dir/getwd_spec.rb new file mode 100644 index 0000000000..26659ddec7 --- /dev/null +++ b/spec/ruby/core/dir/getwd_spec.rb @@ -0,0 +1,15 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/common', __FILE__) +require File.expand_path('../shared/pwd', __FILE__) + +describe "Dir.getwd" do + before :all do + DirSpecs.create_mock_dirs + end + + after :all do + DirSpecs.delete_mock_dirs + end + + it_behaves_like :dir_pwd, :getwd +end diff --git a/spec/ruby/core/dir/glob_spec.rb b/spec/ruby/core/dir/glob_spec.rb new file mode 100644 index 0000000000..b65b738b61 --- /dev/null +++ b/spec/ruby/core/dir/glob_spec.rb @@ -0,0 +1,156 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/common', __FILE__) +require File.expand_path('../shared/glob', __FILE__) + +describe "Dir.glob" do + it_behaves_like :dir_glob, :glob +end + +describe "Dir.glob" do + it_behaves_like :dir_glob_recursive, :glob +end + +describe "Dir.glob" do + before :each do + DirSpecs.create_mock_dirs + + @cwd = Dir.pwd + Dir.chdir DirSpecs.mock_dir + end + + after :each do + Dir.chdir @cwd + + DirSpecs.delete_mock_dirs + end + + it "can take an array of patterns" do + Dir.glob(["file_o*", "file_t*"]).should == + %w!file_one.ext file_two.ext! + 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') + pat2 = mock('file_two.ext') + pat2.should_receive(:to_path).and_return('file_two.ext') + + Dir.glob([pat1, pat2]).should == %w[file_one.ext file_two.ext] + 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 + end + + it "matches files with any beginning with '*' 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 == DirSpecs.expected_paths + end + + it "recursively matches any subdirectories except './' or '../' with '**/' from the current directory and option File::FNM_DOTMATCH" do + expected = %w[ + .dotsubdir/ + brace/ + deeply/ + deeply/nested/ + deeply/nested/directory/ + deeply/nested/directory/structure/ + dir/ + special/ + special/test{1}/ + subdir_one/ + subdir_two/ + ] + + Dir.glob('**/', File::FNM_DOTMATCH).sort.should == expected + end + + # This is a seperate case to check **/ coming after a constant + # directory as well. + it "recursively matches any subdirectories except './' or '../' with '**/' and option File::FNM_DOTMATCH" do + expected = %w[ + ./ + ./.dotsubdir/ + ./brace/ + ./deeply/ + ./deeply/nested/ + ./deeply/nested/directory/ + ./deeply/nested/directory/structure/ + ./dir/ + ./special/ + ./special/test{1}/ + ./subdir_one/ + ./subdir_two/ + ] + + Dir.glob('./**/', File::FNM_DOTMATCH).sort.should == expected + end + + it "matches a list of paths by concatenating their individual results" do + expected = %w[ + deeply/ + deeply/nested/ + deeply/nested/directory/ + deeply/nested/directory/structure/ + subdir_two/nondotfile + subdir_two/nondotfile.ext + ] + + Dir.glob('{deeply/**/,subdir_two/*}').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 } + ret.should be_nil + ary.should == %w!file_one.ext file_two.ext! + end + + it "ignores non-dirs when traversing recursively" do + touch "spec" + Dir.glob("spec/**/*.rb").should == [] + end + + it "matches nothing when given an empty list of paths" do + Dir.glob('{}').should == [] + end + + it "handles infinite directory wildcards" do + Dir.glob('**/**/**').empty?.should == false + end + + platform_is_not(:windows) do + it "matches the literal character '\\' with option File::FNM_NOESCAPE" do + Dir.mkdir 'foo?bar' + + begin + Dir.glob('foo?bar', File::FNM_NOESCAPE).should == %w|foo?bar| + Dir.glob('foo\?bar', File::FNM_NOESCAPE).should == [] + ensure + Dir.rmdir 'foo?bar' + end + + Dir.mkdir 'foo\?bar' + + begin + Dir.glob('foo\?bar', File::FNM_NOESCAPE).should == %w|foo\\?bar| + ensure + Dir.rmdir 'foo\?bar' + end + end + + it "returns nil for directories current user has no permission to read" do + Dir.mkdir('no_permission') + File.chmod(0, 'no_permission') + + begin + Dir.glob('no_permission/*').should == [] + ensure + Dir.rmdir('no_permission') + end + end + end +end diff --git a/spec/ruby/core/dir/home_spec.rb b/spec/ruby/core/dir/home_spec.rb new file mode 100644 index 0000000000..6d99678034 --- /dev/null +++ b/spec/ruby/core/dir/home_spec.rb @@ -0,0 +1,26 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/common', __FILE__) + +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'] + end + home_directory = home_directory.tr('\\', '/').chomp('/') + end + + Dir.home.should == home_directory + 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'] + end + end + + it "raises an ArgumentError if the named user doesn't exist" do + lambda { Dir.home('geuw2n288dh2k') }.should raise_error(ArgumentError) + end +end diff --git a/spec/ruby/core/dir/initialize_spec.rb b/spec/ruby/core/dir/initialize_spec.rb new file mode 100644 index 0000000000..b9420647d1 --- /dev/null +++ b/spec/ruby/core/dir/initialize_spec.rb @@ -0,0 +1,23 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/common', __FILE__) + +describe "Dir#initialize" do + before :each do + DirSpecs.create_mock_dirs + end + + after :each do + DirSpecs.delete_mock_dirs + end + + it "calls #to_path on non-String arguments" do + p = mock('path') + p.stub!(:to_path).and_return(DirSpecs.mock_dir) + dir = Dir.new(p) + begin + dir.path.should == DirSpecs.mock_dir + ensure + dir.close + end + end +end diff --git a/spec/ruby/core/dir/inspect_spec.rb b/spec/ruby/core/dir/inspect_spec.rb new file mode 100644 index 0000000000..01bde8a862 --- /dev/null +++ b/spec/ruby/core/dir/inspect_spec.rb @@ -0,0 +1,24 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/common', __FILE__) + +describe "Dir#inspect" do + before :each do + @dir = Dir.new(Dir.getwd) + end + + after :each do + @dir.close + end + + it "returns a String" do + @dir.inspect.should be_an_instance_of(String) + end + + it "includes the class name" do + @dir.inspect.should =~ /Dir/ + end + + it "includes the directory name" do + @dir.inspect.should include(Dir.getwd) + end +end diff --git a/spec/ruby/core/dir/mkdir_spec.rb b/spec/ruby/core/dir/mkdir_spec.rb new file mode 100644 index 0000000000..7eb8a8fe6c --- /dev/null +++ b/spec/ruby/core/dir/mkdir_spec.rb @@ -0,0 +1,85 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/common', __FILE__) + +describe "Dir.mkdir" do + before :all do + DirSpecs.create_mock_dirs + end + + after :all do + DirSpecs.delete_mock_dirs + end + + it "creates the named directory with the given permissions" do + DirSpecs.clear_dirs + + begin + File.exist?('nonexisting').should == false + Dir.mkdir 'nonexisting' + File.exist?('nonexisting').should == true + 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 + 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 + end + + Dir.mkdir('always_returns_0').should == 0 + platform_is_not(:windows) do + 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") + end + ensure + DirSpecs.clear_dirs + end + end + + it "calls #to_path on non-String arguments" do + DirSpecs.clear_dirs + p = mock('path') + p.should_receive(:to_path).and_return('nonexisting') + Dir.mkdir(p) + DirSpecs.clear_dirs + 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) + end + + it "raises Errno::EEXIST if the specified directory already exists" do + lambda { 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) + 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 + + 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 + + lambda { Dir.mkdir "#{@dir}/subdir" }.should raise_error(SystemCallError) + end + end +end diff --git a/spec/ruby/core/dir/open_spec.rb b/spec/ruby/core/dir/open_spec.rb new file mode 100644 index 0000000000..b3deed47b7 --- /dev/null +++ b/spec/ruby/core/dir/open_spec.rb @@ -0,0 +1,15 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/common', __FILE__) +require File.expand_path('../shared/open', __FILE__) + +describe "Dir.open" do + before :all do + DirSpecs.create_mock_dirs + end + + after :all do + DirSpecs.delete_mock_dirs + end + + it_behaves_like :dir_open, :open +end diff --git a/spec/ruby/core/dir/path_spec.rb b/spec/ruby/core/dir/path_spec.rb new file mode 100644 index 0000000000..1601220636 --- /dev/null +++ b/spec/ruby/core/dir/path_spec.rb @@ -0,0 +1,15 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/common', __FILE__) +require File.expand_path('../shared/path', __FILE__) + +describe "Dir#path" do + before :all do + DirSpecs.create_mock_dirs + end + + after :all do + DirSpecs.delete_mock_dirs + end + + 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 new file mode 100644 index 0000000000..9f05fab250 --- /dev/null +++ b/spec/ruby/core/dir/pos_spec.rb @@ -0,0 +1,40 @@ +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__) + +describe "Dir#pos" do + before :all do + DirSpecs.create_mock_dirs + end + + after :all do + DirSpecs.delete_mock_dirs + end + + it_behaves_like :dir_pos, :pos +end + +describe "Dir#pos" do + before :all do + DirSpecs.create_mock_dirs + end + + after :all do + DirSpecs.delete_mock_dirs + end + + it_behaves_like :dir_closed, :pos +end + +describe "Dir#pos=" do + before :all do + DirSpecs.create_mock_dirs + end + + after :all do + DirSpecs.delete_mock_dirs + end + + it_behaves_like :dir_pos_set, :pos= +end diff --git a/spec/ruby/core/dir/pwd_spec.rb b/spec/ruby/core/dir/pwd_spec.rb new file mode 100644 index 0000000000..4fa86dd6b9 --- /dev/null +++ b/spec/ruby/core/dir/pwd_spec.rb @@ -0,0 +1,39 @@ +# -*- encoding: utf-8 -*- +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/common', __FILE__) +require File.expand_path('../shared/pwd', __FILE__) + +describe "Dir.pwd" do + before :all do + DirSpecs.create_mock_dirs + end + + after :all do + DirSpecs.delete_mock_dirs + end + + it_behaves_like :dir_pwd, :pwd +end + +describe "Dir.pwd" do + before :each do + @name = tmp("あ").force_encoding('binary') + @fs_encoding = Encoding.find('filesystem') + end + + after :each do + rm_r @name + end + + platform_is_not :windows do + it "correctly handles dirs with unicode characters in them" do + Dir.mkdir @name + Dir.chdir @name do + if @fs_encoding == Encoding::UTF_8 + Dir.pwd.encoding.should == Encoding::UTF_8 + end + Dir.pwd.force_encoding('binary').should == @name + end + end + end +end diff --git a/spec/ruby/core/dir/read_spec.rb b/spec/ruby/core/dir/read_spec.rb new file mode 100644 index 0000000000..79ed9b8058 --- /dev/null +++ b/spec/ruby/core/dir/read_spec.rb @@ -0,0 +1,43 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/common', __FILE__) +require File.expand_path('../shared/closed', __FILE__) + +describe "Dir#read" do + before :all do + DirSpecs.create_mock_dirs + end + + after :all do + DirSpecs.delete_mock_dirs + end + + it "returns the file name in the current seek position" do + # an FS does not necessarily impose order + ls = Dir.entries DirSpecs.mock_dir + dir = Dir.open DirSpecs.mock_dir + ls.should include(dir.read) + dir.close + end + + it "returns nil when there are no more entries" do + dir = Dir.open DirSpecs.mock_dir + DirSpecs.expected_paths.size.times do + dir.read.should_not == nil + end + dir.read.should == nil + dir.close + end + + it "returns each entry successively" do + dir = Dir.open DirSpecs.mock_dir + entries = [] + while entry = dir.read + entries << entry + end + dir.close + + entries.sort.should == DirSpecs.expected_paths + 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 new file mode 100644 index 0000000000..65ffcdf1c3 --- /dev/null +++ b/spec/ruby/core/dir/rewind_spec.rb @@ -0,0 +1,36 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/common', __FILE__) +require File.expand_path('../shared/closed', __FILE__) + +describe "Dir#rewind" do + before :all do + DirSpecs.create_mock_dirs + end + + after :all do + DirSpecs.delete_mock_dirs + end + + before :each do + @dir = Dir.open DirSpecs.mock_dir + end + + after :each do + @dir.close + end + + it "resets the next read to start from the first entry" do + a = @dir.read + b = @dir.read + a.should_not == b + @dir.rewind + c = @dir.read + c.should == a + end + + it "returns the Dir instance" do + @dir.rewind.should == @dir + end + + it_behaves_like :dir_closed, :rewind +end diff --git a/spec/ruby/core/dir/rmdir_spec.rb b/spec/ruby/core/dir/rmdir_spec.rb new file mode 100644 index 0000000000..09499572c0 --- /dev/null +++ b/spec/ruby/core/dir/rmdir_spec.rb @@ -0,0 +1,15 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/common', __FILE__) +require File.expand_path('../shared/delete', __FILE__) + +describe "Dir.rmdir" do + before :all do + DirSpecs.create_mock_dirs + end + + after :all do + DirSpecs.delete_mock_dirs + end + + it_behaves_like :dir_delete, :rmdir +end diff --git a/spec/ruby/core/dir/seek_spec.rb b/spec/ruby/core/dir/seek_spec.rb new file mode 100644 index 0000000000..b51e554441 --- /dev/null +++ b/spec/ruby/core/dir/seek_spec.rb @@ -0,0 +1,19 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/common', __FILE__) +require File.expand_path('../shared/pos', __FILE__) + +describe "Dir#seek" do + before :all do + DirSpecs.create_mock_dirs + end + + after :all do + DirSpecs.delete_mock_dirs + end + + it "returns the Dir instance" do + @dir.seek(@dir.pos).should == @dir + end + + it_behaves_like :dir_pos_set, :seek +end diff --git a/spec/ruby/core/dir/shared/chroot.rb b/spec/ruby/core/dir/shared/chroot.rb new file mode 100644 index 0000000000..2ed033dfed --- /dev/null +++ b/spec/ruby/core/dir/shared/chroot.rb @@ -0,0 +1,41 @@ +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) + end + + after :all do + until File.exist?(@ref_dir) + Dir.send(@method, "../") or break + end + + DirSpecs.delete_mock_dirs + end + + 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 + + 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) + 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 + + it "calls #to_path on non-String argument" do + p = mock('path') + p.should_receive(:to_path).and_return(@real_root) + Dir.send(@method, p) + end +end diff --git a/spec/ruby/core/dir/shared/closed.rb b/spec/ruby/core/dir/shared/closed.rb new file mode 100644 index 0000000000..a1bce06a08 --- /dev/null +++ b/spec/ruby/core/dir/shared/closed.rb @@ -0,0 +1,9 @@ +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) {} + }.should raise_error(IOError) + end +end diff --git a/spec/ruby/core/dir/shared/delete.rb b/spec/ruby/core/dir/shared/delete.rb new file mode 100644 index 0000000000..8db17d985f --- /dev/null +++ b/spec/ruby/core/dir/shared/delete.rb @@ -0,0 +1,61 @@ +describe :dir_delete, shared: true do + before :each do + DirSpecs.rmdir_dirs true + end + + after :each do + DirSpecs.rmdir_dirs false + end + + it "removes empty directories" do + Dir.send(@method, DirSpecs.mock_rmdir("empty")).should == 0 + end + + it "calls #to_path on non-String arguments" do + p = mock('path') + p.should_receive(:to_path).and_return(DirSpecs.mock_rmdir("empty")) + 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 + end + + it "raises an Errno::ENOENT when trying to remove a non-existing directory" do + lambda do + Dir.send @method, DirSpecs.nonexistent + end.should raise_error(Errno::ENOENT) + end + + it "raises an Errno::ENOTDIR when trying to remove a non-directory" do + file = DirSpecs.mock_rmdir("nonempty/regular") + touch(file) + lambda 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) + end + end +end diff --git a/spec/ruby/core/dir/shared/exist.rb b/spec/ruby/core/dir/shared/exist.rb new file mode 100644 index 0000000000..fbd2c9862d --- /dev/null +++ b/spec/ruby/core/dir/shared/exist.rb @@ -0,0 +1,56 @@ +describe :dir_exist, shared: true do + it "returns true if the given directory exists" do + Dir.send(@method, File.dirname(__FILE__)).should be_true + end + + it "returns true for '.'" do + Dir.send(@method, '.').should be_true + end + + it "returns true for '..'" do + Dir.send(@method, '..').should be_true + end + + it "understands non-ASCII paths" do + subdir = File.join(tmp("\u{9876}\u{665}")) + Dir.send(@method, subdir).should be_false + Dir.mkdir(subdir) + Dir.send(@method, subdir).should be_true + Dir.rmdir(subdir) + end + + it "understands relative paths" do + Dir.send(@method, File.dirname(__FILE__) + '/../').should be_true + end + + it "returns false if the given directory doesn't exist" do + Dir.send(@method, 'y26dg27n2nwjs8a/').should be_false + end + + it "doesn't require the name to have a trailing slash" do + dir = File.dirname(__FILE__) + dir.sub!(/\/$/,'') + Dir.send(@method, dir).should be_true + end + + it "doesn't expand paths" do + 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 + Dir.send(@method, __FILE__).should be_false + end + + it "doesn't set $! when file doesn't exist" do + Dir.send(@method, "/path/to/non/existent/dir") + $!.should be_nil + end + + it "calls #to_path on non String arguments" do + p = mock('path') + p.should_receive(:to_path).and_return(File.dirname(__FILE__)) + Dir.send(@method, p) + end +end diff --git a/spec/ruby/core/dir/shared/glob.rb b/spec/ruby/core/dir/shared/glob.rb new file mode 100644 index 0000000000..d2201cd6cd --- /dev/null +++ b/spec/ruby/core/dir/shared/glob.rb @@ -0,0 +1,328 @@ +# -*- encoding: utf-8 -*- +describe :dir_glob, shared: true do + before :all do + DirSpecs.create_mock_dirs + @cwd = Dir.pwd + Dir.chdir DirSpecs.mock_dir + end + + after :all do + Dir.chdir @cwd + 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 + end + + it "calls #to_path to convert a pattern" do + obj = mock('file_one.ext') + obj.should_receive(:to_path).and_return('file_one.ext') + + 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! + end + + it "matches non-dotfiles with '*'" do + expected = %w[ + brace + deeply + dir + dir_filename_ordering + file_one.ext + file_two.ext + nondotfile + special + subdir_one + subdir_two + ] + + Dir.send(@method,'*').sort.should == expected + end + + it "returns empty array when empty pattern provided" do + Dir.send(@method, '').should == [] + end + + it "matches regexp special +" do + Dir.send(@method, 'special/+').should == ['special/+'] + end + + platform_is_not :windows do + it "matches regexp special *" do + Dir.send(@method, 'special/\*').should == ['special/*'] + end + + it "matches regexp special ?" do + Dir.send(@method, 'special/\?').should == ['special/?'] + end + + it "matches regexp special |" do + Dir.send(@method, 'special/|').should == ['special/|'] + end + end + + it "matches regexp special ^" do + Dir.send(@method, 'special/^').should == ['special/^'] + end + + it "matches regexp special $" do + Dir.send(@method, 'special/$').should == ['special/$'] + end + + it "matches regexp special (" do + Dir.send(@method, 'special/(').should == ['special/('] + end + + it "matches regexp special )" do + Dir.send(@method, 'special/)').should == ['special/)'] + end + + it "matches regexp special [" do + Dir.send(@method, 'special/\[').should == ['special/['] + end + + it "matches regexp special ]" do + Dir.send(@method, 'special/]').should == ['special/]'] + end + + it "matches regexp special {" do + Dir.send(@method, 'special/\{').should == ['special/{'] + end + + it "matches regexp special }" do + Dir.send(@method, 'special/\}').should == ['special/}'] + end + + it "matches paths with glob patterns" 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 + end + + it "matches non-dotfiles with '*'" do + Dir.send(@method, '*file').sort.should == %w|nondotfile|.sort + end + + it "matches dotfiles with '.*'" do + Dir.send(@method, '.*file').sort.should == %w|.dotfile|.sort + end + + it "matches files with any ending with '*'" do + Dir.send(@method, 'file*').sort.should == %w|file_one.ext file_two.ext|.sort + end + + it "matches files with any middle with '*'" do + Dir.send(@method, 'sub*_one').sort.should == %w|subdir_one|.sort + end + + it "handles directories with globs" do + Dir.send(@method, 'sub*/*').sort.should == %w!subdir_one/nondotfile subdir_two/nondotfile subdir_two/nondotfile.ext! + end + + it "matches files with multiple '*' special characters" do + Dir.send(@method, '*fi*e*').sort.should == %w|dir_filename_ordering nondotfile file_one.ext file_two.ext|.sort + end + + it "matches non-dotfiles in the current directory with '**'" do + expected = %w[ + brace + deeply + dir + dir_filename_ordering + file_one.ext + file_two.ext + nondotfile + special + subdir_one + subdir_two + ] + + 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 + end + + it "recursively matches any nondot subdirectories with '**/'" do + expected = %w[ + brace/ + deeply/ + deeply/nested/ + deeply/nested/directory/ + deeply/nested/directory/structure/ + dir/ + special/ + special/test{1}/ + subdir_one/ + subdir_two/ + ] + + Dir.send(@method, '**/').sort.should == expected + end + + it "recursively matches any subdirectories including ./ and ../ with '.**/'" do + Dir.chdir("#{DirSpecs.mock_dir}/subdir_one") do + Dir.send(@method, '.**/').sort.should == %w|./ ../|.sort + end + end + + it "matches a single character except leading '.' with '?'" do + Dir.send(@method, '?ubdir_one').sort.should == %w|subdir_one|.sort + end + + it "accepts multiple '?' characters in a pattern" do + Dir.send(@method, 'subdir_???').sort.should == %w|subdir_one subdir_two|.sort + end + + it "matches any characters in a set with '[]'" do + Dir.send(@method, '[stfu]ubdir_one').sort.should == %w|subdir_one|.sort + end + + it "matches any characters in a range with '[-]'" do + Dir.send(@method, '[a-zA-Z]ubdir_one').sort.should == %w|subdir_one|.sort + end + + it "matches any characters except those in a set with '[^]'" do + Dir.send(@method, '[^wtf]ubdir_one').sort.should == %w|subdir_one|.sort + end + + it "matches any characters except those in a range with '[^-,,...}'" do + Dir.send(@method, 'subdir_{one,two,three}').sort.should == %w|subdir_one subdir_two|.sort + end + + it "matches a set '{,,...}' which also uses a glob" do + Dir.send(@method, 'sub*_{one,two,three}').sort.should == %w|subdir_one subdir_two|.sort + end + + it "accepts string sets with empty strings with {,,}" do + a = Dir.send(@method, 'deeply/nested/directory/structure/file_one{.ext,}').sort + a.should == %w|deeply/nested/directory/structure/file_one.ext + deeply/nested/directory/structure/file_one|.sort + end + + it "matches dot or non-dotfiles with '{,.}*'" do + Dir.send(@method, '{,.}*').sort.should == DirSpecs.expected_paths + end + + it "respects the order of {} expressions, expanding left most first" do + files = Dir.send(@method, "brace/a{.js,.html}{.erb,.rjs}") + files.should == %w!brace/a.js.rjs brace/a.html.erb! + end + + it "respects the optional nested {} expressions" do + files = Dir.send(@method, "brace/a{.{js,html},}{.{erb,rjs},}") + files.should == %w!brace/a.js.rjs brace/a.js brace/a.html.erb brace/a.erb brace/a! + end + + it "matches special characters by escaping with a backslash with '\\'" do + Dir.mkdir 'foo^bar' + + begin + Dir.send(@method, 'foo?bar').should == %w|foo^bar| + Dir.send(@method, 'foo\?bar').should == [] + Dir.send(@method, 'nond\otfile').should == %w|nondotfile| + ensure + Dir.rmdir 'foo^bar' + end + end + + it "recursively matches directories with '**/'" do + Dir.send(@method, '**/*fil?{,.}*').uniq.sort.should == + %w[deeply/nested/directory/structure/file_one + deeply/nested/directory/structure/file_one.ext + deeply/nondotfile + + dir/filename_ordering + dir_filename_ordering + + file_one.ext + file_two.ext + + nondotfile + + special/test{1}/file[1] + + subdir_one/nondotfile + subdir_two/nondotfile + subdir_two/nondotfile.ext] + end + + it "ignores matching through directories that doen't exist" do + Dir.send(@method, "deeply/notthere/blah*/whatever").should == [] + end + + it "ignores matching only directories under an nonexistant path" do + Dir.send(@method, "deeply/notthere/blah/").should == [] + end + + platform_is_not :windows do + it "matches UTF-8 paths" do + Dir.send(@method, "special/こんにちは{,.txt}").should == ["special/こんにちは.txt"] + end + end +end + +describe :dir_glob_recursive, shared: true do + before :each do + @cwd = Dir.pwd + @mock_dir = File.expand_path tmp('dir_glob_mock') + + %w[ + a/x/b/y/e + a/x/b/y/b/z/e + ].each do |path| + file = File.join @mock_dir, path + mkdir_p File.dirname(file) + touch file + end + + Dir.chdir @mock_dir + end + + after :each do + Dir.chdir @cwd + rm_r @mock_dir + end + + it "matches multiple recursives" do + expected = %w[ + a/x/b/y/b/z/e + a/x/b/y/e + ] + + Dir.send(@method, 'a/**/b/**/e').uniq.sort.should == expected + end + + platform_is_not :windows do + it "ignores symlinks" do + file = File.join @mock_dir, 'b/z/e' + link = File.join @mock_dir, 'a/y' + + mkdir_p File.dirname(file) + touch file + File.symlink(File.dirname(file), link) + + expected = %w[ + a/x/b/y/b/z/e + a/x/b/y/e + ] + + Dir.send(@method, 'a/**/e').uniq.sort.should == expected + end + end +end diff --git a/spec/ruby/core/dir/shared/open.rb b/spec/ruby/core/dir/shared/open.rb new file mode 100644 index 0000000000..3c2b63b6fa --- /dev/null +++ b/spec/ruby/core/dir/shared/open.rb @@ -0,0 +1,63 @@ +describe :dir_open, shared: true do + it "returns a Dir instance representing the specified directory" do + dir = Dir.send(@method, DirSpecs.mock_dir) + dir.should be_kind_of(Dir) + dir.close + end + + it "raises a SystemCallError if the directory does not exist" do + lambda do + Dir.send @method, DirSpecs.nonexistent + end.should raise_error(SystemCallError) + end + + it "may take a block which is yielded to with the Dir instance" do + Dir.send(@method, DirSpecs.mock_dir) {|dir| dir.should be_kind_of(Dir)} + end + + it "returns the value of the block if a block is given" do + Dir.send(@method, DirSpecs.mock_dir) {|dir| :value }.should == :value + end + + 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) + end + + it "closes the Dir instance when the block exits the block even due to an exception" do + closed_dir = nil + + lambda do + Dir.send(@method, DirSpecs.mock_dir) do |dir| + closed_dir = dir + raise + end + end.should raise_error + + lambda { closed_dir.read }.should raise_error(IOError) + end + + it "calls #to_path on non-String arguments" do + p = mock('path') + p.should_receive(:to_path).and_return(DirSpecs.mock_dir) + Dir.send(@method, p) { true } + end + + it "accepts an options Hash" do + dir = Dir.send(@method, DirSpecs.mock_dir, encoding: "utf-8") {|d| d } + dir.should be_kind_of(Dir) + end + + it "calls #to_hash to convert the options object" 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.should be_kind_of(Dir) + end + + it "ignores the :encoding option if it is nil" do + dir = Dir.send(@method, DirSpecs.mock_dir, encoding: nil) {|d| d } + dir.should be_kind_of(Dir) + end +end diff --git a/spec/ruby/core/dir/shared/path.rb b/spec/ruby/core/dir/shared/path.rb new file mode 100644 index 0000000000..829eeb04c2 --- /dev/null +++ b/spec/ruby/core/dir/shared/path.rb @@ -0,0 +1,32 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require File.expand_path('../../fixtures/common', __FILE__) +require File.expand_path('../closed', __FILE__) + +describe :dir_path, shared: true do + it "returns the path that was supplied to .new or .open" do + dir = Dir.open DirSpecs.mock_dir + begin + dir.send(@method).should == DirSpecs.mock_dir + ensure + dir.close rescue nil + end + end + + it "returns the path even when called on a closed Dir instance" do + dir = Dir.open DirSpecs.mock_dir + dir.close + 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 + end + end +end diff --git a/spec/ruby/core/dir/shared/pos.rb b/spec/ruby/core/dir/shared/pos.rb new file mode 100644 index 0000000000..2165932d99 --- /dev/null +++ b/spec/ruby/core/dir/shared/pos.rb @@ -0,0 +1,51 @@ +describe :dir_pos, shared: true do + before :each do + @dir = Dir.open DirSpecs.mock_dir + end + + after :each do + @dir.close rescue nil + end + + it "returns an Integer representing the current position in the directory" do + @dir.send(@method).should be_kind_of(Integer) + @dir.send(@method).should be_kind_of(Integer) + @dir.send(@method).should be_kind_of(Integer) + end + + it "returns a different Integer if moved from previous position" do + a = @dir.send(@method) + @dir.read + b = @dir.send(@method) + + a.should be_kind_of(Integer) + b.should be_kind_of(Integer) + + a.should_not == b + end +end + +describe :dir_pos_set, shared: true do + before :each do + @dir = Dir.open DirSpecs.mock_dir + end + + after :each do + @dir.close + end + + # NOTE: #seek/#pos= to a position not returned by #tell/#pos is undefined + # and should not be spec'd. + + it "moves the read position to a previously obtained position" do + pos = @dir.pos + a = @dir.read + b = @dir.read + @dir.send @method, pos + c = @dir.read + + a.should_not == b + b.should_not == c + c.should == a + end +end diff --git a/spec/ruby/core/dir/shared/pwd.rb b/spec/ruby/core/dir/shared/pwd.rb new file mode 100644 index 0000000000..5f041a9d41 --- /dev/null +++ b/spec/ruby/core/dir/shared/pwd.rb @@ -0,0 +1,49 @@ +describe :dir_pwd, shared: true do + with_feature :encoding do + before :each do + @fs_encoding = Encoding.find('filesystem') + end + end + + it "returns the current working directory" do + pwd = Dir.send(@method) + + File.directory?(pwd).should == true + + # On ubuntu gutsy, for example, /bin/pwd does not + # understand -P. With just `pwd -P`, /bin/pwd is run. + + # The following uses inode rather than file names to account for + # case insensitive file systems like default OS/X file systems + platform_is_not :windows do + File.stat(pwd).ino.should == File.stat(`/bin/sh -c "pwd -P"`.chomp).ino + end + platform_is :windows do + File.stat(pwd).ino.should == File.stat(File.expand_path(`cd`.chomp)).ino + end + end + + it "returns an absolute path" do + pwd = Dir.send(@method) + pwd.should == File.expand_path(pwd) + end + + it "returns an absolute path even when chdir to a relative path" do + Dir.chdir(".") do + pwd = Dir.send(@method) + File.directory?(pwd).should == true + pwd.should == File.expand_path(pwd) + 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 + end + end +end diff --git a/spec/ruby/core/dir/tell_spec.rb b/spec/ruby/core/dir/tell_spec.rb new file mode 100644 index 0000000000..fb9848153d --- /dev/null +++ b/spec/ruby/core/dir/tell_spec.rb @@ -0,0 +1,18 @@ +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__) + +describe "Dir#tell" do + before :all do + DirSpecs.create_mock_dirs + end + + after :all do + DirSpecs.delete_mock_dirs + end + + it_behaves_like :dir_pos, :tell + + it_behaves_like :dir_closed, :tell +end diff --git a/spec/ruby/core/dir/to_path_spec.rb b/spec/ruby/core/dir/to_path_spec.rb new file mode 100644 index 0000000000..85609fbfff --- /dev/null +++ b/spec/ruby/core/dir/to_path_spec.rb @@ -0,0 +1,15 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/common', __FILE__) +require File.expand_path('../shared/path', __FILE__) + +describe "Dir#to_path" do + before :all do + DirSpecs.create_mock_dirs + end + + after :all do + DirSpecs.delete_mock_dirs + end + + 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 new file mode 100644 index 0000000000..4459bef56c --- /dev/null +++ b/spec/ruby/core/dir/unlink_spec.rb @@ -0,0 +1,15 @@ +require File.expand_path('../../../spec_helper', __FILE__) +require File.expand_path('../fixtures/common', __FILE__) +require File.expand_path('../shared/delete', __FILE__) + +describe "Dir.unlink" do + before :all do + DirSpecs.create_mock_dirs + end + + after :all do + DirSpecs.delete_mock_dirs + end + + it_behaves_like :dir_delete, :unlink +end -- cgit v1.2.3