forked from fakefs/fakefs
/
fakefs_test.rb
2066 lines (1640 loc) · 52.4 KB
/
fakefs_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require "test_helper"
class FakeFSTest < Test::Unit::TestCase
include FakeFS
def setup
FakeFS.activate!
FileSystem.clear
end
def teardown
FakeFS.deactivate!
end
def test_can_be_initialized_empty
fs = FileSystem
assert_equal 0, fs.files.size
end
def xtest_can_be_initialized_with_an_existing_directory
fs = FileSystem
fs.clone(File.expand_path(File.dirname(__FILE__))).inspect
assert_equal 1, fs.files.size
end
def test_can_create_directories_with_file_utils_mkdir_p
FileUtils.mkdir_p("/path/to/dir")
assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']
end
def test_can_create_directories_with_options
FileUtils.mkdir_p("/path/to/dir", :mode => 0755)
assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']
end
def test_can_create_directories_with_file_utils_mkdir
FileUtils.mkdir_p("/path/to/dir")
FileUtils.mkdir("/path/to/dir/subdir")
assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']['subdir']
end
def test_raises_error_when_creating_a_new_dir_with_mkdir_in_non_existent_path
assert_raises Errno::ENOENT do
FileUtils.mkdir("/this/path/does/not/exists/newdir")
end
end
def test_can_create_directories_with_mkpath
FileUtils.mkpath("/path/to/dir")
assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']
end
def test_can_create_directories_with_mkpath_and_options
FileUtils.mkpath("/path/to/dir", :mode => 0755)
assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']
end
def test_can_create_directories_with_mkpath
FileUtils.makedirs("/path/to/dir")
assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']
end
def test_can_create_directories_with_mkpath_and_options
FileUtils.makedirs("/path/to/dir", :mode => 0755)
assert_kind_of FakeDir, FileSystem.fs['path']['to']['dir']
end
def test_unlink_errors_on_file_not_found
assert_raise Errno::ENOENT do
FileUtils.rm("/foo")
end
end
def test_can_delete_directories
FileUtils.mkdir_p("/path/to/dir")
FileUtils.rmdir("/path/to/dir")
assert File.exists?("/path/to/")
assert File.exists?("/path/to/dir") == false
end
def test_can_delete_multiple_files
FileUtils.touch(["foo", "bar"])
FileUtils.rm(["foo", "bar"])
assert File.exists?("foo") == false
assert File.exists?("bar") == false
end
def test_knows_directories_exist
FileUtils.mkdir_p(path = "/path/to/dir")
assert File.exists?(path)
end
def test_knows_directories_are_directories
FileUtils.mkdir_p(path = "/path/to/dir")
assert File.directory?(path)
end
def test_knows_symlink_directories_are_directories
FileUtils.mkdir_p(path = "/path/to/dir")
FileUtils.ln_s path, sympath = '/sympath'
assert File.directory?(sympath)
end
def test_knows_non_existent_directories_arent_directories
path = 'does/not/exist/'
assert_equal RealFile.directory?(path), File.directory?(path)
end
def test_doesnt_overwrite_existing_directories
FileUtils.mkdir_p(path = "/path/to/dir")
assert File.exists?(path)
FileUtils.mkdir_p("/path/to")
assert File.exists?(path)
assert_raises Errno::EEXIST do
FileUtils.mkdir("/path/to")
end
assert File.exists?(path)
end
def test_symlink_with_missing_refferent_does_not_exist
File.symlink('/foo', '/bar')
assert !File.exists?('/bar')
end
def test_can_create_symlinks
FileUtils.mkdir_p(target = "/path/to/target")
FileUtils.ln_s(target, "/path/to/link")
assert_kind_of FakeSymlink, FileSystem.fs['path']['to']['link']
assert_raises(Errno::EEXIST) do
FileUtils.ln_s(target, '/path/to/link')
end
end
def test_can_force_creation_of_symlinks
FileUtils.mkdir_p(target = "/path/to/first/target")
FileUtils.ln_s(target, "/path/to/link")
assert_kind_of FakeSymlink, FileSystem.fs['path']['to']['link']
FileUtils.ln_s(target, '/path/to/link', :force => true)
end
def test_create_symlink_using_ln_sf
FileUtils.mkdir_p(target = "/path/to/first/target")
FileUtils.ln_s(target, "/path/to/link")
assert_kind_of FakeSymlink, FileSystem.fs['path']['to']['link']
FileUtils.ln_sf(target, '/path/to/link')
end
def test_can_follow_symlinks
FileUtils.mkdir_p(target = "/path/to/target")
FileUtils.ln_s(target, link = "/path/to/symlink")
assert_equal target, File.readlink(link)
end
def test_symlinks_in_different_directories
FileUtils.mkdir_p("/path/to/bar")
FileUtils.mkdir_p(target = "/path/to/foo/target")
FileUtils.ln_s(target, link = "/path/to/bar/symlink")
assert_equal target, File.readlink(link)
end
def test_symlink_with_relative_path_exists
FileUtils.touch("/file")
FileUtils.mkdir_p("/a/b")
FileUtils.ln_s("../../file", link = "/a/b/symlink")
assert File.exist?('/a/b/symlink')
end
def test_symlink_with_relative_path_and_nonexistant_file_does_not_exist
FileUtils.touch("/file")
FileUtils.mkdir_p("/a/b")
FileUtils.ln_s("../../file_foo", link = "/a/b/symlink")
assert !File.exist?('/a/b/symlink')
end
def test_symlink_with_relative_path_has_correct_target
FileUtils.touch("/file")
FileUtils.mkdir_p("/a/b")
FileUtils.ln_s("../../file", link = "/a/b/symlink")
assert_equal "../../file", File.readlink(link)
end
def test_symlinks_to_symlinks
FileUtils.mkdir_p(target = "/path/to/foo/target")
FileUtils.mkdir_p("/path/to/bar")
FileUtils.mkdir_p("/path/to/bar2")
FileUtils.ln_s(target, link1 = "/path/to/bar/symlink")
FileUtils.ln_s(link1, link2 = "/path/to/bar2/symlink")
assert_equal link1, File.readlink(link2)
end
def test_symlink_to_symlinks_should_raise_error_if_dir_doesnt_exist
FileUtils.mkdir_p(target = "/path/to/foo/target")
assert !Dir.exists?("/path/to/bar")
assert_raise Errno::ENOENT do
FileUtils.ln_s(target, "/path/to/bar/symlink")
end
end
def test_knows_symlinks_are_symlinks
FileUtils.mkdir_p(target = "/path/to/target")
FileUtils.ln_s(target, link = "/path/to/symlink")
assert File.symlink?(link)
end
def test_can_create_files_in_current_dir
path = 'file.txt'
File.open(path, 'w') do |f|
f.write "Yatta!"
end
assert File.exists?(path)
assert File.readable?(path)
assert File.writable?(path)
end
def test_can_create_files_in_existing_dir
FileUtils.mkdir_p "/path/to"
path = "/path/to/file.txt"
File.open(path, 'w') do |f|
f.write "Yatta!"
end
assert File.exists?(path)
assert File.readable?(path)
assert File.writable?(path)
end
def test_raises_ENOENT_trying_to_create_files_in_nonexistent_dir
path = "/path/to/file.txt"
assert_raises(Errno::ENOENT) {
File.open(path, 'w') do |f|
f.write "Yatta!"
end
}
end
def test_raises_ENOENT_trying_to_create_files_in_relative_nonexistent_dir
FileUtils.mkdir_p "/some/path"
Dir.chdir("/some/path") {
assert_raises(Errno::ENOENT) {
File.open("../foo") {|f| f.write "moo" }
}
}
end
def test_raises_ENOENT_trying_to_create_files_in_obscured_nonexistent_dir
FileUtils.mkdir_p "/some/path"
assert_raises(Errno::ENOENT) {
File.open("/some/path/../foo") {|f| f.write "moo" }
}
end
def test_raises_ENOENT_trying_to_create_tilde_referenced_nonexistent_dir
path = "~/fakefs_test_#{$$}_0000"
while File.exist? path
path = path.succ
end
assert_raises(Errno::ENOENT) {
File.open("#{path}/foo") {|f| f.write "moo" }
}
end
def test_raises_EISDIR_if_trying_to_open_existing_directory_name
path = "/path/to"
FileUtils.mkdir_p path
assert_raises(Errno::EISDIR) {
File.open(path, 'w') do |f|
f.write "Yatta!"
end
}
end
def test_can_create_files_with_bitmasks
FileUtils.mkdir_p("/path/to")
path = '/path/to/file.txt'
File.open(path, File::RDWR | File::CREAT) do |f|
f.write "Yatta!"
end
assert File.exists?(path)
assert File.readable?(path)
assert File.writable?(path)
end
def test_file_opens_in_read_only_mode
File.open("foo", "w") { |f| f << "foo" }
f = File.open("foo")
assert_raises(IOError) do
f << "bar"
end
end
def test_file_opens_in_read_only_mode_with_bitmasks
File.open("foo", "w") { |f| f << "foo" }
f = File.open("foo", File::RDONLY)
assert_raises(IOError) do
f << "bar"
end
end
def test_file_opens_in_invalid_mode
FileUtils.touch("foo")
assert_raises(ArgumentError) do
File.open("foo", "an_illegal_mode")
end
end
def test_raises_error_when_cannot_find_file_in_read_mode
assert_raises(Errno::ENOENT) do
File.open("does_not_exist", "r")
end
end
def test_raises_error_when_cannot_find_file_in_read_write_mode
assert_raises(Errno::ENOENT) do
File.open("does_not_exist", "r+")
end
end
def test_creates_files_in_write_only_mode
File.open("foo", "w")
assert File.exists?("foo")
end
def test_creates_files_in_write_only_mode_with_bitmasks
File.open("foo", File::WRONLY | File::CREAT)
assert File.exists?("foo")
end
def test_raises_in_write_only_mode_without_create_bitmask
assert_raises(Errno::ENOENT) do
File.open("foo", File::WRONLY)
end
end
def test_creates_files_in_read_write_truncate_mode
File.open("foo", "w+")
assert File.exists?("foo")
end
def test_creates_files_in_append_write_only
File.open("foo", "a")
assert File.exists?("foo")
end
def test_creates_files_in_append_read_write
File.open("foo", "a+")
assert File.exists?("foo")
end
def test_file_in_write_only_raises_error_when_reading
FileUtils.touch("foo")
f = File.open("foo", "w")
assert_raises(IOError) do
f.read
end
end
def test_file_in_write_mode_truncates_existing_file
File.open("foo", "w") { |f| f << "contents" }
f = File.open("foo", "w")
assert_equal "", File.read("foo")
end
def test_file_in_read_write_truncation_mode_truncates_file
File.open("foo", "w") { |f| f << "foo" }
f = File.open("foo", "w+")
assert_equal "", File.read("foo")
end
def test_file_in_append_write_only_raises_error_when_reading
FileUtils.touch("foo")
f = File.open("foo", "a")
assert_raises(IOError) do
f.read
end
end
def test_can_read_files_once_written
path = 'file.txt'
File.open(path, 'w') do |f|
f.write "Yatta!"
end
assert_equal "Yatta!", File.read(path)
end
def test_can_write_to_files
path = 'file.txt'
File.open(path, 'w') do |f|
f << 'Yada Yada'
end
assert_equal 'Yada Yada', File.read(path)
end
def test_raises_error_when_opening_with_binary_mode_only
assert_raise ArgumentError do
File.open("/foo", "b")
end
end
def test_can_open_file_in_binary_mode
File.open("foo", "wb") { |x| x << "a" }
assert_equal "a", File.read("foo")
end
def test_can_chunk_io_when_reading
FileUtils.mkdir_p "/path/to"
path = '/path/to/file.txt'
File.open(path, 'w') do |f|
f << 'Yada Yada'
end
file = File.new(path, 'r')
assert_equal 'Yada', file.read(4)
assert_equal ' Yada', file.read(5)
assert_equal '', file.read
file.rewind
assert_equal 'Yada Yada', file.read
end
def test_can_get_size_of_files
path = 'file.txt'
File.open(path, 'w') do |f|
f << 'Yada Yada'
end
assert_equal 9, File.size(path)
end
def test_can_check_if_file_has_size?
path = 'file.txt'
File.open(path, 'w') do |f|
f << 'Yada Yada'
end
assert File.size?(path)
assert_nil File.size?("other.txt")
end
def test_can_check_size_of_empty_file
path = 'file.txt'
File.open(path, 'w') do |f|
f << ''
end
assert_nil File.size?("file.txt")
end
def test_raises_error_on_mtime_if_file_does_not_exist
assert_raise Errno::ENOENT do
File.mtime('/path/to/file.txt')
end
end
def test_can_return_mtime_on_existing_file
path = 'file.txt'
File.open(path, 'w') do |f|
f << ''
end
assert File.mtime('file.txt').is_a?(Time)
end
def test_raises_error_on_ctime_if_file_does_not_exist
assert_raise Errno::ENOENT do
File.ctime('file.txt')
end
end
def test_can_return_ctime_on_existing_file
File.open("foo", "w") { |f| f << "some content" }
assert File.ctime('foo').is_a?(Time)
end
def test_raises_error_on_atime_if_file_does_not_exist
assert_raise Errno::ENOENT do
File.atime('file.txt')
end
end
def test_can_return_atime_on_existing_file
File.open("foo", "w") { |f| f << "some content" }
assert File.atime('foo').is_a?(Time)
end
def test_ctime_mtime_and_atime_are_equal_for_new_files
File.open("foo", "w") { |f| f << "some content" }
ctime = File.ctime("foo")
mtime = File.mtime("foo")
atime = File.atime("foo")
assert ctime.is_a?(Time)
assert mtime.is_a?(Time)
assert atime.is_a?(Time)
assert_equal ctime, mtime
assert_equal ctime, atime
File.open("foo", "r") do |f|
assert_equal ctime, f.ctime
assert_equal mtime, f.mtime
assert_equal atime, f.atime
end
end
def test_ctime_mtime_and_atime_are_equal_for_new_directories
FileUtils.mkdir_p("foo")
ctime = File.ctime("foo")
mtime = File.mtime("foo")
atime = File.atime("foo")
assert ctime.is_a?(Time)
assert mtime.is_a?(Time)
assert atime.is_a?(Time)
assert_equal ctime, mtime
assert_equal ctime, atime
end
def test_file_ctime_is_equal_to_file_stat_ctime
File.open("foo", "w") { |f| f << "some content" }
assert_equal File.stat("foo").ctime, File.ctime("foo")
end
def test_directory_ctime_is_equal_to_directory_stat_ctime
FileUtils.mkdir_p("foo")
assert_equal File.stat("foo").ctime, File.ctime("foo")
end
def test_file_mtime_is_equal_to_file_stat_mtime
File.open("foo", "w") { |f| f << "some content" }
assert_equal File.stat("foo").mtime, File.mtime("foo")
end
def test_directory_mtime_is_equal_to_directory_stat_mtime
FileUtils.mkdir_p("foo")
assert_equal File.stat("foo").mtime, File.mtime("foo")
end
def test_file_atime_is_equal_to_file_stat_atime
File.open("foo", "w") { |f| f << "some content" }
assert_equal File.stat("foo").atime, File.atime("foo")
end
def test_directory_atime_is_equal_to_directory_stat_atime
FileUtils.mkdir_p("foo")
assert_equal File.stat("foo").atime, File.atime("foo")
end
def test_utime_raises_error_if_path_does_not_exist
assert_raise Errno::ENOENT do
File.utime(Time.now, Time.now, '/path/to/file.txt')
end
end
def test_can_call_utime_on_an_existing_file
time = Time.now - 300 # Not now
path = 'file.txt'
File.open(path, 'w') do |f|
f << ''
end
File.utime(time, time, path)
assert_equal time, File.mtime('file.txt')
assert_equal time, File.atime('file.txt')
end
def test_utime_returns_number_of_paths
path1, path2 = 'file.txt', 'another_file.txt'
[path1, path2].each do |path|
File.open(path, 'w') do |f|
f << ''
end
end
assert_equal 2, File.utime(Time.now, Time.now, path1, path2)
end
def test_file_a_time_updated_when_file_is_read
old_atime = Time.now - 300
path = "file.txt"
File.open(path, "w") do |f|
f << "Hello"
end
File.utime(old_atime, File.mtime(path), path)
assert_equal File.atime(path), old_atime
File.read(path)
assert File.atime(path) != old_atime
end
def test_can_read_with_File_readlines
path = 'file.txt'
File.open(path, 'w') do |f|
f.puts "Yatta!", "Gatta!"
f.puts ["woot","toot"]
end
assert_equal %w(Yatta! Gatta! woot toot), File.readlines(path)
end
def test_File_close_disallows_further_access
path = 'file.txt'
file = File.open(path, 'w')
file.write 'Yada'
file.close
assert_raise IOError do
file.read
end
end
def test_File_close_disallows_further_writes
path = 'file.txt'
file = File.open(path, 'w')
file.write 'Yada'
file.close
assert_raise IOError do
file << "foo"
end
end
def test_can_read_from_file_objects
path = 'file.txt'
File.open(path, 'w') do |f|
f.write "Yatta!"
end
assert_equal "Yatta!", File.new(path).read
end
if RUBY_VERSION >= "1.9"
def test_file_object_has_default_external_encoding
Encoding.default_external = "UTF-8"
path = 'file.txt'
File.open(path, 'w'){|f| f.write 'Yatta!' }
assert_equal "UTF-8", File.new(path).read.encoding.name
end
end
def test_file_object_initialization_with_mode_in_hash_parameter
assert_nothing_raised do
File.open("file.txt", {:mode => "w"}){ |f| f.write 'Yatta!' }
end
end
def test_file_read_errors_appropriately
assert_raise Errno::ENOENT do
File.read('anything')
end
end
def test_knows_files_are_files
path = 'file.txt'
File.open(path, 'w') do |f|
f.write "Yatta!"
end
assert File.file?(path)
end
def test_File_io_returns_self
f = File.open("foo", "w")
assert_equal f, f.to_io
end
def test_File_to_i_is_alias_for_filno
f = File.open("foo", "w")
assert_equal f.method(:to_i), f.method(:fileno)
end
def test_knows_symlink_files_are_files
path = 'file.txt'
File.open(path, 'w') do |f|
f.write "Yatta!"
end
FileUtils.ln_s path, sympath='/sympath'
assert File.file?(sympath)
end
def test_knows_non_existent_files_arent_files
assert_equal RealFile.file?('does/not/exist.txt'), File.file?('does/not/exist.txt')
end
def test_can_chown_files
good = 'file.txt'
bad = 'nofile.txt'
File.open(good,'w') { |f| f.write "foo" }
username = Etc.getpwuid(Process.uid).name
groupname = Etc.getgrgid(Process.gid).name
out = FileUtils.chown(1337, 1338, good, :verbose => true)
assert_equal [good], out
assert_equal File.stat(good).uid, 1337
assert_equal File.stat(good).gid, 1338
assert_raises(Errno::ENOENT) do
FileUtils.chown(username, groupname, bad, :verbose => true)
end
assert_equal [good], FileUtils.chown(username, groupname, good)
assert_equal File.stat(good).uid, Process.uid
assert_equal File.stat(good).gid, Process.gid
assert_raises(Errno::ENOENT) do
FileUtils.chown(username, groupname, bad)
end
assert_equal [good], FileUtils.chown(username, groupname, [good])
assert_equal File.stat(good).uid, Process.uid
assert_equal File.stat(good).gid, Process.gid
assert_raises(Errno::ENOENT) do
FileUtils.chown(username, groupname, [good, bad])
end
end
def test_can_chown_R_files
username = Etc.getpwuid(Process.uid).name
groupname = Etc.getgrgid(Process.gid).name
FileUtils.mkdir_p '/path/'
File.open('/path/foo', 'w') { |f| f.write 'foo' }
File.open('/path/foobar', 'w') { |f| f.write 'foo' }
assert_equal ['/path'], FileUtils.chown_R(username, groupname, '/path')
%w(/path /path/foo /path/foobar).each do |f|
assert_equal File.stat(f).uid, Process.uid
assert_equal File.stat(f).gid, Process.gid
end
end
def test_can_chmod_files
good = "file.txt"
bad = "nofile.txt"
FileUtils.touch(good)
assert_equal [good], FileUtils.chmod(0600, good, :verbose => true)
assert_equal File.stat(good).mode, 0100600
assert_raises(Errno::ENOENT) do
FileUtils.chmod(0600, bad)
end
assert_equal [good], FileUtils.chmod(0666, good)
assert_equal File.stat(good).mode, 0100666
assert_raises(Errno::ENOENT) do
FileUtils.chmod(0666, bad)
end
assert_equal [good], FileUtils.chmod(0644, [good])
assert_equal File.stat(good).mode, 0100644
assert_raises(Errno::ENOENT) do
FileUtils.chmod(0644, bad)
end
end
def test_can_chmod_R_files
FileUtils.mkdir_p "/path/sub"
FileUtils.touch "/path/file1"
FileUtils.touch "/path/sub/file2"
assert_equal ["/path"], FileUtils.chmod_R(0600, "/path")
assert_equal File.stat("/path").mode, 0100600
assert_equal File.stat("/path/file1").mode, 0100600
assert_equal File.stat("/path/sub").mode, 0100600
assert_equal File.stat("/path/sub/file2").mode, 0100600
FileUtils.mkdir_p "/path2"
FileUtils.touch "/path2/hej"
assert_equal ["/path2"], FileUtils.chmod_R(0600, "/path2")
end
def test_dir_globs_paths
FileUtils.mkdir_p '/path'
File.open('/path/foo', 'w') { |f| f.write 'foo' }
File.open('/path/foobar', 'w') { |f| f.write 'foo' }
FileUtils.mkdir_p '/path/bar'
File.open('/path/bar/baz', 'w') { |f| f.write 'foo' }
FileUtils.cp_r '/path/bar', '/path/bar2'
assert_equal ['/path'], Dir['/path']
assert_equal %w( /path/bar /path/bar2 /path/foo /path/foobar ), Dir['/path/*']
assert_equal ['/path/bar/baz'], Dir['/path/bar/*']
assert_equal ['/path/foo'], Dir['/path/foo']
assert_equal ['/path'], Dir['/path*']
assert_equal ['/path/foo', '/path/foobar'], Dir['/p*h/foo*']
assert_equal ['/path/foo', '/path/foobar'], Dir['/p??h/foo*']
assert_equal ['/path/bar', '/path/bar/baz', '/path/bar2', '/path/bar2/baz', '/path/foo', '/path/foobar'], Dir['/path/**/*']
assert_equal ['/path', '/path/bar', '/path/bar/baz', '/path/bar2', '/path/bar2/baz', '/path/foo', '/path/foobar'], Dir['/**/*']
assert_equal ['/path/bar', '/path/bar/baz', '/path/bar2', '/path/bar2/baz', '/path/foo', '/path/foobar'], Dir['/path/**/*']
assert_equal ['/path/bar/baz'], Dir['/path/bar/**/*']
assert_equal ['/path/bar/baz', '/path/bar2/baz'], Dir['/path/bar/**/*', '/path/bar2/**/*']
assert_equal ['/path/bar/baz', '/path/bar2/baz', '/path/bar/baz'], Dir['/path/ba*/**/*', '/path/bar/**/*']
FileUtils.cp_r '/path', '/otherpath'
assert_equal %w( /otherpath/foo /otherpath/foobar /path/foo /path/foobar ), Dir['/*/foo*']
assert_equal ['/path/bar', '/path/foo'], Dir['/path/{foo,bar}']
assert_equal ['/path/bar', '/path/bar2'], Dir['/path/bar{2,}']
Dir.chdir '/path' do
assert_equal ['foo'], Dir['foo']
end
end
def test_dir_glob_handles_root
FileUtils.mkdir_p '/path'
# this fails. the root dir should be named '/' but it is '.'
assert_equal ['/'], Dir['/']
end
def test_dir_glob_handles_recursive_globs
FileUtils.mkdir_p "/one/two/three"
File.open('/one/two/three/four.rb', 'w')
File.open('/one/five.rb', 'w')
assert_equal ['/one/five.rb', '/one/two/three/four.rb'], Dir['/one/**/*.rb']
assert_equal ['/one/two'], Dir['/one/**/two']
assert_equal ['/one/two/three'], Dir['/one/**/three']
end
def test_dir_recursive_glob_ending_in_wildcards_returns_both_files_and_dirs
FileUtils.mkdir_p "/one/two/three"
File.open('/one/two/three/four.rb', 'w')
File.open('/one/five.rb', 'w')
assert_equal ['/one/five.rb', '/one/two', '/one/two/three', '/one/two/three/four.rb'], Dir['/one/**/*']
assert_equal ['/one/five.rb', '/one/two'], Dir['/one/**']
end
def test_dir_glob_with_block
FileUtils.touch('foo')
FileUtils.touch('bar')
yielded = []
Dir.glob('*') { |file| yielded << file }
assert_equal 2, yielded.size
end
if RUBY_VERSION >= "1.9"
def test_dir_home
assert_equal RealDir.home, Dir.home
end
end
def test_should_report_pos_as_0_when_opening
File.open("foo", "w") do |f|
f << "foobar"
f.rewind
assert_equal 0, f.pos
end
end
def test_should_report_pos_as_1_when_seeking_one_char
File.open("foo", "w") do |f|
f << "foobar"
f.rewind
f.seek(1)
assert_equal 1, f.pos
end
end
def test_should_set_pos
File.open("foo", "w") do |f|
f << "foo"
end
fp = File.open("foo", "r")
fp.pos = 1
assert_equal 1, fp.pos
end
def test_should_set_pos_with_tell_method
File.open("foo", "w") do |f|
f << "foo"
end
fp = File.open("foo", "r")
fp.tell = 1
assert_equal 1, fp.pos
end
# Every method in File is in FakeFS::File
RealFile.instance_methods.each do |method_name|
define_method "test_should_have_method_#{method_name}_from_real_file_class" do
assert File.instance_methods.include?(method_name)
end
end
# No methods which are in StringIO that aren't in File are included into
# FakeFS::File (because of inheritence)
uniq_string_io_methods = StringIO.instance_methods - RealFile.instance_methods
uniq_string_io_methods.each do |method_name|
define_method "test_file_should_not_respond_to_#{method_name}" do
assert !File.instance_methods.include?(method_name)
end
end
def test_does_not_remove_methods_from_stringio
stringio = StringIO.new("foo")
assert stringio.respond_to?(:size)
end
def test_chdir_changes_directories_like_a_boss
# I know memes!
FileUtils.mkdir_p '/path'
assert_equal '/', FileSystem.fs.name
assert_equal [], Dir.glob('/path/*')
Dir.chdir '/path' do
File.open('foo', 'w') { |f| f.write 'foo'}
File.open('foobar', 'w') { |f| f.write 'foo'}
end
assert_equal '/', FileSystem.fs.name
assert_equal(['/path/foo', '/path/foobar'], Dir.glob('/path/*').sort)
c = nil
Dir.chdir '/path' do
c = File.open('foo', 'r') { |f| f.read }
end
assert_equal 'foo', c
end
def test_chdir_shouldnt_keep_us_from_absolute_paths
FileUtils.mkdir_p '/path'
Dir.chdir '/path' do
File.open('foo', 'w') { |f| f.write 'foo'}
File.open('/foobar', 'w') { |f| f.write 'foo'}
end
assert_equal ['/path/foo'], Dir.glob('/path/*').sort
assert_equal ['/foobar', '/path'], Dir.glob('/*').sort
Dir.chdir '/path' do
FileUtils.rm('foo')
FileUtils.rm('/foobar')
end
assert_equal [], Dir.glob('/path/*').sort
assert_equal ['/path'], Dir.glob('/*').sort
end
def test_chdir_should_be_nestable
FileUtils.mkdir_p '/path/me'
Dir.chdir '/path' do
File.open('foo', 'w') { |f| f.write 'foo'}
Dir.chdir 'me' do
File.open('foobar', 'w') { |f| f.write 'foo'}
end
end
assert_equal ['/path/foo','/path/me'], Dir.glob('/path/*').sort
assert_equal ['/path/me/foobar'], Dir.glob('/path/me/*').sort
end
def test_chdir_should_flop_over_and_die_if_the_dir_doesnt_exist
assert_raise(Errno::ENOENT) do
Dir.chdir('/nope') do
1
end
end
end
def test_chdir_shouldnt_lose_state_because_of_errors
FileUtils.mkdir_p '/path'
Dir.chdir '/path' do
File.open('foo', 'w') { |f| f.write 'foo'}
File.open('foobar', 'w') { |f| f.write 'foo'}
end