fakefs 0.1.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,23 @@ 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
76
+ end
77
+
78
+ def test_can_force_creation_of_symlinks
79
+ FileUtils.mkdir_p(target = "/path/to/first/target")
80
+ FileUtils.ln_s(target, "/path/to/link")
81
+ assert_kind_of FakeSymlink, FileSystem.fs['path']['to']['link']
82
+ FileUtils.ln_s(target, '/path/to/link', :force => true)
83
+ end
84
+
85
+ def test_create_symlink_using_ln_sf
86
+ FileUtils.mkdir_p(target = "/path/to/first/target")
87
+ FileUtils.ln_s(target, "/path/to/link")
88
+ assert_kind_of FakeSymlink, FileSystem.fs['path']['to']['link']
89
+ FileUtils.ln_sf(target, '/path/to/link')
65
90
  end
66
91
 
67
92
  def test_can_follow_symlinks
@@ -83,6 +108,126 @@ class FakeFSTest < Test::Unit::TestCase
83
108
  end
84
109
 
85
110
  assert File.exists?(path)
111
+ assert File.readable?(path)
112
+ assert File.writable?(path)
113
+ end
114
+
115
+ def test_can_create_files_with_bitmasks
116
+ path = '/path/to/file.txt'
117
+ File.open(path, File::RDWR | File::CREAT) do |f|
118
+ f.write "Yatta!"
119
+ end
120
+
121
+ assert File.exists?(path)
122
+ assert File.readable?(path)
123
+ assert File.writable?(path)
124
+ end
125
+
126
+ def test_file_opens_in_read_only_mode
127
+ File.open("foo", "w") { |f| f << "foo" }
128
+
129
+ f = File.open("foo")
130
+
131
+ assert_raises(IOError) do
132
+ f << "bar"
133
+ end
134
+ end
135
+
136
+ def test_file_opens_in_read_only_mode_with_bitmasks
137
+ File.open("foo", "w") { |f| f << "foo" }
138
+
139
+ f = File.open("foo", File::RDONLY)
140
+
141
+ assert_raises(IOError) do
142
+ f << "bar"
143
+ end
144
+ end
145
+
146
+ def test_file_opens_in_invalid_mode
147
+ FileUtils.touch("foo")
148
+
149
+ assert_raises(ArgumentError) do
150
+ File.open("foo", "an_illegal_mode")
151
+ end
152
+ end
153
+
154
+ def test_raises_error_when_cannot_find_file_in_read_mode
155
+ assert_raises(Errno::ENOENT) do
156
+ File.open("does_not_exist", "r")
157
+ end
158
+ end
159
+
160
+ def test_raises_error_when_cannot_find_file_in_read_write_mode
161
+ assert_raises(Errno::ENOENT) do
162
+ File.open("does_not_exist", "r+")
163
+ end
164
+ end
165
+
166
+ def test_creates_files_in_write_only_mode
167
+ File.open("foo", "w")
168
+ assert File.exists?("foo")
169
+ end
170
+
171
+ def test_creates_files_in_write_only_mode_with_bitmasks
172
+ File.open("foo", File::WRONLY | File::CREAT)
173
+ assert File.exists?("foo")
174
+ end
175
+
176
+ def test_raises_in_write_only_mode_without_create_bitmask
177
+ assert_raises(Errno::ENOENT) do
178
+ File.open("foo", File::WRONLY)
179
+ end
180
+ end
181
+
182
+ def test_creates_files_in_read_write_truncate_mode
183
+ File.open("foo", "w+")
184
+ assert File.exists?("foo")
185
+ end
186
+
187
+ def test_creates_files_in_append_write_only
188
+ File.open("foo", "a")
189
+ assert File.exists?("foo")
190
+ end
191
+
192
+ def test_creates_files_in_append_read_write
193
+ File.open("foo", "a+")
194
+ assert File.exists?("foo")
195
+ end
196
+
197
+ def test_file_in_write_only_raises_error_when_reading
198
+ FileUtils.touch("foo")
199
+
200
+ f = File.open("foo", "w")
201
+
202
+ assert_raises(IOError) do
203
+ f.read
204
+ end
205
+ end
206
+
207
+ def test_file_in_write_mode_truncates_existing_file
208
+ File.open("foo", "w") { |f| f << "contents" }
209
+
210
+ f = File.open("foo", "w")
211
+
212
+ assert_equal "", File.read("foo")
213
+ end
214
+
215
+ def test_file_in_read_write_truncation_mode_truncates_file
216
+ File.open("foo", "w") { |f| f << "foo" }
217
+
218
+ f = File.open("foo", "w+")
219
+
220
+ assert_equal "", File.read("foo")
221
+ end
222
+
223
+ def test_file_in_append_write_only_raises_error_when_reading
224
+ FileUtils.touch("foo")
225
+
226
+ f = File.open("foo", "a")
227
+
228
+ assert_raises(IOError) do
229
+ f.read
230
+ end
86
231
  end
