madpilot-fakefs 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.markdown +68 -0
- data/Rakefile +40 -0
- data/lib/fakefs/base.rb +37 -0
- data/lib/fakefs/dir.rb +114 -0
- data/lib/fakefs/fake/dir.rb +37 -0
- data/lib/fakefs/fake/file.rb +29 -0
- data/lib/fakefs/fake/symlink.rb +26 -0
- data/lib/fakefs/file.rb +126 -0
- data/lib/fakefs/file_system.rb +118 -0
- data/lib/fakefs/fileutils.rb +108 -0
- data/lib/fakefs/safe.rb +11 -0
- data/lib/fakefs/version.rb +9 -0
- data/lib/fakefs.rb +3 -0
- data/test/fakefs_test.rb +804 -0
- data/test/safe_test.rb +20 -0
- data/test/verify.rb +27 -0
- metadata +72 -0
data/test/fakefs_test.rb
ADDED
@@ -0,0 +1,804 @@
|
|
1
|
+
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
|
2
|
+
require 'fakefs'
|
3
|
+
require 'test/unit'
|
4
|
+
|
5
|
+
class FakeFSTest < Test::Unit::TestCase
|
6
|
+
include FakeFS
|
7
|
+
|
8
|
+
def setup
|
9
|
+
FileSystem.clear
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_can_be_initialized_empty
|
13
|
+
fs = FileSystem
|
14
|
+
assert_equal 0, fs.files.size
|
15
|
+
end
|
16
|
+
|
17
|
+
def xtest_can_be_initialized_with_an_existing_directory
|
18
|
+
fs = FileSystem
|
19
|
+
fs.clone(File.expand_path(File.dirname(__FILE__))).inspect
|
20
|
+
puts fs.files.inspect
|
21
|
+
assert_equal 1, fs.files.size
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_can_create_directories
|
25
|
+
FileUtils.mkdir_p("/path/to/dir")
|
26
|
+
assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_knows_directories_exist
|
30
|
+
FileUtils.mkdir_p(path = "/path/to/dir")
|
31
|
+
assert File.exists?(path)
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_knows_directories_are_directories
|
35
|
+
FileUtils.mkdir_p(path = "/path/to/dir")
|
36
|
+
assert File.directory?(path)
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_knows_symlink_directories_are_directories
|
40
|
+
FileUtils.mkdir_p(path = "/path/to/dir")
|
41
|
+
FileUtils.ln_s path, sympath = '/sympath'
|
42
|
+
assert File.directory?(sympath)
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_knows_non_existent_directories_arent_directories
|
46
|
+
path = 'does/not/exist/'
|
47
|
+
assert_equal RealFile.directory?(path), File.directory?(path)
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_doesnt_overwrite_existing_directories
|
51
|
+
FileUtils.mkdir_p(path = "/path/to/dir")
|
52
|
+
assert File.exists?(path)
|
53
|
+
FileUtils.mkdir_p("/path/to")
|
54
|
+
assert File.exists?(path)
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_can_create_symlinks
|
58
|
+
FileUtils.mkdir_p(target = "/path/to/target")
|
59
|
+
FileUtils.ln_s(target, "/path/to/link")
|
60
|
+
assert_kind_of FakeSymlink, FileSystem.fs['path']['to']['link']
|
61
|
+
|
62
|
+
assert_raises(Errno::EEXIST) {
|
63
|
+
FileUtils.ln_s(target, '/path/to/link')
|
64
|
+
}
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_can_follow_symlinks
|
68
|
+
FileUtils.mkdir_p(target = "/path/to/target")
|
69
|
+
FileUtils.ln_s(target, link = "/path/to/symlink")
|
70
|
+
assert_equal target, File.readlink(link)
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_knows_symlinks_are_symlinks
|
74
|
+
FileUtils.mkdir_p(target = "/path/to/target")
|
75
|
+
FileUtils.ln_s(target, link = "/path/to/symlink")
|
76
|
+
assert File.symlink?(link)
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_can_create_files
|
80
|
+
path = '/path/to/file.txt'
|
81
|
+
File.open(path, 'w') do |f|
|
82
|
+
f.write "Yatta!"
|
83
|
+
end
|
84
|
+
|
85
|
+
assert File.exists?(path)
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_can_read_files_once_written
|
89
|
+
path = '/path/to/file.txt'
|
90
|
+
File.open(path, 'w') do |f|
|
91
|
+
f.write "Yatta!"
|
92
|
+
end
|
93
|
+
|
94
|
+
assert_equal "Yatta!", File.read(path)
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_can_write_to_files
|
98
|
+
path = '/path/to/file.txt'
|
99
|
+
File.open(path, 'w') do |f|
|
100
|
+
f << 'Yada Yada'
|
101
|
+
end
|
102
|
+
assert_equal 'Yada Yada', File.read(path)
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_can_read_with_File_readlines
|
106
|
+
path = '/path/to/file.txt'
|
107
|
+
File.open(path, 'w') do |f|
|
108
|
+
f.puts "Yatta!", "Gatta!"
|
109
|
+
f.puts ["woot","toot"]
|
110
|
+
end
|
111
|
+
|
112
|
+
assert_equal %w(Yatta! Gatta! woot toot), File.readlines(path)
|
113
|
+
end
|
114
|
+
|
115
|
+
def test_File_close_disallows_further_access
|
116
|
+
path = '/path/to/file.txt'
|
117
|
+
file = File.open(path, 'w')
|
118
|
+
file.write 'Yada'
|
119
|
+
file.close
|
120
|
+
assert_raise IOError do
|
121
|
+
file.read
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
def test_can_read_from_file_objects
|
126
|
+
path = '/path/to/file.txt'
|
127
|
+
File.open(path, 'w') do |f|
|
128
|
+
f.write "Yatta!"
|
129
|
+
end
|
130
|
+
|
131
|
+
assert_equal "Yatta!", File.new(path).read
|
132
|
+
end
|
133
|
+
|
134
|
+
def test_file_read_errors_appropriately
|
135
|
+
assert_raise Errno::ENOENT do
|
136
|
+
File.read('anything')
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
def test_knows_files_are_files
|
141
|
+
path = '/path/to/file.txt'
|
142
|
+
File.open(path, 'w') do |f|
|
143
|
+
f.write "Yatta!"
|
144
|
+
end
|
145
|
+
|
146
|
+
assert File.file?(path)
|
147
|
+
end
|
148
|
+
|
149
|
+
def test_knows_symlink_files_are_files
|
150
|
+
path = '/path/to/file.txt'
|
151
|
+
File.open(path, 'w') do |f|
|
152
|
+
f.write "Yatta!"
|
153
|
+
end
|
154
|
+
FileUtils.ln_s path, sympath='/sympath'
|
155
|
+
|
156
|
+
assert File.file?(sympath)
|
157
|
+
end
|
158
|
+
|
159
|
+
def test_knows_non_existent_files_arent_files
|
160
|
+
assert_equal RealFile.file?('does/not/exist.txt'), File.file?('does/not/exist.txt')
|
161
|
+
end
|
162
|
+
|
163
|
+
def test_can_chown_files
|
164
|
+
good = 'file.txt'
|
165
|
+
bad = 'nofile.txt'
|
166
|
+
File.open(good,'w'){|f| f.write "foo" }
|
167
|
+
|
168
|
+
assert_equal [good], FileUtils.chown('noone', 'nogroup', good, :verbose => true)
|
169
|
+
assert_raises(Errno::ENOENT) do
|
170
|
+
FileUtils.chown('noone', 'nogroup', bad, :verbose => true)
|
171
|
+
end
|
172
|
+
|
173
|
+
assert_equal [good], FileUtils.chown('noone', 'nogroup', good)
|
174
|
+
assert_raises(Errno::ENOENT) do
|
175
|
+
FileUtils.chown('noone', 'nogroup', bad)
|
176
|
+
end
|
177
|
+
|
178
|
+
assert_equal [good], FileUtils.chown('noone', 'nogroup', [good])
|
179
|
+
assert_raises(Errno::ENOENT) do
|
180
|
+
FileUtils.chown('noone', 'nogroup', [good, bad])
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
def test_can_chown_R_files
|
185
|
+
FileUtils.mkdir_p '/path/'
|
186
|
+
File.open('/path/foo', 'w'){|f| f.write 'foo' }
|
187
|
+
File.open('/path/foobar', 'w'){|f| f.write 'foo' }
|
188
|
+
resp = FileUtils.chown_R('no', 'no', '/path')
|
189
|
+
assert_equal ['/path'], resp
|
190
|
+
end
|
191
|
+
|
192
|
+
def test_dir_globs_paths
|
193
|
+
FileUtils.mkdir_p '/path'
|
194
|
+
File.open('/path/foo', 'w'){|f| f.write 'foo' }
|
195
|
+
File.open('/path/foobar', 'w'){|f| f.write 'foo' }
|
196
|
+
|
197
|
+
FileUtils.mkdir_p '/path/bar'
|
198
|
+
File.open('/path/bar/baz', 'w'){|f| f.write 'foo' }
|
199
|
+
|
200
|
+
FileUtils.cp_r '/path/bar', '/path/bar2'
|
201
|
+
|
202
|
+
assert_equal ['/path'], Dir['/path']
|
203
|
+
assert_equal %w( /path/bar /path/bar2 /path/foo /path/foobar ), Dir['/path/*']
|
204
|
+
|
205
|
+
assert_equal ['/path/bar/baz'], Dir['/path/bar/*']
|
206
|
+
assert_equal ['/path/foo'], Dir['/path/foo']
|
207
|
+
|
208
|
+
assert_equal ['/path'], Dir['/path*']
|
209
|
+
assert_equal ['/path/foo', '/path/foobar'], Dir['/p*h/foo*']
|
210
|
+
assert_equal ['/path/foo', '/path/foobar'], Dir['/p??h/foo*']
|
211
|
+
|
212
|
+
FileUtils.cp_r '/path', '/otherpath'
|
213
|
+
|
214
|
+
assert_equal %w( /otherpath/foo /otherpath/foobar /path/foo /path/foobar ), Dir['/*/foo*']
|
215
|
+
end
|
216
|
+
|
217
|
+
def test_dir_glob_handles_root
|
218
|
+
FileUtils.mkdir_p '/path'
|
219
|
+
|
220
|
+
# this fails. the root dir should be named '/' but it is '.'
|
221
|
+
#assert_equal ['/'], Dir['/']
|
222
|
+
end
|
223
|
+
|
224
|
+
def test_chdir_changes_directories_like_a_boss
|
225
|
+
# I know memes!
|
226
|
+
FileUtils.mkdir_p '/path'
|
227
|
+
assert_equal '.', FileSystem.fs.name
|
228
|
+
assert_equal({}, FileSystem.fs['path'])
|
229
|
+
Dir.chdir '/path' do
|
230
|
+
File.open('foo', 'w'){|f| f.write 'foo'}
|
231
|
+
File.open('foobar', 'w'){|f| f.write 'foo'}
|
232
|
+
end
|
233
|
+
|
234
|
+
assert_equal '.', FileSystem.fs.name
|
235
|
+
assert_equal(['foo', 'foobar'], FileSystem.fs['path'].keys.sort)
|
236
|
+
|
237
|
+
c = nil
|
238
|
+
Dir.chdir '/path' do
|
239
|
+
c = File.open('foo', 'r'){|f| f.read }
|
240
|
+
end
|
241
|
+
|
242
|
+
assert_equal 'foo', c
|
243
|
+
end
|
244
|
+
|
245
|
+
def test_chdir_shouldnt_keep_us_from_absolute_paths
|
246
|
+
FileUtils.mkdir_p '/path'
|
247
|
+
|
248
|
+
Dir.chdir '/path' do
|
249
|
+
File.open('foo', 'w'){|f| f.write 'foo'}
|
250
|
+
File.open('/foobar', 'w'){|f| f.write 'foo'}
|
251
|
+
end
|
252
|
+
assert_equal ['foo'], FileSystem.fs['path'].keys.sort
|
253
|
+
assert_equal ['foobar', 'path'], FileSystem.fs.keys.sort
|
254
|
+
|
255
|
+
Dir.chdir '/path' do
|
256
|
+
FileUtils.rm('foo')
|
257
|
+
FileUtils.rm('/foobar')
|
258
|
+
end
|
259
|
+
|
260
|
+
assert_equal [], FileSystem.fs['path'].keys.sort
|
261
|
+
assert_equal ['path'], FileSystem.fs.keys.sort
|
262
|
+
end
|
263
|
+
|
264
|
+
def test_chdir_should_be_nestable
|
265
|
+
FileUtils.mkdir_p '/path/me'
|
266
|
+
Dir.chdir '/path' do
|
267
|
+
File.open('foo', 'w'){|f| f.write 'foo'}
|
268
|
+
Dir.chdir 'me' do
|
269
|
+
File.open('foobar', 'w'){|f| f.write 'foo'}
|
270
|
+
end
|
271
|
+
end
|
272
|
+
|
273
|
+
assert_equal ['foo','me'], FileSystem.fs['path'].keys.sort
|
274
|
+
assert_equal ['foobar'], FileSystem.fs['path']['me'].keys.sort
|
275
|
+
end
|
276
|
+
|
277
|
+
def test_chdir_should_flop_over_and_die_if_the_dir_doesnt_exist
|
278
|
+
assert_raise(Errno::ENOENT) do
|
279
|
+
Dir.chdir('/nope') do
|
280
|
+
1
|
281
|
+
end
|
282
|
+
end
|
283
|
+
end
|
284
|
+
|
285
|
+
def test_chdir_shouldnt_lose_state_because_of_errors
|
286
|
+
FileUtils.mkdir_p '/path'
|
287
|
+
|
288
|
+
Dir.chdir '/path' do
|
289
|
+
File.open('foo', 'w'){|f| f.write 'foo'}
|
290
|
+
File.open('foobar', 'w'){|f| f.write 'foo'}
|
291
|
+
end
|
292
|
+
|
293
|
+
begin
|
294
|
+
Dir.chdir('/path') do
|
295
|
+
raise Exception
|
296
|
+
end
|
297
|
+
rescue Exception # hardcore
|
298
|
+
end
|
299
|
+
|
300
|
+
Dir.chdir('/path') do
|
301
|
+
begin
|
302
|
+
Dir.chdir('nope'){ }
|
303
|
+
rescue Errno::ENOENT
|
304
|
+
end
|
305
|
+
|
306
|
+
assert_equal ['/path'], FileSystem.dir_levels
|
307
|
+
end
|
308
|
+
|
309
|
+
assert_equal(['foo', 'foobar'], FileSystem.fs['path'].keys.sort)
|
310
|
+
end
|
311
|
+
|
312
|
+
def test_chdir_with_no_block_is_awesome
|
313
|
+
FileUtils.mkdir_p '/path'
|
314
|
+
Dir.chdir('/path')
|
315
|
+
FileUtils.mkdir_p 'subdir'
|
316
|
+
assert_equal ['subdir'], FileSystem.current_dir.keys
|
317
|
+
Dir.chdir('subdir')
|
318
|
+
File.open('foo', 'w'){|f| f.write 'foo'}
|
319
|
+
assert_equal ['foo'], FileSystem.current_dir.keys
|
320
|
+
|
321
|
+
assert_raises(Errno::ENOENT) do
|
322
|
+
Dir.chdir('subsubdir')
|
323
|
+
end
|
324
|
+
|
325
|
+
assert_equal ['foo'], FileSystem.current_dir.keys
|
326
|
+
end
|
327
|
+
|
328
|
+
def test_current_dir_reflected_by_pwd
|
329
|
+
FileUtils.mkdir_p '/path'
|
330
|
+
Dir.chdir('/path')
|
331
|
+
|
332
|
+
assert_equal '/path', Dir.pwd
|
333
|
+
assert_equal '/path', Dir.getwd
|
334
|
+
|
335
|
+
FileUtils.mkdir_p 'subdir'
|
336
|
+
Dir.chdir('subdir')
|
337
|
+
|
338
|
+
assert_equal '/path/subdir', Dir.pwd
|
339
|
+
assert_equal '/path/subdir', Dir.getwd
|
340
|
+
end
|
341
|
+
|
342
|
+
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 }
|
345
|
+
end
|
346
|
+
|
347
|
+
def test_flush_exists_on_file
|
348
|
+
r = File.open('foo','w'){|f| f.write 'bar'; f.flush }
|
349
|
+
assert_equal 'foo', r.path
|
350
|
+
end
|
351
|
+
|
352
|
+
def test_mv_should_raise_error_on_missing_file
|
353
|
+
assert_raise(Errno::ENOENT) do
|
354
|
+
FileUtils.mv 'blafgag', 'foo'
|
355
|
+
end
|
356
|
+
end
|
357
|
+
|
358
|
+
def test_mv_actually_works
|
359
|
+
File.open('foo', 'w') {|f| f.write 'bar' }
|
360
|
+
FileUtils.mv 'foo', 'baz'
|
361
|
+
assert_equal 'bar', File.open('baz'){|f| f.read }
|
362
|
+
end
|
363
|
+
|
364
|
+
def test_cp_actually_works
|
365
|
+
File.open('foo', 'w') {|f| f.write 'bar' }
|
366
|
+
FileUtils.cp('foo', 'baz')
|
367
|
+
assert_equal 'bar', File.read('baz')
|
368
|
+
end
|
369
|
+
|
370
|
+
def test_cp_file_into_dir
|
371
|
+
File.open('foo', 'w') {|f| f.write 'bar' }
|
372
|
+
FileUtils.mkdir_p 'baz'
|
373
|
+
|
374
|
+
FileUtils.cp('foo', 'baz')
|
375
|
+
assert_equal 'bar', File.read('baz/foo')
|
376
|
+
end
|
377
|
+
|
378
|
+
def test_cp_overwrites_dest_file
|
379
|
+
File.open('foo', 'w') {|f| f.write 'FOO' }
|
380
|
+
File.open('bar', 'w') {|f| f.write 'BAR' }
|
381
|
+
|
382
|
+
FileUtils.cp('foo', 'bar')
|
383
|
+
assert_equal 'FOO', File.read('bar')
|
384
|
+
end
|
385
|
+
|
386
|
+
def test_cp_fails_on_no_source
|
387
|
+
assert_raise Errno::ENOENT do
|
388
|
+
FileUtils.cp('foo', 'baz')
|
389
|
+
end
|
390
|
+
end
|
391
|
+
|
392
|
+
def test_cp_fails_on_directory_copy
|
393
|
+
FileUtils.mkdir_p 'baz'
|
394
|
+
|
395
|
+
assert_raise Errno::EISDIR do
|
396
|
+
FileUtils.cp('baz', 'bar')
|
397
|
+
end
|
398
|
+
end
|
399
|
+
|
400
|
+
def test_cp_r_doesnt_tangle_files_together
|
401
|
+
File.open('foo', 'w') {|f| f.write 'bar' }
|
402
|
+
FileUtils.cp_r('foo', 'baz')
|
403
|
+
File.open('baz', 'w') {|f| f.write 'quux' }
|
404
|
+
assert_equal 'bar', File.open('foo'){|f| f.read }
|
405
|
+
end
|
406
|
+
|
407
|
+
def test_cp_r_should_raise_error_on_missing_file
|
408
|
+
# Yes, this error sucks, but it conforms to the original Ruby
|
409
|
+
# method.
|
410
|
+
assert_raise(RuntimeError) do
|
411
|
+
FileUtils.cp_r 'blafgag', 'foo'
|
412
|
+
end
|
413
|
+
end
|
414
|
+
|
415
|
+
def test_cp_r_handles_copying_directories
|
416
|
+
FileUtils.mkdir_p 'subdir'
|
417
|
+
Dir.chdir('subdir'){ File.open('foo', 'w'){|f| f.write 'footext' } }
|
418
|
+
|
419
|
+
FileUtils.mkdir_p 'baz'
|
420
|
+
|
421
|
+
# To a previously uncreated directory
|
422
|
+
FileUtils.cp_r('subdir', 'quux')
|
423
|
+
assert_equal 'footext', File.open('quux/foo'){|f| f.read }
|
424
|
+
|
425
|
+
# To a directory that already exists
|
426
|
+
FileUtils.cp_r('subdir', 'baz')
|
427
|
+
assert_equal 'footext', File.open('baz/subdir/foo'){|f| f.read }
|
428
|
+
|
429
|
+
# To a subdirectory of a directory that does not exist
|
430
|
+
assert_raises(Errno::ENOENT) {
|
431
|
+
FileUtils.cp_r('subdir', 'nope/something')
|
432
|
+
}
|
433
|
+
end
|
434
|
+
|
435
|
+
def test_cp_r_only_copies_into_directories
|
436
|
+
FileUtils.mkdir_p 'subdir'
|
437
|
+
Dir.chdir('subdir'){ File.open('foo', 'w'){|f| f.write 'footext' } }
|
438
|
+
|
439
|
+
File.open('bar', 'w') {|f| f.write 'bartext' }
|
440
|
+
|
441
|
+
assert_raises(Errno::EEXIST) do
|
442
|
+
FileUtils.cp_r 'subdir', 'bar'
|
443
|
+
end
|
444
|
+
|
445
|
+
FileUtils.mkdir_p 'otherdir'
|
446
|
+
FileUtils.ln_s 'otherdir', 'symdir'
|
447
|
+
|
448
|
+
FileUtils.cp_r 'subdir', 'symdir'
|
449
|
+
assert_equal 'footext', File.open('symdir/subdir/foo'){|f| f.read }
|
450
|
+
end
|
451
|
+
|
452
|
+
def test_cp_r_sets_parent_correctly
|
453
|
+
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' }
|
456
|
+
|
457
|
+
FileUtils.cp_r '/path/foo', '/path/bar'
|
458
|
+
|
459
|
+
assert File.exists?('/path/bar/baz')
|
460
|
+
FileUtils.rm_rf '/path/bar/baz'
|
461
|
+
assert_equal %w( /path/bar/bar ), Dir['/path/bar/*']
|
462
|
+
end
|
463
|
+
|
464
|
+
def test_clone_clones_normal_files
|
465
|
+
RealFile.open(here('foo'), 'w'){|f| f.write 'bar' }
|
466
|
+
assert !File.exists?(here('foo'))
|
467
|
+
FileSystem.clone(here('foo'))
|
468
|
+
assert_equal 'bar', File.open(here('foo')){|f| f.read }
|
469
|
+
ensure
|
470
|
+
RealFile.unlink(here('foo')) if RealFile.exists?(here('foo'))
|
471
|
+
end
|
472
|
+
|
473
|
+
def test_clone_clones_directories
|
474
|
+
RealFileUtils.mkdir_p(here('subdir'))
|
475
|
+
|
476
|
+
FileSystem.clone(here('subdir'))
|
477
|
+
|
478
|
+
assert File.exists?(here('subdir')), 'subdir was cloned'
|
479
|
+
assert File.directory?(here('subdir')), 'subdir is a directory'
|
480
|
+
ensure
|
481
|
+
RealFileUtils.rm_rf(here('subdir')) if RealFile.exists?(here('subdir'))
|
482
|
+
end
|
483
|
+
|
484
|
+
def test_clone_clones_dot_files_even_hard_to_find_ones
|
485
|
+
RealFileUtils.mkdir_p(here('subdir/.bar/baz/.quux/foo'))
|
486
|
+
assert !File.exists?(here('subdir'))
|
487
|
+
|
488
|
+
FileSystem.clone(here('subdir'))
|
489
|
+
assert_equal ['.bar'], FileSystem.find(here('subdir')).keys
|
490
|
+
assert_equal ['foo'], FileSystem.find(here('subdir/.bar/baz/.quux')).keys
|
491
|
+
ensure
|
492
|
+
RealFileUtils.rm_rf(here('subdir')) if RealFile.exists?(here('subdir'))
|
493
|
+
end
|
494
|
+
|
495
|
+
def test_putting_a_dot_at_end_copies_the_contents
|
496
|
+
FileUtils.mkdir_p 'subdir'
|
497
|
+
Dir.chdir('subdir'){ File.open('foo', 'w'){|f| f.write 'footext' } }
|
498
|
+
|
499
|
+
FileUtils.mkdir_p 'newdir'
|
500
|
+
FileUtils.cp_r 'subdir/.', 'newdir'
|
501
|
+
assert_equal 'footext', File.open('newdir/foo'){|f| f.read }
|
502
|
+
end
|
503
|
+
|
504
|
+
def test_file_can_read_from_symlinks
|
505
|
+
File.open('first', 'w'){|f| f.write '1'}
|
506
|
+
FileUtils.ln_s 'first', 'one'
|
507
|
+
assert_equal '1', File.open('one'){|f| f.read }
|
508
|
+
|
509
|
+
FileUtils.mkdir_p 'subdir'
|
510
|
+
File.open('subdir/nother','w'){|f| f.write 'works' }
|
511
|
+
FileUtils.ln_s 'subdir', 'new'
|
512
|
+
assert_equal 'works', File.open('new/nother'){|f| f.read }
|
513
|
+
end
|
514
|
+
|
515
|
+
def test_files_can_be_touched
|
516
|
+
FileUtils.touch('touched_file')
|
517
|
+
assert File.exists?('touched_file')
|
518
|
+
list = ['newfile', 'another']
|
519
|
+
FileUtils.touch(list)
|
520
|
+
list.each { |fp| assert(File.exists?(fp)) }
|
521
|
+
end
|
522
|
+
|
523
|
+
def test_touch_does_not_work_if_the_dir_path_cannot_be_found
|
524
|
+
assert_raises(Errno::ENOENT) {
|
525
|
+
FileUtils.touch('this/path/should/not/be/here')
|
526
|
+
}
|
527
|
+
FileUtils.mkdir_p('subdir')
|
528
|
+
list = ['subdir/foo', 'nosubdir/bar']
|
529
|
+
|
530
|
+
assert_raises(Errno::ENOENT) {
|
531
|
+
FileUtils.touch(list)
|
532
|
+
}
|
533
|
+
end
|
534
|
+
|
535
|
+
# Directory tests
|
536
|
+
def test_new_directory
|
537
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
538
|
+
|
539
|
+
assert_nothing_raised {
|
540
|
+
Dir.new('/this/path/should/be/here')
|
541
|
+
}
|
542
|
+
end
|
543
|
+
|
544
|
+
def test_new_directory_does_not_work_if_dir_path_cannot_be_found
|
545
|
+
assert_raises(Errno::ENOENT) {
|
546
|
+
Dir.new('/this/path/should/not/be/here')
|
547
|
+
}
|
548
|
+
end
|
549
|
+
|
550
|
+
def test_directory_close
|
551
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
552
|
+
dir = Dir.new('/this/path/should/be/here')
|
553
|
+
assert dir.close.nil?
|
554
|
+
|
555
|
+
assert_raises(IOError) {
|
556
|
+
dir.each { |dir| dir }
|
557
|
+
}
|
558
|
+
end
|
559
|
+
|
560
|
+
def test_directory_each
|
561
|
+
test = ['.', '..', '/this/path/should/be/here/file_1', '/this/path/should/be/here/file_2', '/this/path/should/be/here/file_3', '/this/path/should/be/here/file_4', '/this/path/should/be/here/file_5' ]
|
562
|
+
|
563
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
564
|
+
|
565
|
+
test.each do |f|
|
566
|
+
FileUtils.touch(f)
|
567
|
+
end
|
568
|
+
|
569
|
+
dir = Dir.new('/this/path/should/be/here')
|
570
|
+
|
571
|
+
yielded = []
|
572
|
+
dir.each do |dir|
|
573
|
+
yielded << dir
|
574
|
+
end
|
575
|
+
|
576
|
+
assert yielded.size == test.size
|
577
|
+
test.each { |t| assert yielded.include?(t) }
|
578
|
+
end
|
579
|
+
|
580
|
+
def test_directory_path
|
581
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
582
|
+
assert Dir.new('/this/path/should/be/here').path == '/this/path/should/be/here'
|
583
|
+
end
|
584
|
+
|
585
|
+
def test_directory_pos
|
586
|
+
test = ['.', '..', '/this/path/should/be/here/file_1', '/this/path/should/be/here/file_2', '/this/path/should/be/here/file_3', '/this/path/should/be/here/file_4', '/this/path/should/be/here/file_5' ]
|
587
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
588
|
+
test.each do |f|
|
589
|
+
FileUtils.touch(f)
|
590
|
+
end
|
591
|
+
|
592
|
+
dir = Dir.new('/this/path/should/be/here')
|
593
|
+
|
594
|
+
assert dir.pos == 0
|
595
|
+
dir.read
|
596
|
+
assert dir.pos == 1
|
597
|
+
dir.read
|
598
|
+
assert dir.pos == 2
|
599
|
+
dir.read
|
600
|
+
assert dir.pos == 3
|
601
|
+
dir.read
|
602
|
+
assert dir.pos == 4
|
603
|
+
dir.read
|
604
|
+
assert dir.pos == 5
|
605
|
+
end
|
606
|
+
|
607
|
+
def test_directory_pos_assign
|
608
|
+
test = ['.', '..', '/this/path/should/be/here/file_1', '/this/path/should/be/here/file_2', '/this/path/should/be/here/file_3', '/this/path/should/be/here/file_4', '/this/path/should/be/here/file_5' ]
|
609
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
610
|
+
test.each do |f|
|
611
|
+
FileUtils.touch(f)
|
612
|
+
end
|
613
|
+
|
614
|
+
dir = Dir.new('/this/path/should/be/here')
|
615
|
+
|
616
|
+
assert dir.pos == 0
|
617
|
+
dir.pos = 2
|
618
|
+
assert dir.pos == 2
|
619
|
+
end
|
620
|
+
|
621
|
+
def test_directory_read
|
622
|
+
test = ['.', '..', '/this/path/should/be/here/file_1', '/this/path/should/be/here/file_2', '/this/path/should/be/here/file_3', '/this/path/should/be/here/file_4', '/this/path/should/be/here/file_5' ]
|
623
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
624
|
+
test.each do |f|
|
625
|
+
FileUtils.touch(f)
|
626
|
+
end
|
627
|
+
|
628
|
+
dir = Dir.new('/this/path/should/be/here')
|
629
|
+
|
630
|
+
assert dir.pos == 0
|
631
|
+
d = dir.read
|
632
|
+
assert dir.pos == 1
|
633
|
+
assert d == '.'
|
634
|
+
|
635
|
+
d = dir.read
|
636
|
+
assert dir.pos == 2
|
637
|
+
assert d == '..'
|
638
|
+
end
|
639
|
+
|
640
|
+
def test_directory_read_past_length
|
641
|
+
test = ['.', '..', '/this/path/should/be/here/file_1', '/this/path/should/be/here/file_2', '/this/path/should/be/here/file_3', '/this/path/should/be/here/file_4', '/this/path/should/be/here/file_5' ]
|
642
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
643
|
+
test.each do |f|
|
644
|
+
FileUtils.touch(f)
|
645
|
+
end
|
646
|
+
|
647
|
+
dir = Dir.new('/this/path/should/be/here')
|
648
|
+
|
649
|
+
d = dir.read
|
650
|
+
assert_not_nil d
|
651
|
+
d = dir.read
|
652
|
+
assert_not_nil d
|
653
|
+
d = dir.read
|
654
|
+
assert_not_nil d
|
655
|
+
d = dir.read
|
656
|
+
assert_not_nil d
|
657
|
+
d = dir.read
|
658
|
+
assert_not_nil d
|
659
|
+
d = dir.read
|
660
|
+
assert_not_nil d
|
661
|
+
d = dir.read
|
662
|
+
assert_not_nil d
|
663
|
+
d = dir.read
|
664
|
+
assert_nil d
|
665
|
+
end
|
666
|
+
|
667
|
+
def test_directory_rewind
|
668
|
+
test = ['.', '..', '/this/path/should/be/here/file_1', '/this/path/should/be/here/file_2', '/this/path/should/be/here/file_3', '/this/path/should/be/here/file_4', '/this/path/should/be/here/file_5' ]
|
669
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
670
|
+
test.each do |f|
|
671
|
+
FileUtils.touch(f)
|
672
|
+
end
|
673
|
+
|
674
|
+
dir = Dir.new('/this/path/should/be/here')
|
675
|
+
|
676
|
+
d = dir.read
|
677
|
+
d = dir.read
|
678
|
+
assert dir.pos == 2
|
679
|
+
dir.rewind
|
680
|
+
assert dir.pos == 0
|
681
|
+
end
|
682
|
+
|
683
|
+
def test_directory_seek
|
684
|
+
test = ['.', '..', '/this/path/should/be/here/file_1', '/this/path/should/be/here/file_2', '/this/path/should/be/here/file_3', '/this/path/should/be/here/file_4', '/this/path/should/be/here/file_5' ]
|
685
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
686
|
+
test.each do |f|
|
687
|
+
FileUtils.touch(f)
|
688
|
+
end
|
689
|
+
|
690
|
+
dir = Dir.new('/this/path/should/be/here')
|
691
|
+
|
692
|
+
d = dir.seek 1
|
693
|
+
puts d
|
694
|
+
assert d == '..'
|
695
|
+
assert dir.pos == 1
|
696
|
+
end
|
697
|
+
|
698
|
+
def test_directory_class_delete
|
699
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
700
|
+
Dir.delete('/this/path/should/be/here')
|
701
|
+
assert File.exists?('/this/path/should/be/here') == false
|
702
|
+
end
|
703
|
+
|
704
|
+
def test_directory_class_delete_does_not_act_on_non_empty_directory
|
705
|
+
test = ['.', '..', '/this/path/should/be/here/file_1', '/this/path/should/be/here/file_2', '/this/path/should/be/here/file_3', '/this/path/should/be/here/file_4', '/this/path/should/be/here/file_5' ]
|
706
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
707
|
+
test.each do |f|
|
708
|
+
FileUtils.touch(f)
|
709
|
+
end
|
710
|
+
|
711
|
+
assert_raises(SystemCallError) do
|
712
|
+
Dir.delete('/this/path/should/be/here')
|
713
|
+
end
|
714
|
+
end
|
715
|
+
|
716
|
+
def test_directory_entries
|
717
|
+
test = ['.', '..', '/this/path/should/be/here/file_1', '/this/path/should/be/here/file_2', '/this/path/should/be/here/file_3', '/this/path/should/be/here/file_4', '/this/path/should/be/here/file_5' ]
|
718
|
+
|
719
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
720
|
+
|
721
|
+
test.each do |f|
|
722
|
+
FileUtils.touch(f)
|
723
|
+
end
|
724
|
+
|
725
|
+
yielded = Dir.entries('/this/path/should/be/here')
|
726
|
+
assert yielded.size == test.size
|
727
|
+
test.each { |t| assert yielded.include?(t) }
|
728
|
+
end
|
729
|
+
|
730
|
+
def test_directory_foreach
|
731
|
+
test = ['.', '..', '/this/path/should/be/here/file_1', '/this/path/should/be/here/file_2', '/this/path/should/be/here/file_3', '/this/path/should/be/here/file_4', '/this/path/should/be/here/file_5' ]
|
732
|
+
|
733
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
734
|
+
|
735
|
+
test.each do |f|
|
736
|
+
FileUtils.touch(f)
|
737
|
+
end
|
738
|
+
|
739
|
+
yielded = []
|
740
|
+
Dir.foreach('/this/path/should/be/here') do |dir|
|
741
|
+
yielded << dir
|
742
|
+
end
|
743
|
+
|
744
|
+
assert yielded.size == test.size
|
745
|
+
test.each { |t| assert yielded.include?(t) }
|
746
|
+
end
|
747
|
+
|
748
|
+
def test_directory_mkdir
|
749
|
+
Dir.mkdir('/path')
|
750
|
+
assert File.exists?('/path')
|
751
|
+
end
|
752
|
+
|
753
|
+
def test_directory_mkdir_relative
|
754
|
+
FileUtils.mkdir_p('/new/root')
|
755
|
+
FileSystem.chdir('/new/root')
|
756
|
+
Dir.mkdir('path')
|
757
|
+
assert File.exists?('/new/root/path')
|
758
|
+
end
|
759
|
+
|
760
|
+
def test_directory_mkdir_not_recursive
|
761
|
+
assert_raises(Errno::ENOENT) do
|
762
|
+
Dir.mkdir('/path/does/not/exist')
|
763
|
+
end
|
764
|
+
end
|
765
|
+
|
766
|
+
def test_directory_open
|
767
|
+
test = ['.', '..', '/this/path/should/be/here/file_1', '/this/path/should/be/here/file_2', '/this/path/should/be/here/file_3', '/this/path/should/be/here/file_4', '/this/path/should/be/here/file_5' ]
|
768
|
+
|
769
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
770
|
+
|
771
|
+
test.each do |f|
|
772
|
+
FileUtils.touch(f)
|
773
|
+
end
|
774
|
+
|
775
|
+
dir = Dir.open('/this/path/should/be/here')
|
776
|
+
assert dir.path == '/this/path/should/be/here'
|
777
|
+
end
|
778
|
+
|
779
|
+
def test_directory_open_block
|
780
|
+
test = ['.', '..', '/this/path/should/be/here/file_1', '/this/path/should/be/here/file_2', '/this/path/should/be/here/file_3', '/this/path/should/be/here/file_4', '/this/path/should/be/here/file_5' ]
|
781
|
+
|
782
|
+
FileUtils.mkdir_p('/this/path/should/be/here')
|
783
|
+
|
784
|
+
test.each do |f|
|
785
|
+
FileUtils.touch(f)
|
786
|
+
end
|
787
|
+
|
788
|
+
yielded = []
|
789
|
+
Dir.open('/this/path/should/be/here') do |dir|
|
790
|
+
yielded << dir
|
791
|
+
end
|
792
|
+
|
793
|
+
assert yielded.size == test.size
|
794
|
+
test.each { |t| assert yielded.include?(t) }
|
795
|
+
end
|
796
|
+
|
797
|
+
def test_tmpdir
|
798
|
+
assert Dir.tmp_dir == "/tmp"
|
799
|
+
end
|
800
|
+
|
801
|
+
def here(fname)
|
802
|
+
RealFile.expand_path(RealFile.dirname(__FILE__)+'/'+fname)
|
803
|
+
end
|
804
|
+
end
|