Skip to content

Commit 8b6d6fd

Browse files
committed
Move Dir to rely less on shared examples
1 parent e3cd39b commit 8b6d6fd

14 files changed

Lines changed: 225 additions & 283 deletions

core/data/inspect_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require_relative '../../spec_helper'
2+
require_relative 'fixtures/classes'
23

34
describe "Data#inspect" do
45
it "returns a string representation showing members and values" do

core/dir/exist_spec.rb

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
require_relative '../../spec_helper'
22
require_relative 'fixtures/common'
3-
require_relative 'shared/exist'
43

54
describe "Dir.exist?" do
65
before :all do
@@ -11,7 +10,61 @@
1110
DirSpecs.delete_mock_dirs
1211
end
1312

14-
it_behaves_like :dir_exist, :exist?
13+
it "returns true if the given directory exists" do
14+
Dir.exist?(__dir__).should == true
15+
end
16+
17+
it "returns true for '.'" do
18+
Dir.exist?('.').should == true
19+
end
20+
21+
it "returns true for '..'" do
22+
Dir.exist?('..').should == true
23+
end
24+
25+
it "understands non-ASCII paths" do
26+
subdir = File.join(tmp("\u{9876}\u{665}"))
27+
Dir.exist?(subdir).should == false
28+
Dir.mkdir(subdir)
29+
Dir.exist?(subdir).should == true
30+
Dir.rmdir(subdir)
31+
end
32+
33+
it "understands relative paths" do
34+
Dir.exist?(__dir__ + '/../').should == true
35+
end
36+
37+
it "returns false if the given directory doesn't exist" do
38+
Dir.exist?('y26dg27n2nwjs8a/').should == false
39+
end
40+
41+
it "doesn't require the name to have a trailing slash" do
42+
dir = __dir__
43+
dir.sub!(/\/$/,'')
44+
Dir.exist?(dir).should == true
45+
end
46+
47+
it "doesn't expand paths" do
48+
skip "$HOME not valid directory" unless ENV['HOME'] && File.directory?(ENV['HOME'])
49+
Dir.exist?(File.expand_path('~')).should == true
50+
Dir.exist?('~').should == false
51+
end
52+
53+
it "returns false if the argument exists but is a file" do
54+
File.should.exist?(__FILE__)
55+
Dir.exist?(__FILE__).should == false
56+
end
57+
58+
it "doesn't set $! when file doesn't exist" do
59+
Dir.exist?("/path/to/non/existent/dir")
60+
$!.should == nil
61+
end
62+
63+
it "calls #to_path on non String arguments" do
64+
p = mock('path')
65+
p.should_receive(:to_path).and_return(__dir__)
66+
Dir.exist?(p)
67+
end
1568
end
1669

1770
describe "Dir.exists?" do

core/dir/getwd_spec.rb

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
require_relative '../../spec_helper'
2-
require_relative 'fixtures/common'
3-
require_relative 'shared/pwd'
42

53
describe "Dir.getwd" do
6-
before :all do
7-
DirSpecs.create_mock_dirs
4+
it "is an alias of Dir.pwd" do
5+
Dir.method(:getwd).should == Dir.method(:pwd)
86
end
9-
10-
after :all do
11-
DirSpecs.delete_mock_dirs
12-
end
13-
14-
it_behaves_like :dir_pwd, :getwd
157
end

core/dir/open_spec.rb

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
require_relative '../../spec_helper'
22
require_relative 'fixtures/common'
3-
require_relative 'shared/open'
43

54
describe "Dir.open" do
65
before :all do
@@ -11,5 +10,75 @@
1110
DirSpecs.delete_mock_dirs
1211
end
1312

14-
it_behaves_like :dir_open, :open
13+
it "returns a Dir instance representing the specified directory" do
14+
dir = Dir.open(DirSpecs.mock_dir)
15+
dir.should.is_a?(Dir)
16+
dir.close
17+
end
18+
19+
it "raises a SystemCallError if the directory does not exist" do
20+
-> do
21+
Dir.open(DirSpecs.nonexistent)
22+
end.should.raise(SystemCallError)
23+
end
24+
25+
it "may take a block which is yielded to with the Dir instance" do
26+
Dir.open(DirSpecs.mock_dir) {|dir| dir.should.is_a?(Dir)}
27+
end
28+
29+
it "returns the value of the block if a block is given" do
30+
Dir.open(DirSpecs.mock_dir) {|dir| :value }.should == :value
31+
end
32+
33+
it "closes the Dir instance when the block exits if given a block" do
34+
closed_dir = Dir.open(DirSpecs.mock_dir) { |dir| dir }
35+
-> { closed_dir.read }.should.raise(IOError)
36+
end
37+
38+
it "closes the Dir instance when the block exits the block even due to an exception" do
39+
closed_dir = nil
40+
41+
-> do
42+
Dir.open(DirSpecs.mock_dir) do |dir|
43+
closed_dir = dir
44+
raise "dir specs"
45+
end
46+
end.should.raise(RuntimeError, "dir specs")
47+
48+
-> { closed_dir.read }.should.raise(IOError)
49+
end
50+
51+
it "calls #to_path on non-String arguments" do
52+
p = mock('path')
53+
p.should_receive(:to_path).and_return(DirSpecs.mock_dir)
54+
Dir.open(p) { true }
55+
end
56+
57+
it "accepts an options Hash" do
58+
dir = Dir.open(DirSpecs.mock_dir, encoding: "utf-8") {|d| d }
59+
dir.should.is_a?(Dir)
60+
end
61+
62+
it "calls #to_hash to convert the options object" do
63+
options = mock("dir_open")
64+
options.should_receive(:to_hash).and_return({ encoding: Encoding::UTF_8 })
65+
66+
dir = Dir.open(DirSpecs.mock_dir, **options) {|d| d }
67+
dir.should.is_a?(Dir)
68+
end
69+
70+
it "ignores the :encoding option if it is nil" do
71+
dir = Dir.open(DirSpecs.mock_dir, encoding: nil) {|d| d }
72+
dir.should.is_a?(Dir)
73+
end
74+
75+
platform_is_not :windows do
76+
it 'sets the close-on-exec flag for the directory file descriptor' do
77+
Dir.open(DirSpecs.mock_dir) do |dir|
78+
io = IO.for_fd(dir.fileno)
79+
io.autoclose = false
80+
io.should.close_on_exec?
81+
end
82+
end
83+
end
1584
end