87
232
 
88
233
  def test_can_read_files_once_written
@@ -102,14 +247,77 @@ class FakeFSTest < Test::Unit::TestCase
102
247
  assert_equal 'Yada Yada', File.read(path)
103
248
  end
104
249
 
250
+ def test_raises_error_when_opening_with_binary_mode_only
251
+ assert_raise ArgumentError do
252
+ File.open("/foo", "b")
253
+ end
254
+ end
255
+
256
+ def test_can_open_file_in_binary_mode
257
+ File.open("/foo", "wb") { |x| x << "a" }
258
+ assert_equal "a", File.read("/foo")
259
+ end
260
+
261
+ def test_can_chunk_io_when_reading
262
+ path = '/path/to/file.txt'
263
+ File.open(path, 'w') do |f|
264
+ f << 'Yada Yada'
265
+ end
266
+ file = File.new(path, 'r')
267
+ assert_equal 'Yada', file.read(4)
268
+ assert_equal ' Yada', file.read(5)
269
+ assert_equal '', file.read
270
+ file.rewind
271
+ assert_equal 'Yada Yada', file.read
272
+ end
273
+
274
+ def test_can_get_size_of_files
275
+ path = '/path/to/file.txt'
276
+ File.open(path, 'w') do |f|
277
+ f << 'Yada Yada'
278
+ end
279
+ assert_equal 9, File.size(path)
280
+ end
281
+
282
+ def test_can_check_if_file_has_size?
283
+ path = '/path/to/file.txt'
284
+ File.open(path, 'w') do |f|
285
+ f << 'Yada Yada'
286
+ end
287
+ assert File.size?(path)
288
+ assert_nil File.size?("/path/to/other.txt")
289
+ end
290
+
291
+ def test_can_check_size?_of_empty_file
292
+ path = '/path/to/file.txt'
293
+ File.open(path, 'w') do |f|
294
+ f << ''
295
+ end
296
+ assert_nil File.size?("/path/to/file.txt")
297
+ end
298
+
299
+ def test_raises_error_on_mtime_if_file_does_not_exist
300
+ assert_raise Errno::ENOENT do
301
+ File.mtime('/path/to/file.txt')
302
+ end
303
+ end
304
+
305
+ def test_can_return_mtime_on_existing_file
306
+ path = '/path/to/file.txt'
307
+ File.open(path, 'w') do |f|
308
+ f << ''
309
+ end
310
+ assert File.mtime('/path/to/file.txt').is_a?(Time)
311
+ end
312
+
105
313
  def test_can_read_with_File_readlines
106
314
  path = '/path/to/file.txt'
107
315
  File.open(path, 'w') do |f|
108
- f.puts "Yatta!"
109
- f.puts "woot"
316
+ f.puts "Yatta!", "Gatta!"
317
+ f.puts ["woot","toot"]
110
318
  end
111
319
 
112
- assert_equal ["Yatta!", "woot"], File.readlines(path)
320
+ assert_equal %w(Yatta! Gatta! woot toot), File.readlines(path)
113
321
  end
114
322
 
115
323
  def test_File_close_disallows_further_access
@@ -163,9 +371,10 @@ class FakeFSTest < Test::Unit::TestCase
163
371
  def test_can_chown_files
164
372
  good = 'file.txt'
165
373
  bad = 'nofile.txt'
166
- File.open(good,'w'){|f| f.write "foo" }
374
+ File.open(good,'w') { |f| f.write "foo" }
167
375
 
168
- assert_equal [good], FileUtils.chown('noone', 'nogroup', good, :verbose => true)
376
+ out = FileUtils.chown('noone', 'nogroup', good, :verbose => true)
377
+ assert_equal [good], out
169
378
  assert_raises(Errno::ENOENT) do
170
379
  FileUtils.chown('noone', 'nogroup', bad, :verbose => true)
171
380
  end
@@ -183,19 +392,19 @@ class FakeFSTest < Test::Unit::TestCase
183
392
 
184
393
  def test_can_chown_R_files
185
394
  FileUtils.mkdir_p '/path/'
186
- File.open('/path/foo', 'w'){|f| f.write 'foo' }
187
- File.open('/path/foobar', 'w'){|f| f.write 'foo' }
395
+ File.open('/path/foo', 'w') { |f| f.write 'foo' }
396
+ File.open('/path/foobar', 'w') { |f| f.write 'foo' }
188
397
  resp = FileUtils.chown_R('no', 'no', '/path')
189
398
  assert_equal ['/path'], resp
190
399
  end
191
400
 
192
401
  def test_dir_globs_paths
193
402
  FileUtils.mkdir_p '/path'
194
- File.open('/path/foo', 'w'){|f| f.write 'foo' }
195
- File.open('/path/foobar', 'w'){|f| f.write 'foo' }
403
+ File.open('/path/foo', 'w') { |f| f.write 'foo' }
404
+ File.open('/path/foobar', 'w') { |f| f.write 'foo' }
196
405
 
