Repository navigation
Expand file tree
/
Copy pathtest_file.rb
More file actions
1385 lines (1214 loc) · 52 KB
/
Copy pathtest_file.rb
File metadata and controls
1385 lines (1214 loc) · 52 KB
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
# -*- coding: utf-8 -*-
require 'test/unit'
require 'test/jruby/test_helper'
require 'fileutils'
require 'tempfile'
class TestFile < Test::Unit::TestCase
include TestHelper
def setup
@teardown_blocks = []
end
def teardown
@teardown_blocks.each do |b|
b.call
end
end
def test_file_separator_constants_defined
assert(File::SEPARATOR)
assert(File::PATH_SEPARATOR)
end
def test_basename
assert_equal("", File.basename(""))
assert_equal("a", File.basename("a"))
assert_equal("b", File.basename("a/b"))
assert_equal("c", File.basename("a/b/c"))
assert_equal("b", File.basename("a/b", ""))
assert_equal("b", File.basename("a/bc", "c"))
assert_equal("b", File.basename("a/b.c", ".c"))
assert_equal("b", File.basename("a/b.c", ".*"))
assert_equal(".c", File.basename("a/.c", ".*"))
assert_equal("a", File.basename("a/"))
assert_equal("b", File.basename("a/b/"))
assert_equal("/", File.basename("/"))
end
def test_basename_uri_like_paths
assert_equal('uri:classloader:/', File.basename('uri:classloader:/'))
assert_equal('uri:classloader://', File.basename('uri:classloader://'))
assert_equal('uri:file:/', File.basename('uri:file:/'))
assert_equal('uri:file://', File.basename('uri:file://'))
assert_equal('classpath:/', File.basename('classpath:/'))
assert_equal('classpath://', File.basename('classpath://'))
assert_equal('file:/', File.basename('file:/'))
assert_equal('file://', File.basename('file://'))
assert_equal('jar:file:/my.jar!/', File.basename('jar:file:/my.jar!/'))
assert_equal('jar:file://my.jar!/', File.basename('jar:file://my.jar!/'))
assert_equal('jar:/my.jar!/', File.basename('jar:/my.jar!/'))
assert_equal('jar://my.jar!/', File.basename('jar://my.jar!/'))
assert_equal('file:/my.jar!/', File.basename('file:/my.jar!/'))
assert_equal('file://my.jar!/', File.basename('file://my.jar!/'))
assert_equal('my.jar!/', File.basename('my.jar!/'))
end if IS_JRUBY
# Added to add more flexibility on windows. Depending on subsystem (msys,
# cygwin, cmd) environment sometimes we have mixed case drive letters. Since
# windows is still case insensitive, downcasing seems a simple solution.
def paths_equal(expected, actual)
if WINDOWS
assert_equal(expected.downcase, actual.downcase)
else
assert_equal(expected, actual)
end
end
def test_expand_path_cross_platform
paths_equal(Dir.pwd, File.expand_path(""))
paths_equal(Dir.pwd, File.expand_path("."))
paths_equal(Dir.pwd, File.expand_path(".", "."))
paths_equal(Dir.pwd, File.expand_path("", "."))
paths_equal(Dir.pwd, File.expand_path(".", ""))
paths_equal(Dir.pwd, File.expand_path("", ""))
paths_equal(File.join(Dir.pwd, "x/y/z/a/b"), File.expand_path("a/b", "x/y/z"))
paths_equal(File.join(Dir.pwd, "bin"), File.expand_path("../../bin", "tmp/x"))
# JRUBY-2143
assert_nothing_raised {
File.expand_path("../../bin", "/tmp/x")
File.expand_path("/bin", "/tmp/x")
File.expand_path("../../bin", "C:/tmp/x")
File.expand_path("../bin", "C:/tmp/x")
}
end
# JRUBY-3849
def test_expand_path_encoding; require 'pathname'
assert_equal(Encoding.find('UTF-8'), File.expand_path('/'.force_encoding('UTF-8')).encoding)
assert_equal(Encoding.find('US-ASCII'), File.expand_path('/'.force_encoding('US-ASCII')).encoding)
assert_equal(Encoding.find('UTF-8'), File.expand_path(Pathname.new('/'.force_encoding('UTF-8'))).encoding)
assert_equal(Encoding.find('US-ASCII'), File.expand_path(Pathname.new('/'.force_encoding('US-ASCII'))).encoding)
end
def test_expand_path_nil
assert_raise(TypeError) { File.expand_path(nil) }
assert_raise(TypeError) { File.expand_path(nil, "/") }
assert_raise(TypeError) { File.expand_path(nil, nil) }
end
def test_copy_file_and_preserve
FileUtils.touch("original")
FileUtils.copy_file('original', 'copy', true)
assert(File.exist?('original'))
assert(File.exist?('copy'))
ensure
FileUtils.rm_rf %w[ original copy ]
end
# JRUBY-1116: these are currently broken on windows
# what are these testing anyway?!?!
if WINDOWS
def test_windows_basename
assert_equal "", File.basename("c:")
assert_equal "\\", File.basename("c:\\")
assert_equal "abc", File.basename("c:abc")
end
def test_windows_expand_file_nul
assert_equal "//./nul", File.expand_path("nul:")
assert_equal "//./nul", File.expand_path("nul")
assert_equal "//./NUL", File.expand_path("NUL")
assert_equal "//./NUL", File.expand_path("NUL:")
end
# JRUBY-2052: this is important for fileutils
def test_windows_dirname
assert_equal("C:/", File.dirname("C:/"))
assert_equal("C:\\", File.dirname("C:\\"))
assert_equal("C:/", File.dirname("C:///////"))
assert_equal("C:\\", File.dirname("C:\\\\\\\\"))
assert_equal("C:/", File.dirname("C:///////blah"))
assert_equal("C:\\", File.dirname("C:\\\\\\\\blah"))
assert_equal("C:.", File.dirname("C:blah"))
assert_equal "C:/", File.dirname("C:/temp/")
assert_equal "c:\\", File.dirname('c:\\temp')
assert_equal "C:.", File.dirname("C:")
assert_equal "C:/temp", File.dirname("C:/temp/foobar.txt")
end
def test_windows_network_path
assert_equal("\\\\network\\share", File.dirname("\\\\network\\share\\file.bat"))
assert_equal("\\\\network\\share", File.dirname("\\\\network\\share"))
assert_equal("\\\\localhost\\c$", File.dirname("\\\\localhost\\c$\\boot.bat"))
assert_equal("\\\\localhost\\c$", File.dirname("\\\\localhost\\c$"))
end
def test_expand_path_windows
assert_equal("C:/", File.expand_path("C:/"))
assert_equal("C:/dir", File.expand_path("C:/dir"))
assert_equal("C:/dir", File.expand_path("C:/dir/two/../"))
assert_equal("C:/dir/two", File.expand_path("C:/dir/two/", "D:/"))
assert_equal("C:/", File.expand_path("C:/", nil))
# JRUBY-2161
assert_equal("C:/", File.expand_path("C:/dir/../"))
assert_equal("C:/", File.expand_path("C:/.."))
assert_equal("C:/", File.expand_path("C:/../../"))
assert_equal("C:/", File.expand_path("..", "C:/"))
assert_equal("C:/", File.expand_path("..", "C:"))
assert_equal("C:/", File.expand_path("C:/dir/two/../../"))
assert_equal("C:/", File.expand_path("C:/dir/two/../../../../../"))
# JRUBY-546
current_drive_letter = Dir.pwd[0..2]
paths_equal(current_drive_letter, File.expand_path(".", "/"))
paths_equal(current_drive_letter, File.expand_path("..", "/"))
paths_equal(current_drive_letter, File.expand_path("/", "/"))
paths_equal(current_drive_letter, File.expand_path("../..", "/"))
paths_equal(current_drive_letter, File.expand_path("../..", "/dir/two"))
paths_equal(current_drive_letter + "dir", File.expand_path("../..", "/dir/two/three"))
paths_equal(current_drive_letter, File.expand_path("/../..", "/"))
paths_equal(current_drive_letter + "hello", File.expand_path("hello", "/"))
paths_equal(current_drive_letter, File.expand_path("hello/..", "/"))
paths_equal(current_drive_letter, File.expand_path("hello/../../..", "/"))
paths_equal(current_drive_letter + "three/four", File.expand_path("/three/four", "/dir/two"))
paths_equal(current_drive_letter + "two", File.expand_path("/two", "/one"))
paths_equal(current_drive_letter + "three/four", File.expand_path("/three/four", "/dir/two"))
assert_equal("C:/two", File.expand_path("/two", "C:/one"))
assert_equal("C:/two", File.expand_path("/two", "C:/one/.."))
assert_equal("C:/", File.expand_path("/two/..", "C:/one/.."))
assert_equal("C:/", File.expand_path("/two/..", "C:/one"))
assert_equal("//two", File.expand_path("//two", "/one"))
assert_equal("//two", File.expand_path("//two", "//one"))
assert_equal("//two", File.expand_path("//two", "///one"))
assert_equal("//two", File.expand_path("//two", "////one"))
assert_equal("//", File.expand_path("//", "//one"))
# Corner cases that fail with JRuby on Windows (but pass with MRI 1.8.6)
#
### assert_equal("///two", File.expand_path("///two", "/one"))
### assert_equal("///two", File.expand_path("///two/..", "/one"))
### assert_equal("////two", File.expand_path("////two", "/one"))
### assert_equal("////two", File.expand_path("////two", "//one"))
### assert_equal("//two", File.expand_path("//two/..", "/one"))
### assert_equal("////two", File.expand_path("////two/..", "/one"))
#
### assert_equal("//bar/foo", File.expand_path("../foo", "//bar"))
### assert_equal("///bar/foo", File.expand_path("../foo", "///bar"))
### assert_equal("//one/two", File.expand_path("/two", "//one"))
end
# JRUBY-3132
def test_realpath_windows
assert_equal('C:/', File.realpath('C:/'))
assert_equal('C:/', File.realpath('C:\\'))
end
else
def test_expand_path
assert_equal("/bin", File.expand_path("../../bin", "/foo/bar"))
assert_equal("/foo/bin", File.expand_path("../bin", "/foo/bar"))
assert_equal("//abc/def/jkl/mno", File.expand_path("//abc//def/jkl//mno"))
assert_equal("//abc/def/jkl/mno/foo", File.expand_path("foo", "//abc//def/jkl//mno"))
begin
File.expand_path("~nonexistent")
assert(false)
rescue ArgumentError => e
assert(true)
rescue Exception => e
assert(false)
end
assert_equal("/bin", File.expand_path("../../bin", "/tmp/x"))
assert_equal("/bin", File.expand_path("../../bin", "/tmp"))
assert_equal("/bin", File.expand_path("../../bin", "/"))
assert_equal("/bin", File.expand_path("../../../../../../../bin", "/"))
assert_equal(File.join(Dir.pwd, "x/y/z/a/b"), File.expand_path("a/b", "x/y/z"))
assert_equal(File.join(Dir.pwd, "bin"), File.expand_path("../../bin", "tmp/x"))
assert_equal("/bin", File.expand_path("./../foo/./.././../bin", "/a/b"))
# JRUBY-2160
assert_equal("/dir1/subdir", File.expand_path("/dir1/subdir/stuff/../"))
assert_equal("///fedora/stuff/blah/MORE", File.expand_path("///fedora///stuff/blah/again//..////MORE/"))
assert_equal("/", File.expand_path("/dir1/../"))
assert_equal("/", File.expand_path("/"))
assert_equal("/", File.expand_path("/.."))
assert_equal("/hello", File.expand_path("/hello/world/three/../../"))
assert_equal("/dir/two", File.expand_path("", "/dir/two"))
assert_equal("/dir", File.expand_path("..", "/dir/two"))
assert_equal("/file/abs", File.expand_path("/file/abs", '/abs/dir/here'))
assert_equal("/", File.expand_path("/", nil))
assert_equal("//two", File.expand_path("//two", "//one"))
assert_equal("/", File.expand_path("/two/..", "//one"))
end
def test_expand_path_with_slashes
assert_equal '///x/bar', File.expand_path("..///bar/", '///x/////y///')
assert_equal '/oo/bar', File.expand_path("/oo///bar/", '///x/////y///')
# Corner cases that fail with JRuby on Linux (but pass with MRI 2.5)
#
#assert_equal("/", File.expand_path("//two/..", "//one"))
#assert_equal("/", File.expand_path("///two/..", "//one"))
#assert_equal("/blah", File.expand_path("///two/../blah", "//one"))
end
def test_expand_path_with_file_prefix_slash
assert_equal "file:/foo/bar", File.expand_path("file:/foo/bar")
assert_equal "file:/bar", File.expand_path("file:/foo/../bar")
assert_equal "file:/foo/bar/baz", File.expand_path("baz", "file:/foo/bar")
assert_equal "file:/foo/bar", File.expand_path("file:/foo/bar", "file:/baz/quux")
assert_equal "file:/foo/bar", File.expand_path("../../foo/bar", "file:/baz/quux")
assert_equal "file:/foo/bar", File.expand_path("../../../foo/bar", "file:/baz/quux")
end if IS_JRUBY
def test_expand_path_with_file_prefix_slash_slash
assert_equal "file://foo/bar", File.expand_path("file://foo/bar")
assert_equal "file://bar", File.expand_path("file://foo/../bar")
assert_equal "file://foo/bar/baz", File.expand_path("baz", "file://foo/bar")
# TODO
#assert_equal "file://foo/bar", File.expand_path("file://foo/bar", "file://baz/quux")
assert_equal "file://baz/foo/bar", File.expand_path("../../foo/bar", "file://baz/quux")
assert_equal "file://baz/foo/bar", File.expand_path("../../../foo/bar", "file://baz/quux")
end if IS_JRUBY
def test_expand_path_with_file_prefix_slash_slash_slash
assert_equal "file:///foo/bar", File.expand_path("file:///foo/bar")
assert_equal "file:///bar", File.expand_path("file:///foo/../bar")
assert_equal "file:///foo/bar/baz", File.expand_path("baz", "file:///foo/bar")
assert_equal "file:///foo/bar", File.expand_path("file:///foo/bar", "file:///baz/quux")
assert_equal "file:///foo/bar", File.expand_path("../../foo/bar", "file:////baz/quux")
assert_equal "file:///foo/bar", File.expand_path("../../../foo/bar", "file:///baz/quux")
end if IS_JRUBY
def test_expand_path_as_jar_with_file_prefix
assert_equal "file:/my.jar!/foo/bar", File.expand_path("file:/my.jar!/foo/bar")
assert_equal "file:/my.jar!/foo/bar", File.expand_path("file:/my.jar!/../foo/bar")
assert_equal "file:/my.jar!/bar", File.expand_path("file:/my.jar!/foo/../bar")
assert_equal "file:/my.jar!/foo/bar/baz", File.expand_path("baz", "file:/my.jar!/foo/bar")
assert_equal "file:/my.jar!/foo/bar", File.expand_path("file:/my.jar!/foo/bar", "file:/my.jar!/baz/quux")
assert_equal "file:/my.jar!/foo/bar", File.expand_path("../../foo/bar", "file:/my.jar!/baz/quux")
#assert_equal "file:/my.jar!/foo/bar", File.expand_path("../../../foo/bar", "file:/my.jar!/baz/quux")
end if IS_JRUBY
def test_expand_path_with_jar_file_prefix
assert_equal "jar:file:/my.jar!/foo/bar", File.expand_path("jar:file:/my.jar!/foo/bar")
assert_equal "jar:file:/my.jar!/bar", File.expand_path("jar:file:/my.jar!/foo/../bar")
assert_equal "jar:file:/my.jar!/foo/bar/baz", File.expand_path("baz", "jar:file:/my.jar!/foo/bar")
assert_equal "jar:file:/my.jar!/foo/bar", File.expand_path("jar:file:/my.jar!/foo/bar", "file:/my.jar!/baz/quux")
assert_equal "jar:file:/my.jar!/foo/bar", File.expand_path("../../foo/bar", "jar:file:/my.jar!/baz/quux")
#assert_equal "jar:file:/my.jar!/foo/bar", File.expand_path("../../../foo/bar", "jar:file:/my.jar!/baz/quux")
end if IS_JRUBY
def test_expand_path_with_uri_file_prefix_slash
# file-protocol file://host/path, treated as file:/path
assert_equal "uri:file:/foo/bar", File.expand_path("uri:file:/foo/bar")
#assert_equal "uri:file:/bar", File.expand_path("uri:file:/foo/../bar")
# TODO why uri:file:// ?
assert_equal "uri:file://foo/bar/baz", File.expand_path("baz", "uri:file:/foo/bar")
assert_equal "uri:file:/foo/bar", File.expand_path("uri:file:/foo/bar", "uri:file:/baz/quux")
# TODO why uri:file:// ?
assert_equal "uri:file://foo/bar", File.expand_path("../../foo/bar", "uri:file:/baz/quux")
assert_equal "uri:file:/foo/bar", File.expand_path("../../../foo/bar", "uri:file:/baz/quux")
end if IS_JRUBY
def test_expand_path_with_uri_file_prefix_slash_slash
# file-protocol file://host/path
assert_equal "uri:file://foo/bar", File.expand_path("uri:file://foo/bar")
assert_equal "uri:file://bar", File.expand_path("uri:file://foo/../bar")
assert_equal "uri:file://foo/bar/baz", File.expand_path("baz", "uri:file://foo/bar")
assert_equal "uri:file://foo/bar", File.expand_path("uri:file://foo/bar", "uri:file://baz/quux")
assert_equal "uri:file://foo/bar", File.expand_path("../../foo/bar", "uri:file://baz//quux")
# TODO remove the use of JRubyFile
#assert_equal "uri:file://foo/bar", File.expand_path("../../../foo/bar", "uri:file://baz/quux")
end if IS_JRUBY
def test_expand_path_with_uri_file_prefix_slash_slash_slash
# file-protocol file://host/path i.e. host is empty string here
assert_equal "uri:file:///foo/bar", File.expand_path("uri:file:///foo/bar")
assert_equal "uri:file:///bar", File.expand_path("uri:file:///foo/../bar")
# TODO remove the use of JRubyFile
#assert_equal "uri:file:///foo/bar/baz", File.expand_path("baz", "uri:file:///foo/bar")
assert_equal "uri:file:///foo/bar", File.expand_path("uri:file:///foo/bar", "uri:file://baz/quux")
#assert_equal "uri:file:///foo/bar", File.expand_path("../../foo/bar", "uri:file:///baz//quux")
#assert_equal "uri:file:///foo/bar", File.expand_path("../../../foo/bar", "uri:file:///baz/quux")
end if IS_JRUBY
def test_expand_path_with_uri_classloader_prefix
assert_equal "uri:classloader:/foo/bar", File.expand_path("uri:classloader:/foo/bar")
assert_equal "uri:classloader:/bar", File.expand_path("uri:classloader:/foo/../bar")
assert_equal "uri:classloader:/foo/bar/baz", File.expand_path("baz", "uri:classloader:/foo/bar")
assert_equal "uri:classloader:/foo/bar", File.expand_path("uri:classloader:/foo/bar", "uri:classloader:/baz/quux")
assert_equal "uri:classloader:/foo/bar", File.expand_path("../../foo/bar", "uri:classloader:/baz/quux")
assert_equal "uri:classloader:/foo/bar", File.expand_path("../../../foo/bar", "uri:classloader:/baz/quux")
end if IS_JRUBY
def test_expand_path_with_classpath_prefix
assert_equal "classpath:/foo/bar", File.expand_path("classpath:/foo/bar")
assert_equal "classpath:/bar", File.expand_path("classpath:/foo/../bar")
assert_equal "classpath:/foo/bar/baz", File.expand_path("baz", "classpath:/foo/bar")
assert_equal "classpath:/foo/bar", File.expand_path("classpath:/foo/bar", "classpath:/baz/quux")
assert_equal "classpath:/foo/bar", File.expand_path("../../foo/bar", "classpath:/baz/quux")
assert_equal "classpath:/foo/bar", File.expand_path("../../../foo/bar", "classpath:/baz/quux")
end if IS_JRUBY
def test_expand_path_with_file_url_relative_path
assert_equal "file:#{Dir.pwd}/foo/bar", File.expand_path("file:foo/bar")
end if IS_JRUBY
# GH-3176
def test_expand_path_with_relative_reference_and_inside_uri_classloader
Dir.chdir( 'uri:classloader:/') do
assert_equal 'uri:classloader:/something/foo', File.expand_path('foo', 'something')
assert_equal 'uri:classloader:/foo', File.expand_path('foo', '.')
end
end if IS_JRUBY
# JRUBY-5219
def test_expand_path_looks_like_url
assert_equal "classpath:/META-INF/jruby.home", File.expand_path("classpath:/META-INF/jruby.home")
# file-protocol file://host/path
assert_equal "uri:file://META-INF/jruby.home", File.expand_path("uri:file://META-INF/jruby.home")
assert_equal "http://example.com/a.jar", File.expand_path("http://example.com/a.jar")
assert_equal "http://example.com/", File.expand_path("..", "http://example.com/a.jar")
assert_equal "classpath:/foo/bar/baz", File.expand_path("baz", "classpath:/foo/bar")
assert_equal "classpath:/foo/bar", File.expand_path("classpath:/foo/bar", "classpath:/baz/quux")
assert_equal "classpath:/foo", File.expand_path("..", "classpath:/foo/bar")
assert_equal "uri:jar://foo/bar/baz", File.expand_path("baz", "uri:jar://foo/bar")
# file-protocol file://host/path
assert_equal "uri:file://foo/bar", File.expand_path("uri:file://foo/bar", "uri:file://baz/quux")
assert_equal "uri:jar://foo", File.expand_path("..", "uri:jar://foo/bar")
end if IS_JRUBY
def test_mkdir_with_non_file_uri_raises_error
assert_raises(Errno::ENOTDIR) { FileUtils.mkdir_p("classpath:/META-INF/MANIFEST.MF") }
assert !File.directory?("classpath:/META-INF/MANIFEST.MF")
end if IS_JRUBY
def test_mkdir_with_file_uri_works_as_expected
FileUtils.mkdir("file:test_mkdir_with_file_uri_works_as_expected")
assert File.directory?("test_mkdir_with_file_uri_works_as_expected")
assert File.directory?("file:test_mkdir_with_file_uri_works_as_expected")
ensure
FileUtils.rm_rf("test_mkdir_with_file_uri_works_as_expected")
end if IS_JRUBY
# GH-2972
def test_mkdir_with_file_uri_and_absolute_path_works_as_expected
# the extra slashes were the problem
FileUtils.mkdir("file://#{Dir.pwd}/test_mkdir_with_file_uri_works_as_expected")
assert File.directory?("#{Dir.pwd}/test_mkdir_with_file_uri_works_as_expected")
ensure
FileUtils.rm_rf("#{Dir.pwd}/test_mkdir_with_file_uri_works_as_expected")
end if IS_JRUBY
def test_expand_path_corner_case
# this would fail on MRI 1.8.6 (MRI returns "/foo").
assert_equal("//foo", File.expand_path("../foo", "//bar"))
end
def test_realpath
assert_equal('/', File.realpath('/'))
end
end # if WINDOWS else ... end
def test_paths_do_not_get_normalized_on_non_windows
# Linux doesn't mind '\' in file/dir names :
Dir.mkdir backslash = '_back\\slash'
path = File.expand_path backslash
assert path.end_with?(backslash), "path: #{path.inspect} does not end with: #{backslash.inspect}"
path = File.realpath path
assert path.end_with?(backslash), "path: #{path.inspect} does not end with: #{backslash.inspect}"
File.realpath backslash
ensure
Dir.rmdir '_back\\slash' rescue nil
end unless WINDOWS
def test_dirname
assert_equal(".", File.dirname(""))
assert_equal(".", File.dirname("."))
assert_equal(".", File.dirname(".."))
assert_equal(".", File.dirname("a"))
assert_equal(".", File.dirname("./a"))
assert_equal("./a", File.dirname("./a/b"))
assert_equal("/", File.dirname("/"))
assert_equal("/", File.dirname("/a"))
assert_equal("/a", File.dirname("/a/b"))
assert_equal("/a", File.dirname("/a/b/"))
assert_equal("/", File.dirname("/"))
end
def test_dirname_file_protocol
assert_equal("file:/a", File.dirname("file:/a/b"))
assert_equal("file:/", File.dirname("file:/a"))
assert_equal("file:/", File.dirname("file:/"))
assert_equal("uri:file:/a", File.dirname("uri:file:/a/b"))
assert_equal("uri:file:/", File.dirname("uri:file:/a"))
assert_equal("uri:file:/", File.dirname("uri:file:/"))
end if IS_JRUBY
def test_dirname_uri_classloader_protocol
assert_equal("uri:classloader:/a", File.dirname("uri:classloader:/a/b"))
assert_equal("uri:classloader:/", File.dirname("uri:classloader:/a"))
assert_equal("uri:classloader:/", File.dirname("uri:classloader:/"))
end if IS_JRUBY
def test_dirname_jar_protocol
assert_equal("/my.jar!/a", File.dirname("/my.jar!/a/b"))
assert_equal("/my.jar!", File.dirname("/my.jar!/a"))
assert_equal("/my.jar!", File.dirname("/my.jar!/"))
assert_equal("file:/my.jar!/a", File.dirname("file:/my.jar!/a/b"))
assert_equal("file:/my.jar!", File.dirname("file:/my.jar!/a"))
assert_equal("file:/my.jar!", File.dirname("file:/my.jar!/"))
assert_equal("jar:file:/my.jar!/a", File.dirname("jar:file:/my.jar!/a/b"))
assert_equal("jar:file:/my.jar!", File.dirname("jar:file:/my.jar!/a"))
assert_equal("jar:file:/my.jar!", File.dirname("jar:file:/my.jar!/"))
end if IS_JRUBY
def test_extname
assert_equal("", File.extname(""))
assert_equal("", File.extname("abc"))
assert_equal(".foo", File.extname("abc.foo"))
assert_equal(".foo", File.extname("abc.bar.foo"))
assert_equal("", File.extname("abc.bar/foo"))
assert_equal("", File.extname(".bashrc"))
assert_equal("", File.extname("."))
assert_equal("", File.extname("/."))
assert_equal("", File.extname(".."))
assert_equal(".", File.extname(".foo."))
assert_equal(".", File.extname("foo."))
end
def test_fnmatch
assert_equal(true, File.fnmatch('cat', 'cat'))
assert_equal(false, File.fnmatch('cat', 'category'))
assert_equal(false, File.fnmatch('c{at,ub}s', 'cats'))
assert_equal(false, File.fnmatch('c{at,ub}s', 'cubs'))
assert_equal(false, File.fnmatch('c{at,ub}s', 'cat'))
assert_equal(true, File.fnmatch('c?t', 'cat'))
assert_equal(false, File.fnmatch('c\?t', 'cat'))
assert_equal(true, File.fnmatch('c\?t', 'c?t'))
assert_equal(false, File.fnmatch('c??t', 'cat'))
assert_equal(true, File.fnmatch('c*', 'cats'));
assert_equal(true, File.fnmatch('c*t', 'cat'))
#assert_equal(true, File.fnmatch('c\at', 'cat')) # Doesn't work correctly on both Unix and Win32
assert_equal(false, File.fnmatch('c\at', 'cat', File::FNM_NOESCAPE))
assert_equal(true, File.fnmatch('a?b', 'a/b'))
assert_equal(false, File.fnmatch('a?b', 'a/b', File::FNM_PATHNAME))
assert_equal(false, File.fnmatch('a?b', 'a/b', File::FNM_PATHNAME))
assert_equal(false, File.fnmatch('*', '.profile'))
assert_equal(true, File.fnmatch('*', '.profile', File::FNM_DOTMATCH))
assert_equal(true, File.fnmatch('*', 'dave/.profile'))
assert_equal(true, File.fnmatch('*', 'dave/.profile', File::FNM_DOTMATCH))
assert_equal(false, File.fnmatch('*', 'dave/.profile', File::FNM_PATHNAME))
assert_equal(false, File.fnmatch("/.ht*",""))
assert_equal(false, File.fnmatch("/*~",""))
assert_equal(false, File.fnmatch("/.ht*","/"))
assert_equal(false, File.fnmatch("/*~","/"))
assert_equal(false, File.fnmatch("/.ht*",""))
assert_equal(false, File.fnmatch("/*~",""))
assert_equal(false, File.fnmatch("/.ht*","/stylesheets"))
assert_equal(false, File.fnmatch("/*~","/stylesheets"))
assert_equal(false, File.fnmatch("/.ht*",""))
assert_equal(false, File.fnmatch("/*~",""))
assert_equal(false, File.fnmatch("/.ht*","/favicon.ico"))
assert_equal(false, File.fnmatch("/*~","/favicon.ico"))
# JRUBY-1986, make sure that fnmatch is sharing aware
assert_equal(true, File.fnmatch("foobar"[/foo(.*)/, 1], "bar"))
end
# JRUBY-2196
def test_fnmatch_double_star
assert(File.fnmatch('**/foo', 'a/b/c/foo', File::FNM_PATHNAME))
assert(File.fnmatch('**/foo', '/foo', File::FNM_PATHNAME))
assert(!File.fnmatch('**/foo', 'a/.b/c/foo', File::FNM_PATHNAME))
assert(File.fnmatch('**/foo', 'a/.b/c/foo', File::FNM_PATHNAME | File::FNM_DOTMATCH))
assert(File.fnmatch('**/foo', '/root/foo', File::FNM_PATHNAME))
assert(File.fnmatch('**/foo', 'c:/root/foo', File::FNM_PATHNAME))
assert(File.fnmatch("lib/**/*.rb", "lib/a.rb", File::FNM_PATHNAME | File::FNM_DOTMATCH))
assert(File.fnmatch("lib/**/*.rb", "lib/a/b.rb", File::FNM_PATHNAME | File::FNM_DOTMATCH))
assert(File.fnmatch('**/b/**/*', 'c/a/b/c/t', File::FNM_PATHNAME))
assert(File.fnmatch('c/**/b/**/*', 'c/a/b/c/t', File::FNM_PATHNAME))
assert(File.fnmatch('c**/**/b/**/*', 'c/a/b/c/t', File::FNM_PATHNAME))
assert(File.fnmatch('h**o/**/b/**/*', 'hello/a/b/c/t', File::FNM_PATHNAME))
assert(!File.fnmatch('h**o/**/b/**', 'hello/a/b/c/t', File::FNM_PATHNAME))
assert(File.fnmatch('**', 'hello', File::FNM_PATHNAME))
assert(!File.fnmatch('**/', 'hello', File::FNM_PATHNAME))
assert(File.fnmatch('**/*', 'hello', File::FNM_PATHNAME))
assert(File.fnmatch("**/*", "one/two/three/four", File::FNM_PATHNAME))
assert(!File.fnmatch("**", "one/two/three", File::FNM_PATHNAME))
assert(!File.fnmatch("**/three", ".one/two/three", File::FNM_PATHNAME))
assert(File.fnmatch("**/three", ".one/two/three", File::FNM_PATHNAME | File::FNM_DOTMATCH))
assert(!File.fnmatch("*/**", "one/two/three", File::FNM_PATHNAME))
assert(!File.fnmatch("*/**/", "one/two/three", File::FNM_PATHNAME))
assert(File.fnmatch("*/**/*", "one/two/three", File::FNM_PATHNAME))
assert(File.fnmatch("**/*", ".one/two/three/four", File::FNM_PATHNAME | File::FNM_DOTMATCH))
assert(!File.fnmatch("**/.one/*", ".one/.two/.three/.four", File::FNM_PATHNAME | File::FNM_DOTMATCH))
end
# JRUBY-2199
def test_fnmatch_bracket_pattern
assert(!File.fnmatch('[a-z]', 'D'))
assert(File.fnmatch('[a-z]', 'D', File::FNM_CASEFOLD))
assert(!File.fnmatch('[a-zA-Y]', 'Z'))
assert(File.fnmatch('[^a-zA-Y]', 'Z'))
assert(File.fnmatch('[a-zA-Y]', 'Z', File::FNM_CASEFOLD))
assert(File.fnmatch('[a-zA-Z]', 'Z'))
assert(File.fnmatch('[a-zA-Z]', 'A'))
assert(File.fnmatch('[a-zA-Z]', 'a'))
assert(!File.fnmatch('[b-zA-Z]', 'a'))
assert(File.fnmatch('[^b-zA-Z]', 'a'))
assert(File.fnmatch('[b-zA-Z]', 'a', File::FNM_CASEFOLD))
end
def test_join
[
["a", "b", "c", "d"],
["a"],
[],
["a", "b", "..", "c"]
].each do |a|
assert_equal(a.join(File::SEPARATOR), File.join(*a))
end
assert_equal("////heh////bar/heh", File.join("////heh////bar", "heh"))
assert_equal("/heh/bar/heh", File.join("/heh/bar/", "heh"))
assert_equal("/heh/bar/heh", File.join("/heh/bar/", "/heh"))
assert_equal("/heh//bar/heh", File.join("/heh//bar/", "/heh"))
assert_equal("/heh/bar/heh", File.join("/heh/", "/bar/", "/heh"))
assert_equal("/HEH/BAR/FOO/HOH", File.join("/HEH", ["/BAR", "FOO"], "HOH"))
assert_equal("/heh/bar", File.join("/heh//", "/bar"))
assert_equal("/heh///bar", File.join("/heh", "///bar"))
end
def test_split
assert_equal([".", ""], File.split(""))
assert_equal([".", "."], File.split("."))
assert_equal([".", ".."], File.split(".."))
assert_equal(["/", "/"], File.split("/"))
assert_equal([".", "a"], File.split("a"))
assert_equal([".", "a"], File.split("a/"))
assert_equal(["a", "b"], File.split("a/b"))
assert_equal(["a/b", "c"], File.split("a/b/c"))
assert_equal(["/", "a"], File.split("/a"))
assert_equal(["/", "a"], File.split("/a/"))
assert_equal(["/a", "b"], File.split("/a/b"))
assert_equal(["/a/b", "c"], File.split("/a/b/c"))
#assert_equal(["//", "a"], File.split("//a"))
assert_equal(["../a/..", "b"], File.split("../a/../b/"))
end
def test_io_readlines
# IO#readlines, IO::readlines, open, close, delete, ...
assert_raises(Errno::ENOENT) { File.open("NO_SUCH_FILE_EVER") }
f = open("testFile_tmp", "w")
f.write("one\ntwo\nthree\n")
f.close
f = open("testFile_tmp")
assert_equal(["one", "two", "three"],
f.readlines.collect {|l| l.strip })
f.close
assert_equal(["one", "two", "three"],
IO.readlines("testFile_tmp").collect {|l| l.strip })
assert(File.delete("testFile_tmp"))
end
def test_mkdir
begin
Dir.mkdir("dir_tmp")
assert(File.lstat("dir_tmp").directory?)
ensure
Dir.rmdir("dir_tmp")
end
end
def test_directory_query # - directory?
begin
Dir.mkdir("dir_tmp")
assert(File.directory?("dir_tmp"))
assert(! File.directory?("test/jruby/test_file.rb"))
assert(! File.directory?("dir_not_tmp"))
result = jruby("-e \"print File.directory?('dir_not_tmp');print File.directory?('dir_tmp');print File.directory?('test/jruby/test_file.rb')\"", 'jruby.native.enabled' => 'false')
assert_equal(result, 'falsetruefalse')
ensure
Dir.rmdir("dir_tmp") rescue nil
end
end
# ensure that uri:file: and file: URIs are treated the same by File.directory?
def test_uri_file_directory_query
["uri:file://", "uri:file:/#{Dir.pwd}"].each do |uri|
assert(File.directory?(uri), uri)
end
end
def test_file_query # - file?
assert(File.file?('test/jruby/test_file.rb'))
assert(! File.file?('test'))
assert(! File.file?('test_not'))
result = jruby("-e \"print File.file?('test_not');print File.file?('test');print File.file?('test/jruby/test_file.rb')\"", 'jruby.native.enabled' => 'false' )
assert_equal('falsefalsetrue', result)
end
def test_readable_query # - readable?
assert(File.readable?('test/jruby/test_file.rb'))
assert(File.readable?('test'))
assert(! File.readable?('test_not'))
result = jruby("-e \"print File.readable?('test_not');print File.readable?('test');print File.readable?('test/jruby/test_file.rb')\"", 'jruby.native.enabled' => 'false' )
assert_equal('falsetruetrue', result)
end
[ :executable?, :executable_real? ].each do |method|
define_method :"test_#{method}_query" do # - executable?/executable_real?
if WINDOWS
exec_file = 'bin/jruby.bat'
else
exec_file = 'bin/jruby.sh'
end
assert(File.send(method, exec_file))
assert(!File.send(method, 'test/test_file.rb'))
assert(File.send(method, 'test'))
assert(!File.send(method, 'test_not'))
result = jruby("-e \"print File.#{method}('#{exec_file}');print File.#{method}('test_not');print File.#{method}('test');print File.#{method}('test/test_file.rb')\"", 'jruby.native.enabled' => 'false' )
assert_equal('truefalsetruefalse', result)
end
end
def test_file_exist_query
assert(File.exist?('test'))
assert(! File.exist?('test_not'))
assert(jruby("-e \"print File.exists?('test_not');print File.exists?('test')\"", 'jruby.native.enabled' => 'false' ) == 'falsetrue')
end
def test_file_exist_in_jar_file
assert(File.exist?("file:" + File.expand_path("test/jruby/dir with spaces/test_jar.jar") + "!/abc/foo.rb"))
assert(File.exist?("file:" + File.expand_path("test/jruby/dir with spaces/test_jar.jar") + "!/inside_jar.rb"))
assert(!File.exist?("file:" + File.expand_path("test/jruby/dir with spaces/test_jar.jar") + "!/inside_jar2.rb"))
assert(File.exist?("file:" + File.expand_path("test/jruby/dir with spaces/test_jar.jar") + "!/"))
end if IS_JRUBY
def with_load_path(entry)
begin
$LOAD_PATH.unshift entry
yield
ensure
$LOAD_PATH.shift
end
end
def test_require_from_jar_url_with_spaces_in_load_path
assert_nothing_raised do
with_load_path("file:" + File.expand_path("test/jruby/dir with spaces/test_jar.jar") + "!/abc") do
assert require('foo')
assert $LOADED_FEATURES.pop =~ /foo\.rb$/
end
with_load_path("file:" + File.expand_path("test/jruby/dir with spaces/test_jar.jar") + "!") do
assert require('abc/foo')
assert $LOADED_FEATURES.pop =~ /foo\.rb$/
end
with_load_path(File.expand_path("test/jruby/dir with spaces/test_jar.jar!/")) do
assert require('abc/foo')
assert $LOADED_FEATURES.pop =~ /foo\.rb$/
end
end
end if IS_JRUBY
def test_file_read_in_jar_file
assert_equal("foobarx\n", File.read("file:" + File.expand_path("test/jruby/test_jar2.jar") + "!/test_value.rb"))
assert_raises(Errno::ENOENT) do
File.read("file:" + File.expand_path("test/jruby/test_jar2.jar") + "!/inside_jar2.rb")
end
assert_raises(Errno::ENOENT) do
File.read("file:" + File.expand_path("test/jruby/test_jar3.jar") + "!/inside_jar2.rb")
end
val = ""
open("file:" + File.expand_path("test/jruby/test_jar2.jar") + "!/test_value.rb") do |f|
val = f.read
end
assert_equal "foobarx\n", val
values = ""
File.open("file:" + File.expand_path("test/jruby/test_jar2.jar") + "!/test_value.rb") do |f|
f.each do |s|
values << s
end
end
assert_equal "foobarx\n", values
end if IS_JRUBY
# JRUBY-2357
def test_truncate_file_in_jar_file
File.open("file:" + File.expand_path("test/jruby/test_jar2.jar") + "!/test_value.rb", "r+") do |f|
assert_raise(Errno::EINVAL) { f.truncate(2) }
end
end if IS_JRUBY
# JRUBY-1886
def test_file_truncated_after_changing_directory
subdir = "./testDir_1"
Dir.mkdir(subdir)
Dir.chdir(subdir) { |dir|
begin
file = File.open("__dummy_file.txt", "wb")
file.write("dummy text")
file.close
assert_nothing_raised { File.truncate(file.path, 0) }
ensure
file.close rescue nil
File.unlink(file.path)
end
}
ensure
Dir.rmdir(subdir)
end
def test_file_size_query
assert(File.size?('pom.xml'))
end
# JRUBY-2275
def test_file_size_empty_file
filename = "__empty_test__file"
File.open(filename, "w+") { }
assert_equal(nil, File.size?(filename))
assert_equal(nil, FileTest.size?(filename))
assert_equal(0, File.size(filename))
assert_equal(0, FileTest.size(filename))
ensure
File.delete(filename)
end
# JRUBY-2524
def test_filetest_exists_uri_prefixes
assert(!FileTest.exists?("file:/!"))
end if IS_JRUBY
def test_file_exists_uri_prefixes
assert(File.exists?("file:test/jruby/dir with spaces/test_jar.jar!/abc/foo.rb"))
assert(File.exists?("jar:file:test/jruby/dir with spaces/test_jar.jar!/abc/foo.rb"))
assert(File.exists?("jar:file:test/jruby/dir with spaces/./test_jar.jar!/abc/./foo/../foo.rb"))
end if IS_JRUBY
# JRUBY-2524
def test_file_stat_uri_prefixes
assert_raise(Errno::ENOENT) do
File.lstat("file:")
end
assert_raise(Errno::ENOENT) do
File.lstat("file:!")
end
assert_raise(Errno::ENOENT) do
File.stat("file:")
end
assert_raise(Errno::ENOENT) do
File.stat("file:!")
end
end
# JRUBY-2524
def test_file_time_uri_prefixes
assert_raise(Errno::ENOENT) do
File.atime("file:")
end
assert_raise(Errno::ENOENT) do
File.atime("file:!")
end
assert_raise(Errno::ENOENT) do
File.ctime("file:")
end
assert_raise(Errno::ENOENT) do
File.ctime("file:!")
end
end
def test_file_open_utime
filename = "__test__file"
File.open(filename, "w") {|f| }
time = Time.now - 3600
begin
File.utime(time, time, filename)
rescue Object => o
o.printStackTrace
o.getCause.printStackTrace
end
# File mtime resolution may not be sub-second on all platforms (e.g., windows)
# allow for some slop
assert((time.to_i - File.atime(filename).to_i).abs < 5)
assert((time.to_i - File.mtime(filename).to_i).abs < 5)
File.unlink(filename)
end
def test_file_utime_nil
filename = '__test__file'
File.open(filename, 'w') {|f| }
time = File.mtime(filename)
sleep 2
File.utime(nil, nil, filename)
assert((File.atime(filename).to_i - time.to_i) >= 2)
assert((File.mtime(filename).to_i - time.to_i) >= 2)
File.unlink(filename)
end
def test_file_utime_bad_time_raises_typeerror
args = [ [], {}, '4000' ]
filename = '__test__file'
File.open(filename, 'w') {|f| }
args.each do |arg|
assert_raises(TypeError) { File.utime(arg, nil, filename) }
assert_raises(TypeError) { File.utime(nil, arg, filename) }
assert_raises(TypeError) { File.utime(arg, arg, filename) }
end
time = Time.now
assert_raises(TypeError) { File.utime(time, nil, filename) }
assert_raises(TypeError) { File.utime(nil, time, filename) }
File.unlink(filename)
end
# JRUBY-1982 and JRUBY-1983
def test_file_mtime_after_fileutils_touch
filename = '__test__file'
File.open(filename, 'w') {|f| }
time = File.mtime(filename)
FileUtils.touch(filename)
# File mtime resolution may not be sub-second on all platforms (e.g., windows)
# allow for some slop
assert((time.to_i - File.mtime(filename).to_i).abs < 2)
end
def test_file_stat # File::Stat tests
stat = File.stat('test');
stat2 = File.stat('pom.xml');
assert(stat.directory?)
assert(stat2.file?)
assert_equal("directory", stat.ftype)
assert_equal("file", stat2.ftype)
assert(stat2.readable?)
end
if WINDOWS && JRuby::runtime.posix.native?
# JRUBY-2351
def test_not_implemented_methods_on_windows
# the goal here is to make sure that those "weird"
# POSIX methods don't break JRuby, since there were
# numerous regressions in this area
begin
# TODO: See JRUBY-2818.
File.readlink('pom.xml')
rescue NotImplementedError
rescue Errno::EINVAL # TODO: this exception is wrong (see bug above)
end
begin
# TODO: See JRUBY-2817.
File.chown(100, 100, 'pom.xml')
rescue NotImplementedError
end
assert_raise(NotImplementedError) { File.lchown(100, 100, 'pom.xml') }
assert_raise(NotImplementedError) { File.lchmod(0644, 'pom.xml') }
end
end
unless(WINDOWS) # no symlinks on Windows
def test_file_symlink
# Test File.symlink? if possible
system("ln -s pom.xml pom.xml.link")
if File.exist? "pom.xml.link"
assert(File.symlink?("pom.xml.link"))
# JRUBY-683 - Test that symlinks don't effect Dir and File.expand_path
assert_equal(['pom.xml.link'], Dir['pom.xml.link'])
assert_equal(File.expand_path('.') + '/pom.xml.link', File.expand_path('pom.xml.link'))
File.delete("pom.xml.link")
end
end
def test_require_symlink
# Create a ruby file that sets a global variable to its view of __FILE__
f = File.open("real_file.rb", "w")
f.write("$test_require_symlink_filename=__FILE__")
f.close()
system("ln -s real_file.rb linked_file.rb")
assert(File.symlink?("linked_file.rb"))
# JRUBY-5167 - Test that symlinks don't effect __FILE__ during load or require
# Note: This bug only manifests for absolute paths that point to symlinks.
abs_path = File.join(Dir.pwd, "real_file.rb")
require abs_path
assert_equal($test_require_symlink_filename, abs_path)
abs_path_linked = File.join(Dir.pwd, "linked_file.rb")
ensure
File.delete("real_file.rb")
File.delete("linked_file.rb")
end
end
def test_file_times
assert_nothing_raised do
[ "pom.xml", "file:test/jruby/dir with spaces/test_jar.jar!/abc/foo.rb" ].each do |file|
File.mtime(file)
File.atime(file)
File.ctime(file)
File.new(file).mtime
File.new(file).atime
File.new(file).ctime
end
end if IS_JRUBY
assert_raises(Errno::ENOENT) { File.mtime("NO_SUCH_FILE_EVER") }
end
def test_file_times_types
assert_instance_of Time, File.mtime("pom.xml")
assert_instance_of Time, File.atime("pom.xml")
assert_instance_of Time, File.ctime("pom.xml")
assert_instance_of Time, File.new("pom.xml").mtime
assert_instance_of Time, File.new("pom.xml").atime
assert_instance_of Time, File.new("pom.xml").ctime
end
def test_more_constants
if Java::java.lang.System.get_property("file.separator") == '/'
assert_equal(nil, File::ALT_SEPARATOR)
else
assert_equal("\\", File::ALT_SEPARATOR)