fakefs 0.4.0 → 0.4.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/.travis.yml +9 -0
- data/Gemfile +0 -1
- data/Gemfile.lock +31 -0
- data/README.markdown +65 -29
- data/Rakefile +16 -13
- data/fakefs.gemspec +11 -11
- data/lib/fakefs/base.rb +69 -32
- data/lib/fakefs/dir.rb +22 -15
- data/lib/fakefs/fake/dir.rb +36 -10
- data/lib/fakefs/fake/file.rb +7 -2
- data/lib/fakefs/file.rb +90 -17
- data/lib/fakefs/file_system.rb +18 -10
- data/lib/fakefs/fileutils.rb +84 -41
- data/lib/fakefs/pathname.rb +864 -0
- data/lib/fakefs/safe.rb +2 -0
- data/lib/fakefs/version.rb +1 -1
- data/test/fakefs_test.rb +355 -49
- data/test/file/stat_test.rb +36 -0
- data/test/safe_test.rb +41 -3
- data/test/test_helper.rb +8 -1
- metadata +61 -60
data/lib/fakefs/safe.rb
CHANGED
data/lib/fakefs/version.rb
CHANGED
data/test/fakefs_test.rb
CHANGED
@@ -55,12 +55,12 @@ class FakeFSTest < Test::Unit::TestCase
|
|
55
55
|
assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']
|
56
56
|
end
|
57
57
|
|
58
|
-
def
|
58
|
+
def test_can_create_directories_with_mkdirs
|
59
59
|
FileUtils.makedirs("/path/to/dir")
|
60
60
|
assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']
|
61
61
|
end
|
62
62
|
|
63
|
-
def
|
63
|
+
def test_can_create_directories_with_mkdirs_and_options
|
64
64
|
FileUtils.makedirs("/path/to/dir", :mode => 0755)
|
65
65
|
assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']
|
66
66
|
end
|
@@ -71,6 +71,12 @@ class FakeFSTest < Test::Unit::TestCase
|
|
71
71
|
end
|
72
72
|
end
|
73
73
|
|
74
|
+
def test_unlink_doesnt_error_on_file_not_found_when_forced
|
75
|
+
assert_nothing_raised do
|
76
|
+
FileUtils.rm("/foo", :force => true)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
74
80
|
def test_can_delete_directories
|
75
81
|
FileUtils.mkdir_p("/path/to/dir")
|
76
82
|
FileUtils.rmdir("/path/to/dir")
|
@@ -95,6 +101,13 @@ class FakeFSTest < Test::Unit::TestCase
|
|
95
101
|
assert File.directory?(path)
|
96
102
|
end
|
97
103
|
|
104
|
+
def test_knows_directories_are_directories_with_periods
|
105
|
+
FileUtils.mkdir_p(period_path = "/path/to/periodfiles/one.one")
|
106
|
+
FileUtils.mkdir("/path/to/periodfiles/one-one")
|
107
|
+
|
108
|
+
assert File.directory?(period_path)
|
109
|
+
end
|
110
|
+
|
98
111
|
def test_knows_symlink_directories_are_directories
|
99
112
|
FileUtils.mkdir_p(path = "/path/to/dir")
|
100
113
|
FileUtils.ln_s path, sympath = '/sympath'
|
@@ -412,6 +425,15 @@ class FakeFSTest < Test::Unit::TestCase
|
|
412
425
|
assert_equal "Yatta!", File.read(path)
|
413
426
|
end
|
414
427
|
|
428
|
+
def test_file_read_accepts_hashes
|
429
|
+
path = 'file.txt'
|
430
|
+
File.open(path, 'w') do |f|
|
431
|
+
f.write 'Yatta!'
|
432
|
+
end
|
433
|
+
|
434
|
+
assert_nothing_raised { File.read(path, :mode => 'r:UTF-8:-') }
|
435
|
+
end
|
436
|
+
|
415
437
|
def test_can_write_to_files
|
416
438
|
path = 'file.txt'
|
417
439
|
File.open(path, 'w') do |f|
|
@@ -495,27 +517,46 @@ class FakeFSTest < Test::Unit::TestCase
|
|
495
517
|
assert File.ctime('foo').is_a?(Time)
|
496
518
|
end
|
497
519
|
|
498
|
-
def
|
520
|
+
def test_raises_error_on_atime_if_file_does_not_exist
|
521
|
+
assert_raise Errno::ENOENT do
|
522
|
+
File.atime('file.txt')
|
523
|
+
end
|
524
|
+
end
|
525
|
+
|
526
|
+
def test_can_return_atime_on_existing_file
|
499
527
|
File.open("foo", "w") { |f| f << "some content" }
|
528
|
+
assert File.atime('foo').is_a?(Time)
|
529
|
+
end
|
530
|
+
|
531
|
+
def test_ctime_mtime_and_atime_are_equal_for_new_files
|
532
|
+
FileUtils.touch('foo')
|
533
|
+
|
500
534
|
ctime = File.ctime("foo")
|
501
535
|
mtime = File.mtime("foo")
|
536
|
+
atime = File.atime("foo")
|
502
537
|
assert ctime.is_a?(Time)
|
503
538
|
assert mtime.is_a?(Time)
|
539
|
+
assert atime.is_a?(Time)
|
504
540
|
assert_equal ctime, mtime
|
541
|
+
assert_equal ctime, atime
|
505
542
|
|
506
543
|
File.open("foo", "r") do |f|
|
507
544
|
assert_equal ctime, f.ctime
|
508
545
|
assert_equal mtime, f.mtime
|
546
|
+
assert_equal atime, f.atime
|
509
547
|
end
|
510
548
|
end
|
511
549
|
|
512
|
-
def
|
550
|
+
def test_ctime_mtime_and_atime_are_equal_for_new_directories
|
513
551
|
FileUtils.mkdir_p("foo")
|
514
552
|
ctime = File.ctime("foo")
|
515
553
|
mtime = File.mtime("foo")
|
554
|
+
atime = File.atime("foo")
|
516
555
|
assert ctime.is_a?(Time)
|
517
556
|
assert mtime.is_a?(Time)
|
557
|
+
assert atime.is_a?(Time)
|
518
558
|
assert_equal ctime, mtime
|
559
|
+
assert_equal ctime, atime
|
519
560
|
end
|
520
561
|
|
521
562
|
def test_file_ctime_is_equal_to_file_stat_ctime
|
@@ -538,6 +579,16 @@ class FakeFSTest < Test::Unit::TestCase
|
|
538
579
|
assert_equal File.stat("foo").mtime, File.mtime("foo")
|
539
580
|
end
|
540
581
|
|
582
|
+
def test_file_atime_is_equal_to_file_stat_atime
|
583
|
+
File.open("foo", "w") { |f| f << "some content" }
|
584
|
+
assert_equal File.stat("foo").atime, File.atime("foo")
|
585
|
+
end
|
586
|
+
|
587
|
+
def test_directory_atime_is_equal_to_directory_stat_atime
|
588
|
+
FileUtils.mkdir_p("foo")
|
589
|
+
assert_equal File.stat("foo").atime, File.atime("foo")
|
590
|
+
end
|
591
|
+
|
541
592
|
def test_utime_raises_error_if_path_does_not_exist
|
542
593
|
assert_raise Errno::ENOENT do
|
543
594
|
File.utime(Time.now, Time.now, '/path/to/file.txt')
|
@@ -552,6 +603,7 @@ class FakeFSTest < Test::Unit::TestCase
|
|
552
603
|
end
|
553
604
|
File.utime(time, time, path)
|
554
605
|
assert_equal time, File.mtime('file.txt')
|
606
|
+
assert_equal time, File.atime('file.txt')
|
555
607
|
end
|
556
608
|
|
557
609
|
def test_utime_returns_number_of_paths
|
@@ -564,6 +616,21 @@ class FakeFSTest < Test::Unit::TestCase
|
|
564
616
|
assert_equal 2, File.utime(Time.now, Time.now, path1, path2)
|
565
617
|
end
|
566
618
|
|
619
|
+
def test_file_a_time_updated_when_file_is_read
|
620
|
+
old_atime = Time.now - 300
|
621
|
+
|
622
|
+
path = "file.txt"
|
623
|
+
File.open(path, "w") do |f|
|
624
|
+
f << "Hello"
|
625
|
+
end
|
626
|
+
|
627
|
+
File.utime(old_atime, File.mtime(path), path)
|
628
|
+
|
629
|
+
assert_equal File.atime(path), old_atime
|
630
|
+
File.read(path)
|
631
|
+
assert File.atime(path) != old_atime
|
632
|
+
end
|
633
|
+
|
567
634
|
def test_can_read_with_File_readlines
|
568
635
|
path = 'file.txt'
|
569
636
|
File.open(path, 'w') do |f|
|
@@ -603,6 +670,21 @@ class FakeFSTest < Test::Unit::TestCase
|
|
603
670
|
assert_equal "Yatta!", File.new(path).read
|
604
671
|
end
|
605
672
|
|
673
|
+
if RUBY_VERSION >= "1.9"
|
674
|
+
def test_file_object_has_default_external_encoding
|
675
|
+
Encoding.default_external = "UTF-8"
|
676
|
+
path = 'file.txt'
|
677
|
+
File.open(path, 'w'){|f| f.write 'Yatta!' }
|
678
|
+
assert_equal "UTF-8", File.new(path).read.encoding.name
|
679
|
+
end
|
680
|
+
end
|
681
|
+
|
682
|
+
def test_file_object_initialization_with_mode_in_hash_parameter
|
683
|
+
assert_nothing_raised do
|
684
|
+
File.open("file.txt", {:mode => "w"}){ |f| f.write 'Yatta!' }
|
685
|
+
end
|
686
|
+
end
|
687
|
+
|
606
688
|
def test_file_read_errors_appropriately
|
607
689
|
assert_raise Errno::ENOENT do
|
608
690
|
File.read('anything')
|
@@ -646,30 +728,83 @@ class FakeFSTest < Test::Unit::TestCase
|
|
646
728
|
good = 'file.txt'
|
647
729
|
bad = 'nofile.txt'
|
648
730
|
File.open(good,'w') { |f| f.write "foo" }
|
731
|
+
username = Etc.getpwuid(Process.uid).name
|
732
|
+
groupname = Etc.getgrgid(Process.gid).name
|
649
733
|
|
650
|
-
out = FileUtils.chown(
|
734
|
+
out = FileUtils.chown(1337, 1338, good, :verbose => true)
|
651
735
|
assert_equal [good], out
|
736
|
+
assert_equal File.stat(good).uid, 1337
|
737
|
+
assert_equal File.stat(good).gid, 1338
|
652
738
|
assert_raises(Errno::ENOENT) do
|
653
|
-
FileUtils.chown(
|
739
|
+
FileUtils.chown(username, groupname, bad, :verbose => true)
|
654
740
|
end
|
655
741
|
|
656
|
-
assert_equal [good], FileUtils.chown(
|
742
|
+
assert_equal [good], FileUtils.chown(username, groupname, good)
|
743
|
+
assert_equal File.stat(good).uid, Process.uid
|
744
|
+
assert_equal File.stat(good).gid, Process.gid
|
657
745
|
assert_raises(Errno::ENOENT) do
|
658
|
-
FileUtils.chown(
|
746
|
+
FileUtils.chown(username, groupname, bad)
|
659
747
|
end
|
660
748
|
|
661
|
-
assert_equal [good], FileUtils.chown(
|
749
|
+
assert_equal [good], FileUtils.chown(username, groupname, [good])
|
750
|
+
assert_equal File.stat(good).uid, Process.uid
|
751
|
+
assert_equal File.stat(good).gid, Process.gid
|
662
752
|
assert_raises(Errno::ENOENT) do
|
663
|
-
FileUtils.chown(
|
753
|
+
FileUtils.chown(username, groupname, [good, bad])
|
664
754
|
end
|
665
755
|
end
|
666
756
|
|
667
757
|
def test_can_chown_R_files
|
758
|
+
username = Etc.getpwuid(Process.uid).name
|
759
|
+
groupname = Etc.getgrgid(Process.gid).name
|
668
760
|
FileUtils.mkdir_p '/path/'
|
669
761
|
File.open('/path/foo', 'w') { |f| f.write 'foo' }
|
670
762
|
File.open('/path/foobar', 'w') { |f| f.write 'foo' }
|
671
|
-
|
672
|
-
|
763
|
+
assert_equal ['/path'], FileUtils.chown_R(username, groupname, '/path')
|
764
|
+
%w(/path /path/foo /path/foobar).each do |f|
|
765
|
+
assert_equal File.stat(f).uid, Process.uid
|
766
|
+
assert_equal File.stat(f).gid, Process.gid
|
767
|
+
end
|
768
|
+
end
|
769
|
+
|
770
|
+
def test_can_chmod_files
|
771
|
+
good = "file.txt"
|
772
|
+
bad = "nofile.txt"
|
773
|
+
FileUtils.touch(good)
|
774
|
+
|
775
|
+
assert_equal [good], FileUtils.chmod(0600, good, :verbose => true)
|
776
|
+
assert_equal File.stat(good).mode, 0100600
|
777
|
+
assert_raises(Errno::ENOENT) do
|
778
|
+
FileUtils.chmod(0600, bad)
|
779
|
+
end
|
780
|
+
|
781
|
+
assert_equal [good], FileUtils.chmod(0666, good)
|
782
|
+
assert_equal File.stat(good).mode, 0100666
|
783
|
+
assert_raises(Errno::ENOENT) do
|
784
|
+
FileUtils.chmod(0666, bad)
|
785
|
+
end
|
786
|
+
|
787
|
+
assert_equal [good], FileUtils.chmod(0644, [good])
|
788
|
+
assert_equal File.stat(good).mode, 0100644
|
789
|
+
assert_raises(Errno::ENOENT) do
|
790
|
+
FileUtils.chmod(0644, bad)
|
791
|
+
end
|
792
|
+
end
|
793
|
+
|
794
|
+
def test_can_chmod_R_files
|
795
|
+
FileUtils.mkdir_p "/path/sub"
|
796
|
+
FileUtils.touch "/path/file1"
|
797
|
+
FileUtils.touch "/path/sub/file2"
|
798
|
+
|
799
|
+
assert_equal ["/path"], FileUtils.chmod_R(0600, "/path")
|
800
|
+
assert_equal File.stat("/path").mode, 0100600
|
801
|
+
assert_equal File.stat("/path/file1").mode, 0100600
|
802
|
+
assert_equal File.stat("/path/sub").mode, 0100600
|
803
|
+
assert_equal File.stat("/path/sub/file2").mode, 0100600
|
804
|
+
|
805
|
+
FileUtils.mkdir_p "/path2"
|
806
|
+
FileUtils.touch "/path2/hej"
|
807
|
+
assert_equal ["/path2"], FileUtils.chmod_R(0600, "/path2")
|
673
808
|
end
|
674
809
|
|
675
810
|
def test_dir_globs_paths
|
@@ -698,6 +833,9 @@ class FakeFSTest < Test::Unit::TestCase
|
|
698
833
|
assert_equal ['/path/bar', '/path/bar/baz', '/path/bar2', '/path/bar2/baz', '/path/foo', '/path/foobar'], Dir['/path/**/*']
|
699
834
|
assert_equal ['/path/bar/baz'], Dir['/path/bar/**/*']
|
700
835
|
|
836
|
+
assert_equal ['/path/bar/baz', '/path/bar2/baz'], Dir['/path/bar/**/*', '/path/bar2/**/*']
|
837
|
+
assert_equal ['/path/bar/baz', '/path/bar2/baz', '/path/bar/baz'], Dir['/path/ba*/**/*', '/path/bar/**/*']
|
838
|
+
|
701
839
|
FileUtils.cp_r '/path', '/otherpath'
|
702
840
|
|
703
841
|
assert_equal %w( /otherpath/foo /otherpath/foobar /path/foo /path/foobar ), Dir['/*/foo*']
|
@@ -705,6 +843,10 @@ class FakeFSTest < Test::Unit::TestCase
|
|
705
843
|
assert_equal ['/path/bar', '/path/foo'], Dir['/path/{foo,bar}']
|
706
844
|
|
707
845
|
assert_equal ['/path/bar', '/path/bar2'], Dir['/path/bar{2,}']
|
846
|
+
|
847
|
+
Dir.chdir '/path' do
|
848
|
+
assert_equal ['foo'], Dir['foo']
|
849
|
+
end
|
708
850
|
end
|
709
851
|
|
710
852
|
def test_dir_glob_handles_root
|
@@ -741,6 +883,12 @@ class FakeFSTest < Test::Unit::TestCase
|
|
741
883
|
assert_equal 2, yielded.size
|
742
884
|
end
|
743
885
|
|
886
|
+
if RUBY_VERSION >= "1.9"
|
887
|
+
def test_dir_home
|
888
|
+
assert_equal RealDir.home, Dir.home
|
889
|
+
end
|
890
|
+
end
|
891
|
+
|
744
892
|
def test_should_report_pos_as_0_when_opening
|
745
893
|
File.open("foo", "w") do |f|
|
746
894
|
f << "foobar"
|
@@ -783,19 +931,16 @@ class FakeFSTest < Test::Unit::TestCase
|
|
783
931
|
assert_equal 1, fp.pos
|
784
932
|
end
|
785
933
|
|
786
|
-
|
787
|
-
|
788
|
-
|
789
|
-
assert File.instance_methods.include?(method_name)
|
934
|
+
def test_every_method_in_file_is_in_fake_fs_file
|
935
|
+
RealFile.instance_methods.each do |method_name|
|
936
|
+
assert File.instance_methods.include?(method_name), "#{method_name} method is not available in File :("
|
790
937
|
end
|
791
938
|
end
|
792
939
|
|
793
|
-
|
794
|
-
|
795
|
-
|
796
|
-
|
797
|
-
define_method "test_file_should_not_respond_to_#{method_name}" do
|
798
|
-
assert !File.instance_methods.include?(method_name)
|
940
|
+
def test_file_should_not_respond_to_string_io_unique_methods
|
941
|
+
uniq_string_io_methods = StringIO.instance_methods - RealFile.instance_methods
|
942
|
+
uniq_string_io_methods.each do |method_name|
|
943
|
+
assert !File.instance_methods.include?(method_name), "File responds to #{method_name}"
|
799
944
|
end
|
800
945
|
end
|
801
946
|
|
@@ -808,14 +953,14 @@ class FakeFSTest < Test::Unit::TestCase
|
|
808
953
|
# I know memes!
|
809
954
|
FileUtils.mkdir_p '/path'
|
810
955
|
assert_equal '/', FileSystem.fs.name
|
811
|
-
assert_equal
|
956
|
+
assert_equal [], Dir.glob('/path/*')
|
812
957
|
Dir.chdir '/path' do
|
813
958
|
File.open('foo', 'w') { |f| f.write 'foo'}
|
814
959
|
File.open('foobar', 'w') { |f| f.write 'foo'}
|
815
960
|
end
|
816
961
|
|
817
962
|
assert_equal '/', FileSystem.fs.name
|
818
|
-
assert_equal(['foo', 'foobar'],
|
963
|
+
assert_equal(['/path/foo', '/path/foobar'], Dir.glob('/path/*').sort)
|
819
964
|
|
820
965
|
c = nil
|
821
966
|
Dir.chdir '/path' do
|
@@ -832,16 +977,16 @@ class FakeFSTest < Test::Unit::TestCase
|
|
832
977
|
File.open('foo', 'w') { |f| f.write 'foo'}
|
833
978
|
File.open('/foobar', 'w') { |f| f.write 'foo'}
|
834
979
|
end
|
835
|
-
assert_equal ['foo'],
|
836
|
-
assert_equal ['foobar', 'path'],
|
980
|
+
assert_equal ['/path/foo'], Dir.glob('/path/*').sort
|
981
|
+
assert_equal ['/foobar', '/path'], Dir.glob('/*').sort
|
837
982
|
|
838
983
|
Dir.chdir '/path' do
|
839
984
|
FileUtils.rm('foo')
|
840
985
|
FileUtils.rm('/foobar')
|
841
986
|
end
|
842
987
|
|
843
|
-
assert_equal [],
|
844
|
-
assert_equal ['path'],
|
988
|
+
assert_equal [], Dir.glob('/path/*').sort
|
989
|
+
assert_equal ['/path'], Dir.glob('/*').sort
|
845
990
|
end
|
846
991
|
|
847
992
|
def test_chdir_should_be_nestable
|
@@ -853,8 +998,8 @@ class FakeFSTest < Test::Unit::TestCase
|
|
853
998
|
end
|
854
999
|
end
|
855
1000
|
|
856
|
-
assert_equal ['foo','me'],
|
857
|
-
assert_equal ['foobar'],
|
1001
|
+
assert_equal ['/path/foo','/path/me'], Dir.glob('/path/*').sort
|
1002
|
+
assert_equal ['/path/me/foobar'], Dir.glob('/path/me/*').sort
|
858
1003
|
end
|
859
1004
|
|
860
1005
|
def test_chdir_should_flop_over_and_die_if_the_dir_doesnt_exist
|
@@ -889,23 +1034,23 @@ class FakeFSTest < Test::Unit::TestCase
|
|
889
1034
|
assert_equal ['/path'], FileSystem.dir_levels
|
890
1035
|
end
|
891
1036
|
|
892
|
-
assert_equal(['foo', 'foobar'],
|
1037
|
+
assert_equal(['/path/foo', '/path/foobar'], Dir.glob('/path/*').sort)
|
893
1038
|
end
|
894
1039
|
|
895
1040
|
def test_chdir_with_no_block_is_awesome
|
896
1041
|
FileUtils.mkdir_p '/path'
|
897
1042
|
Dir.chdir('/path')
|
898
1043
|
FileUtils.mkdir_p 'subdir'
|
899
|
-
assert_equal ['subdir'],
|
1044
|
+
assert_equal ['subdir'], Dir.glob('*')
|
900
1045
|
Dir.chdir('subdir')
|
901
1046
|
File.open('foo', 'w') { |f| f.write 'foo'}
|
902
|
-
assert_equal ['foo'],
|
1047
|
+
assert_equal ['foo'], Dir.glob('*')
|
903
1048
|
|
904
1049
|
assert_raises(Errno::ENOENT) do
|
905
1050
|
Dir.chdir('subsubdir')
|
906
1051
|
end
|
907
1052
|
|
908
|
-
assert_equal ['foo'],
|
1053
|
+
assert_equal ['foo'], Dir.glob('*')
|
909
1054
|
end
|
910
1055
|
|
911
1056
|
def test_current_dir_reflected_by_pwd
|
@@ -985,6 +1130,25 @@ class FakeFSTest < Test::Unit::TestCase
|
|
985
1130
|
assert_equal 'bar', File.read('baz/foo')
|
986
1131
|
end
|
987
1132
|
|
1133
|
+
def test_cp_array_of_files_into_directory
|
1134
|
+
File.open('foo', 'w') { |f| f.write 'footext' }
|
1135
|
+
File.open('bar', 'w') { |f| f.write 'bartext' }
|
1136
|
+
FileUtils.mkdir_p 'destdir'
|
1137
|
+
FileUtils.cp(%w(foo bar), 'destdir')
|
1138
|
+
|
1139
|
+
assert_equal 'footext', File.read('destdir/foo')
|
1140
|
+
assert_equal 'bartext', File.read('destdir/bar')
|
1141
|
+
end
|
1142
|
+
|
1143
|
+
def test_cp_fails_on_array_of_files_into_non_directory
|
1144
|
+
File.open('foo', 'w') { |f| f.write 'footext' }
|
1145
|
+
|
1146
|
+
exception = assert_raise(Errno::ENOTDIR) do
|
1147
|
+
FileUtils.cp(%w(foo), 'baz')
|
1148
|
+
end
|
1149
|
+
assert_equal "Not a directory - baz", exception.to_s
|
1150
|
+
end
|
1151
|
+
|
988
1152
|
def test_cp_overwrites_dest_file
|
989
1153
|
File.open('foo', 'w') {|f| f.write 'FOO' }
|
990
1154
|
File.open('bar', 'w') {|f| f.write 'BAR' }
|
@@ -1042,6 +1206,26 @@ class FakeFSTest < Test::Unit::TestCase
|
|
1042
1206
|
end
|
1043
1207
|
end
|
1044
1208
|
|
1209
|
+
def test_cp_r_array_of_files
|
1210
|
+
FileUtils.mkdir_p 'subdir'
|
1211
|
+
File.open('foo', 'w') { |f| f.write 'footext' }
|
1212
|
+
File.open('bar', 'w') { |f| f.write 'bartext' }
|
1213
|
+
FileUtils.cp_r(%w(foo bar), 'subdir')
|
1214
|
+
|
1215
|
+
assert_equal 'footext', File.open('subdir/foo') { |f| f.read }
|
1216
|
+
assert_equal 'bartext', File.open('subdir/bar') { |f| f.read }
|
1217
|
+
end
|
1218
|
+
|
1219
|
+
def test_cp_r_array_of_directories
|
1220
|
+
%w(foo bar subdir).each { |d| FileUtils.mkdir_p d }
|
1221
|
+
File.open('foo/baz', 'w') { |f| f.write 'baztext' }
|
1222
|
+
File.open('bar/quux', 'w') { |f| f.write 'quuxtext' }
|
1223
|
+
|
1224
|
+
FileUtils.cp_r(%w(foo bar), 'subdir')
|
1225
|
+
assert_equal 'baztext', File.open('subdir/foo/baz') { |f| f.read }
|
1226
|
+
assert_equal 'quuxtext', File.open('subdir/bar/quux') { |f| f.read }
|
1227
|
+
end
|
1228
|
+
|
1045
1229
|
def test_cp_r_only_copies_into_directories
|
1046
1230
|
FileUtils.mkdir_p 'subdir'
|
1047
1231
|
Dir.chdir('subdir') { File.open('foo', 'w') { |f| f.write 'footext' } }
|
@@ -1081,34 +1265,49 @@ class FakeFSTest < Test::Unit::TestCase
|
|
1081
1265
|
end
|
1082
1266
|
|
1083
1267
|
def test_clone_clones_directories
|
1084
|
-
|
1085
|
-
RealFileUtils.mkdir_p(here('subdir'))
|
1086
|
-
FakeFS.activate!
|
1268
|
+
act_on_real_fs { RealFileUtils.mkdir_p(here('subdir')) }
|
1087
1269
|
|
1088
1270
|
FileSystem.clone(here('subdir'))
|
1089
1271
|
|
1090
1272
|
assert File.exists?(here('subdir')), 'subdir was cloned'
|
1091
1273
|
assert File.directory?(here('subdir')), 'subdir is a directory'
|
1092
1274
|
ensure
|
1093
|
-
|
1094
|
-
RealFileUtils.rm_rf(here('subdir'))
|
1095
|
-
FakeFS.activate!
|
1275
|
+
act_on_real_fs { RealFileUtils.rm_rf(here('subdir')) }
|
1096
1276
|
end
|
1097
1277
|
|
1098
1278
|
def test_clone_clones_dot_files_even_hard_to_find_ones
|
1099
|
-
|
1100
|
-
RealFileUtils.mkdir_p(here('subdir/.bar/baz/.quux/foo'))
|
1101
|
-
FakeFS.activate!
|
1279
|
+
act_on_real_fs { RealFileUtils.mkdir_p(here('subdir/.bar/baz/.quux/foo')) }
|
1102
1280
|
|
1103
1281
|
assert !File.exists?(here('subdir'))
|
1104
1282
|
|
1105
1283
|
FileSystem.clone(here('subdir'))
|
1106
|
-
assert_equal ['.bar'],
|
1107
|
-
assert_equal ['foo'],
|
1284
|
+
assert_equal ['.', '..', '.bar'], Dir.entries(here('subdir'))
|
1285
|
+
assert_equal ['.', '..', 'foo'], Dir.entries(here('subdir/.bar/baz/.quux'))
|
1108
1286
|
ensure
|
1109
|
-
|
1110
|
-
|
1111
|
-
|
1287
|
+
act_on_real_fs { RealFileUtils.rm_rf(here('subdir')) }
|
1288
|
+
end
|
1289
|
+
|
1290
|
+
def test_dir_glob_on_clone_with_absolute_path
|
1291
|
+
act_on_real_fs { RealFileUtils.mkdir_p(here('subdir/.bar/baz/.quux/foo')) }
|
1292
|
+
FileUtils.mkdir_p '/path'
|
1293
|
+
Dir.chdir('/path')
|
1294
|
+
FileSystem.clone(here('subdir'), "/foo")
|
1295
|
+
assert Dir.glob "/foo/*"
|
1296
|
+
ensure
|
1297
|
+
act_on_real_fs { RealFileUtils.rm_rf(here('subdir')) }
|
1298
|
+
end
|
1299
|
+
|
1300
|
+
def test_clone_with_target_specified
|
1301
|
+
act_on_real_fs { RealFileUtils.mkdir_p(here('subdir/.bar/baz/.quux/foo')) }
|
1302
|
+
|
1303
|
+
assert !File.exists?(here('subdir'))
|
1304
|
+
|
1305
|
+
FileSystem.clone(here('subdir'), here('subdir2'))
|
1306
|
+
assert !File.exists?(here('subdir'))
|
1307
|
+
assert_equal ['.', '..', '.bar'], Dir.entries(here('subdir2'))
|
1308
|
+
assert_equal ['.', '..', 'foo'], Dir.entries(here('subdir2/.bar/baz/.quux'))
|
1309
|
+
ensure
|
1310
|
+
act_on_real_fs { RealFileUtils.rm_rf(here('subdir')) }
|
1112
1311
|
end
|
1113
1312
|
|
1114
1313
|
def test_putting_a_dot_at_end_copies_the_contents
|
@@ -1413,6 +1612,29 @@ class FakeFSTest < Test::Unit::TestCase
|
|
1413
1612
|
assert File.exists?('/path')
|
1414
1613
|
end
|
1415
1614
|
|
1615
|
+
def test_directory_mkdir_nested
|
1616
|
+
Dir.mkdir("/tmp")
|
1617
|
+
Dir.mkdir("/tmp/stream20120103-11847-xc8pb.lock")
|
1618
|
+
assert File.exists?("/tmp/stream20120103-11847-xc8pb.lock")
|
1619
|
+
end
|
1620
|
+
|
1621
|
+
def test_can_create_subdirectories_with_dir_mkdir
|
1622
|
+
Dir.mkdir 'foo'
|
1623
|
+
Dir.mkdir 'foo/bar'
|
1624
|
+
assert Dir.exists?('foo/bar')
|
1625
|
+
end
|
1626
|
+
|
1627
|
+
def test_can_create_absolute_subdirectories_with_dir_mkdir
|
1628
|
+
Dir.mkdir '/foo'
|
1629
|
+
Dir.mkdir '/foo/bar'
|
1630
|
+
assert Dir.exists?('/foo/bar')
|
1631
|
+
end
|
1632
|
+
|
1633
|
+
def test_can_create_directories_starting_with_dot
|
1634
|
+
Dir.mkdir './path'
|
1635
|
+
assert File.exists? './path'
|
1636
|
+
end
|
1637
|
+
|
1416
1638
|
def test_directory_mkdir_relative
|
1417
1639
|
FileUtils.mkdir_p('/new/root')
|
1418
1640
|
FileSystem.chdir('/new/root')
|
@@ -1623,7 +1845,7 @@ class FakeFSTest < Test::Unit::TestCase
|
|
1623
1845
|
File.link("/foo", "/bar")
|
1624
1846
|
|
1625
1847
|
File.unlink("/bar")
|
1626
|
-
File.read("/foo")
|
1848
|
+
assert_equal "some_content", File.read("/foo")
|
1627
1849
|
end
|
1628
1850
|
|
1629
1851
|
def test_link_reports_correct_stat_info_after_unlinking
|
@@ -1774,6 +1996,80 @@ class FakeFSTest < Test::Unit::TestCase
|
|
1774
1996
|
assert_equal filename, "expect.txt"
|
1775
1997
|
end
|
1776
1998
|
|
1999
|
+
#########################
|
2000
|
+
def test_file_default_mode
|
2001
|
+
FileUtils.touch "foo"
|
2002
|
+
assert_equal File.stat("foo").mode, (0100000 + 0666 - File.umask)
|
2003
|
+
end
|
2004
|
+
|
2005
|
+
def test_dir_default_mode
|
2006
|
+
Dir.mkdir "bar"
|
2007
|
+
assert_equal File.stat("bar").mode, (0100000 + 0777 - File.umask)
|
2008
|
+
end
|
2009
|
+
|
2010
|
+
def test_file_default_uid_and_gid
|
2011
|
+
FileUtils.touch "foo"
|
2012
|
+
assert_equal File.stat("foo").uid, Process.uid
|
2013
|
+
assert_equal File.stat("foo").gid, Process.gid
|
2014
|
+
end
|
2015
|
+
|
2016
|
+
def test_file_chmod_of_file
|
2017
|
+
FileUtils.touch "foo"
|
2018
|
+
File.chmod 0600, "foo"
|
2019
|
+
assert_equal File.stat("foo").mode, 0100600
|
2020
|
+
File.new("foo").chmod 0644
|
2021
|
+
assert_equal File.stat("foo").mode, 0100644
|
2022
|
+
end
|
2023
|
+
|
2024
|
+
def test_file_chmod_of_dir
|
2025
|
+
Dir.mkdir "bar"
|
2026
|
+
File.chmod 0777, "bar"
|
2027
|
+
assert_equal File.stat("bar").mode, 0100777
|
2028
|
+
File.new("bar").chmod 01700
|
2029
|
+
assert_equal File.stat("bar").mode, 0101700
|
2030
|
+
end
|
2031
|
+
|
2032
|
+
def test_file_chown_of_file
|
2033
|
+
FileUtils.touch "foo"
|
2034
|
+
File.chown 1337, 1338, "foo"
|
2035
|
+
assert_equal File.stat("foo").uid, 1337
|
2036
|
+
assert_equal File.stat("foo").gid, 1338
|
2037
|
+
end
|
2038
|
+
|
2039
|
+
def test_file_chown_of_dir
|
2040
|
+
Dir.mkdir "bar"
|
2041
|
+
File.chown 1337, 1338, "bar"
|
2042
|
+
assert_equal File.stat("bar").uid, 1337
|
2043
|
+
assert_equal File.stat("bar").gid, 1338
|
2044
|
+
end
|
2045
|
+
|
2046
|
+
def test_file_umask
|
2047
|
+
assert_equal File.umask, RealFile.umask
|
2048
|
+
end
|
2049
|
+
|
2050
|
+
def test_file_stat_comparable
|
2051
|
+
a_time = Time.new
|
2052
|
+
|
2053
|
+
same1 = File.new("s1", "w")
|
2054
|
+
same2 = File.new("s2", "w")
|
2055
|
+
different1 = File.new("d1", "w")
|
2056
|
+
different2 = File.new("d2", "w")
|
2057
|
+
|
2058
|
+
FileSystem.find("s1").mtime = a_time
|
2059
|
+
FileSystem.find("s2").mtime = a_time
|
2060
|
+
|
2061
|
+
FileSystem.find("d1").mtime = a_time
|
2062
|
+
FileSystem.find("d2").mtime = a_time + 1
|
2063
|
+
|
2064
|
+
assert same1.mtime == same2.mtime
|
2065
|
+
assert different1.mtime != different2.mtime
|
2066
|
+
|
2067
|
+
assert same1.stat == same2.stat
|
2068
|
+
assert (same1.stat <=> same2.stat) == 0
|
2069
|
+
|
2070
|
+
assert different1.stat != different2.stat
|
2071
|
+
assert (different1.stat <=> different2.stat) == -1
|
2072
|
+
end
|
1777
2073
|
|
1778
2074
|
def here(fname)
|
1779
2075
|
RealFile.expand_path(File.join(RealFile.dirname(__FILE__), fname))
|
@@ -1810,4 +2106,14 @@ class FakeFSTest < Test::Unit::TestCase
|
|
1810
2106
|
end
|
1811
2107
|
end
|
1812
2108
|
end
|
2109
|
+
|
2110
|
+
if RUBY_VERSION >= "1.9.3"
|
2111
|
+
def test_advise
|
2112
|
+
File.open("foo", 'w') do |f|
|
2113
|
+
assert_nothing_raised do
|
2114
|
+
f.advise(:normal, 0, 0)
|
2115
|
+
end
|
2116
|
+
end
|
2117
|
+
end
|
2118
|
+
end
|
1813
2119
|
end
|