197
406
  FileUtils.mkdir_p '/path/bar'
198
- File.open('/path/bar/baz', 'w'){|f| f.write 'foo' }
407
+ File.open('/path/bar/baz', 'w') { |f| f.write 'foo' }
199
408
 
200
409
  FileUtils.cp_r '/path/bar', '/path/bar2'
201
410
 
@@ -221,14 +430,29 @@ class FakeFSTest < Test::Unit::TestCase
221
430
  #assert_equal ['/'], Dir['/']
222
431
  end
223
432
 
433
+ def test_dir_glob_handles_recursive_globs
434
+ File.open('/one/two/three/four.rb', 'w')
435
+ File.open('/one/five.rb', 'w')
436
+ assert_equal ['/one/five.rb', '/one/two/three/four.rb'], Dir['/one/**/*.rb']
437
+ assert_equal ['/one/two'], Dir['/one/**/two']
438
+ assert_equal ['/one/two/three'], Dir['/one/**/three']
439
+ end
440
+
441
+ def test_dir_recursive_glob_ending_in_wildcards_only_returns_files
442
+ File.open('/one/two/three/four.rb', 'w')
443
+ File.open('/one/five.rb', 'w')
444
+ assert_equal ['/one/five.rb', '/one/two/three/four.rb'], Dir['/one/**/*']
445
+ assert_equal ['/one/five.rb', '/one/two/three/four.rb'], Dir['/one/**']
446
+ end
447
+
224
448
  def test_chdir_changes_directories_like_a_boss
225
449
  # I know memes!
226
450
  FileUtils.mkdir_p '/path'
227
451
  assert_equal '.', FileSystem.fs.name
228
452
  assert_equal({}, FileSystem.fs['path'])
229
453
  Dir.chdir '/path' do
230
- File.open('foo', 'w'){|f| f.write 'foo'}
231
- File.open('foobar', 'w'){|f| f.write 'foo'}
454
+ File.open('foo', 'w') { |f| f.write 'foo'}
455
+ File.open('foobar', 'w') { |f| f.write 'foo'}
232
456
  end
233
457
 
234
458
  assert_equal '.', FileSystem.fs.name
@@ -236,7 +460,7 @@ class FakeFSTest < Test::Unit::TestCase
236
460
 
237
461
  c = nil
238
462
  Dir.chdir '/path' do
239
- c = File.open('foo', 'r'){|f| f.read }
463
+ c = File.open('foo', 'r') { |f| f.read }
240
464
  end
241
465
 
242
466
  assert_equal 'foo', c
@@ -246,8 +470,8 @@ class FakeFSTest < Test::Unit::TestCase
246
470
  FileUtils.mkdir_p '/path'
247
471
 
248
472
  Dir.chdir '/path' do
249
- File.open('foo', 'w'){|f| f.write 'foo'}
250
- File.open('/foobar', 'w'){|f| f.write 'foo'}
473
+ File.open('foo', 'w') { |f| f.write 'foo'}
474
+ File.open('/foobar', 'w') { |f| f.write 'foo'}
251
475
  end
252
476
  assert_equal ['foo'], FileSystem.fs['path'].keys.sort
253
477
  assert_equal ['foobar', 'path'], FileSystem.fs.keys.sort
@@ -264,9 +488,9 @@ class FakeFSTest < Test::Unit::TestCase
264
488
  def test_chdir_should_be_nestable
265
489
  FileUtils.mkdir_p '/path/me'
266
490
  Dir.chdir '/path' do
267
- File.open('foo', 'w'){|f| f.write 'foo'}
491
+ File.open('foo', 'w') { |f| f.write 'foo'}
268
492
  Dir.chdir 'me' do
269
- File.open('foobar', 'w'){|f| f.write 'foo'}
493
+ File.open('foobar', 'w') { |f| f.write 'foo'}
270
494
  end
271
495
  end
272
496
 
@@ -286,8 +510,8 @@ class FakeFSTest < Test::Unit::TestCase
286
510
  FileUtils.mkdir_p '/path'
287
511
 
288
512
  Dir.chdir '/path' do
289
- File.open('foo', 'w'){|f| f.write 'foo'}
290
- File.open('foobar', 'w'){|f| f.write 'foo'}
513
+ File.open('foo', 'w') { |f| f.write 'foo'}
514
+ File.open('foobar', 'w') { |f| f.write 'foo'}
291
515
  end
292
516
 
293
517
  begin
@@ -315,7 +539,7 @@ class FakeFSTest < Test::Unit::TestCase
315
539
  FileUtils.mkdir_p 'subdir'
316
540
  assert_equal ['subdir'], FileSystem.current_dir.keys
317
541
  Dir.chdir('subdir')
318
- File.open('foo', 'w'){|f| f.write 'foo'}
542
+ File.open('foo', 'w') { |f| f.write 'foo'}
319
543
  assert_equal ['foo'], FileSystem.current_dir.keys
