fakefs 0.2.1 → 0.3.2
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/.autotest +5 -0
- data/.rspec +1 -0
- data/CONTRIBUTORS +30 -12
- data/Gemfile +8 -0
- data/Gemfile.lock +35 -0
- data/README.markdown +29 -7
- data/Rakefile +20 -8
- data/fakefs.gemspec +84 -0
- data/lib/fakefs/base.rb +16 -9
- data/lib/fakefs/dir.rb +50 -2
- data/lib/fakefs/fake/dir.rb +4 -1
- data/lib/fakefs/fake/file.rb +3 -1
- data/lib/fakefs/fake/symlink.rb +1 -1
- data/lib/fakefs/file.rb +160 -35
- data/lib/fakefs/file_system.rb +5 -2
- data/lib/fakefs/file_test.rb +11 -0
- data/lib/fakefs/fileutils.rb +23 -11
- data/lib/fakefs/safe.rb +1 -1
- data/lib/fakefs/version.rb +1 -1
- data/spec/fakefs/spec_helpers_spec.rb +1 -1
- data/spec/spec.opts +1 -1
- data/spec/spec_helper.rb +1 -1
- data/test/fake/file/join_test.rb +19 -0
- data/test/fake/file/lstat_test.rb +59 -0
- data/test/fake/file/stat_test.rb +39 -0
- data/test/fake/file/sysseek_test.rb +44 -0
- data/test/fake/file/syswrite_test.rb +62 -0
- data/test/fake/file_test.rb +15 -6
- data/test/fake/symlink_test.rb +1 -3
- data/test/fakefs_test.rb +427 -23
- data/test/file/stat_test.rb +6 -3
- data/test/safe_test.rb +9 -7
- data/test/test_helper.rb +8 -0
- data/test/verify.rb +22 -18
- metadata +71 -20
- data/.gitignore +0 -1
data/test/fakefs_test.rb
CHANGED
|
@@ -1,14 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
require 'fakefs'
|
|
3
|
-
require 'test/unit'
|
|
1
|
+
require "test_helper"
|
|
4
2
|
|
|
5
3
|
class FakeFSTest < Test::Unit::TestCase
|
|
6
4
|
include FakeFS
|
|
7
5
|
|
|
8
6
|
def setup
|
|
7
|
+
FakeFS.activate!
|
|
9
8
|
FileSystem.clear
|
|
10
9
|
end
|
|
11
10
|
|
|
11
|
+
def teardown
|
|
12
|
+
FakeFS.deactivate!
|
|
13
|
+
end
|
|
14
|
+
|
|
12
15
|
def test_can_be_initialized_empty
|
|
13
16
|
fs = FileSystem
|
|
14
17
|
assert_equal 0, fs.files.size
|
|
@@ -20,16 +23,48 @@ class FakeFSTest < Test::Unit::TestCase
|
|
|
20
23
|
assert_equal 1, fs.files.size
|
|
21
24
|
end
|
|
22
25
|
|
|
23
|
-
def
|
|
26
|
+
def test_can_create_directories_with_file_utils_mkdir_p
|
|
24
27
|
FileUtils.mkdir_p("/path/to/dir")
|
|
25
28
|
assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']
|
|
26
29
|
end
|
|
27
|
-
|
|
30
|
+
|
|
31
|
+
def test_can_create_directories_with_options
|
|
32
|
+
FileUtils.mkdir_p("/path/to/dir", :mode => 0755)
|
|
33
|
+
assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def test_can_create_directories_with_file_utils_mkdir
|
|
37
|
+
FileUtils.mkdir_p("/path/to/dir")
|
|
38
|
+
FileUtils.mkdir("/path/to/dir/subdir")
|
|
39
|
+
assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']['subdir']
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def test_raises_error_when_creating_a_new_dir_with_mkdir_in_non_existent_path
|
|
43
|
+
assert_raises Errno::ENOENT do
|
|
44
|
+
FileUtils.mkdir("/this/path/does/not/exists/newdir")
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
28
48
|
def test_can_create_directories_with_mkpath
|
|
29
49
|
FileUtils.mkpath("/path/to/dir")
|
|
30
50
|
assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']
|
|
31
51
|
end
|
|
32
52
|
|
|
53
|
+
def test_can_create_directories_with_mkpath_and_options
|
|
54
|
+
FileUtils.mkpath("/path/to/dir", :mode => 0755)
|
|
55
|
+
assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def test_can_create_directories_with_mkpath
|
|
59
|
+
FileUtils.makedirs("/path/to/dir")
|
|
60
|
+
assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def test_can_create_directories_with_mkpath_and_options
|
|
64
|
+
FileUtils.makedirs("/path/to/dir", :mode => 0755)
|
|
65
|
+
assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']
|
|
66
|
+
end
|
|
67
|
+
|
|
33
68
|
def test_can_delete_directories
|
|
34
69
|
FileUtils.mkdir_p("/path/to/dir")
|
|
35
70
|
FileUtils.rmdir("/path/to/dir")
|
|
@@ -37,6 +72,13 @@ class FakeFSTest < Test::Unit::TestCase
|
|
|
37
72
|
assert File.exists?("/path/to/dir") == false
|
|
38
73
|
end
|
|
39
74
|
|
|
75
|
+
def test_can_delete_multiple_files
|
|
76
|
+
FileUtils.touch(["foo", "bar"])
|
|
77
|
+
FileUtils.rm(["foo", "bar"])
|
|
78
|
+
assert File.exists?("foo") == false
|
|
79
|
+
assert File.exists?("bar") == false
|
|
80
|
+
end
|
|
81
|
+
|
|
40
82
|
def test_knows_directories_exist
|
|
41
83
|
FileUtils.mkdir_p(path = "/path/to/dir")
|
|
42
84
|
assert File.exists?(path)
|
|
@@ -63,6 +105,10 @@ class FakeFSTest < Test::Unit::TestCase
|
|
|
63
105
|
assert File.exists?(path)
|
|
64
106
|
FileUtils.mkdir_p("/path/to")
|
|
65
107
|
assert File.exists?(path)
|
|
108
|
+
assert_raises Errno::EEXIST do
|
|
109
|
+
FileUtils.mkdir("/path/to")
|
|
110
|
+
end
|
|
111
|
+
assert File.exists?(path)
|
|
66
112
|
end
|
|
67
113
|
|
|
68
114
|
def test_can_create_symlinks
|
|
@@ -74,14 +120,14 @@ class FakeFSTest < Test::Unit::TestCase
|
|
|
74
120
|
FileUtils.ln_s(target, '/path/to/link')
|
|
75
121
|
end
|
|
76
122
|
end
|
|
77
|
-
|
|
123
|
+
|
|
78
124
|
def test_can_force_creation_of_symlinks
|
|
79
125
|
FileUtils.mkdir_p(target = "/path/to/first/target")
|
|
80
126
|
FileUtils.ln_s(target, "/path/to/link")
|
|
81
127
|
assert_kind_of FakeSymlink, FileSystem.fs['path']['to']['link']
|
|
82
128
|
FileUtils.ln_s(target, '/path/to/link', :force => true)
|
|
83
129
|
end
|
|
84
|
-
|
|
130
|
+
|
|
85
131
|
def test_create_symlink_using_ln_sf
|
|
86
132
|
FileUtils.mkdir_p(target = "/path/to/first/target")
|
|
87
133
|
FileUtils.ln_s(target, "/path/to/link")
|
|
@@ -288,7 +334,7 @@ class FakeFSTest < Test::Unit::TestCase
|
|
|
288
334
|
assert_nil File.size?("/path/to/other.txt")
|
|
289
335
|
end
|
|
290
336
|
|
|
291
|
-
def
|
|
337
|
+
def test_can_check_size_of_empty_file
|
|
292
338
|
path = '/path/to/file.txt'
|
|
293
339
|
File.open(path, 'w') do |f|
|
|
294
340
|
f << ''
|
|
@@ -310,6 +356,86 @@ class FakeFSTest < Test::Unit::TestCase
|
|
|
310
356
|
assert File.mtime('/path/to/file.txt').is_a?(Time)
|
|
311
357
|
end
|
|
312
358
|
|
|
359
|
+
def test_raises_error_on_ctime_if_file_does_not_exist
|
|
360
|
+
assert_raise Errno::ENOENT do
|
|
361
|
+
File.ctime('/path/to/file.txt')
|
|
362
|
+
end
|
|
363
|
+
end
|
|
364
|
+
|
|
365
|
+
def test_can_return_ctime_on_existing_file
|
|
366
|
+
File.open("foo", "w") { |f| f << "some content" }
|
|
367
|
+
assert File.ctime('foo').is_a?(Time)
|
|
368
|
+
end
|
|
369
|
+
|
|
370
|
+
def test_ctime_and_mtime_are_equal_for_new_files
|
|
371
|
+
File.open("foo", "w") { |f| f << "some content" }
|
|
372
|
+
ctime = File.ctime("foo")
|
|
373
|
+
mtime = File.mtime("foo")
|
|
374
|
+
assert ctime.is_a?(Time)
|
|
375
|
+
assert mtime.is_a?(Time)
|
|
376
|
+
assert_equal ctime, mtime
|
|
377
|
+
|
|
378
|
+
File.open("foo", "r") do |f|
|
|
379
|
+
assert_equal ctime, f.ctime
|
|
380
|
+
assert_equal mtime, f.mtime
|
|
381
|
+
end
|
|
382
|
+
end
|
|
383
|
+
|
|
384
|
+
def test_ctime_and_mtime_are_equal_for_new_directories
|
|
385
|
+
FileUtils.mkdir_p("foo")
|
|
386
|
+
ctime = File.ctime("foo")
|
|
387
|
+
mtime = File.mtime("foo")
|
|
388
|
+
assert ctime.is_a?(Time)
|
|
389
|
+
assert mtime.is_a?(Time)
|
|
390
|
+
assert_equal ctime, mtime
|
|
391
|
+
end
|
|
392
|
+
|
|
393
|
+
def test_file_ctime_is_equal_to_file_stat_ctime
|
|
394
|
+
File.open("foo", "w") { |f| f << "some content" }
|
|
395
|
+
assert_equal File.stat("foo").ctime, File.ctime("foo")
|
|
396
|
+
end
|
|
397
|
+
|
|
398
|
+
def test_directory_ctime_is_equal_to_directory_stat_ctime
|
|
399
|
+
FileUtils.mkdir_p("foo")
|
|
400
|
+
assert_equal File.stat("foo").ctime, File.ctime("foo")
|
|
401
|
+
end
|
|
402
|
+
|
|
403
|
+
def test_file_mtime_is_equal_to_file_stat_mtime
|
|
404
|
+
File.open("foo", "w") { |f| f << "some content" }
|
|
405
|
+
assert_equal File.stat("foo").mtime, File.mtime("foo")
|
|
406
|
+
end
|
|
407
|
+
|
|
408
|
+
def test_directory_mtime_is_equal_to_directory_stat_mtime
|
|
409
|
+
FileUtils.mkdir_p("foo")
|
|
410
|
+
assert_equal File.stat("foo").mtime, File.mtime("foo")
|
|
411
|
+
end
|
|
412
|
+
|
|
413
|
+
def test_utime_raises_error_if_path_does_not_exist
|
|
414
|
+
assert_raise Errno::ENOENT do
|
|
415
|
+
File.utime(Time.now, Time.now, '/path/to/file.txt')
|
|
416
|
+
end
|
|
417
|
+
end
|
|
418
|
+
|
|
419
|
+
def test_can_call_utime_on_an_existing_file
|
|
420
|
+
time = Time.now - 300 # Not now
|
|
421
|
+
path = '/path/to/file.txt'
|
|
422
|
+
File.open(path, 'w') do |f|
|
|
423
|
+
f << ''
|
|
424
|
+
end
|
|
425
|
+
File.utime(time, time, path)
|
|
426
|
+
assert_equal time, File.mtime('/path/to/file.txt')
|
|
427
|
+
end
|
|
428
|
+
|
|
429
|
+
def test_utime_returns_number_of_paths
|
|
430
|
+
path1, path2 = '/path/to/file.txt', '/path/to/another_file.txt'
|
|
431
|
+
[path1, path2].each do |path|
|
|
432
|
+
File.open(path, 'w') do |f|
|
|
433
|
+
f << ''
|
|
434
|
+
end
|
|
435
|
+
end
|
|
436
|
+
assert_equal 2, File.utime(Time.now, Time.now, path1, path2)
|
|
437
|
+
end
|
|
438
|
+
|
|
313
439
|
def test_can_read_with_File_readlines
|
|
314
440
|
path = '/path/to/file.txt'
|
|
315
441
|
File.open(path, 'w') do |f|
|
|
@@ -330,6 +456,16 @@ class FakeFSTest < Test::Unit::TestCase
|
|
|
330
456
|
end
|
|
331
457
|
end
|
|
332
458
|
|
|
459
|
+
def test_File_close_disallows_further_writes
|
|
460
|
+
path = '/path/to/file.txt'
|
|
461
|
+
file = File.open(path, 'w')
|
|
462
|
+
file.write 'Yada'
|
|
463
|
+
file.close
|
|
464
|
+
assert_raise IOError do
|
|
465
|
+
file << "foo"
|
|
466
|
+
end
|
|
467
|
+
end
|
|
468
|
+
|
|
333
469
|
def test_can_read_from_file_objects
|
|
334
470
|
path = '/path/to/file.txt'
|
|
335
471
|
File.open(path, 'w') do |f|
|
|
@@ -354,6 +490,16 @@ class FakeFSTest < Test::Unit::TestCase
|
|
|
354
490
|
assert File.file?(path)
|
|
355
491
|
end
|
|
356
492
|
|
|
493
|
+
def test_File_io_returns_self
|
|
494
|
+
f = File.open("/foo", "w")
|
|
495
|
+
assert_equal f, f.to_io
|
|
496
|
+
end
|
|
497
|
+
|
|
498
|
+
def test_File_to_i_is_alias_for_filno
|
|
499
|
+
f = File.open("/foo", "w")
|
|
500
|
+
assert_equal f.method(:to_i), f.method(:fileno)
|
|
501
|
+
end
|
|
502
|
+
|
|
357
503
|
def test_knows_symlink_files_are_files
|
|
358
504
|
path = '/path/to/file.txt'
|
|
359
505
|
File.open(path, 'w') do |f|
|
|
@@ -418,6 +564,12 @@ class FakeFSTest < Test::Unit::TestCase
|
|
|
418
564
|
assert_equal ['/path/foo', '/path/foobar'], Dir['/p*h/foo*']
|
|
419
565
|
assert_equal ['/path/foo', '/path/foobar'], Dir['/p??h/foo*']
|
|
420
566
|
|
|
567
|
+
assert_equal ['/path/bar', '/path/bar/baz', '/path/bar2', '/path/bar2/baz', '/path/foo', '/path/foobar'], Dir['/path/**/*']
|
|
568
|
+
assert_equal ['/path', '/path/bar', '/path/bar/baz', '/path/bar2', '/path/bar2/baz', '/path/foo', '/path/foobar'], Dir['/**/*']
|
|
569
|
+
|
|
570
|
+
assert_equal ['/path/bar', '/path/bar/baz', '/path/bar2', '/path/bar2/baz', '/path/foo', '/path/foobar'], Dir['/path/**/*']
|
|
571
|
+
assert_equal ['/path/bar/baz'], Dir['/path/bar/**/*']
|
|
572
|
+
|
|
421
573
|
FileUtils.cp_r '/path', '/otherpath'
|
|
422
574
|
|
|
423
575
|
assert_equal %w( /otherpath/foo /otherpath/foobar /path/foo /path/foobar ), Dir['/*/foo*']
|
|
@@ -438,11 +590,84 @@ class FakeFSTest < Test::Unit::TestCase
|
|
|
438
590
|
assert_equal ['/one/two/three'], Dir['/one/**/three']
|
|
439
591
|
end
|
|
440
592
|
|
|
441
|
-
def
|
|
593
|
+
def test_dir_recursive_glob_ending_in_wildcards_returns_both_files_and_dirs
|
|
442
594
|
File.open('/one/two/three/four.rb', 'w')
|
|
443
595
|
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
|
|
596
|
+
assert_equal ['/one/five.rb', '/one/two', '/one/two/three', '/one/two/three/four.rb'], Dir['/one/**/*']
|
|
597
|
+
assert_equal ['/one/five.rb', '/one/two'], Dir['/one/**']
|
|
598
|
+
end
|
|
599
|
+
|
|
600
|
+
def test_dir_glob_with_block
|
|
601
|
+
FileUtils.touch('foo')
|
|
602
|
+
FileUtils.touch('bar')
|
|
603
|
+
|
|
604
|
+
yielded = []
|
|
605
|
+
Dir.glob('*') { |file| yielded << file }
|
|
606
|
+
|
|
607
|
+
assert_equal 2, yielded.size
|
|
608
|
+
end
|
|
609
|
+
|
|
610
|
+
def test_should_report_pos_as_0_when_opening
|
|
611
|
+
File.open("/foo", "w") do |f|
|
|
612
|
+
f << "foobar"
|
|
613
|
+
f.rewind
|
|
614
|
+
|
|
615
|
+
assert_equal 0, f.pos
|
|
616
|
+
end
|
|
617
|
+
end
|
|
618
|
+
|
|
619
|
+
def test_should_report_pos_as_1_when_seeking_one_char
|
|
620
|
+
File.open("/foo", "w") do |f|
|
|
621
|
+
f << "foobar"
|
|
622
|
+
|
|
623
|
+
f.rewind
|
|
624
|
+
f.seek(1)
|
|
625
|
+
|
|
626
|
+
assert_equal 1, f.pos
|
|
627
|
+
end
|
|
628
|
+
end
|
|
629
|
+
|
|
630
|
+
def test_should_set_pos
|
|
631
|
+
File.open("/foo", "w") do |f|
|
|
632
|
+
f << "foo"
|
|
633
|
+
end
|
|
634
|
+
|
|
635
|
+
fp = File.open("/foo", "r")
|
|
636
|
+
fp.pos = 1
|
|
637
|
+
|
|
638
|
+
assert_equal 1, fp.pos
|
|
639
|
+
end
|
|
640
|
+
|
|
641
|
+
def test_should_set_pos_with_tell_method
|
|
642
|
+
File.open("/foo", "w") do |f|
|
|
643
|
+
f << "foo"
|
|
644
|
+
end
|
|
645
|
+
|
|
646
|
+
fp = File.open("/foo", "r")
|
|
647
|
+
fp.tell = 1
|
|
648
|
+
|
|
649
|
+
assert_equal 1, fp.pos
|
|
650
|
+
end
|
|
651
|
+
|
|
652
|
+
# Every method in File is in FakeFS::File
|
|
653
|
+
RealFile.instance_methods.each do |method_name|
|
|
654
|
+
define_method "test_should_have_method_#{method_name}_from_real_file_class" do
|
|
655
|
+
assert File.instance_methods.include?(method_name)
|
|
656
|
+
end
|
|
657
|
+
end
|
|
658
|
+
|
|
659
|
+
# No methods which are in StringIO that aren't in File are included into
|
|
660
|
+
# FakeFS::File (because of inheritence)
|
|
661
|
+
uniq_string_io_methods = StringIO.instance_methods - RealFile.instance_methods
|
|
662
|
+
uniq_string_io_methods.each do |method_name|
|
|
663
|
+
define_method "test_file_should_not_respond_to_#{method_name}" do
|
|
664
|
+
assert !File.instance_methods.include?(method_name)
|
|
665
|
+
end
|
|
666
|
+
end
|
|
667
|
+
|
|
668
|
+
def test_does_not_remove_methods_from_stringio
|
|
669
|
+
stringio = StringIO.new("foo")
|
|
670
|
+
assert stringio.respond_to?(:size)
|
|
446
671
|
end
|
|
447
672
|
|
|
448
673
|
def test_chdir_changes_directories_like_a_boss
|
|
@@ -577,6 +802,10 @@ class FakeFSTest < Test::Unit::TestCase
|
|
|
577
802
|
assert_raise(Errno::ENOENT) do
|
|
578
803
|
FileUtils.mv 'blafgag', 'foo'
|
|
579
804
|
end
|
|
805
|
+
exception = assert_raise(Errno::ENOENT) do
|
|
806
|
+
FileUtils.mv ['foo', 'bar'], 'destdir'
|
|
807
|
+
end
|
|
808
|
+
assert_equal "No such file or directory - foo", exception.message
|
|
580
809
|
end
|
|
581
810
|
|
|
582
811
|
def test_mv_actually_works
|
|
@@ -585,6 +814,29 @@ class FakeFSTest < Test::Unit::TestCase
|
|
|
585
814
|
assert_equal 'bar', File.open('baz') { |f| f.read }
|
|
586
815
|
end
|
|
587
816
|
|
|
817
|
+
def test_mv_works_with_options
|
|
818
|
+
File.open('foo', 'w') {|f| f.write 'bar'}
|
|
819
|
+
FileUtils.mv 'foo', 'baz', :force => true
|
|
820
|
+
assert_equal('bar', File.open('baz') { |f| f.read })
|
|
821
|
+
end
|
|
822
|
+
|
|
823
|
+
def test_mv_to_directory
|
|
824
|
+
File.open('foo', 'w') {|f| f.write 'bar'}
|
|
825
|
+
FileUtils.mkdir_p 'destdir'
|
|
826
|
+
FileUtils.mv 'foo', 'destdir'
|
|
827
|
+
assert_equal('bar', File.open('destdir/foo') {|f| f.read })
|
|
828
|
+
assert File.directory?('destdir')
|
|
829
|
+
end
|
|
830
|
+
|
|
831
|
+
def test_mv_array
|
|
832
|
+
File.open('foo', 'w') {|f| f.write 'bar' }
|
|
833
|
+
File.open('baz', 'w') {|f| f.write 'binky' }
|
|
834
|
+
FileUtils.mkdir_p 'destdir'
|
|
835
|
+
FileUtils.mv %w(foo baz), 'destdir'
|
|
836
|
+
assert_equal('bar', File.open('destdir/foo') {|f| f.read })
|
|
837
|
+
assert_equal('binky', File.open('destdir/baz') {|f| f.read })
|
|
838
|
+
end
|
|
839
|
+
|
|
588
840
|
def test_cp_actually_works
|
|
589
841
|
File.open('foo', 'w') {|f| f.write 'bar' }
|
|
590
842
|
FileUtils.cp('foo', 'baz')
|
|
@@ -695,25 +947,34 @@ class FakeFSTest < Test::Unit::TestCase
|
|
|
695
947
|
end
|
|
696
948
|
|
|
697
949
|
def test_clone_clones_directories
|
|
950
|
+
FakeFS.deactivate!
|
|
698
951
|
RealFileUtils.mkdir_p(here('subdir'))
|
|
952
|
+
FakeFS.activate!
|
|
699
953
|
|
|
700
954
|
FileSystem.clone(here('subdir'))
|
|
701
955
|
|
|
702
956
|
assert File.exists?(here('subdir')), 'subdir was cloned'
|
|
703
957
|
assert File.directory?(here('subdir')), 'subdir is a directory'
|
|
704
958
|
ensure
|
|
705
|
-
|
|
959
|
+
FakeFS.deactivate!
|
|
960
|
+
RealFileUtils.rm_rf(here('subdir'))
|
|
961
|
+
FakeFS.activate!
|
|
706
962
|
end
|
|
707
963
|
|
|
708
964
|
def test_clone_clones_dot_files_even_hard_to_find_ones
|
|
965
|
+
FakeFS.deactivate!
|
|
709
966
|
RealFileUtils.mkdir_p(here('subdir/.bar/baz/.quux/foo'))
|
|
967
|
+
FakeFS.activate!
|
|
968
|
+
|
|
710
969
|
assert !File.exists?(here('subdir'))
|
|
711
970
|
|
|
712
971
|
FileSystem.clone(here('subdir'))
|
|
713
972
|
assert_equal ['.bar'], FileSystem.find(here('subdir')).keys
|
|
714
973
|
assert_equal ['foo'], FileSystem.find(here('subdir/.bar/baz/.quux')).keys
|
|
715
974
|
ensure
|
|
716
|
-
|
|
975
|
+
FakeFS.deactivate!
|
|
976
|
+
RealFileUtils.rm_rf(here('subdir'))
|
|
977
|
+
FakeFS.activate!
|
|
717
978
|
end
|
|
718
979
|
|
|
719
980
|
def test_putting_a_dot_at_end_copies_the_contents
|
|
@@ -1019,6 +1280,14 @@ class FakeFSTest < Test::Unit::TestCase
|
|
|
1019
1280
|
end
|
|
1020
1281
|
end
|
|
1021
1282
|
|
|
1283
|
+
def test_mkdir_raises_error_if_already_created
|
|
1284
|
+
Dir.mkdir "foo"
|
|
1285
|
+
|
|
1286
|
+
assert_raises(Errno::EEXIST) do
|
|
1287
|
+
Dir.mkdir "foo"
|
|
1288
|
+
end
|
|
1289
|
+
end
|
|
1290
|
+
|
|
1022
1291
|
def test_directory_open
|
|
1023
1292
|
test = ['.', '..', 'file_1', 'file_2', 'file_3', 'file_4', 'file_5' ]
|
|
1024
1293
|
|
|
@@ -1053,38 +1322,92 @@ class FakeFSTest < Test::Unit::TestCase
|
|
|
1053
1322
|
def test_tmpdir
|
|
1054
1323
|
assert Dir.tmpdir == "/tmp"
|
|
1055
1324
|
end
|
|
1056
|
-
|
|
1325
|
+
|
|
1326
|
+
def test_rename_renames_a_file
|
|
1327
|
+
FileUtils.touch("/foo")
|
|
1328
|
+
File.rename("/foo", "/bar")
|
|
1329
|
+
assert File.file?("/bar")
|
|
1330
|
+
end
|
|
1331
|
+
|
|
1332
|
+
def test_rename_returns
|
|
1333
|
+
FileUtils.touch("/foo")
|
|
1334
|
+
assert_equal 0, File.rename("/foo", "/bar")
|
|
1335
|
+
end
|
|
1336
|
+
|
|
1337
|
+
def test_rename_renames_two_files
|
|
1338
|
+
FileUtils.touch("/foo")
|
|
1339
|
+
FileUtils.touch("/bar")
|
|
1340
|
+
File.rename("/foo", "/bar")
|
|
1341
|
+
assert File.file?("/bar")
|
|
1342
|
+
end
|
|
1343
|
+
|
|
1344
|
+
def test_rename_renames_a_directories
|
|
1345
|
+
Dir.mkdir("/foo")
|
|
1346
|
+
File.rename("/foo", "/bar")
|
|
1347
|
+
assert File.directory?("/bar")
|
|
1348
|
+
end
|
|
1349
|
+
|
|
1350
|
+
def test_rename_renames_two_directories
|
|
1351
|
+
Dir.mkdir("/foo")
|
|
1352
|
+
Dir.mkdir("/bar")
|
|
1353
|
+
File.rename("/foo", "/bar")
|
|
1354
|
+
assert File.directory?("/bar")
|
|
1355
|
+
end
|
|
1356
|
+
|
|
1357
|
+
def test_rename_file_to_directory_raises_error
|
|
1358
|
+
FileUtils.touch("/foo")
|
|
1359
|
+
Dir.mkdir("/bar")
|
|
1360
|
+
assert_raises(Errno::EISDIR) do
|
|
1361
|
+
File.rename("/foo", "/bar")
|
|
1362
|
+
end
|
|
1363
|
+
end
|
|
1364
|
+
|
|
1365
|
+
def test_rename_directory_to_file_raises_error
|
|
1366
|
+
Dir.mkdir("/foo")
|
|
1367
|
+
FileUtils.touch("/bar")
|
|
1368
|
+
assert_raises(Errno::ENOTDIR) do
|
|
1369
|
+
File.rename("/foo", "/bar")
|
|
1370
|
+
end
|
|
1371
|
+
end
|
|
1372
|
+
|
|
1373
|
+
|
|
1374
|
+
def test_rename_with_missing_source_raises_error
|
|
1375
|
+
assert_raises(Errno::ENOENT) do
|
|
1376
|
+
File.rename("/no_such_file", "/bar")
|
|
1377
|
+
end
|
|
1378
|
+
end
|
|
1379
|
+
|
|
1057
1380
|
def test_hard_link_creates_file
|
|
1058
1381
|
FileUtils.touch("/foo")
|
|
1059
|
-
|
|
1382
|
+
|
|
1060
1383
|
File.link("/foo", "/bar")
|
|
1061
1384
|
assert File.exists?("/bar")
|
|
1062
1385
|
end
|
|
1063
|
-
|
|
1386
|
+
|
|
1064
1387
|
def test_hard_link_with_missing_file_raises_error
|
|
1065
1388
|
assert_raises(Errno::ENOENT) do
|
|
1066
1389
|
File.link("/foo", "/bar")
|
|
1067
1390
|
end
|
|
1068
1391
|
end
|
|
1069
|
-
|
|
1392
|
+
|
|
1070
1393
|
def test_hard_link_with_existing_destination_file
|
|
1071
1394
|
FileUtils.touch("/foo")
|
|
1072
1395
|
FileUtils.touch("/bar")
|
|
1073
|
-
|
|
1396
|
+
|
|
1074
1397
|
assert_raises(Errno::EEXIST) do
|
|
1075
1398
|
File.link("/foo", "/bar")
|
|
1076
1399
|
end
|
|
1077
1400
|
end
|
|
1078
|
-
|
|
1401
|
+
|
|
1079
1402
|
def test_hard_link_returns_0_when_successful
|
|
1080
1403
|
FileUtils.touch("/foo")
|
|
1081
|
-
|
|
1404
|
+
|
|
1082
1405
|
assert_equal 0, File.link("/foo", "/bar")
|
|
1083
1406
|
end
|
|
1084
|
-
|
|
1407
|
+
|
|
1085
1408
|
def test_hard_link_returns_duplicate_file
|
|
1086
1409
|
File.open("/foo", "w") { |x| x << "some content" }
|
|
1087
|
-
|
|
1410
|
+
|
|
1088
1411
|
File.link("/foo", "/bar")
|
|
1089
1412
|
assert_equal "some content", File.read("/bar")
|
|
1090
1413
|
end
|
|
@@ -1245,7 +1568,88 @@ class FakeFSTest < Test::Unit::TestCase
|
|
|
1245
1568
|
assert_equal IO::SEEK_SET, File::SEEK_SET
|
|
1246
1569
|
end
|
|
1247
1570
|
|
|
1571
|
+
def test_filetest_exists_return_correct_values
|
|
1572
|
+
FileUtils.mkdir_p("/path/to/dir")
|
|
1573
|
+
assert FileTest.exist?("/path/to/")
|
|
1574
|
+
|
|
1575
|
+
FileUtils.rmdir("/path/to/dir")
|
|
1576
|
+
assert !FileTest.exist?("/path/to/dir")
|
|
1577
|
+
end
|
|
1578
|
+
|
|
1579
|
+
def test_filetest_directory_returns_correct_values
|
|
1580
|
+
FileUtils.mkdir_p '/path/to/somedir'
|
|
1581
|
+
assert FileTest.directory?('/path/to/somedir')
|
|
1582
|
+
|
|
1583
|
+
FileUtils.rm_r '/path/to/somedir'
|
|
1584
|
+
assert !FileTest.directory?('/path/to/somedir')
|
|
1585
|
+
end
|
|
1586
|
+
|
|
1587
|
+
def test_pathname_exists_returns_correct_value
|
|
1588
|
+
FileUtils.touch "foo"
|
|
1589
|
+
assert Pathname.new("foo").exist?
|
|
1590
|
+
|
|
1591
|
+
assert !Pathname.new("bar").exist?
|
|
1592
|
+
end
|
|
1593
|
+
|
|
1594
|
+
def test_dir_mktmpdir
|
|
1595
|
+
FileUtils.mkdir '/tmp'
|
|
1596
|
+
|
|
1597
|
+
tmpdir = Dir.mktmpdir
|
|
1598
|
+
assert File.directory?(tmpdir)
|
|
1599
|
+
FileUtils.rm_r tmpdir
|
|
1600
|
+
|
|
1601
|
+
Dir.mktmpdir do |t|
|
|
1602
|
+
tmpdir = t
|
|
1603
|
+
assert File.directory?(t)
|
|
1604
|
+
end
|
|
1605
|
+
assert !File.directory?(tmpdir)
|
|
1606
|
+
end
|
|
1607
|
+
|
|
1608
|
+
def test_activating_returns_true
|
|
1609
|
+
FakeFS.deactivate!
|
|
1610
|
+
assert_equal true, FakeFS.activate!
|
|
1611
|
+
end
|
|
1612
|
+
|
|
1613
|
+
def test_deactivating_returns_true
|
|
1614
|
+
assert_equal true, FakeFS.deactivate!
|
|
1615
|
+
end
|
|
1616
|
+
|
|
1617
|
+
def test_split
|
|
1618
|
+
assert File.respond_to? :split
|
|
1619
|
+
filename = "/this/is/what/we/expect.txt"
|
|
1620
|
+
path,filename = File.split(filename)
|
|
1621
|
+
assert_equal path, "/this/is/what/we"
|
|
1622
|
+
assert_equal filename, "expect.txt"
|
|
1623
|
+
end
|
|
1624
|
+
|
|
1625
|
+
|
|
1248
1626
|
def here(fname)
|
|
1249
|
-
RealFile.expand_path(RealFile.dirname(__FILE__)
|
|
1627
|
+
RealFile.expand_path(File.join(RealFile.dirname(__FILE__), fname))
|
|
1628
|
+
end
|
|
1629
|
+
|
|
1630
|
+
if RUBY_VERSION >= "1.9.2"
|
|
1631
|
+
def test_file_size
|
|
1632
|
+
File.open("foo", 'w') do |f|
|
|
1633
|
+
f << 'Yada Yada'
|
|
1634
|
+
assert_equal 9, f.size
|
|
1635
|
+
end
|
|
1636
|
+
end
|
|
1637
|
+
|
|
1638
|
+
def test_fdatasync
|
|
1639
|
+
File.open("foo", 'w') do |f|
|
|
1640
|
+
f << 'Yada Yada'
|
|
1641
|
+
assert_nothing_raised do
|
|
1642
|
+
f.fdatasync
|
|
1643
|
+
end
|
|
1644
|
+
end
|
|
1645
|
+
end
|
|
1646
|
+
|
|
1647
|
+
def test_autoclose
|
|
1648
|
+
File.open("foo", 'w') do |f|
|
|
1649
|
+
assert_equal true, f.autoclose?
|
|
1650
|
+
f.autoclose = false
|
|
1651
|
+
assert_equal false, f.autoclose?
|
|
1652
|
+
end
|
|
1653
|
+
end
|
|
1250
1654
|
end
|
|
1251
1655
|
end
|
data/test/file/stat_test.rb
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
require 'fakefs/safe'
|
|
3
|
-
require 'test/unit'
|
|
1
|
+
require "test_helper"
|
|
4
2
|
|
|
5
3
|
class FileStatTest < Test::Unit::TestCase
|
|
6
4
|
include FakeFS
|
|
@@ -67,4 +65,9 @@ class FileStatTest < Test::Unit::TestCase
|
|
|
67
65
|
|
|
68
66
|
assert_equal 2, File.stat("testfile").nlink
|
|
69
67
|
end
|
|
68
|
+
|
|
69
|
+
def test_file_size
|
|
70
|
+
File.open('testfile', 'w') { |f| f << 'test' }
|
|
71
|
+
assert_equal 4, File.stat('testfile').size
|
|
72
|
+
end
|
|
70
73
|
end
|
data/test/safe_test.rb
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
require 'fakefs/safe'
|
|
3
|
-
require 'test/unit'
|
|
1
|
+
require "test_helper"
|
|
4
2
|
|
|
5
3
|
class FakeFSSafeTest < Test::Unit::TestCase
|
|
6
4
|
def setup
|
|
7
5
|
FakeFS.deactivate!
|
|
8
6
|
end
|
|
9
7
|
|
|
8
|
+
def teardown
|
|
9
|
+
FakeFS.activate!
|
|
10
|
+
end
|
|
11
|
+
|
|
10
12
|
def test_FakeFS_method_does_not_intrude_on_global_namespace
|
|
11
13
|
path = '/path/to/file.txt'
|
|
12
14
|
|
|
@@ -26,7 +28,7 @@ class FakeFSSafeTest < Test::Unit::TestCase
|
|
|
26
28
|
|
|
27
29
|
assert_equal result, "Yatta!"
|
|
28
30
|
end
|
|
29
|
-
|
|
31
|
+
|
|
30
32
|
def test_FakeFS_method_deactivates_FakeFS_when_block_raises_exception
|
|
31
33
|
begin
|
|
32
34
|
FakeFS do
|
|
@@ -34,9 +36,9 @@ class FakeFSSafeTest < Test::Unit::TestCase
|
|
|
34
36
|
end
|
|
35
37
|
rescue
|
|
36
38
|
end
|
|
37
|
-
|
|
39
|
+
|
|
38
40
|
assert_equal RealFile, File, "File is #{File} (should be #{RealFile})"
|
|
39
|
-
assert_equal RealFileUtils, FileUtils, "FileUtils is #{FileUtils} (should be #{RealFileUtils})"
|
|
41
|
+
assert_equal RealFileUtils, FileUtils, "FileUtils is #{FileUtils} (should be #{RealFileUtils})"
|
|
40
42
|
assert_equal RealDir, Dir, "Dir is #{Dir} (should be #{RealDir})"
|
|
41
|
-
end
|
|
43
|
+
end
|
|
42
44
|
end
|