core/dir/path_spec.rb

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
require_relative '../../spec_helper'
22
require_relative 'fixtures/common'
3-
require_relative 'shared/path'
43

54
describe "Dir#path" do
65
before :all do
@@ -11,5 +10,28 @@
1110
DirSpecs.delete_mock_dirs
1211
end
1312

14-
it_behaves_like :dir_path, :path
13+
it "returns the path that was supplied to .new or .open" do
14+
dir = Dir.open DirSpecs.mock_dir
15+
begin
16+
dir.path.should == DirSpecs.mock_dir
17+
ensure
18+
dir.close rescue nil
19+
end
20+
end
21+
22+
it "returns the path even when called on a closed Dir instance" do
23+
dir = Dir.open DirSpecs.mock_dir
24+
dir.close
25+
dir.path.should == DirSpecs.mock_dir
26+
end
27+
28+
it "returns a String with the same encoding as the argument to .open" do
29+
path = DirSpecs.mock_dir.force_encoding Encoding::IBM866
30+
dir = Dir.open path
31+
begin
32+
dir.path.encoding.should.equal?(Encoding::IBM866)
33+
ensure
34+
dir.close
35+
end
36+
end
1537
end

core/dir/pos_spec.rb

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,11 @@
11
require_relative '../../spec_helper'
22
require_relative 'fixtures/common'
3-
require_relative 'shared/closed'
43
require_relative 'shared/pos'
54

65
describe "Dir#pos" do
7-
before :all do
8-
DirSpecs.create_mock_dirs
9-
end
10-
11-
after :all do
12-
DirSpecs.delete_mock_dirs
13-
end
14-
15-
it_behaves_like :dir_pos, :pos
16-
end
17-
18-
describe "Dir#pos" do
19-
before :all do
20-
DirSpecs.create_mock_dirs
6+
it "is an alias of Dir#tell" do
7+
Dir.instance_method(:pos).should == Dir.instance_method(:tell)
218
end
22-
23-
after :all do
24-
DirSpecs.delete_mock_dirs
25-
end
26-
27-
it_behaves_like :dir_closed, :pos
289
end
2910

3011
describe "Dir#pos=" do

core/dir/pwd_spec.rb

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# -*- encoding: utf-8 -*-
22
require_relative '../../spec_helper'
33
require_relative 'fixtures/common'
4-
require_relative 'shared/pwd'
54

65
describe "Dir.pwd" do
76
before :all do
@@ -12,7 +11,49 @@
1211
DirSpecs.delete_mock_dirs
1312
end
1413

15-
it_behaves_like :dir_pwd, :pwd
14+
before :each do
15+
@fs_encoding = Encoding.find('filesystem')
16+
end
17+
18+
it "returns the current working directory" do
19+
pwd = Dir.pwd
20+
21+
File.directory?(pwd).should == true
22+
23+
# On ubuntu gutsy, for example, /bin/pwd does not
24+
# understand -P. With just `pwd -P`, /bin/pwd is run.
25+
26+
# The following uses inode rather than file names to account for
27+
# case insensitive file systems like default OS/X file systems
28+
platform_is_not :windows do
29+
File.stat(pwd).ino.should == File.stat(`/bin/sh -c "pwd -P"`.chomp).ino
30+
end
31+
platform_is :windows do
32+
File.stat(pwd).ino.should == File.stat(File.expand_path(`cd`.chomp)).ino
33+
end
34+
end
35+
36+
it "returns an absolute path" do
37+
pwd = Dir.pwd
38+
pwd.should == File.expand_path(pwd)
39+
end
40+
41+
it "returns an absolute path even when chdir to a relative path" do
42+
Dir.chdir(".") do
43+
pwd = Dir.pwd
44+
File.directory?(pwd).should == true
45+
pwd.should == File.expand_path(pwd)
46+
end
47+
end
48+
49+
it "returns a String with the filesystem encoding" do
50+
enc = Dir.pwd.encoding
51+
if @fs_encoding == Encoding::US_ASCII
52+
[Encoding::US_ASCII, Encoding::BINARY].should.include?(enc)
53+
else
54+
enc.should.equal?(@fs_encoding)
55+
end
56+
end
1657
end
1758

1859
describe "Dir.pwd" do

core/dir/shared/exist.rb

Lines changed: 0 additions & 57 deletions
This file was deleted.

0 commit comments

Comments
 (0)