320
544
 
321
545
  assert_raises(Errno::ENOENT) do
@@ -340,12 +564,12 @@ class FakeFSTest < Test::Unit::TestCase
340
564
  end
341
565
 
342
566
  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 }
567
+ File.open('foo','w') { |f| f.write 'bar' }
568
+ assert_equal 'bar', File.open('foo') { |f| f.read }
345
569
  end
346
570
 
347
571
  def test_flush_exists_on_file
348
- r = File.open('foo','w'){|f| f.write 'bar'; f.flush }
572
+ r = File.open('foo','w') { |f| f.write 'bar'; f.flush }
349
573
  assert_equal 'foo', r.path
350
574
  end
351
575
 
@@ -356,9 +580,9 @@ class FakeFSTest < Test::Unit::TestCase
356
580
  end
357
581
 
358
582
  def test_mv_actually_works
359
- File.open('foo', 'w') {|f| f.write 'bar' }
583
+ File.open('foo', 'w') { |f| f.write 'bar' }
360
584
  FileUtils.mv 'foo', 'baz'
361
- assert_equal 'bar', File.open('baz'){|f| f.read }
585
+ assert_equal 'bar', File.open('baz') { |f| f.read }
362
586
  end
363
587
 
364
588
  def test_cp_actually_works
@@ -398,10 +622,10 @@ class FakeFSTest < Test::Unit::TestCase
398
622
  end
399
623
 
400
624
  def test_cp_r_doesnt_tangle_files_together
401
- File.open('foo', 'w') {|f| f.write 'bar' }
625
+ File.open('foo', 'w') { |f| f.write 'bar' }
402
626
  FileUtils.cp_r('foo', 'baz')
403
- File.open('baz', 'w') {|f| f.write 'quux' }
404
- assert_equal 'bar', File.open('foo'){|f| f.read }
627
+ File.open('baz', 'w') { |f| f.write 'quux' }
628
+ assert_equal 'bar', File.open('foo') { |f| f.read }
405
629
  end
406
630
 
407
631
  def test_cp_r_should_raise_error_on_missing_file
@@ -414,29 +638,29 @@ class FakeFSTest < Test::Unit::TestCase
414
638
 
415
639
  def test_cp_r_handles_copying_directories
416
640
  FileUtils.mkdir_p 'subdir'
417
- Dir.chdir('subdir'){ File.open('foo', 'w'){|f| f.write 'footext' } }
641
+ Dir.chdir('subdir'){ File.open('foo', 'w') { |f| f.write 'footext' } }
418
642
 
419
643
  FileUtils.mkdir_p 'baz'
420
644
 
421
645
  # To a previously uncreated directory
422
646
  FileUtils.cp_r('subdir', 'quux')
423
- assert_equal 'footext', File.open('quux/foo'){|f| f.read }
647
+ assert_equal 'footext', File.open('quux/foo') { |f| f.read }
424
648
 
425
649
  # To a directory that already exists
426
650
  FileUtils.cp_r('subdir', 'baz')
427
- assert_equal 'footext', File.open('baz/subdir/foo'){|f| f.read }
651
+ assert_equal 'footext', File.open('baz/subdir/foo') { |f| f.read }
428
652
 
429
653
  # To a subdirectory of a directory that does not exist
430
- assert_raises(Errno::ENOENT) {
654
+ assert_raises(Errno::ENOENT) do
431
655
  FileUtils.cp_r('subdir', 'nope/something')
432
- }
656
+ end
433
657
  end
434
658
 
435
659
  def test_cp_r_only_copies_into_directories
436
660
  FileUtils.mkdir_p 'subdir'
437
- Dir.chdir('subdir'){ File.open('foo', 'w'){|f| f.write 'footext' } }
661
+ Dir.chdir('subdir') { File.open('foo', 'w') { |f| f.write 'footext' } }
438
662
 
439
- File.open('bar', 'w') {|f| f.write 'bartext' }
663
+ File.open('bar', 'w') { |f| f.write 'bartext' }
440
664
 
441
665
  assert_raises(Errno::EEXIST) do
442
666
  FileUtils.cp_r 'subdir', 'bar'
@@ -446,13 +670,13 @@ class FakeFSTest < Test::Unit::TestCase
446
670
  FileUtils.ln_s 'otherdir', 'symdir'
447
671
 
448
672
  FileUtils.cp_r 'subdir', 'symdir'
449
- assert_equal 'footext', File.open('symdir/subdir/foo'){|f| f.read }
673
+ assert_equal 'footext', File.open('symdir/subdir/foo') { |f| f.read }
450
674
  end
451
675
 
452
676
  def test_cp_r_sets_parent_correctly
453
677
  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' }
678
+ File.open('/path/foo/bar', 'w') { |f| f.write 'foo' }
679
+ File.open('/path/foo/baz', 'w') { |f| f.write 'foo' }
456
680
 
457
681
  FileUtils.cp_r '/path/foo', '/path/bar'
