fakefs 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +40 -7
- data/Rakefile +13 -20
- data/lib/fakefs/base.rb +2 -1
- data/lib/fakefs/dir.rb +93 -2
- data/lib/fakefs/fake/dir.rb +8 -0
- data/lib/fakefs/fake/file.rb +46 -3
- data/lib/fakefs/fake/symlink.rb +8 -2
- data/lib/fakefs/file.rb +163 -12
- data/lib/fakefs/file_system.rb +6 -6
- data/lib/fakefs/fileutils.rb +14 -0
- data/lib/fakefs/version.rb +1 -1
- data/test/fake/file_test.rb +88 -0
- data/test/fake/symlink_test.rb +11 -0
- data/test/fakefs_test.rb +581 -53
- data/test/file/stat_test.rb +70 -0
- data/test/safe_test.rb +9 -0
- metadata +9 -3
data/lib/fakefs/file_system.rb
CHANGED
@@ -22,9 +22,9 @@ module FakeFS
|
|
22
22
|
def find(path)
|
23
23
|
parts = path_parts(normalize_path(path))
|
24
24
|
return fs if parts.empty? # '/'
|
25
|
-
|
25
|
+
|
26
26
|
entries = find_recurser(fs, parts).flatten
|
27
|
-
|
27
|
+
|
28
28
|
case entries.length
|
29
29
|
when 0 then nil
|
30
30
|
when 1 then entries.first
|
@@ -54,7 +54,7 @@ module FakeFS
|
|
54
54
|
files.each do |f|
|
55
55
|
if RealFile.file?(f)
|
56
56
|
FileUtils.mkdir_p(File.dirname(f))
|
57
|
-
File.open(f,
|
57
|
+
File.open(f, File::WRITE_ONLY) do |g|
|
58
58
|
g.print RealFile.open(f){|h| h.read }
|
59
59
|
end
|
60
60
|
elsif RealFile.directory?(f)
|
@@ -66,8 +66,8 @@ module FakeFS
|
|
66
66
|
end
|
67
67
|
|
68
68
|
def delete(path)
|
69
|
-
if
|
70
|
-
|
69
|
+
if node = FileSystem.find(path)
|
70
|
+
node.delete
|
71
71
|
end
|
72
72
|
end
|
73
73
|
|
@@ -113,6 +113,6 @@ module FakeFS
|
|
113
113
|
else
|
114
114
|
matches.map{|entry| find_recurser(entry, parts) }
|
115
115
|
end
|
116
|
-
end
|
116
|
+
end
|
117
117
|
end
|
118
118
|
end
|
data/lib/fakefs/fileutils.rb
CHANGED
@@ -5,10 +5,24 @@ module FakeFS
|
|
5
5
|
def mkdir_p(path)
|
6
6
|
FileSystem.add(path, FakeDir.new)
|
7
7
|
end
|
8
|
+
alias_method :mkpath, :mkdir_p
|
9
|
+
|
10
|
+
def rmdir(list, options = {})
|
11
|
+
list = [ list ] unless list.is_a?(Array)
|
12
|
+
list.each do |l|
|
13
|
+
parent = l.split('/')
|
14
|
+
parent.pop
|
15
|
+
raise Errno::ENOENT, "No such file or directory - #{l}" unless parent.join == "" || FileSystem.find(parent.join('/'))
|
16
|
+
raise Errno::ENOENT, l unless FileSystem.find(l)
|
17
|
+
raise Errno::ENOTEMPTY, l unless FileSystem.find(l).values.empty?
|
18
|
+
rm(l)
|
19
|
+
end
|
20
|
+
end
|
8
21
|
|
9
22
|
def rm(path)
|
10
23
|
FileSystem.delete(path)
|
11
24
|
end
|
25
|
+
|
12
26
|
alias_method :rm_rf, :rm
|
13
27
|
alias_method :rm_r, :rm
|
14
28
|
|
data/lib/fakefs/version.rb
CHANGED
@@ -0,0 +1,88 @@
|
|
1
|
+
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', '..', 'lib')
|
2
|
+
require 'fakefs/safe'
|
3
|
+
require 'test/unit'
|
4
|
+
|
5
|
+
class FakeFileTest < Test::Unit::TestCase
|
6
|
+
include FakeFS
|
7
|
+
|
8
|
+
def setup
|
9
|
+
FileSystem.clear
|
10
|
+
|
11
|
+
@file = FakeFile.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_fake_file_has_empty_content_by_default
|
15
|
+
assert_equal "", @file.content
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_fake_file_can_read_and_write_to_content
|
19
|
+
@file.content = "foobar"
|
20
|
+
assert_equal "foobar", @file.content
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_fake_file_has_1_link_by_default
|
24
|
+
assert_equal [@file], @file.links
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_fake_file_can_create_link
|
28
|
+
other_file = FakeFile.new
|
29
|
+
|
30
|
+
@file.link(other_file)
|
31
|
+
|
32
|
+
assert_equal [@file, other_file], @file.links
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_fake_file_wont_add_link_to_same_file_twice
|
36
|
+
other_file = FakeFile.new
|
37
|
+
|
38
|
+
@file.link other_file
|
39
|
+
@file.link other_file
|
40
|
+
|
41
|
+
assert_equal [@file, other_file], @file.links
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_links_are_mutual
|
45
|
+
other_file = FakeFile.new
|
46
|
+
|
47
|
+
@file.link(other_file)
|
48
|
+
|
49
|
+
assert_equal [@file, other_file], other_file.links
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_can_link_multiple_files
|
53
|
+
file_two = FakeFile.new
|
54
|
+
file_three = FakeFile.new
|
55
|
+
|
56
|
+
@file.link file_two
|
57
|
+
@file.link file_three
|
58
|
+
|
59
|
+
assert_equal [@file, file_two, file_three], @file.links
|
60
|
+
assert_equal [@file, file_two, file_three], file_two.links
|
61
|
+
assert_equal [@file, file_two, file_three], file_three.links
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_links_share_same_content
|
65
|
+
other_file = FakeFile.new
|
66
|
+
|
67
|
+
@file.link other_file
|
68
|
+
|
69
|
+
@file.content = "foobar"
|
70
|
+
|
71
|
+
assert_equal "foobar", other_file.content
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_clone_creates_new_inode
|
75
|
+
clone = @file.clone
|
76
|
+
assert !clone.inode.equal?(@file.inode)
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_cloning_does_not_use_same_content_object
|
80
|
+
clone = @file.clone
|
81
|
+
|
82
|
+
clone.content = "foo"
|
83
|
+
@file.content = "bar"
|
84
|
+
|
85
|
+
assert_equal "foo", clone.content
|
86
|
+
assert_equal "bar", @file.content
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', '..', 'lib')
|
2
|
+
require 'fakefs/safe'
|
3
|
+
require 'test/unit'
|
4
|
+
|
5
|
+
class FakeSymlinkTest < Test::Unit::TestCase
|
6
|
+
include FakeFS
|
7
|
+
|
8
|
+
def test_symlink_has_method_missing_as_private
|
9
|
+
assert FakeSymlink.private_instance_methods.include?("method_missing")
|
10
|
+
end
|
11
|
+
end
|
data/test/fakefs_test.rb
CHANGED
@@ -17,7 +17,6 @@ class FakeFSTest < Test::Unit::TestCase
|
|
17
17
|
def xtest_can_be_initialized_with_an_existing_directory
|
18
18
|
fs = FileSystem
|
19
19
|
fs.clone(File.expand_path(File.dirname(__FILE__))).inspect
|
20
|
-
puts fs.files.inspect
|
21
20
|
assert_equal 1, fs.files.size
|
22
21
|
end
|
23
22
|
|
@@ -25,6 +24,18 @@ class FakeFSTest < Test::Unit::TestCase
|
|
25
24
|
FileUtils.mkdir_p("/path/to/dir")
|
26
25
|
assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']
|
27
26
|
end
|
27
|
+
|
28
|
+
def test_can_create_directories_with_mkpath
|
29
|
+
FileUtils.mkpath("/path/to/dir")
|
30
|
+
assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_can_delete_directories
|
34
|
+
FileUtils.mkdir_p("/path/to/dir")
|
35
|
+
FileUtils.rmdir("/path/to/dir")
|
36
|
+
assert File.exists?("/path/to/")
|
37
|
+
assert File.exists?("/path/to/dir") == false
|
38
|
+
end
|
28
39
|
|
29
40
|
def test_knows_directories_exist
|
30
41
|
FileUtils.mkdir_p(path = "/path/to/dir")
|
@@ -59,9 +70,9 @@ class FakeFSTest < Test::Unit::TestCase
|
|
59
70
|
FileUtils.ln_s(target, "/path/to/link")
|
60
71
|
assert_kind_of FakeSymlink, FileSystem.fs['path']['to']['link']
|
61
72
|
|
62
|
-
assert_raises(Errno::EEXIST)
|
73
|
+
assert_raises(Errno::EEXIST) do
|
63
74
|
FileUtils.ln_s(target, '/path/to/link')
|
64
|
-
|
75
|
+
end
|
65
76
|
end
|
66
77
|
|
67
78
|
def test_can_follow_symlinks
|
@@ -85,6 +96,92 @@ class FakeFSTest < Test::Unit::TestCase
|
|
85
96
|
assert File.exists?(path)
|
86
97
|
end
|
87
98
|
|
99
|
+
def test_file_opens_in_read_only_mode
|
100
|
+
File.open("foo", "w") { |f| f << "foo" }
|
101
|
+
|
102
|
+
f = File.open("foo")
|
103
|
+
|
104
|
+
assert_raises(IOError) do
|
105
|
+
f << "bar"
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
def test_file_opens_in_invalid_mode
|
110
|
+
FileUtils.touch("foo")
|
111
|
+
|
112
|
+
assert_raises(ArgumentError) do
|
113
|
+
File.open("foo", "an_illegal_mode")
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_raises_error_when_cannot_find_file_in_read_mode
|
118
|
+
assert_raises(Errno::ENOENT) do
|
119
|
+
File.open("does_not_exist", "r")
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_raises_error_when_cannot_find_file_in_read_write_mode
|
124
|
+
assert_raises(Errno::ENOENT) do
|
125
|
+
File.open("does_not_exist", "r+")
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def test_creates_files_in_write_only_mode
|
130
|
+
File.open("foo", "w")
|
131
|
+
assert File.exists?("foo")
|
132
|
+
end
|
133
|
+
|
134
|
+
def test_creates_files_in_read_write_truncate_mode
|
135
|
+
File.open("foo", "w+")
|
136
|
+
assert File.exists?("foo")
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_creates_files_in_append_write_only
|
140
|
+
File.open("foo", "a")
|
141
|
+
assert File.exists?("foo")
|
142
|
+
end
|
143
|
+
|
144
|
+
def test_creates_files_in_append_read_write
|
145
|
+
File.open("foo", "a+")
|
146
|
+
assert File.exists?("foo")
|
147
|
+
end
|
148
|
+
|
149
|
+
def test_file_in_write_only_raises_error_when_reading
|
150
|
+
FileUtils.touch("foo")
|
151
|
+
|
152
|
+
f = File.open("foo", "w")
|
153
|
+
|
154
|
+
assert_raises(IOError) do
|
155
|
+
f.read
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
def test_file_in_write_mode_truncates_existing_file
|
160
|
+
File.open("foo", "w") { |f| f << "contents" }
|
161
|
+
|
162
|
+
f = File.open("foo", "w")
|
163
|
+
|
164
|
+
assert_equal "", File.read("foo")
|
165
|
+
end
|
166
|
+
|
167
|
+
def test_file_in_read_write_truncation_mode_truncates_file
|
168
|
+
File.open("foo", "w") { |f| f << "foo" }
|
169
|
+
|
170
|
+
f = File.open("foo", "w+")
|
171
|
+
|
172
|
+
assert_equal "", File.read("foo")
|
173
|
+
end
|
174
|
+
|
175
|
+
def test_file_in_append_write_only_raises_error_when_reading
|
176
|
+
FileUtils.touch("foo")
|
177
|
+
|
178
|
+
f = File.open("foo", "a")
|
179
|
+
|
180
|
+
assert_raises(IOError) do
|
181
|
+
f.read
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
88
185
|
def test_can_read_files_once_written
|
89
186
|
path = '/path/to/file.txt'
|
90
187
|
File.open(path, 'w') do |f|
|
@@ -102,14 +199,22 @@ class FakeFSTest < Test::Unit::TestCase
|
|
102
199
|
assert_equal 'Yada Yada', File.read(path)
|
103
200
|
end
|
104
201
|
|
202
|
+
def test_can_get_size_of_files
|
203
|
+
path = '/path/to/file.txt'
|
204
|
+
File.open(path, 'w') do |f|
|
205
|
+
f << 'Yada Yada'
|
206
|
+
end
|
207
|
+
assert_equal 9, File.size(path)
|
208
|
+
end
|
209
|
+
|
105
210
|
def test_can_read_with_File_readlines
|
106
211
|
path = '/path/to/file.txt'
|
107
212
|
File.open(path, 'w') do |f|
|
108
|
-
f.puts "Yatta!"
|
109
|
-
f.puts "woot"
|
213
|
+
f.puts "Yatta!", "Gatta!"
|
214
|
+
f.puts ["woot","toot"]
|
110
215
|
end
|
111
216
|
|
112
|
-
assert_equal
|
217
|
+
assert_equal %w(Yatta! Gatta! woot toot), File.readlines(path)
|
113
218
|
end
|
114
219
|
|
115
220
|
def test_File_close_disallows_further_access
|
@@ -163,9 +268,10 @@ class FakeFSTest < Test::Unit::TestCase
|
|
163
268
|
def test_can_chown_files
|
164
269
|
good = 'file.txt'
|
165
270
|
bad = 'nofile.txt'
|
166
|
-
File.open(good,'w'){|f| f.write "foo" }
|
271
|
+
File.open(good,'w') { |f| f.write "foo" }
|
167
272
|
|
168
|
-
|
273
|
+
out = FileUtils.chown('noone', 'nogroup', good, :verbose => true)
|
274
|
+
assert_equal [good], out
|
169
275
|
assert_raises(Errno::ENOENT) do
|
170
276
|
FileUtils.chown('noone', 'nogroup', bad, :verbose => true)
|
171
277
|
end
|
@@ -183,19 +289,19 @@ class FakeFSTest < Test::Unit::TestCase
|
|
183
289
|
|
184
290
|
def test_can_chown_R_files
|
185
291
|
FileUtils.mkdir_p '/path/'
|
186
|
-
File.open('/path/foo', 'w'){|f| f.write 'foo' }
|
187
|
-
File.open('/path/foobar', 'w'){|f| f.write 'foo' }
|
292
|
+
File.open('/path/foo', 'w') { |f| f.write 'foo' }
|
293
|
+
File.open('/path/foobar', 'w') { |f| f.write 'foo' }
|
188
294
|
resp = FileUtils.chown_R('no', 'no', '/path')
|
189
295
|
assert_equal ['/path'], resp
|
190
296
|
end
|
191
297
|
|
192
298
|
def test_dir_globs_paths
|
193
299
|
FileUtils.mkdir_p '/path'
|
194
|
-
File.open('/path/foo', 'w'){|f| f.write 'foo' }
|
195
|
-
File.open('/path/foobar', 'w'){|f| f.write 'foo' }
|
300
|
+
File.open('/path/foo', 'w') { |f| f.write 'foo' }
|
301
|
+
File.open('/path/foobar', 'w') { |f| f.write 'foo' }
|
196
302
|
|
197
303
|
FileUtils.mkdir_p '/path/bar'
|
198
|
-
File.open('/path/bar/baz', 'w'){|f| f.write 'foo' }
|
304
|
+
File.open('/path/bar/baz', 'w') { |f| f.write 'foo' }
|
199
305
|
|
200
306
|
FileUtils.cp_r '/path/bar', '/path/bar2'
|
201
307
|
|
@@ -227,8 +333,8 @@ class FakeFSTest < Test::Unit::TestCase
|
|
227
333
|
assert_equal '.', FileSystem.fs.name
|
228
334
|
assert_equal({}, FileSystem.fs['path'])
|
229
335
|
Dir.chdir '/path' do
|
230
|
-
File.open('foo', 'w'){|f| f.write 'foo'}
|
231
|
-
File.open('foobar', 'w'){|f| f.write 'foo'}
|
336
|
+
File.open('foo', 'w') { |f| f.write 'foo'}
|
337
|
+
File.open('foobar', 'w') { |f| f.write 'foo'}
|
232
338
|
end
|
233
339
|
|
234
340
|
assert_equal '.', FileSystem.fs.name
|
@@ -236,7 +342,7 @@ class FakeFSTest < Test::Unit::TestCase
|
|
236
342
|
|
237
343
|
c = nil
|
238
344
|
Dir.chdir '/path' do
|
239
|
-
c = File.open('foo', 'r'){|f| f.read }
|
345
|
+
c = File.open('foo', 'r') { |f| f.read }
|
240
346
|
end
|
241
347
|
|
242
348
|
assert_equal 'foo', c
|
@@ -246,8 +352,8 @@ class FakeFSTest < Test::Unit::TestCase
|
|
246
352
|
FileUtils.mkdir_p '/path'
|
247
353
|
|
248
354
|
Dir.chdir '/path' do
|
249
|
-
File.open('foo', 'w'){|f| f.write 'foo'}
|
250
|
-
File.open('/foobar', 'w'){|f| f.write 'foo'}
|
355
|
+
File.open('foo', 'w') { |f| f.write 'foo'}
|
356
|
+
File.open('/foobar', 'w') { |f| f.write 'foo'}
|
251
357
|
end
|
252
358
|
assert_equal ['foo'], FileSystem.fs['path'].keys.sort
|
253
359
|
assert_equal ['foobar', 'path'], FileSystem.fs.keys.sort
|
@@ -264,9 +370,9 @@ class FakeFSTest < Test::Unit::TestCase
|
|
264
370
|
def test_chdir_should_be_nestable
|
265
371
|
FileUtils.mkdir_p '/path/me'
|
266
372
|
Dir.chdir '/path' do
|
267
|
-
File.open('foo', 'w'){|f| f.write 'foo'}
|
373
|
+
File.open('foo', 'w') { |f| f.write 'foo'}
|
268
374
|
Dir.chdir 'me' do
|
269
|
-
File.open('foobar', 'w'){|f| f.write 'foo'}
|
375
|
+
File.open('foobar', 'w') { |f| f.write 'foo'}
|
270
376
|
end
|
271
377
|
end
|
272
378
|
|
@@ -286,8 +392,8 @@ class FakeFSTest < Test::Unit::TestCase
|
|
286
392
|
FileUtils.mkdir_p '/path'
|
287
393
|
|
288
394
|
Dir.chdir '/path' do
|
289
|
-
File.open('foo', 'w'){|f| f.write 'foo'}
|
290
|
-
File.open('foobar', 'w'){|f| f.write 'foo'}
|
395
|
+
File.open('foo', 'w') { |f| f.write 'foo'}
|
396
|
+
File.open('foobar', 'w') { |f| f.write 'foo'}
|
291
397
|
end
|
292
398
|
|
293
399
|
begin
|
@@ -315,7 +421,7 @@ class FakeFSTest < Test::Unit::TestCase
|
|
315
421
|
FileUtils.mkdir_p 'subdir'
|
316
422
|
assert_equal ['subdir'], FileSystem.current_dir.keys
|
317
423
|
Dir.chdir('subdir')
|
318
|
-
File.open('foo', 'w'){|f| f.write 'foo'}
|
424
|
+
File.open('foo', 'w') { |f| f.write 'foo'}
|
319
425
|
assert_equal ['foo'], FileSystem.current_dir.keys
|
320
426
|
|
321
427
|
assert_raises(Errno::ENOENT) do
|
@@ -340,12 +446,12 @@ class FakeFSTest < Test::Unit::TestCase
|
|
340
446
|
end
|
341
447
|
|
342
448
|
def test_file_open_defaults_to_read
|
343
|
-
File.open('foo','w'){|f| f.write 'bar' }
|
344
|
-
assert_equal 'bar', File.open('foo'){|f| f.read }
|
449
|
+
File.open('foo','w') { |f| f.write 'bar' }
|
450
|
+
assert_equal 'bar', File.open('foo') { |f| f.read }
|
345
451
|
end
|
346
452
|
|
347
453
|
def test_flush_exists_on_file
|
348
|
-
r = File.open('foo','w'){|f| f.write 'bar'; f.flush }
|
454
|
+
r = File.open('foo','w') { |f| f.write 'bar'; f.flush }
|
349
455
|
assert_equal 'foo', r.path
|
350
456
|
end
|
351
457
|
|
@@ -356,9 +462,9 @@ class FakeFSTest < Test::Unit::TestCase
|
|
356
462
|
end
|
357
463
|
|
358
464
|
def test_mv_actually_works
|
359
|
-
File.open('foo', 'w') {|f| f.write 'bar' }
|
465
|
+
File.open('foo', 'w') { |f| f.write 'bar' }
|
360
466
|
FileUtils.mv 'foo', 'baz'
|
361
|
-
assert_equal 'bar', File.open('baz'){|f| f.read }
|
467
|
+
assert_equal 'bar', File.open('baz') { |f| f.read }
|
362
468
|
end
|
363
469
|
|
364
470
|
def test_cp_actually_works
|
@@ -398,10 +504,10 @@ class FakeFSTest < Test::Unit::TestCase
|
|
398
504
|
end
|
399
505
|
|
400
506
|
def test_cp_r_doesnt_tangle_files_together
|
401
|
-
File.open('foo', 'w') {|f| f.write 'bar' }
|
507
|
+
File.open('foo', 'w') { |f| f.write 'bar' }
|
402
508
|
FileUtils.cp_r('foo', 'baz')
|
403
|
-
File.open('baz', 'w') {|f| f.write 'quux' }
|
404
|
-
assert_equal 'bar', File.open('foo'){|f| f.read }
|
509
|
+
File.open('baz', 'w') { |f| f.write 'quux' }
|
510
|
+
assert_equal 'bar', File.open('foo') { |f| f.read }
|
405
511
|
end
|
406
512
|
|
407
513
|
def test_cp_r_should_raise_error_on_missing_file
|
@@ -414,29 +520,29 @@ class FakeFSTest < Test::Unit::TestCase
|
|
414
520
|
|
415
521
|
def test_cp_r_handles_copying_directories
|
416
522
|
FileUtils.mkdir_p 'subdir'
|
417
|
-
Dir.chdir('subdir'){ File.open('foo', 'w'){|f| f.write 'footext' } }
|
523
|
+
Dir.chdir('subdir'){ File.open('foo', 'w') { |f| f.write 'footext' } }
|
418
524
|
|
419
525
|
FileUtils.mkdir_p 'baz'
|
420
526
|
|
421
527
|
# To a previously uncreated directory
|
422
528
|
FileUtils.cp_r('subdir', 'quux')
|
423
|
-
assert_equal 'footext', File.open('quux/foo'){|f| f.read }
|
529
|
+
assert_equal 'footext', File.open('quux/foo') { |f| f.read }
|
424
530
|
|
425
531
|
# To a directory that already exists
|
426
532
|
FileUtils.cp_r('subdir', 'baz')
|
427
|
-
assert_equal 'footext', File.open('baz/subdir/foo'){|f| f.read }
|
533
|
+
assert_equal 'footext', File.open('baz/subdir/foo') { |f| f.read }
|
428
534
|
|
429
535
|
# To a subdirectory of a directory that does not exist
|
430
|
-
assert_raises(Errno::ENOENT)
|
536
|
+
assert_raises(Errno::ENOENT) do
|
431
537
|
FileUtils.cp_r('subdir', 'nope/something')
|
432
|
-
|
538
|
+
end
|
433
539
|
end
|
434
540
|
|
435
541
|
def test_cp_r_only_copies_into_directories
|
436
542
|
FileUtils.mkdir_p 'subdir'
|
437
|
-
Dir.chdir('subdir'){ File.open('foo', 'w'){|f| f.write 'footext' } }
|
543
|
+
Dir.chdir('subdir') { File.open('foo', 'w') { |f| f.write 'footext' } }
|
438
544
|
|
439
|
-
File.open('bar', 'w') {|f| f.write 'bartext' }
|
545
|
+
File.open('bar', 'w') { |f| f.write 'bartext' }
|
440
546
|
|
441
547
|
assert_raises(Errno::EEXIST) do
|
442
548
|
FileUtils.cp_r 'subdir', 'bar'
|
@@ -446,13 +552,13 @@ class FakeFSTest < Test::Unit::TestCase
|
|
446
552
|
FileUtils.ln_s 'otherdir', 'symdir'
|
447
553
|
|
448
554
|
FileUtils.cp_r 'subdir', 'symdir'
|
449
|
-
assert_equal 'footext', File.open('symdir/subdir/foo'){|f| f.read }
|
555
|
+
assert_equal 'footext', File.open('symdir/subdir/foo') { |f| f.read }
|
450
556
|
end
|
451
557
|
|
452
558
|
def test_cp_r_sets_parent_correctly
|
453
559
|
FileUtils.mkdir_p '/path/foo'
|
454
|
-
File.open('/path/foo/bar', 'w'){|f| f.write 'foo' }
|
455
|
-
File.open('/path/foo/baz', 'w'){|f| f.write 'foo' }
|
560
|
+
File.open('/path/foo/bar', 'w') { |f| f.write 'foo' }
|
561
|
+
File.open('/path/foo/baz', 'w') { |f| f.write 'foo' }
|
456
562
|
|
457
563
|
FileUtils.cp_r '/path/foo', '/path/bar'
|
458
564
|
|
@@ -462,10 +568,10 @@ class FakeFSTest < Test::Unit::TestCase
|
|
462
568
|
end
|
463
569
|
|
464
570
|
def test_clone_clones_normal_files
|
465
|
-
RealFile.open(here('foo'), 'w'){|f| f.write 'bar' }
|
571
|
+
RealFile.open(here('foo'), 'w') { |f| f.write 'bar' }
|
466
572
|
assert !File.exists?(here('foo'))
|
467
573
|
FileSystem.clone(here('foo'))
|
468
|
-
assert_equal 'bar', File.open(here('foo')){|f| f.read }
|
574
|
+
assert_equal 'bar', File.open(here('foo')) { |f| f.read }
|
469
575
|
ensure
|
470
576
|
RealFile.unlink(here('foo')) if RealFile.exists?(here('foo'))
|
471
577
|
end
|
@@ -494,22 +600,30 @@ class FakeFSTest < Test::Unit::TestCase
|
|
494
600
|
|
495
601
|
def test_putting_a_dot_at_end_copies_the_contents
|
496
602
|
FileUtils.mkdir_p 'subdir'
|
497
|
-
Dir.chdir('subdir'){ File.open('foo', 'w'){|f| f.write 'footext' } }
|
603
|
+
Dir.chdir('subdir') { File.open('foo', 'w') { |f| f.write 'footext' } }
|
498
604
|
|
499
605
|
FileUtils.mkdir_p 'newdir'
|
500
606
|
FileUtils.cp_r 'subdir/.', 'newdir'
|
501
|
-
assert_equal 'footext', File.open('newdir/foo'){|f| f.read }
|
607
|
+
assert_equal 'footext', File.open('newdir/foo') { |f| f.read }
|
502
608
|
end
|
503
609
|
|
504
610
|
def test_file_can_read_from_symlinks
|
505
|
-
File.open('first', 'w'){|f| f.write '1'}
|
611
|
+
File.open('first', 'w') { |f| f.write '1'}
|
506
612
|
FileUtils.ln_s 'first', 'one'
|
507
|
-
assert_equal '1', File.open('one'){|f| f.read }
|
613
|
+
assert_equal '1', File.open('one') { |f| f.read }
|
508
614
|
|
509
615
|
FileUtils.mkdir_p 'subdir'
|
510
|
-
File.open('subdir/nother','w'){|f| f.write 'works' }
|
616
|
+
File.open('subdir/nother','w') { |f| f.write 'works' }
|
511
617
|
FileUtils.ln_s 'subdir', 'new'
|
512
|
-
assert_equal 'works', File.open('new/nother'){|f| f.read }
|
618
|
+
assert_equal 'works', File.open('new/nother') { |f| f.read }
|
619
|
+
end
|
620
|
+
|
621
|
+
def test_can_symlink_through_file
|
622
|
+
FileUtils.touch("/foo")
|
623
|
+
|
624
|
+
File.symlink("/foo", "/bar")
|
625
|
+
|
626
|
+
assert File.symlink?("/bar")
|
513
627
|
end
|
514
628
|
|
515
629
|
def test_files_can_be_touched
|
@@ -521,15 +635,429 @@ class FakeFSTest < Test::Unit::TestCase
|
|
521
635
|
end
|
522
636
|
|
523
637
|
def test_touch_does_not_work_if_the_dir_path_cannot_be_found
|
524
|
-
assert_raises(Errno::ENOENT)
|
638
|
+
assert_raises(Errno::ENOENT) do
|
525
639
|
FileUtils.touch('this/path/should/not/be/here')
|
526
|
-
|
640
|
+
end
|
527
641
|
FileUtils.mkdir_p('subdir')
|
528
642
|
list = ['subdir/foo', 'nosubdir/bar']
|
529
643
|
|
530
|
-
assert_raises(Errno::ENOENT)
|
644
|
+
assert_raises(Errno::ENOENT) do
|
531
645
|
FileUtils.touch(list)
|
532
|
-
|
646
|
+
end
|
647
|
+
end
|
648
|
+
|
649
|
+
def test_extname
|
650
|
+
assert File.extname("test.doc") == ".doc"
|
651
|
+
end
|
652
|
+
|
653
|
+
# Directory tests
|
654
|
+
def test_new_directory
|
655
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
656
|
+
|
657
|
+
assert_nothing_raised do
|
658
|
+
Dir.new('/this/path/should/be/here')
|
659
|
+
end
|
660
|
+
end
|
661
|
+
|
662
|
+
def test_new_directory_does_not_work_if_dir_path_cannot_be_found
|
663
|
+
assert_raises(Errno::ENOENT) do
|
664
|
+
Dir.new('/this/path/should/not/be/here')
|
665
|
+
end
|
666
|
+
end
|
667
|
+
|
668
|
+
def test_directory_close
|
669
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
670
|
+
dir = Dir.new('/this/path/should/be/here')
|
671
|
+
assert dir.close.nil?
|
672
|
+
|
673
|
+
assert_raises(IOError) do
|
674
|
+
dir.each { |dir| dir }
|
675
|
+
end
|
676
|
+
end
|
677
|
+
|
678
|
+
def test_directory_each
|
679
|
+
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
680
|
+
|
681
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
682
|
+
|
683
|
+
test.each do |f|
|
684
|
+
FileUtils.touch("/this/path/should/be/here/#{f}")
|
685
|
+
end
|
686
|
+
|
687
|
+
dir = Dir.new('/this/path/should/be/here')
|
688
|
+
|
689
|
+
yielded = []
|
690
|
+
dir.each do |dir|
|
691
|
+
yielded << dir
|
692
|
+
end
|
693
|
+
|
694
|
+
assert yielded.size == test.size
|
695
|
+
test.each { |t| assert yielded.include?(t) }
|
696
|
+
end
|
697
|
+
|
698
|
+
def test_directory_path
|
699
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
700
|
+
good_path = '/this/path/should/be/here'
|
701
|
+
assert_equal good_path, Dir.new('/this/path/should/be/here').path
|
702
|
+
end
|
703
|
+
|
704
|
+
def test_directory_pos
|
705
|
+
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
706
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
707
|
+
test.each do |f|
|
708
|
+
FileUtils.touch("/this/path/should/be/here/#{f}")
|
709
|
+
end
|
710
|
+
|
711
|
+
dir = Dir.new('/this/path/should/be/here')
|
712
|
+
|
713
|
+
assert dir.pos == 0
|
714
|
+
dir.read
|
715
|
+
assert dir.pos == 1
|
716
|
+
dir.read
|
717
|
+
assert dir.pos == 2
|
718
|
+
dir.read
|
719
|
+
assert dir.pos == 3
|
720
|
+
dir.read
|
721
|
+
assert dir.pos == 4
|
722
|
+
dir.read
|
723
|
+
assert dir.pos == 5
|
724
|
+
end
|
725
|
+
|
726
|
+
def test_directory_pos_assign
|
727
|
+
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
728
|
+
|
729
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
730
|
+
test.each do |f|
|
731
|
+
FileUtils.touch("/this/path/should/be/here/#{f}")
|
732
|
+
end
|
733
|
+
|
734
|
+
dir = Dir.new('/this/path/should/be/here')
|
735
|
+
|
736
|
+
assert dir.pos == 0
|
737
|
+
dir.pos = 2
|
738
|
+
assert dir.pos == 2
|
739
|
+
end
|
740
|
+
|
741
|
+
def test_directory_read
|
742
|
+
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
743
|
+
|
744
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
745
|
+
test.each do |f|
|
746
|
+
FileUtils.touch("/this/path/should/be/here/#{f}")
|
747
|
+
end
|
748
|
+
|
749
|
+
dir = Dir.new('/this/path/should/be/here')
|
750
|
+
|
751
|
+
assert dir.pos == 0
|
752
|
+
d = dir.read
|
753
|
+
assert dir.pos == 1
|
754
|
+
assert d == '.'
|
755
|
+
|
756
|
+
d = dir.read
|
757
|
+
assert dir.pos == 2
|
758
|
+
assert d == '..'
|
759
|
+
end
|
760
|
+
|
761
|
+
def test_directory_read_past_length
|
762
|
+
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
763
|
+
|
764
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
765
|
+
test.each do |f|
|
766
|
+
FileUtils.touch("/this/path/should/be/here/#{f}")
|
767
|
+
end
|
768
|
+
|
769
|
+
dir = Dir.new('/this/path/should/be/here')
|
770
|
+
|
771
|
+
d = dir.read
|
772
|
+
assert_not_nil d
|
773
|
+
d = dir.read
|
774
|
+
assert_not_nil d
|
775
|
+
d = dir.read
|
776
|
+
assert_not_nil d
|
777
|
+
d = dir.read
|
778
|
+
assert_not_nil d
|
779
|
+
d = dir.read
|
780
|
+
assert_not_nil d
|
781
|
+
d = dir.read
|
782
|
+
assert_not_nil d
|
783
|
+
d = dir.read
|
784
|
+
assert_not_nil d
|
785
|
+
d = dir.read
|
786
|
+
assert_nil d
|
787
|
+
end
|
788
|
+
|
789
|
+
def test_directory_rewind
|
790
|
+
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
791
|
+
|
792
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
793
|
+
test.each do |f|
|
794
|
+
FileUtils.touch("/this/path/should/be/here/#{f}")
|
795
|
+
end
|
796
|
+
|
797
|
+
dir = Dir.new('/this/path/should/be/here')
|
798
|
+
|
799
|
+
d = dir.read
|
800
|
+
d = dir.read
|
801
|
+
assert dir.pos == 2
|
802
|
+
dir.rewind
|
803
|
+
assert dir.pos == 0
|
804
|
+
end
|
805
|
+
|
806
|
+
def test_directory_seek
|
807
|
+
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
808
|
+
|
809
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
810
|
+
test.each do |f|
|
811
|
+
FileUtils.touch("/this/path/should/be/here/#{f}")
|
812
|
+
end
|
813
|
+
|
814
|
+
dir = Dir.new('/this/path/should/be/here')
|
815
|
+
|
816
|
+
d = dir.seek 1
|
817
|
+
assert d == '..'
|
818
|
+
assert dir.pos == 1
|
819
|
+
end
|
820
|
+
|
821
|
+
def test_directory_class_delete
|
822
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
823
|
+
Dir.delete('/this/path/should/be/here')
|
824
|
+
assert File.exists?('/this/path/should/be/here') == false
|
825
|
+
end
|
826
|
+
|
827
|
+
def test_directory_class_delete_does_not_act_on_non_empty_directory
|
828
|
+
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
829
|
+
|
830
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
831
|
+
test.each do |f|
|
832
|
+
FileUtils.touch("/this/path/should/be/here/#{f}")
|
833
|
+
end
|
834
|
+
|
835
|
+
assert_raises(SystemCallError) do
|
836
|
+
Dir.delete('/this/path/should/be/here')
|
837
|
+
end
|
838
|
+
end
|
839
|
+
|
840
|
+
def test_directory_entries
|
841
|
+
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
842
|
+
|
843
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
844
|
+
|
845
|
+
test.each do |f|
|
846
|
+
FileUtils.touch("/this/path/should/be/here/#{f}")
|
847
|
+
end
|
848
|
+
|
849
|
+
yielded = Dir.entries('/this/path/should/be/here')
|
850
|
+
assert yielded.size == test.size
|
851
|
+
test.each { |t| assert yielded.include?(t) }
|
852
|
+
end
|
853
|
+
|
854
|
+
def test_directory_foreach
|
855
|
+
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
856
|
+
|
857
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
858
|
+
|
859
|
+
test.each do |f|
|
860
|
+
FileUtils.touch("/this/path/should/be/here/#{f}")
|
861
|
+
end
|
862
|
+
|
863
|
+
yielded = []
|
864
|
+
Dir.foreach('/this/path/should/be/here') do |dir|
|
865
|
+
yielded << dir
|
866
|
+
end
|
867
|
+
|
868
|
+
assert yielded.size == test.size
|
869
|
+
test.each { |t| assert yielded.include?(t) }
|
870
|
+
end
|
871
|
+
|
872
|
+
def test_directory_mkdir
|
873
|
+
Dir.mkdir('/path')
|
874
|
+
assert File.exists?('/path')
|
875
|
+
end
|
876
|
+
|
877
|
+
def test_directory_mkdir_relative
|
878
|
+
FileUtils.mkdir_p('/new/root')
|
879
|
+
FileSystem.chdir('/new/root')
|
880
|
+
Dir.mkdir('path')
|
881
|
+
assert File.exists?('/new/root/path')
|
882
|
+
end
|
883
|
+
|
884
|
+
def test_directory_mkdir_not_recursive
|
885
|
+
assert_raises(Errno::ENOENT) do
|
886
|
+
Dir.mkdir('/path/does/not/exist')
|
887
|
+
end
|
888
|
+
end
|
889
|
+
|
890
|
+
def test_directory_open
|
891
|
+
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
892
|
+
|
893
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
894
|
+
|
895
|
+
test.each do |f|
|
896
|
+
FileUtils.touch("/this/path/should/be/here/#{f}")
|
897
|
+
end
|
898
|
+
|
899
|
+
dir = Dir.open('/this/path/should/be/here')
|
900
|
+
assert dir.path == '/this/path/should/be/here'
|
901
|
+
end
|
902
|
+
|
903
|
+
def test_directory_open_block
|
904
|
+
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
905
|
+
|
906
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
907
|
+
|
908
|
+
test.each do |f|
|
909
|
+
FileUtils.touch("/this/path/should/be/here/#{f}")
|
910
|
+
end
|
911
|
+
|
912
|
+
yielded = []
|
913
|
+
Dir.open('/this/path/should/be/here') do |dir|
|
914
|
+
yielded << dir
|
915
|
+
end
|
916
|
+
|
917
|
+
assert yielded.size == test.size
|
918
|
+
test.each { |t| assert yielded.include?(t) }
|
919
|
+
end
|
920
|
+
|
921
|
+
def test_tmpdir
|
922
|
+
assert Dir.tmpdir == "/tmp"
|
923
|
+
end
|
924
|
+
|
925
|
+
def test_hard_link_creates_file
|
926
|
+
FileUtils.touch("/foo")
|
927
|
+
|
928
|
+
File.link("/foo", "/bar")
|
929
|
+
assert File.exists?("/bar")
|
930
|
+
end
|
931
|
+
|
932
|
+
def test_hard_link_with_missing_file_raises_error
|
933
|
+
assert_raises(Errno::ENOENT) do
|
934
|
+
File.link("/foo", "/bar")
|
935
|
+
end
|
936
|
+
end
|
937
|
+
|
938
|
+
def test_hard_link_with_existing_destination_file
|
939
|
+
FileUtils.touch("/foo")
|
940
|
+
FileUtils.touch("/bar")
|
941
|
+
|
942
|
+
assert_raises(Errno::EEXIST) do
|
943
|
+
File.link("/foo", "/bar")
|
944
|
+
end
|
945
|
+
end
|
946
|
+
|
947
|
+
def test_hard_link_returns_0_when_successful
|
948
|
+
FileUtils.touch("/foo")
|
949
|
+
|
950
|
+
assert_equal 0, File.link("/foo", "/bar")
|
951
|
+
end
|
952
|
+
|
953
|
+
def test_hard_link_returns_duplicate_file
|
954
|
+
File.open("/foo", "w") { |x| x << "some content" }
|
955
|
+
|
956
|
+
File.link("/foo", "/bar")
|
957
|
+
assert_equal "some content", File.read("/bar")
|
958
|
+
end
|
959
|
+
|
960
|
+
def test_hard_link_with_directory_raises_error
|
961
|
+
Dir.mkdir "/foo"
|
962
|
+
|
963
|
+
assert_raises(Errno::EPERM) do
|
964
|
+
File.link("/foo", "/bar")
|
965
|
+
end
|
966
|
+
end
|
967
|
+
|
968
|
+
def test_file_stat_returns_file_stat_object
|
969
|
+
FileUtils.touch("/foo")
|
970
|
+
assert_equal File::Stat, File.stat("/foo").class
|
971
|
+
end
|
972
|
+
|
973
|
+
def test_can_delete_file_with_delete
|
974
|
+
FileUtils.touch("/foo")
|
975
|
+
|
976
|
+
File.delete("/foo")
|
977
|
+
|
978
|
+
assert !File.exists?("/foo")
|
979
|
+
end
|
980
|
+
|
981
|
+
def test_can_delete_multiple_files_with_delete
|
982
|
+
FileUtils.touch("/foo")
|
983
|
+
FileUtils.touch("/bar")
|
984
|
+
|
985
|
+
File.delete("/foo", "/bar")
|
986
|
+
|
987
|
+
assert !File.exists?("/foo")
|
988
|
+
assert !File.exists?("/bar")
|
989
|
+
end
|
990
|
+
|
991
|
+
def test_delete_raises_argument_error_with_no_filename_given
|
992
|
+
assert_raises ArgumentError do
|
993
|
+
File.delete
|
994
|
+
end
|
995
|
+
end
|
996
|
+
|
997
|
+
def test_delete_returns_number_one_when_given_one_arg
|
998
|
+
FileUtils.touch("/foo")
|
999
|
+
|
1000
|
+
assert_equal 1, File.delete("/foo")
|
1001
|
+
end
|
1002
|
+
|
1003
|
+
def test_delete_returns_number_two_when_given_two_args
|
1004
|
+
FileUtils.touch("/foo")
|
1005
|
+
FileUtils.touch("/bar")
|
1006
|
+
|
1007
|
+
assert_equal 2, File.delete("/foo", "/bar")
|
1008
|
+
end
|
1009
|
+
|
1010
|
+
def test_delete_raises_error_when_first_file_does_not_exist
|
1011
|
+
assert_raises Errno::ENOENT do
|
1012
|
+
File.delete("/foo")
|
1013
|
+
end
|
1014
|
+
end
|
1015
|
+
|
1016
|
+
def test_delete_does_not_raise_error_when_second_file_does_not_exist
|
1017
|
+
FileUtils.touch("/foo")
|
1018
|
+
|
1019
|
+
assert_nothing_raised do
|
1020
|
+
File.delete("/foo", "/bar")
|
1021
|
+
end
|
1022
|
+
end
|
1023
|
+
|
1024
|
+
def test_unlink_is_alias_for_delete
|
1025
|
+
assert_equal File.method(:unlink), File.method(:delete)
|
1026
|
+
end
|
1027
|
+
|
1028
|
+
def test_unlink_removes_only_one_file_content
|
1029
|
+
File.open("/foo", "w") { |f| f << "some_content" }
|
1030
|
+
File.link("/foo", "/bar")
|
1031
|
+
|
1032
|
+
File.unlink("/bar")
|
1033
|
+
File.read("/foo") == "some_content"
|
1034
|
+
end
|
1035
|
+
|
1036
|
+
def test_link_reports_correct_stat_info_after_unlinking
|
1037
|
+
File.open("/foo", "w") { |f| f << "some_content" }
|
1038
|
+
File.link("/foo", "/bar")
|
1039
|
+
|
1040
|
+
File.unlink("/bar")
|
1041
|
+
assert_equal 1, File.stat("/foo").nlink
|
1042
|
+
end
|
1043
|
+
|
1044
|
+
def test_delete_works_with_symlink
|
1045
|
+
FileUtils.touch("/foo")
|
1046
|
+
File.symlink("/foo", "/bar")
|
1047
|
+
|
1048
|
+
File.unlink("/bar")
|
1049
|
+
|
1050
|
+
assert File.exists?("/foo")
|
1051
|
+
assert !File.exists?("/bar")
|
1052
|
+
end
|
1053
|
+
|
1054
|
+
def test_delete_works_with_symlink_source
|
1055
|
+
FileUtils.touch("/foo")
|
1056
|
+
File.symlink("/foo", "/bar")
|
1057
|
+
|
1058
|
+
File.unlink("/foo")
|
1059
|
+
|
1060
|
+
assert !File.exists?("/foo")
|
533
1061
|
end
|
534
1062
|
|
535
1063
|
def here(fname)
|