blob: 015386a9026cf3ce371585b1b33925fcc037c28b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
require_relative '../../spec_helper'
require_relative 'fixtures/common'
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
skip "$HOME not valid directory" unless ENV['HOME'] && File.directory?(ENV['HOME'])
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
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
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 = ""
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
-> { 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')
Dir.should_not.exist?(dir1)
Dir.should_not.exist?(dir2)
Dir.mkdir dir1
Dir.mkdir dir2
begin
-> {
Dir.chdir dir1 do
Dir.chdir(dir2) { Dir.unlink dir1 }
end
}.should raise_error(Errno::ENOENT)
ensure
Dir.unlink dir1 if Dir.exist?(dir1)
Dir.unlink dir2 if Dir.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
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
|