458
682
 
@@ -462,10 +686,10 @@ class FakeFSTest < Test::Unit::TestCase
462
686
  end
463
687
 
464
688
  def test_clone_clones_normal_files
465
- RealFile.open(here('foo'), 'w'){|f| f.write 'bar' }
689
+ RealFile.open(here('foo'), 'w') { |f| f.write 'bar' }
466
690
  assert !File.exists?(here('foo'))
467
691
  FileSystem.clone(here('foo'))
468
- assert_equal 'bar', File.open(here('foo')){|f| f.read }
692
+ assert_equal 'bar', File.open(here('foo')) { |f| f.read }
469
693
  ensure
470
694
  RealFile.unlink(here('foo')) if RealFile.exists?(here('foo'))
471
695
  end
@@ -494,22 +718,30 @@ class FakeFSTest < Test::Unit::TestCase
494
718
 
495
719
  def test_putting_a_dot_at_end_copies_the_contents
496
720
  FileUtils.mkdir_p 'subdir'
497
- Dir.chdir('subdir'){ File.open('foo', 'w'){|f| f.write 'footext' } }
721
+ Dir.chdir('subdir') { File.open('foo', 'w') { |f| f.write 'footext' } }
498
722
 
499
723
  FileUtils.mkdir_p 'newdir'
500
724
  FileUtils.cp_r 'subdir/.', 'newdir'
501
- assert_equal 'footext', File.open('newdir/foo'){|f| f.read }
725
+ assert_equal 'footext', File.open('newdir/foo') { |f| f.read }
502
726
  end
503
727
 
504
728
  def test_file_can_read_from_symlinks
505
- File.open('first', 'w'){|f| f.write '1'}
729
+ File.open('first', 'w') { |f| f.write '1'}
506
730
  FileUtils.ln_s 'first', 'one'
507
- assert_equal '1', File.open('one'){|f| f.read }
731
+ assert_equal '1', File.open('one') { |f| f.read }
508
732
 
509
733
  FileUtils.mkdir_p 'subdir'
510
- File.open('subdir/nother','w'){|f| f.write 'works' }
734
+ File.open('subdir/nother','w') { |f| f.write 'works' }
511
735
  FileUtils.ln_s 'subdir', 'new'
512
- assert_equal 'works', File.open('new/nother'){|f| f.read }
736
+ assert_equal 'works', File.open('new/nother') { |f| f.read }
737
+ end
738
+
739
+ def test_can_symlink_through_file
740
+ FileUtils.touch("/foo")
741
+
742
+ File.symlink("/foo", "/bar")
743
+
744
+ assert File.symlink?("/bar")
513
745
  end
514
746
 
515
747
  def test_files_can_be_touched
@@ -521,15 +753,496 @@ class FakeFSTest < Test::Unit::TestCase
521
753
  end
522
754
 
523
755
  def test_touch_does_not_work_if_the_dir_path_cannot_be_found
524
- assert_raises(Errno::ENOENT) {
756
+ assert_raises(Errno::ENOENT) do
525
757
  FileUtils.touch('this/path/should/not/be/here')
526
- }
758
+ end
527
759
  FileUtils.mkdir_p('subdir')
528
760
  list = ['subdir/foo', 'nosubdir/bar']
529
761
 
