ktheory-fakefs 0.2.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,39 @@
1
+ require "test_helper"
2
+
3
+ class FileStat < Test::Unit::TestCase
4
+ def setup
5
+ FakeFS.activate!
6
+ FakeFS::FileSystem.clear
7
+ end
8
+
9
+ def teardown
10
+ FakeFS.deactivate!
11
+ end
12
+
13
+ def test_calling_stat_should_create_a_new_file_stat_object
14
+ File.open("foo", "w") do |f|
15
+ f << "bar"
16
+ end
17
+
18
+ File.open("foo") do |f|
19
+ assert_equal File::Stat, f.stat.class
20
+ end
21
+ end
22
+
23
+ def test_stat_should_use_correct_file
24
+ File.open("bar", "w") do |f|
25
+ f << "1"
26
+ end
27
+
28
+ File.open("bar") do |f|
29
+ assert_equal 1, f.stat.size
30
+ end
31
+ end
32
+
33
+ def test_stat_should_report_on_symlink_pointer
34
+ File.open("foo", "w") { |f| f << "some content" }
35
+ File.symlink "foo", "my_symlink"
36
+
37
+ assert_equal File.stat("my_symlink").size, File.stat("foo").size
38
+ end
39
+ end
@@ -0,0 +1,44 @@
1
+ require "test_helper"
2
+
3
+ class FileSysSeek < Test::Unit::TestCase
4
+ def setup
5
+ FakeFS.activate!
6
+ FakeFS::FileSystem.clear
7
+ end
8
+
9
+ def teardown
10
+ FakeFS.deactivate!
11
+ end
12
+
13
+ def test_should_seek_to_position
14
+ file = File.open("foo", "w") do |f|
15
+ f << "0123456789"
16
+ end
17
+
18
+ File.open("foo", "r") do |f|
19
+ f.sysseek(3)
20
+ assert_equal 3, f.pos
21
+
22
+ f.sysseek(0)
23
+ assert_equal 0, f.pos
24
+ end
25
+ end
26
+
27
+ def test_seek_returns_offset_into_file
28
+ File.open("foo", "w") do |f|
29
+ # 66 chars long
30
+ str = "0123456789" +
31
+ "0123456789" +
32
+ "0123456789" +
33
+ "0123456789" +
34
+ "0123456789" +
35
+ "0123456789" +
36
+ "012345"
37
+
38
+ f << str
39
+ end
40
+
41
+ f = File.open("foo")
42
+ assert_equal 53, f.sysseek(-13, IO::SEEK_END)
43
+ end
44
+ end
@@ -0,0 +1,62 @@
1
+ require "test_helper"
2
+
3
+ class FileSysWriteTest < Test::Unit::TestCase
4
+ def setup
5
+ FakeFS.activate!
6
+ FakeFS::FileSystem.clear
7
+ end
8
+
9
+ def teardown
10
+ FakeFS.deactivate!
11
+ end
12
+
13
+ def test_returns_one_byte_when_written
14
+ f = File.open "foo", "w"
15
+ result = f.syswrite "a"
16
+ assert_equal 1, result
17
+ end
18
+
19
+ def test_returns_two_bytes_when_two_written
20
+ f = File.open "foo", "w"
21
+ result = f.syswrite "ab"
22
+ assert_equal 2, result
23
+ end
24
+
25
+ def test_syswrite_writes_file
26
+ f = File.open "foo", "w"
27
+ f.syswrite "abcdef"
28
+ f.close
29
+
30
+ assert_equal "abcdef", File.read("foo")
31
+ end
32
+
33
+ def test_writes_to_the_actual_position_when_called_after_buffered_io_read
34
+ File.open("foo", "w") do |file|
35
+ file.syswrite("012345678901234567890123456789")
36
+ end
37
+
38
+ file = File.open("foo", "r+")
39
+ file.read(5)
40
+ file.syswrite("abcde")
41
+
42
+ File.open("foo") do |file|
43
+ assert_equal "01234abcde", file.sysread(10)
44
+ end
45
+ end
46
+
47
+ def test_writes_all_of_the_strings_bytes_but_does_not_buffer_them
48
+ File.open("foo", "w") do |file|
49
+ file.syswrite("012345678901234567890123456789")
50
+ end
51
+
52
+ file = File.open("foo", "r+")
53
+ written = file.syswrite("abcde")
54
+
55
+ File.open("foo") do |file|
56
+ assert_equal "abcde56789", file.sysread(10)
57
+ file.seek(0)
58
+ file.fsync
59
+ assert_equal "abcde56789", file.sysread(10)
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,86 @@
1
+ require "test_helper"
2
+
3
+ class FakeFileTest < Test::Unit::TestCase
4
+ include FakeFS
5
+
6
+ def setup
7
+ FileSystem.clear
8
+
9
+ @file = FakeFile.new
10
+ end
11
+
12
+ def test_fake_file_has_empty_content_by_default
13
+ assert_equal "", @file.content
14
+ end
15
+
16
+ def test_fake_file_can_read_and_write_to_content
17
+ @file.content = "foobar"
18
+ assert_equal "foobar", @file.content
19
+ end
20
+
21
+ def test_fake_file_has_1_link_by_default
22
+ assert_equal [@file], @file.links
23
+ end
24
+
25
+ def test_fake_file_can_create_link
26
+ other_file = FakeFile.new
27
+
28
+ @file.link(other_file)
29
+
30
+ assert_equal [@file, other_file], @file.links
31
+ end
32
+
33
+ def test_fake_file_wont_add_link_to_same_file_twice
34
+ other_file = FakeFile.new
35
+
36
+ @file.link other_file
37
+ @file.link other_file
38
+
39
+ assert_equal [@file, other_file], @file.links
40
+ end
41
+
42
+ def test_links_are_mutual
43
+ other_file = FakeFile.new
44
+
45
+ @file.link(other_file)
46
+
47
+ assert_equal [@file, other_file], other_file.links
48
+ end
49
+
50
+ def test_can_link_multiple_files
51
+ file_two = FakeFile.new
52
+ file_three = FakeFile.new
53
+
54
+ @file.link file_two
55
+ @file.link file_three
56
+
57
+ assert_equal [@file, file_two, file_three], @file.links
58
+ assert_equal [@file, file_two, file_three], file_two.links
59
+ assert_equal [@file, file_two, file_three], file_three.links
60
+ end
61
+
62
+ def test_links_share_same_content
63
+ other_file = FakeFile.new
64
+
65
+ @file.link other_file
66
+
67
+ @file.content = "foobar"
68
+
69
+ assert_equal "foobar", other_file.content
70
+ end
71
+
72
+ def test_clone_creates_new_inode
73
+ clone = @file.clone
74
+ assert !clone.inode.equal?(@file.inode)
75
+ end
76
+
77
+ def test_cloning_does_not_use_same_content_object
78
+ clone = @file.clone
79
+
80
+ clone.content = "foo"
81
+ @file.content = "bar"
82
+
83
+ assert_equal "foo", clone.content
84
+ assert_equal "bar", @file.content
85
+ end
86
+ end
@@ -0,0 +1,10 @@
1
+ require "test_helper"
2
+
3
+ class FakeSymlinkTest < Test::Unit::TestCase
4
+ include FakeFS
5
+
6
+ def test_symlink_has_method_missing_as_private
7
+ methods = FakeSymlink.private_instance_methods.map { |m| m.to_s }
8
+ assert methods.include?("method_missing")
9
+ end
10
+ end
@@ -0,0 +1,1466 @@
1
+ require "test_helper"
2
+
3
+ class FakeFSTest < Test::Unit::TestCase
4
+ include FakeFS
5
+
6
+ def setup
7
+ FakeFS.activate!
8
+ FileSystem.clear
9
+ end
10
+
11
+ def teardown
12
+ FakeFS.deactivate!
13
+ end
14
+
15
+ def test_can_be_initialized_empty
16
+ fs = FileSystem
17
+ assert_equal 0, fs.files.size
18
+ end
19
+
20
+ def xtest_can_be_initialized_with_an_existing_directory
21
+ fs = FileSystem
22
+ fs.clone(File.expand_path(File.dirname(__FILE__))).inspect
23
+ assert_equal 1, fs.files.size
24
+ end
25
+
26
+ def test_can_create_directories
27
+ FileUtils.mkdir_p("/path/to/dir")
28
+ assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']
29
+ end
30
+
31
+ def test_can_create_directories_with_mkpath
32
+ FileUtils.mkpath("/path/to/dir")
33
+ assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']
34
+ end
35
+
36
+ def test_can_delete_directories
37
+ FileUtils.mkdir_p("/path/to/dir")
38
+ FileUtils.rmdir("/path/to/dir")
39
+ assert File.exists?("/path/to/")
40
+ assert File.exists?("/path/to/dir") == false
41
+ end
42
+
43
+ def test_can_delete_multiple_files
44
+ FileUtils.touch(["foo", "bar"])
45
+ FileUtils.rm(["foo", "bar"])
46
+ assert File.exists?("foo") == false
47
+ assert File.exists?("bar") == false
48
+ end
49
+
50
+ def test_knows_directories_exist
51
+ FileUtils.mkdir_p(path = "/path/to/dir")
52
+ assert File.exists?(path)
53
+ end
54
+
55
+ def test_knows_directories_are_directories
56
+ FileUtils.mkdir_p(path = "/path/to/dir")
57
+ assert File.directory?(path)
58
+ end
59
+
60
+ def test_knows_symlink_directories_are_directories
61
+ FileUtils.mkdir_p(path = "/path/to/dir")
62
+ FileUtils.ln_s path, sympath = '/sympath'
63
+ assert File.directory?(sympath)
64
+ end
65
+
66
+ def test_knows_non_existent_directories_arent_directories
67
+ path = 'does/not/exist/'
68
+ assert_equal RealFile.directory?(path), File.directory?(path)
69
+ end
70
+
71
+ def test_doesnt_overwrite_existing_directories
72
+ FileUtils.mkdir_p(path = "/path/to/dir")
73
+ assert File.exists?(path)
74
+ FileUtils.mkdir_p("/path/to")
75
+ assert File.exists?(path)
76
+ end
77
+
78
+ def test_can_create_symlinks
79
+ FileUtils.mkdir_p(target = "/path/to/target")
80
+ FileUtils.ln_s(target, "/path/to/link")
81
+ assert_kind_of FakeSymlink, FileSystem.fs['path']['to']['link']
82
+
83
+ assert_raises(Errno::EEXIST) do
84
+ FileUtils.ln_s(target, '/path/to/link')
85
+ end
86
+ end
87
+
88
+ def test_can_force_creation_of_symlinks
89
+ FileUtils.mkdir_p(target = "/path/to/first/target")
90
+ FileUtils.ln_s(target, "/path/to/link")
91
+ assert_kind_of FakeSymlink, FileSystem.fs['path']['to']['link']
92
+ FileUtils.ln_s(target, '/path/to/link', :force => true)
93
+ end
94
+
95
+ def test_create_symlink_using_ln_sf
96
+ FileUtils.mkdir_p(target = "/path/to/first/target")
97
+ FileUtils.ln_s(target, "/path/to/link")
98
+ assert_kind_of FakeSymlink, FileSystem.fs['path']['to']['link']
99
+ FileUtils.ln_sf(target, '/path/to/link')
100
+ end
101
+
102
+ def test_can_follow_symlinks
103
+ FileUtils.mkdir_p(target = "/path/to/target")
104
+ FileUtils.ln_s(target, link = "/path/to/symlink")
105
+ assert_equal target, File.readlink(link)
106
+ end
107
+
108
+ def test_knows_symlinks_are_symlinks
109
+ FileUtils.mkdir_p(target = "/path/to/target")
110
+ FileUtils.ln_s(target, link = "/path/to/symlink")
111
+ assert File.symlink?(link)
112
+ end
113
+
114
+ def test_can_create_files
115
+ path = '/path/to/file.txt'
116
+ File.open(path, 'w') do |f|
117
+ f.write "Yatta!"
118
+ end
119
+
120
+ assert File.exists?(path)
121
+ assert File.readable?(path)
122
+ assert File.writable?(path)
123
+ end
124
+
125
+ def test_can_create_files_with_bitmasks
126
+ path = '/path/to/file.txt'
127
+ File.open(path, File::RDWR | File::CREAT) do |f|
128
+ f.write "Yatta!"
129
+ end
130
+
131
+ assert File.exists?(path)
132
+ assert File.readable?(path)
133
+ assert File.writable?(path)
134
+ end
135
+
136
+ def test_file_opens_in_read_only_mode
137
+ File.open("foo", "w") { |f| f << "foo" }
138
+
139
+ f = File.open("foo")
140
+
141
+ assert_raises(IOError) do
142
+ f << "bar"
143
+ end
144
+ end
145
+
146
+ def test_file_opens_in_read_only_mode_with_bitmasks
147
+ File.open("foo", "w") { |f| f << "foo" }
148
+
149
+ f = File.open("foo", File::RDONLY)
150
+
151
+ assert_raises(IOError) do
152
+ f << "bar"
153
+ end
154
+ end
155
+
156
+ def test_file_opens_in_invalid_mode
157
+ FileUtils.touch("foo")
158
+
159
+ assert_raises(ArgumentError) do
160
+ File.open("foo", "an_illegal_mode")
161
+ end
162
+ end
163
+
164
+ def test_raises_error_when_cannot_find_file_in_read_mode
165
+ assert_raises(Errno::ENOENT) do
166
+ File.open("does_not_exist", "r")
167
+ end
168
+ end
169
+
170
+ def test_raises_error_when_cannot_find_file_in_read_write_mode
171
+ assert_raises(Errno::ENOENT) do
172
+ File.open("does_not_exist", "r+")
173
+ end
174
+ end
175
+
176
+ def test_creates_files_in_write_only_mode
177
+ File.open("foo", "w")
178
+ assert File.exists?("foo")
179
+ end
180
+
181
+ def test_creates_files_in_write_only_mode_with_bitmasks
182
+ File.open("foo", File::WRONLY | File::CREAT)
183
+ assert File.exists?("foo")
184
+ end
185
+
186
+ def test_raises_in_write_only_mode_without_create_bitmask
187
+ assert_raises(Errno::ENOENT) do
188
+ File.open("foo", File::WRONLY)
189
+ end
190
+ end
191
+
192
+ def test_creates_files_in_read_write_truncate_mode
193
+ File.open("foo", "w+")
194
+ assert File.exists?("foo")
195
+ end
196
+
197
+ def test_creates_files_in_append_write_only
198
+ File.open("foo", "a")
199
+ assert File.exists?("foo")
200
+ end
201
+
202
+ def test_creates_files_in_append_read_write
203
+ File.open("foo", "a+")
204
+ assert File.exists?("foo")
205
+ end
206
+
207
+ def test_file_in_write_only_raises_error_when_reading
208
+ FileUtils.touch("foo")
209
+
210
+ f = File.open("foo", "w")
211
+
212
+ assert_raises(IOError) do
213
+ f.read
214
+ end
215
+ end
216
+
217
+ def test_file_in_write_mode_truncates_existing_file
218
+ File.open("foo", "w") { |f| f << "contents" }
219
+
220
+ f = File.open("foo", "w")
221
+
222
+ assert_equal "", File.read("foo")
223
+ end
224
+
225
+ def test_file_in_read_write_truncation_mode_truncates_file
226
+ File.open("foo", "w") { |f| f << "foo" }
227
+
228
+ f = File.open("foo", "w+")
229
+
230
+ assert_equal "", File.read("foo")
231
+ end
232
+
233
+ def test_file_in_append_write_only_raises_error_when_reading
234
+ FileUtils.touch("foo")
235
+
236
+ f = File.open("foo", "a")
237
+
238
+ assert_raises(IOError) do
239
+ f.read
240
+ end
241
+ end
242
+
243
+ def test_can_read_files_once_written
244
+ path = '/path/to/file.txt'
245
+ File.open(path, 'w') do |f|
246
+ f.write "Yatta!"
247
+ end
248
+
249
+ assert_equal "Yatta!", File.read(path)
250
+ end
251
+
252
+ def test_can_write_to_files
253
+ path = '/path/to/file.txt'
254
+ File.open(path, 'w') do |f|
255
+ f << 'Yada Yada'
256
+ end
257
+ assert_equal 'Yada Yada', File.read(path)
258
+ end
259
+
260
+ def test_raises_error_when_opening_with_binary_mode_only
261
+ assert_raise ArgumentError do
262
+ File.open("/foo", "b")
263
+ end
264
+ end
265
+
266
+ def test_can_open_file_in_binary_mode
267
+ File.open("/foo", "wb") { |x| x << "a" }
268
+ assert_equal "a", File.read("/foo")
269
+ end
270
+
271
+ def test_can_chunk_io_when_reading
272
+ path = '/path/to/file.txt'
273
+ File.open(path, 'w') do |f|
274
+ f << 'Yada Yada'
275
+ end
276
+ file = File.new(path, 'r')
277
+ assert_equal 'Yada', file.read(4)
278
+ assert_equal ' Yada', file.read(5)
279
+ assert_equal '', file.read
280
+ file.rewind
281
+ assert_equal 'Yada Yada', file.read
282
+ end
283
+
284
+ def test_can_get_size_of_files
285
+ path = '/path/to/file.txt'
286
+ File.open(path, 'w') do |f|
287
+ f << 'Yada Yada'
288
+ end
289
+ assert_equal 9, File.size(path)
290
+ end
291
+
292
+ def test_can_check_if_file_has_size?
293
+ path = '/path/to/file.txt'
294
+ File.open(path, 'w') do |f|
295
+ f << 'Yada Yada'
296
+ end
297
+ assert File.size?(path)
298
+ assert_nil File.size?("/path/to/other.txt")
299
+ end
300
+
301
+ def test_can_check_size_of_empty_file
302
+ path = '/path/to/file.txt'
303
+ File.open(path, 'w') do |f|
304
+ f << ''
305
+ end
306
+ assert_nil File.size?("/path/to/file.txt")
307
+ end
308
+
309
+ def test_raises_error_on_mtime_if_file_does_not_exist
310
+ assert_raise Errno::ENOENT do
311
+ File.mtime('/path/to/file.txt')
312
+ end
313
+ end
314
+
315
+ def test_can_return_mtime_on_existing_file
316
+ path = '/path/to/file.txt'
317
+ File.open(path, 'w') do |f|
318
+ f << ''
319
+ end
320
+ assert File.mtime('/path/to/file.txt').is_a?(Time)
321
+ end
322
+
323
+ def test_utime_raises_error_if_path_does_not_exist
324
+ assert_raise Errno::ENOENT do
325
+ File.utime(Time.now, Time.now, '/path/to/file.txt')
326
+ end
327
+ end
328
+
329
+ def test_can_call_utime_on_an_existing_file
330
+ time = Time.now - 300 # Not now
331
+ path = '/path/to/file.txt'
332
+ File.open(path, 'w') do |f|
333
+ f << ''
334
+ end
335
+ File.utime(time, time, path)
336
+ assert_equal time, File.mtime('/path/to/file.txt')
337
+ end
338
+
339
+ def test_utime_returns_number_of_paths
340
+ path1, path2 = '/path/to/file.txt', '/path/to/another_file.txt'
341
+ [path1, path2].each do |path|
342
+ File.open(path, 'w') do |f|
343
+ f << ''
344
+ end
345
+ end
346
+ assert_equal 2, File.utime(Time.now, Time.now, path1, path2)
347
+ end
348
+
349
+ def test_can_read_with_File_readlines
350
+ path = '/path/to/file.txt'
351
+ File.open(path, 'w') do |f|
352
+ f.puts "Yatta!", "Gatta!"
353
+ f.puts ["woot","toot"]
354
+ end
355
+
356
+ assert_equal %w(Yatta! Gatta! woot toot), File.readlines(path)
357
+ end
358
+
359
+ def test_File_close_disallows_further_access
360
+ path = '/path/to/file.txt'
361
+ file = File.open(path, 'w')
362
+ file.write 'Yada'
363
+ file.close
364
+ assert_raise IOError do
365
+ file.read
366
+ end
367
+ end
368
+
369
+ def test_File_close_disallows_further_writes
370
+ path = '/path/to/file.txt'
371
+ file = File.open(path, 'w')
372
+ file.write 'Yada'
373
+ file.close
374
+ assert_raise IOError do
375
+ file << "foo"
376
+ end
377
+ end
378
+
379
+ def test_can_read_from_file_objects
380
+ path = '/path/to/file.txt'
381
+ File.open(path, 'w') do |f|
382
+ f.write "Yatta!"
383
+ end
384
+
385
+ assert_equal "Yatta!", File.new(path).read
386
+ end
387
+
388
+ def test_file_read_errors_appropriately
389
+ assert_raise Errno::ENOENT do
390
+ File.read('anything')
391
+ end
392
+ end
393
+
394
+ def test_knows_files_are_files
395
+ path = '/path/to/file.txt'
396
+ File.open(path, 'w') do |f|
397
+ f.write "Yatta!"
398
+ end
399
+
400
+ assert File.file?(path)
401
+ end
402
+
403
+ def test_File_io_returns_self
404
+ f = File.open("/foo", "w")
405
+ assert_equal f, f.to_io
406
+ end
407
+
408
+ def test_File_to_i_is_alias_for_filno
409
+ f = File.open("/foo", "w")
410
+ assert_equal f.method(:to_i), f.method(:fileno)
411
+ end
412
+
413
+ def test_knows_symlink_files_are_files
414
+ path = '/path/to/file.txt'
415
+ File.open(path, 'w') do |f|
416
+ f.write "Yatta!"
417
+ end
418
+ FileUtils.ln_s path, sympath='/sympath'
419
+
420
+ assert File.file?(sympath)
421
+ end
422
+
423
+ def test_knows_non_existent_files_arent_files
424
+ assert_equal RealFile.file?('does/not/exist.txt'), File.file?('does/not/exist.txt')
425
+ end
426
+
427
+ def test_can_chown_files
428
+ good = 'file.txt'
429
+ bad = 'nofile.txt'
430
+ File.open(good,'w') { |f| f.write "foo" }
431
+
432
+ out = FileUtils.chown('noone', 'nogroup', good, :verbose => true)
433
+ assert_equal [good], out
434
+ assert_raises(Errno::ENOENT) do
435
+ FileUtils.chown('noone', 'nogroup', bad, :verbose => true)
436
+ end
437
+
438
+ assert_equal [good], FileUtils.chown('noone', 'nogroup', good)
439
+ assert_raises(Errno::ENOENT) do
440
+ FileUtils.chown('noone', 'nogroup', bad)
441
+ end
442
+
443
+ assert_equal [good], FileUtils.chown('noone', 'nogroup', [good])
444
+ assert_raises(Errno::ENOENT) do
445
+ FileUtils.chown('noone', 'nogroup', [good, bad])
446
+ end
447
+ end
448
+
449
+ def test_can_chown_R_files
450
+ FileUtils.mkdir_p '/path/'
451
+ File.open('/path/foo', 'w') { |f| f.write 'foo' }
452
+ File.open('/path/foobar', 'w') { |f| f.write 'foo' }
453
+ resp = FileUtils.chown_R('no', 'no', '/path')
454
+ assert_equal ['/path'], resp
455
+ end
456
+
457
+ def test_dir_globs_paths
458
+ FileUtils.mkdir_p '/path'
459
+ File.open('/path/foo', 'w') { |f| f.write 'foo' }
460
+ File.open('/path/foobar', 'w') { |f| f.write 'foo' }
461
+
462
+ FileUtils.mkdir_p '/path/bar'
463
+ File.open('/path/bar/baz', 'w') { |f| f.write 'foo' }
464
+
465
+ FileUtils.cp_r '/path/bar', '/path/bar2'
466
+
467
+ assert_equal ['/path'], Dir['/path']
468
+ assert_equal %w( /path/bar /path/bar2 /path/foo /path/foobar ), Dir['/path/*']
469
+
470
+ assert_equal ['/path/bar/baz'], Dir['/path/bar/*']
471
+ assert_equal ['/path/foo'], Dir['/path/foo']
472
+
473
+ assert_equal ['/path'], Dir['/path*']
474
+ assert_equal ['/path/foo', '/path/foobar'], Dir['/p*h/foo*']
475
+ assert_equal ['/path/foo', '/path/foobar'], Dir['/p??h/foo*']
476
+
477
+ FileUtils.cp_r '/path', '/otherpath'
478
+
479
+ assert_equal %w( /otherpath/foo /otherpath/foobar /path/foo /path/foobar ), Dir['/*/foo*']
480
+ end
481
+
482
+ def test_dir_glob_handles_root
483
+ FileUtils.mkdir_p '/path'
484
+
485
+ # this fails. the root dir should be named '/' but it is '.'
486
+ #assert_equal ['/'], Dir['/']
487
+ end
488
+
489
+ def test_dir_glob_handles_recursive_globs
490
+ File.open('/one/two/three/four.rb', 'w')
491
+ File.open('/one/five.rb', 'w')
492
+ assert_equal ['/one/five.rb', '/one/two/three/four.rb'], Dir['/one/**/*.rb']
493
+ assert_equal ['/one/two'], Dir['/one/**/two']
494
+ assert_equal ['/one/two/three'], Dir['/one/**/three']
495
+ end
496
+
497
+ def test_dir_recursive_glob_ending_in_wildcards_only_returns_files
498
+ File.open('/one/two/three/four.rb', 'w')
499
+ File.open('/one/five.rb', 'w')
500
+ assert_equal ['/one/five.rb', '/one/two/three/four.rb'], Dir['/one/**/*']
501
+ assert_equal ['/one/five.rb', '/one/two/three/four.rb'], Dir['/one/**']
502
+ end
503
+
504
+ def test_should_report_pos_as_0_when_opening
505
+ File.open("/foo", "w") do |f|
506
+ f << "foobar"
507
+ f.rewind
508
+
509
+ assert_equal 0, f.pos
510
+ end
511
+ end
512
+
513
+ def test_should_report_pos_as_1_when_seeking_one_char
514
+ File.open("/foo", "w") do |f|
515
+ f << "foobar"
516
+
517
+ f.rewind
518
+ f.seek(1)
519
+
520
+ assert_equal 1, f.pos
521
+ end
522
+ end
523
+
524
+ def test_should_set_pos
525
+ File.open("/foo", "w") do |f|
526
+ f << "foo"
527
+ end
528
+
529
+ fp = File.open("/foo", "r")
530
+ fp.pos = 1
531
+
532
+ assert_equal 1, fp.pos
533
+ end
534
+
535
+ def test_should_set_pos_with_tell_method
536
+ File.open("/foo", "w") do |f|
537
+ f << "foo"
538
+ end
539
+
540
+ fp = File.open("/foo", "r")
541
+ fp.tell = 1
542
+
543
+ assert_equal 1, fp.pos
544
+ end
545
+
546
+ # Every method in File is in FakeFS::File
547
+ RealFile.instance_methods.each do |method_name|
548
+ define_method "test_should_have_method_#{method_name}_from_real_file_class" do
549
+ assert File.instance_methods.include?(method_name)
550
+ end
551
+ end
552
+
553
+ # No methods which are in StringIO that aren't in File are included into
554
+ # FakeFS::File (because of inheritence)
555
+ uniq_string_io_methods = StringIO.instance_methods - RealFile.instance_methods
556
+ uniq_string_io_methods.each do |method_name|
557
+ define_method "test_file_should_not_respond_to_#{method_name}" do
558
+ assert !File.instance_methods.include?(method_name)
559
+ end
560
+ end
561
+
562
+ def test_does_not_remove_methods_from_stringio
563
+ stringio = StringIO.new("foo")
564
+ assert stringio.respond_to?(:size)
565
+ end
566
+
567
+ def test_chdir_changes_directories_like_a_boss
568
+ # I know memes!
569
+ FileUtils.mkdir_p '/path'
570
+ assert_equal '.', FileSystem.fs.name
571
+ assert_equal({}, FileSystem.fs['path'])
572
+ Dir.chdir '/path' do
573
+ File.open('foo', 'w') { |f| f.write 'foo'}
574
+ File.open('foobar', 'w') { |f| f.write 'foo'}
575
+ end
576
+
577
+ assert_equal '.', FileSystem.fs.name
578
+ assert_equal(['foo', 'foobar'], FileSystem.fs['path'].keys.sort)
579
+
580
+ c = nil
581
+ Dir.chdir '/path' do
582
+ c = File.open('foo', 'r') { |f| f.read }
583
+ end
584
+
585
+ assert_equal 'foo', c
586
+ end
587
+
588
+ def test_chdir_shouldnt_keep_us_from_absolute_paths
589
+ FileUtils.mkdir_p '/path'
590
+
591
+ Dir.chdir '/path' do
592
+ File.open('foo', 'w') { |f| f.write 'foo'}
593
+ File.open('/foobar', 'w') { |f| f.write 'foo'}
594
+ end
595
+ assert_equal ['foo'], FileSystem.fs['path'].keys.sort
596
+ assert_equal ['foobar', 'path'], FileSystem.fs.keys.sort
597
+
598
+ Dir.chdir '/path' do
599
+ FileUtils.rm('foo')
600
+ FileUtils.rm('/foobar')
601
+ end
602
+
603
+ assert_equal [], FileSystem.fs['path'].keys.sort
604
+ assert_equal ['path'], FileSystem.fs.keys.sort
605
+ end
606
+
607
+ def test_chdir_should_be_nestable
608
+ FileUtils.mkdir_p '/path/me'
609
+ Dir.chdir '/path' do
610
+ File.open('foo', 'w') { |f| f.write 'foo'}
611
+ Dir.chdir 'me' do
612
+ File.open('foobar', 'w') { |f| f.write 'foo'}
613
+ end
614
+ end
615
+
616
+ assert_equal ['foo','me'], FileSystem.fs['path'].keys.sort
617
+ assert_equal ['foobar'], FileSystem.fs['path']['me'].keys.sort
618
+ end
619
+
620
+ def test_chdir_should_flop_over_and_die_if_the_dir_doesnt_exist
621
+ assert_raise(Errno::ENOENT) do
622
+ Dir.chdir('/nope') do
623
+ 1
624
+ end
625
+ end
626
+ end
627
+
628
+ def test_chdir_shouldnt_lose_state_because_of_errors
629
+ FileUtils.mkdir_p '/path'
630
+
631
+ Dir.chdir '/path' do
632
+ File.open('foo', 'w') { |f| f.write 'foo'}
633
+ File.open('foobar', 'w') { |f| f.write 'foo'}
634
+ end
635
+
636
+ begin
637
+ Dir.chdir('/path') do
638
+ raise Exception
639
+ end
640
+ rescue Exception # hardcore
641
+ end
642
+
643
+ Dir.chdir('/path') do
644
+ begin
645
+ Dir.chdir('nope'){ }
646
+ rescue Errno::ENOENT
647
+ end
648
+
649
+ assert_equal ['/path'], FileSystem.dir_levels
650
+ end
651
+
652
+ assert_equal(['foo', 'foobar'], FileSystem.fs['path'].keys.sort)
653
+ end
654
+
655
+ def test_chdir_with_no_block_is_awesome
656
+ FileUtils.mkdir_p '/path'
657
+ Dir.chdir('/path')
658
+ FileUtils.mkdir_p 'subdir'
659
+ assert_equal ['subdir'], FileSystem.current_dir.keys
660
+ Dir.chdir('subdir')
661
+ File.open('foo', 'w') { |f| f.write 'foo'}
662
+ assert_equal ['foo'], FileSystem.current_dir.keys
663
+
664
+ assert_raises(Errno::ENOENT) do
665
+ Dir.chdir('subsubdir')
666
+ end
667
+
668
+ assert_equal ['foo'], FileSystem.current_dir.keys
669
+ end
670
+
671
+ def test_current_dir_reflected_by_pwd
672
+ FileUtils.mkdir_p '/path'
673
+ Dir.chdir('/path')
674
+
675
+ assert_equal '/path', Dir.pwd
676
+ assert_equal '/path', Dir.getwd
677
+
678
+ FileUtils.mkdir_p 'subdir'
679
+ Dir.chdir('subdir')
680
+
681
+ assert_equal '/path/subdir', Dir.pwd
682
+ assert_equal '/path/subdir', Dir.getwd
683
+ end
684
+
685
+ def test_file_open_defaults_to_read
686
+ File.open('foo','w') { |f| f.write 'bar' }
687
+ assert_equal 'bar', File.open('foo') { |f| f.read }
688
+ end
689
+
690
+ def test_flush_exists_on_file
691
+ r = File.open('foo','w') { |f| f.write 'bar'; f.flush }
692
+ assert_equal 'foo', r.path
693
+ end
694
+
695
+ def test_mv_should_raise_error_on_missing_file
696
+ assert_raise(Errno::ENOENT) do
697
+ FileUtils.mv 'blafgag', 'foo'
698
+ end
699
+ end
700
+
701
+ def test_mv_actually_works
702
+ File.open('foo', 'w') { |f| f.write 'bar' }
703
+ FileUtils.mv 'foo', 'baz'
704
+ assert_equal 'bar', File.open('baz') { |f| f.read }
705
+ end
706
+
707
+ def test_cp_actually_works
708
+ File.open('foo', 'w') {|f| f.write 'bar' }
709
+ FileUtils.cp('foo', 'baz')
710
+ assert_equal 'bar', File.read('baz')
711
+ end
712
+
713
+ def test_cp_file_into_dir
714
+ File.open('foo', 'w') {|f| f.write 'bar' }
715
+ FileUtils.mkdir_p 'baz'
716
+
717
+ FileUtils.cp('foo', 'baz')
718
+ assert_equal 'bar', File.read('baz/foo')
719
+ end
720
+
721
+ def test_cp_overwrites_dest_file
722
+ File.open('foo', 'w') {|f| f.write 'FOO' }
723
+ File.open('bar', 'w') {|f| f.write 'BAR' }
724
+
725
+ FileUtils.cp('foo', 'bar')
726
+ assert_equal 'FOO', File.read('bar')
727
+ end
728
+
729
+ def test_cp_fails_on_no_source
730
+ assert_raise Errno::ENOENT do
731
+ FileUtils.cp('foo', 'baz')
732
+ end
733
+ end
734
+
735
+ def test_cp_fails_on_directory_copy
736
+ FileUtils.mkdir_p 'baz'
737
+
738
+ assert_raise Errno::EISDIR do
739
+ FileUtils.cp('baz', 'bar')
740
+ end
741
+ end
742
+
743
+ def test_cp_r_doesnt_tangle_files_together
744
+ File.open('foo', 'w') { |f| f.write 'bar' }
745
+ FileUtils.cp_r('foo', 'baz')
746
+ File.open('baz', 'w') { |f| f.write 'quux' }
747
+ assert_equal 'bar', File.open('foo') { |f| f.read }
748
+ end
749
+
750
+ def test_cp_r_should_raise_error_on_missing_file
751
+ # Yes, this error sucks, but it conforms to the original Ruby
752
+ # method.
753
+ assert_raise(RuntimeError) do
754
+ FileUtils.cp_r 'blafgag', 'foo'
755
+ end
756
+ end
757
+
758
+ def test_cp_r_handles_copying_directories
759
+ FileUtils.mkdir_p 'subdir'
760
+ Dir.chdir('subdir'){ File.open('foo', 'w') { |f| f.write 'footext' } }
761
+
762
+ FileUtils.mkdir_p 'baz'
763
+
764
+ # To a previously uncreated directory
765
+ FileUtils.cp_r('subdir', 'quux')
766
+ assert_equal 'footext', File.open('quux/foo') { |f| f.read }
767
+
768
+ # To a directory that already exists
769
+ FileUtils.cp_r('subdir', 'baz')
770
+ assert_equal 'footext', File.open('baz/subdir/foo') { |f| f.read }
771
+
772
+ # To a subdirectory of a directory that does not exist
773
+ assert_raises(Errno::ENOENT) do
774
+ FileUtils.cp_r('subdir', 'nope/something')
775
+ end
776
+ end
777
+
778
+ def test_cp_r_only_copies_into_directories
779
+ FileUtils.mkdir_p 'subdir'
780
+ Dir.chdir('subdir') { File.open('foo', 'w') { |f| f.write 'footext' } }
781
+
782
+ File.open('bar', 'w') { |f| f.write 'bartext' }
783
+
784
+ assert_raises(Errno::EEXIST) do
785
+ FileUtils.cp_r 'subdir', 'bar'
786
+ end
787
+
788
+ FileUtils.mkdir_p 'otherdir'
789
+ FileUtils.ln_s 'otherdir', 'symdir'
790
+
791
+ FileUtils.cp_r 'subdir', 'symdir'
792
+ assert_equal 'footext', File.open('symdir/subdir/foo') { |f| f.read }
793
+ end
794
+
795
+ def test_cp_r_sets_parent_correctly
796
+ FileUtils.mkdir_p '/path/foo'
797
+ File.open('/path/foo/bar', 'w') { |f| f.write 'foo' }
798
+ File.open('/path/foo/baz', 'w') { |f| f.write 'foo' }
799
+
800
+ FileUtils.cp_r '/path/foo', '/path/bar'
801
+
802
+ assert File.exists?('/path/bar/baz')
803
+ FileUtils.rm_rf '/path/bar/baz'
804
+ assert_equal %w( /path/bar/bar ), Dir['/path/bar/*']
805
+ end
806
+
807
+ def test_clone_clones_normal_files
808
+ RealFile.open(here('foo'), 'w') { |f| f.write 'bar' }
809
+ assert !File.exists?(here('foo'))
810
+ FileSystem.clone(here('foo'))
811
+ assert_equal 'bar', File.open(here('foo')) { |f| f.read }
812
+ ensure
813
+ RealFile.unlink(here('foo')) if RealFile.exists?(here('foo'))
814
+ end
815
+
816
+ def test_clone_clones_directories
817
+ FakeFS.deactivate!
818
+ RealFileUtils.mkdir_p(here('subdir'))
819
+ FakeFS.activate!
820
+
821
+ FileSystem.clone(here('subdir'))
822
+
823
+ assert File.exists?(here('subdir')), 'subdir was cloned'
824
+ assert File.directory?(here('subdir')), 'subdir is a directory'
825
+ ensure
826
+ FakeFS.deactivate!
827
+ RealFileUtils.rm_rf(here('subdir'))
828
+ FakeFS.activate!
829
+ end
830
+
831
+ def test_clone_clones_dot_files_even_hard_to_find_ones
832
+ FakeFS.deactivate!
833
+ RealFileUtils.mkdir_p(here('subdir/.bar/baz/.quux/foo'))
834
+ FakeFS.activate!
835
+
836
+ assert !File.exists?(here('subdir'))
837
+
838
+ FileSystem.clone(here('subdir'))
839
+ assert_equal ['.bar'], FileSystem.find(here('subdir')).keys
840
+ assert_equal ['foo'], FileSystem.find(here('subdir/.bar/baz/.quux')).keys
841
+ ensure
842
+ FakeFS.deactivate!
843
+ RealFileUtils.rm_rf(here('subdir'))
844
+ FakeFS.activate!
845
+ end
846
+
847
+ def test_putting_a_dot_at_end_copies_the_contents
848
+ FileUtils.mkdir_p 'subdir'
849
+ Dir.chdir('subdir') { File.open('foo', 'w') { |f| f.write 'footext' } }
850
+
851
+ FileUtils.mkdir_p 'newdir'
852
+ FileUtils.cp_r 'subdir/.', 'newdir'
853
+ assert_equal 'footext', File.open('newdir/foo') { |f| f.read }
854
+ end
855
+
856
+ def test_file_can_read_from_symlinks
857
+ File.open('first', 'w') { |f| f.write '1'}
858
+ FileUtils.ln_s 'first', 'one'
859
+ assert_equal '1', File.open('one') { |f| f.read }
860
+
861
+ FileUtils.mkdir_p 'subdir'
862
+ File.open('subdir/nother','w') { |f| f.write 'works' }
863
+ FileUtils.ln_s 'subdir', 'new'
864
+ assert_equal 'works', File.open('new/nother') { |f| f.read }
865
+ end
866
+
867
+ def test_can_symlink_through_file
868
+ FileUtils.touch("/foo")
869
+
870
+ File.symlink("/foo", "/bar")
871
+
872
+ assert File.symlink?("/bar")
873
+ end
874
+
875
+ def test_files_can_be_touched
876
+ FileUtils.touch('touched_file')
877
+ assert File.exists?('touched_file')
878
+ list = ['newfile', 'another']
879
+ FileUtils.touch(list)
880
+ list.each { |fp| assert(File.exists?(fp)) }
881
+ end
882
+
883
+ def test_touch_does_not_work_if_the_dir_path_cannot_be_found
884
+ assert_raises(Errno::ENOENT) do
885
+ FileUtils.touch('this/path/should/not/be/here')
886
+ end
887
+ FileUtils.mkdir_p('subdir')
888
+ list = ['subdir/foo', 'nosubdir/bar']
889
+
890
+ assert_raises(Errno::ENOENT) do
891
+ FileUtils.touch(list)
892
+ end
893
+ end
894
+
895
+ def test_extname
896
+ assert File.extname("test.doc") == ".doc"
897
+ end
898
+
899
+ # Directory tests
900
+ def test_new_directory
901
+ FileUtils.mkdir_p('/this/path/should/be/here')
902
+
903
+ assert_nothing_raised do
904
+ Dir.new('/this/path/should/be/here')
905
+ end
906
+ end
907
+
908
+ def test_new_directory_does_not_work_if_dir_path_cannot_be_found
909
+ assert_raises(Errno::ENOENT) do
910
+ Dir.new('/this/path/should/not/be/here')
911
+ end
912
+ end
913
+
914
+ def test_directory_close
915
+ FileUtils.mkdir_p('/this/path/should/be/here')
916
+ dir = Dir.new('/this/path/should/be/here')
917
+ assert dir.close.nil?
918
+
919
+ assert_raises(IOError) do
920
+ dir.each { |dir| dir }
921
+ end
922
+ end
923
+
924
+ def test_directory_each
925
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
926
+
927
+ FileUtils.mkdir_p('/this/path/should/be/here')
928
+
929
+ test.each do |f|
930
+ FileUtils.touch("/this/path/should/be/here/#{f}")
931
+ end
932
+
933
+ dir = Dir.new('/this/path/should/be/here')
934
+
935
+ yielded = []
936
+ dir.each do |dir|
937
+ yielded << dir
938
+ end
939
+
940
+ assert yielded.size == test.size
941
+ test.each { |t| assert yielded.include?(t) }
942
+ end
943
+
944
+ def test_directory_path
945
+ FileUtils.mkdir_p('/this/path/should/be/here')
946
+ good_path = '/this/path/should/be/here'
947
+ assert_equal good_path, Dir.new('/this/path/should/be/here').path
948
+ end
949
+
950
+ def test_directory_pos
951
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
952
+ FileUtils.mkdir_p('/this/path/should/be/here')
953
+ test.each do |f|
954
+ FileUtils.touch("/this/path/should/be/here/#{f}")
955
+ end
956
+
957
+ dir = Dir.new('/this/path/should/be/here')
958
+
959
+ assert dir.pos == 0
960
+ dir.read
961
+ assert dir.pos == 1
962
+ dir.read
963
+ assert dir.pos == 2
964
+ dir.read
965
+ assert dir.pos == 3
966
+ dir.read
967
+ assert dir.pos == 4
968
+ dir.read
969
+ assert dir.pos == 5
970
+ end
971
+
972
+ def test_directory_pos_assign
973
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
974
+
975
+ FileUtils.mkdir_p('/this/path/should/be/here')
976
+ test.each do |f|
977
+ FileUtils.touch("/this/path/should/be/here/#{f}")
978
+ end
979
+
980
+ dir = Dir.new('/this/path/should/be/here')
981
+
982
+ assert dir.pos == 0
983
+ dir.pos = 2
984
+ assert dir.pos == 2
985
+ end
986
+
987
+ def test_directory_read
988
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
989
+
990
+ FileUtils.mkdir_p('/this/path/should/be/here')
991
+ test.each do |f|
992
+ FileUtils.touch("/this/path/should/be/here/#{f}")
993
+ end
994
+
995
+ dir = Dir.new('/this/path/should/be/here')
996
+
997
+ assert dir.pos == 0
998
+ d = dir.read
999
+ assert dir.pos == 1
1000
+ assert d == '.'
1001
+
1002
+ d = dir.read
1003
+ assert dir.pos == 2
1004
+ assert d == '..'
1005
+ end
1006
+
1007
+ def test_directory_read_past_length
1008
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
1009
+
1010
+ FileUtils.mkdir_p('/this/path/should/be/here')
1011
+ test.each do |f|
1012
+ FileUtils.touch("/this/path/should/be/here/#{f}")
1013
+ end
1014
+
1015
+ dir = Dir.new('/this/path/should/be/here')
1016
+
1017
+ d = dir.read
1018
+ assert_not_nil d
1019
+ d = dir.read
1020
+ assert_not_nil d
1021
+ d = dir.read
1022
+ assert_not_nil d
1023
+ d = dir.read
1024
+ assert_not_nil d
1025
+ d = dir.read
1026
+ assert_not_nil d
1027
+ d = dir.read
1028
+ assert_not_nil d
1029
+ d = dir.read
1030
+ assert_not_nil d
1031
+ d = dir.read
1032
+ assert_nil d
1033
+ end
1034
+
1035
+ def test_directory_rewind
1036
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
1037
+
1038
+ FileUtils.mkdir_p('/this/path/should/be/here')
1039
+ test.each do |f|
1040
+ FileUtils.touch("/this/path/should/be/here/#{f}")
1041
+ end
1042
+
1043
+ dir = Dir.new('/this/path/should/be/here')
1044
+
1045
+ d = dir.read
1046
+ d = dir.read
1047
+ assert dir.pos == 2
1048
+ dir.rewind
1049
+ assert dir.pos == 0
1050
+ end
1051
+
1052
+ def test_directory_seek
1053
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
1054
+
1055
+ FileUtils.mkdir_p('/this/path/should/be/here')
1056
+ test.each do |f|
1057
+ FileUtils.touch("/this/path/should/be/here/#{f}")
1058
+ end
1059
+
1060
+ dir = Dir.new('/this/path/should/be/here')
1061
+
1062
+ d = dir.seek 1
1063
+ assert d == '..'
1064
+ assert dir.pos == 1
1065
+ end
1066
+
1067
+ def test_directory_class_delete
1068
+ FileUtils.mkdir_p('/this/path/should/be/here')
1069
+ Dir.delete('/this/path/should/be/here')
1070
+ assert File.exists?('/this/path/should/be/here') == false
1071
+ end
1072
+
1073
+ def test_directory_class_delete_does_not_act_on_non_empty_directory
1074
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
1075
+
1076
+ FileUtils.mkdir_p('/this/path/should/be/here')
1077
+ test.each do |f|
1078
+ FileUtils.touch("/this/path/should/be/here/#{f}")
1079
+ end
1080
+
1081
+ assert_raises(SystemCallError) do
1082
+ Dir.delete('/this/path/should/be/here')
1083
+ end
1084
+ end
1085
+
1086
+ def test_directory_entries
1087
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
1088
+
1089
+ FileUtils.mkdir_p('/this/path/should/be/here')
1090
+
1091
+ test.each do |f|
1092
+ FileUtils.touch("/this/path/should/be/here/#{f}")
1093
+ end
1094
+
1095
+ yielded = Dir.entries('/this/path/should/be/here')
1096
+ assert yielded.size == test.size
1097
+ test.each { |t| assert yielded.include?(t) }
1098
+ end
1099
+
1100
+ def test_directory_entries_works_with_trailing_slash
1101
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
1102
+
1103
+ FileUtils.mkdir_p('/this/path/should/be/here')
1104
+
1105
+ test.each do |f|
1106
+ FileUtils.touch("/this/path/should/be/here/#{f}")
1107
+ end
1108
+
1109
+ yielded = Dir.entries('/this/path/should/be/here/')
1110
+ assert yielded.size == test.size
1111
+ test.each { |t| assert yielded.include?(t) }
1112
+ end
1113
+
1114
+ def test_directory_foreach
1115
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
1116
+
1117
+ FileUtils.mkdir_p('/this/path/should/be/here')
1118
+
1119
+ test.each do |f|
1120
+ FileUtils.touch("/this/path/should/be/here/#{f}")
1121
+ end
1122
+
1123
+ yielded = []
1124
+ Dir.foreach('/this/path/should/be/here') do |dir|
1125
+ yielded << dir
1126
+ end
1127
+
1128
+ assert yielded.size == test.size
1129
+ test.each { |t| assert yielded.include?(t) }
1130
+ end
1131
+
1132
+ def test_directory_mkdir
1133
+ Dir.mkdir('/path')
1134
+ assert File.exists?('/path')
1135
+ end
1136
+
1137
+ def test_directory_mkdir_relative
1138
+ FileUtils.mkdir_p('/new/root')
1139
+ FileSystem.chdir('/new/root')
1140
+ Dir.mkdir('path')
1141
+ assert File.exists?('/new/root/path')
1142
+ end
1143
+
1144
+ def test_directory_mkdir_not_recursive
1145
+ assert_raises(Errno::ENOENT) do
1146
+ Dir.mkdir('/path/does/not/exist')
1147
+ end
1148
+ end
1149
+
1150
+ def test_mkdir_raises_error_if_already_created
1151
+ Dir.mkdir "foo"
1152
+
1153
+ assert_raises(Errno::EEXIST) do
1154
+ Dir.mkdir "foo"
1155
+ end
1156
+ end
1157
+
1158
+ def test_directory_open
1159
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
1160
+
1161
+ FileUtils.mkdir_p('/this/path/should/be/here')
1162
+
1163
+ test.each do |f|
1164
+ FileUtils.touch("/this/path/should/be/here/#{f}")
1165
+ end
1166
+
1167
+ dir = Dir.open('/this/path/should/be/here')
1168
+ assert dir.path == '/this/path/should/be/here'
1169
+ end
1170
+
1171
+ def test_directory_open_block
1172
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
1173
+
1174
+ FileUtils.mkdir_p('/this/path/should/be/here')
1175
+
1176
+ test.each do |f|
1177
+ FileUtils.touch("/this/path/should/be/here/#{f}")
1178
+ end
1179
+
1180
+ yielded = []
1181
+ Dir.open('/this/path/should/be/here') do |dir|
1182
+ yielded << dir
1183
+ end
1184
+
1185
+ assert yielded.size == test.size
1186
+ test.each { |t| assert yielded.include?(t) }
1187
+ end
1188
+
1189
+ def test_tmpdir
1190
+ assert Dir.tmpdir == "/tmp"
1191
+ end
1192
+
1193
+ def test_rename_renames_a_file
1194
+ FileUtils.touch("/foo")
1195
+ File.rename("/foo", "/bar")
1196
+ assert File.file?("/bar")
1197
+ end
1198
+
1199
+ def test_rename_returns
1200
+ FileUtils.touch("/foo")
1201
+ assert_equal 0, File.rename("/foo", "/bar")
1202
+ end
1203
+
1204
+ def test_rename_renames_two_files
1205
+ FileUtils.touch("/foo")
1206
+ FileUtils.touch("/bar")
1207
+ File.rename("/foo", "/bar")
1208
+ assert File.file?("/bar")
1209
+
1210
+ end
1211
+
1212
+ def test_rename_renames_a_directories
1213
+ Dir.mkdir("/foo")
1214
+ File.rename("/foo", "/bar")
1215
+ assert File.directory?("/bar")
1216
+ end
1217
+
1218
+ def test_rename_renames_two_directories
1219
+ Dir.mkdir("/foo")
1220
+ Dir.mkdir("/bar")
1221
+ File.rename("/foo", "/bar")
1222
+ assert File.directory?("/bar")
1223
+ end
1224
+
1225
+ def test_rename_file_to_directory_raises_error
1226
+ FileUtils.touch("/foo")
1227
+ Dir.mkdir("/bar")
1228
+ assert_raises(Errno::EISDIR) do
1229
+ File.rename("/foo", "/bar")
1230
+ end
1231
+ end
1232
+
1233
+ def test_rename_directory_to_file_raises_error
1234
+ Dir.mkdir("/foo")
1235
+ FileUtils.touch("/bar")
1236
+ assert_raises(Errno::ENOTDIR) do
1237
+ File.rename("/foo", "/bar")
1238
+ end
1239
+ end
1240
+
1241
+
1242
+ def test_rename_with_missing_source_raises_error
1243
+ assert_raises(Errno::ENOENT) do
1244
+ File.rename("/no_such_file", "/bar")
1245
+ end
1246
+ end
1247
+
1248
+ def test_hard_link_creates_file
1249
+ FileUtils.touch("/foo")
1250
+
1251
+ File.link("/foo", "/bar")
1252
+ assert File.exists?("/bar")
1253
+ end
1254
+
1255
+ def test_hard_link_with_missing_file_raises_error
1256
+ assert_raises(Errno::ENOENT) do
1257
+ File.link("/foo", "/bar")
1258
+ end
1259
+ end
1260
+
1261
+ def test_hard_link_with_existing_destination_file
1262
+ FileUtils.touch("/foo")
1263
+ FileUtils.touch("/bar")
1264
+
1265
+ assert_raises(Errno::EEXIST) do
1266
+ File.link("/foo", "/bar")
1267
+ end
1268
+ end
1269
+
1270
+ def test_hard_link_returns_0_when_successful
1271
+ FileUtils.touch("/foo")
1272
+
1273
+ assert_equal 0, File.link("/foo", "/bar")
1274
+ end
1275
+
1276
+ def test_hard_link_returns_duplicate_file
1277
+ File.open("/foo", "w") { |x| x << "some content" }
1278
+
1279
+ File.link("/foo", "/bar")
1280
+ assert_equal "some content", File.read("/bar")
1281
+ end
1282
+
1283
+ def test_hard_link_with_directory_raises_error
1284
+ Dir.mkdir "/foo"
1285
+
1286
+ assert_raises(Errno::EPERM) do
1287
+ File.link("/foo", "/bar")
1288
+ end
1289
+ end
1290
+
1291
+ def test_file_stat_returns_file_stat_object
1292
+ FileUtils.touch("/foo")
1293
+ assert_equal File::Stat, File.stat("/foo").class
1294
+ end
1295
+
1296
+ def test_can_delete_file_with_delete
1297
+ FileUtils.touch("/foo")
1298
+
1299
+ File.delete("/foo")
1300
+
1301
+ assert !File.exists?("/foo")
1302
+ end
1303
+
1304
+ def test_can_delete_multiple_files_with_delete
1305
+ FileUtils.touch("/foo")
1306
+ FileUtils.touch("/bar")
1307
+
1308
+ File.delete("/foo", "/bar")
1309
+
1310
+ assert !File.exists?("/foo")
1311
+ assert !File.exists?("/bar")
1312
+ end
1313
+
1314
+ def test_delete_raises_argument_error_with_no_filename_given
1315
+ assert_raises ArgumentError do
1316
+ File.delete
1317
+ end
1318
+ end
1319
+
1320
+ def test_delete_returns_number_one_when_given_one_arg
1321
+ FileUtils.touch("/foo")
1322
+
1323
+ assert_equal 1, File.delete("/foo")
1324
+ end
1325
+
1326
+ def test_delete_returns_number_two_when_given_two_args
1327
+ FileUtils.touch("/foo")
1328
+ FileUtils.touch("/bar")
1329
+
1330
+ assert_equal 2, File.delete("/foo", "/bar")
1331
+ end
1332
+
1333
+ def test_delete_raises_error_when_first_file_does_not_exist
1334
+ assert_raises Errno::ENOENT do
1335
+ File.delete("/foo")
1336
+ end
1337
+ end
1338
+
1339
+ def test_delete_does_not_raise_error_when_second_file_does_not_exist
1340
+ FileUtils.touch("/foo")
1341
+
1342
+ assert_nothing_raised do
1343
+ File.delete("/foo", "/bar")
1344
+ end
1345
+ end
1346
+
1347
+ def test_unlink_is_alias_for_delete
1348
+ assert_equal File.method(:unlink), File.method(:delete)
1349
+ end
1350
+
1351
+ def test_unlink_removes_only_one_file_content
1352
+ File.open("/foo", "w") { |f| f << "some_content" }
1353
+ File.link("/foo", "/bar")
1354
+
1355
+ File.unlink("/bar")
1356
+ File.read("/foo") == "some_content"
1357
+ end
1358
+
1359
+ def test_link_reports_correct_stat_info_after_unlinking
1360
+ File.open("/foo", "w") { |f| f << "some_content" }
1361
+ File.link("/foo", "/bar")
1362
+
1363
+ File.unlink("/bar")
1364
+ assert_equal 1, File.stat("/foo").nlink
1365
+ end
1366
+
1367
+ def test_delete_works_with_symlink
1368
+ FileUtils.touch("/foo")
1369
+ File.symlink("/foo", "/bar")
1370
+
1371
+ File.unlink("/bar")
1372
+
1373
+ assert File.exists?("/foo")
1374
+ assert !File.exists?("/bar")
1375
+ end
1376
+
1377
+ def test_delete_works_with_symlink_source
1378
+ FileUtils.touch("/foo")
1379
+ File.symlink("/foo", "/bar")
1380
+
1381
+ File.unlink("/foo")
1382
+
1383
+ assert !File.exists?("/foo")
1384
+ end
1385
+
1386
+ def test_file_seek_returns_0
1387
+ File.open("/foo", "w") do |f|
1388
+ f << "one\ntwo\nthree"
1389
+ end
1390
+
1391
+ file = File.open("/foo", "r")
1392
+
1393
+ assert_equal 0, file.seek(1)
1394
+ end
1395
+
1396
+ def test_file_seek_seeks_to_location
1397
+ File.open("/foo", "w") do |f|
1398
+ f << "123"
1399
+ end
1400
+
1401
+ file = File.open("/foo", "r")
1402
+ file.seek(1)
1403
+ assert_equal "23", file.read
1404
+ end
1405
+
1406
+ def test_file_seek_seeks_to_correct_location
1407
+ File.open("/foo", "w") do |f|
1408
+ f << "123"
1409
+ end
1410
+
1411
+ file = File.open("/foo", "r")
1412
+ file.seek(2)
1413
+ assert_equal "3", file.read
1414
+ end
1415
+
1416
+ def test_file_seek_can_take_negative_offset
1417
+ File.open("/foo", "w") do |f|
1418
+ f << "123456789"
1419
+ end
1420
+
1421
+ file = File.open("/foo", "r")
1422
+
1423
+ file.seek(-1, IO::SEEK_END)
1424
+ assert_equal "9", file.read
1425
+
1426
+ file.seek(-2, IO::SEEK_END)
1427
+ assert_equal "89", file.read
1428
+
1429
+ file.seek(-3, IO::SEEK_END)
1430
+ assert_equal "789", file.read
1431
+ end
1432
+
1433
+ def test_should_have_constants_inherited_from_descending_from_io
1434
+ assert_equal IO::SEEK_CUR, File::SEEK_CUR
1435
+ assert_equal IO::SEEK_END, File::SEEK_END
1436
+ assert_equal IO::SEEK_SET, File::SEEK_SET
1437
+ end
1438
+
1439
+ def test_filetest_exists_return_correct_values
1440
+ FileUtils.mkdir_p("/path/to/dir")
1441
+ assert FileTest.exist?("/path/to/")
1442
+
1443
+ FileUtils.rmdir("/path/to/dir")
1444
+ assert !FileTest.exist?("/path/to/dir")
1445
+ end
1446
+
1447
+ def test_pathname_exists_returns_correct_value
1448
+ FileUtils.touch "foo"
1449
+ assert Pathname.new("foo").exist?
1450
+
1451
+ assert !Pathname.new("bar").exist?
1452
+ end
1453
+
1454
+ def test_activating_returns_true
1455
+ FakeFS.deactivate!
1456
+ assert_equal true, FakeFS.activate!
1457
+ end
1458
+
1459
+ def test_deactivating_returns_true
1460
+ assert_equal true, FakeFS.deactivate!
1461
+ end
1462
+
1463
+ def here(fname)
1464
+ RealFile.expand_path(File.join(RealFile.dirname(__FILE__), fname))
1465
+ end
1466
+ end