530
- assert_raises(Errno::ENOENT) {
762
+ assert_raises(Errno::ENOENT) do
531
763
  FileUtils.touch(list)
532
- }
764
+ end
765
+ end
766
+
767
+ def test_extname
768
+ assert File.extname("test.doc") == ".doc"
769
+ end
770
+
771
+ # Directory tests
772
+ def test_new_directory
773
+ FileUtils.mkdir_p('/this/path/should/be/here')
774
+
775
+ assert_nothing_raised do
776
+ Dir.new('/this/path/should/be/here')
777
+ end
778
+ end
779
+
780
+ def test_new_directory_does_not_work_if_dir_path_cannot_be_found
781
+ assert_raises(Errno::ENOENT) do
782
+ Dir.new('/this/path/should/not/be/here')
783
+ end
784
+ end
785
+
786
+ def test_directory_close
787
+ FileUtils.mkdir_p('/this/path/should/be/here')
788
+ dir = Dir.new('/this/path/should/be/here')
789
+ assert dir.close.nil?
790
+
791
+ assert_raises(IOError) do
792
+ dir.each { |dir| dir }
793
+ end
794
+ end
795
+
796
+ def test_directory_each
797
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
798
+
799
+ FileUtils.mkdir_p('/this/path/should/be/here')
800
+
801
+ test.each do |f|
802
+ FileUtils.touch("/this/path/should/be/here/#{f}")
803
+ end
804
+
805
+ dir = Dir.new('/this/path/should/be/here')
806
+
807
+ yielded = []
808
+ dir.each do |dir|
809
+ yielded << dir
810
+ end
811
+
812
+ assert yielded.size == test.size
813
+ test.each { |t| assert yielded.include?(t) }
814
+ end
815
+
816
+ def test_directory_path
817
+ FileUtils.mkdir_p('/this/path/should/be/here')
818
+ good_path = '/this/path/should/be/here'
819
+ assert_equal good_path, Dir.new('/this/path/should/be/here').path
820
+ end
821
+
822
+ def test_directory_pos
823
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
824
+ FileUtils.mkdir_p('/this/path/should/be/here')
825
+ test.each do |f|
826
+ FileUtils.touch("/this/path/should/be/here/#{f}")
827
+ end
828
+
829
+ dir = Dir.new('/this/path/should/be/here')
830
+
831
+ assert dir.pos == 0
832
+ dir.read
833
+ assert dir.pos == 1
834
+ dir.read
835
+ assert dir.pos == 2
836
+ dir.read
837
+ assert dir.pos == 3
838
+ dir.read
839
+ assert dir.pos == 4
840
+ dir.read
841
+ assert dir.pos == 5
842
+ end
843
+
844
+ def test_directory_pos_assign
845
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
846
+
847
+ FileUtils.mkdir_p('/this/path/should/be/here')
848
+ test.each do |f|
849
+ FileUtils.touch("/this/path/should/be/here/#{f}")
850
+ end
851
+
852
+ dir = Dir.new('/this/path/should/be/here')
853
+
854
+ assert dir.pos == 0
855
+ dir.pos = 2
856
+ assert dir.pos == 2
857
+ end
858
+
859
+ def test_directory_read
860
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
861
+
862
+ FileUtils.mkdir_p('/this/path/should/be/here')
863
+ test.each do |f|
864
+ FileUtils.touch("/this/path/should/be/here/#{f}")
865
+ end
866
+
867
+ dir = Dir.new('/this/path/should/be/here')
868
+
869
+ assert dir.pos == 0
870
+ d = dir.read
871
+ assert dir.pos == 1
872
+ assert d == '.'
873
+
874
+ d = dir.read
875
+ assert dir.pos == 2
876
+ assert d == '..'
877
+ end
878
+
879
+ def test_directory_read_past_length
880
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
881
+
882
+ FileUtils.mkdir_p('/this/path/should/be/here')
883
+ test.each do |f|
884
+ FileUtils.touch("/this/path/should/be/here/#{f}")
885
+ end
886
+
887
+ dir = Dir.new('/this/path/should/be/here')
888
+
889
+ d = dir.read
890
+ assert_not_nil d
891
+ d = dir.read
892
+ assert_not_nil d
893
+ d = dir.read
894
+ assert_not_nil d
895
+ d = dir.read
896
+ assert_not_nil d
897
+ d = dir.read
898
+ assert_not_nil d
899
+ d = dir.read
900
+ assert_not_nil d
901
+ d = dir.read
902
+ assert_not_nil d
903
+ d = dir.read
904
+ assert_nil d
905
+ end
906
+
907
+ def test_directory_rewind
908
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
909
+
910
+ FileUtils.mkdir_p('/this/path/should/be/here')
911
+ test.each do |f|
912
+ FileUtils.touch("/this/path/should/be/here/#{f}")
913
+ end
914
+
915
+ dir = Dir.new('/this/path/should/be/here')
916
+
917
+ d = dir.read
918
+ d = dir.read
919
+ assert dir.pos == 2
920
+ dir.rewind
921
+ assert dir.pos == 0
922
+ end
923
+
924
+ def test_directory_seek
925
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
926
+
927
+ FileUtils.mkdir_p('/this/path/should/be/here')
928
+ test.each do |f|
929
+ FileUtils.touch("/this/path/should/be/here/#{f}")
930
+ end
931
+
932
+ dir = Dir.new('/this/path/should/be/here')
933
+
934
+ d = dir.seek 1
935
+ assert d == '..'
936
+ assert dir.pos == 1
937
+ end
938
+
939
+ def test_directory_class_delete
940
+ FileUtils.mkdir_p('/this/path/should/be/here')
941
+ Dir.delete('/this/path/should/be/here')
942
+ assert File.exists?('/this/path/should/be/here') == false
943
+ end
944
+
945
+ def test_directory_class_delete_does_not_act_on_non_empty_directory
946
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
947
+
948
+ FileUtils.mkdir_p('/this/path/should/be/here')
949
+ test.each do |f|
950
+ FileUtils.touch("/this/path/should/be/here/#{f}")
951
+ end
952
+
953
+ assert_raises(SystemCallError) do
954
+ Dir.delete('/this/path/should/be/here')
955
+ end
956
+ end
957
+
958
+ def test_directory_entries
959
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
960
+
961
+ FileUtils.mkdir_p('/this/path/should/be/here')
962
+
963
+ test.each do |f|
964
+ FileUtils.touch("/this/path/should/be/here/#{f}")
965
+ end
966
+
967
+ yielded = Dir.entries('/this/path/should/be/here')
968
+ assert yielded.size == test.size
969
+ test.each { |t| assert yielded.include?(t) }
970
+ end
971
+
972
+ def test_directory_entries_works_with_trailing_slash
973
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
974
+
975
+ FileUtils.mkdir_p('/this/path/should/be/here')
976
+
977
+ test.each do |f|
978
+ FileUtils.touch("/this/path/should/be/here/#{f}")
979
+ end
980
+
981
+ yielded = Dir.entries('/this/path/should/be/here/')
982
+ assert yielded.size == test.size
983
+ test.each { |t| assert yielded.include?(t) }
984
+ end
985
+
986
+ def test_directory_foreach
987
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
988
+
989
+ FileUtils.mkdir_p('/this/path/should/be/here')
990
+
991
+ test.each do |f|
992
+ FileUtils.touch("/this/path/should/be/here/#{f}")
993
+ end
994
+
995
+ yielded = []
996
+ Dir.foreach('/this/path/should/be/here') do |dir|
997
+ yielded << dir
998
+ end
999
+
1000
+ assert yielded.size == test.size
1001
+ test.each { |t| assert yielded.include?(t) }
1002
+ end
1003
+
1004
+ def test_directory_mkdir
1005
+ Dir.mkdir('/path')
1006
+ assert File.exists?('/path')
1007
+ end
1008
+
1009
+ def test_directory_mkdir_relative
1010
+ FileUtils.mkdir_p('/new/root')
1011
+ FileSystem.chdir('/new/root')
1012
+ Dir.mkdir('path')
1013
+ assert File.exists?('/new/root/path')
1014
+ end
1015
+
1016
+ def test_directory_mkdir_not_recursive
1017
+ assert_raises(Errno::ENOENT) do
1018
+ Dir.mkdir('/path/does/not/exist')
1019
+ end
1020
+ end
1021
+
1022
+ def test_directory_open
1023
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
1024
+
1025
+ FileUtils.mkdir_p('/this/path/should/be/here')
1026
+
1027
+ test.each do |f|
1028
+ FileUtils.touch("/this/path/should/be/here/#{f}")
1029
+ end
1030
+
1031
+ dir = Dir.open('/this/path/should/be/here')
1032
+ assert dir.path == '/this/path/should/be/here'
1033
+ end
1034
+
1035
+ def test_directory_open_block
1036
+ test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
1037
+
1038
+ FileUtils.mkdir_p('/this/path/should/be/here')
1039
+
1040
+ test.each do |f|
1041
+ FileUtils.touch("/this/path/should/be/here/#{f}")
1042
+ end
1043
+
1044
+ yielded = []
1045
+ Dir.open('/this/path/should/be/here') do |dir|
1046
+ yielded << dir
1047
+ end
1048
+
1049
+ assert yielded.size == test.size
1050
+ test.each { |t| assert yielded.include?(t) }
1051
+ end
1052
+
1053
+ def test_tmpdir
1054
+ assert Dir.tmpdir == "/tmp"
1055
+ end
1056
+
1057
+ def test_hard_link_creates_file
1058
+ FileUtils.touch("/foo")
1059
+
1060
+ File.link("/foo", "/bar")
1061
+ assert File.exists?("/bar")
1062
+ end
1063
+
1064
+ def test_hard_link_with_missing_file_raises_error
1065
+ assert_raises(Errno::ENOENT) do
1066
+ File.link("/foo", "/bar")
1067
+ end
1068
+ end
1069
+
1070
+ def test_hard_link_with_existing_destination_file
1071
+ FileUtils.touch("/foo")
1072
+ FileUtils.touch("/bar")
1073
+
1074
+ assert_raises(Errno::EEXIST) do
1075
+ File.link("/foo", "/bar")
1076
+ end
1077
+ end
1078
+
1079
+ def test_hard_link_returns_0_when_successful
1080
+ FileUtils.touch("/foo")
1081
+
1082
+ assert_equal 0, File.link("/foo", "/bar")
1083
+ end
1084
+
1085
+ def test_hard_link_returns_duplicate_file
1086
+ File.open("/foo", "w") { |x| x << "some content" }
1087
+
1088
+ File.link("/foo", "/bar")
1089
+ assert_equal "some content", File.read("/bar")
1090
+ end
1091
+
1092
+ def test_hard_link_with_directory_raises_error
1093
+ Dir.mkdir "/foo"
1094
+
1095
+ assert_raises(Errno::EPERM) do
1096
+ File.link("/foo", "/bar")
1097
+ end
1098
+ end
1099
+
1100
+ def test_file_stat_returns_file_stat_object
1101
+ FileUtils.touch("/foo")
1102
+ assert_equal File::Stat, File.stat("/foo").class
1103
+ end
1104
+
1105
+ def test_can_delete_file_with_delete
1106
+ FileUtils.touch("/foo")
1107
+
1108
+ File.delete("/foo")
1109
+
1110
+ assert !File.exists?("/foo")
1111
+ end
1112
+
1113
+ def test_can_delete_multiple_files_with_delete
1114
+ FileUtils.touch("/foo")
1115
+ FileUtils.touch("/bar")
1116
+
1117
+ File.delete("/foo", "/bar")
1118
+
1119
+ assert !File.exists?("/foo")
1120
+ assert !File.exists?("/bar")
1121
+ end
1122
+
1123
+ def test_delete_raises_argument_error_with_no_filename_given
1124
+ assert_raises ArgumentError do
1125
+ File.delete
1126
+ end
1127
+ end
1128
+
1129
+ def test_delete_returns_number_one_when_given_one_arg
1130
+ FileUtils.touch("/foo")
1131
+
1132
+ assert_equal 1, File.delete("/foo")
1133
+ end
1134
+
1135
+ def test_delete_returns_number_two_when_given_two_args
1136
+ FileUtils.touch("/foo")
1137
+ FileUtils.touch("/bar")
1138
+
1139
+ assert_equal 2, File.delete("/foo", "/bar")
1140
+ end
1141
+
1142
+ def test_delete_raises_error_when_first_file_does_not_exist
1143
+ assert_raises Errno::ENOENT do
1144
+ File.delete("/foo")
1145
+ end
1146
+ end
1147
+
1148
+ def test_delete_does_not_raise_error_when_second_file_does_not_exist
1149
+ FileUtils.touch("/foo")
1150
+
1151
+ assert_nothing_raised do
1152
+ File.delete("/foo", "/bar")
1153
+ end
1154
+ end
1155
+
1156
+ def test_unlink_is_alias_for_delete
1157
+ assert_equal File.method(:unlink), File.method(:delete)
1158
+ end
1159
+
1160
+ def test_unlink_removes_only_one_file_content
1161
+ File.open("/foo", "w") { |f| f << "some_content" }
1162
+ File.link("/foo", "/bar")
1163
+
1164
+ File.unlink("/bar")
1165
+ File.read("/foo") == "some_content"
1166
+ end
1167
+
1168
+ def test_link_reports_correct_stat_info_after_unlinking
1169
+ File.open("/foo", "w") { |f| f << "some_content" }
1170
+ File.link("/foo", "/bar")
1171
+
1172
+ File.unlink("/bar")
1173
+ assert_equal 1, File.stat("/foo").nlink
1174
+ end
1175
+
1176
+ def test_delete_works_with_symlink
1177
+ FileUtils.touch("/foo")
1178
+ File.symlink("/foo", "/bar")
1179
+
1180
+ File.unlink("/bar")
1181
+
1182
+ assert File.exists?("/foo")
1183
+ assert !File.exists?("/bar")
1184
+ end
1185
+
1186
+ def test_delete_works_with_symlink_source
1187
+ FileUtils.touch("/foo")
1188
+ File.symlink("/foo", "/bar")
1189
+
1190
+ File.unlink("/foo")
1191
+
1192
+ assert !File.exists?("/foo")
1193
+ end
1194
+
1195
+ def test_file_seek_returns_0
1196
+ File.open("/foo", "w") do |f|
1197
+ f << "one\ntwo\nthree"
1198
+ end
1199
+
1200
+ file = File.open("/foo", "r")
1201
+
1202
+ assert_equal 0, file.seek(1)
1203
+ end
1204
+
1205
+ def test_file_seek_seeks_to_location
1206
+ File.open("/foo", "w") do |f|
1207
+ f << "123"
1208
+ end
1209
+
1210
+ file = File.open("/foo", "r")
1211
+ file.seek(1)
1212
+ assert_equal "23", file.read
1213
+ end
1214
+
1215
+ def test_file_seek_seeks_to_correct_location
1216
+ File.open("/foo", "w") do |f|
1217
+ f << "123"
1218
+ end
1219
+
1220
+ file = File.open("/foo", "r")
1221
+ file.seek(2)
1222
+ assert_equal "3", file.read
1223
+ end
1224
+
1225
+ def test_file_seek_can_take_negative_offset
1226
+ File.open("/foo", "w") do |f|
1227
+ f << "123456789"
1228
+ end
1229
+
1230
+ file = File.open("/foo", "r")
1231
+
1232
+ file.seek(-1, IO::SEEK_END)
1233
+ assert_equal "9", file.read
1234
+
1235
+ file.seek(-2, IO::SEEK_END)
1236
+ assert_equal "89", file.read
1237
+
1238
+ file.seek(-3, IO::SEEK_END)
1239
+ assert_equal "789", file.read
1240
+ end
1241
+
1242
+ def test_should_have_constants_inherited_from_descending_from_io
1243
+ assert_equal IO::SEEK_CUR, File::SEEK_CUR
1244
+ assert_equal IO::SEEK_END, File::SEEK_END
1245
+ assert_equal IO::SEEK_SET, File::SEEK_SET
533
1246
  end
534
1247
 
535
1248
  def here(fname)