This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
File::Basename::dirname bugs
[perl5.git] / t / lib / basename.t
CommitLineData
78201403
PP
1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
6}
7
8use File::Basename qw(fileparse basename dirname);
9
42568e28 10print "1..34\n";
78201403
PP
11
12# import correctly?
13print +(defined(&basename) && !defined(&fileparse_set_fstype) ?
14 '' : 'not '),"ok 1\n";
15
16# set fstype -- should replace non-null default
17print +(length(File::Basename::fileparse_set_fstype('unix')) ?
18 '' : 'not '),"ok 2\n";
19
20# Unix syntax tests
21($base,$path,$type) = fileparse('/virgil/aeneid/draft.book7','\.book\d+');
22if ($base eq 'draft' and $path eq '/virgil/aeneid/' and $type eq '.book7') {
23 print "ok 3\n";
24}
25else {
26 print "not ok 3 |$base|$path|$type|\n";
27}
28print +(basename('/arma/virumque.cano') eq 'virumque.cano' ?
29 '' : 'not '),"ok 4\n";
30print +(dirname('/arma/virumque.cano') eq '/arma' ? '' : 'not '),"ok 5\n";
31print +(dirname('arma/') eq '.' ? '' : 'not '),"ok 6\n";
32print +(dirname('/') eq '/' ? '' : 'not '),"ok 7\n";
33
34
35# set fstype -- should replace non-null default
36print +(File::Basename::fileparse_set_fstype('VMS') eq 'unix' ?
37 '' : 'not '),"ok 8\n";
38
39# VMS syntax tests
40($base,$path,$type) = fileparse('virgil:[aeneid]draft.book7','\.book\d+');
41if ($base eq 'draft' and $path eq 'virgil:[aeneid]' and $type eq '.book7') {
42 print "ok 9\n";
43}
44else {
45 print "not ok 9 |$base|$path|$type|\n";
46}
47print +(basename('arma:[virumque]cano.trojae') eq 'cano.trojae' ?
48 '' : 'not '),"ok 10\n";
49print +(dirname('arma:[virumque]cano.trojae') eq 'arma:[virumque]' ?
50 '' : 'not '),"ok 11\n";
51print +(dirname('arma:<virumque>cano.trojae') eq 'arma:<virumque>' ?
52 '' : 'not '),"ok 12\n";
53print +(dirname('arma:virumque.cano') eq 'arma:' ? '' : 'not '),"ok 13\n";
54print +(dirname('virumque.cano') eq $ENV{DEFAULT} ? '' : 'not '),"ok 14\n";
55print +(dirname('arma/') eq '.' ? '' : 'not '),"ok 15\n";
56
57# set fstype -- should replace non-null default
58print +(File::Basename::fileparse_set_fstype('MSDOS') eq 'VMS' ?
59 '' : 'not '),"ok 16\n";
60
61# MSDOS syntax tests
62($base,$path,$type) = fileparse('C:\\virgil\\aeneid\\draft.book7','\.book\d+');
63if ($base eq 'draft' and $path eq 'C:\\virgil\\aeneid\\' and $type eq '.book7') {
64 print "ok 17\n";
65}
66else {
67 print "not ok 17 |$base|$path|$type|\n";
68}
69print +(basename('A:virumque\\cano.trojae') eq 'cano.trojae' ?
70 '' : 'not '),"ok 18\n";
71print +(dirname('A:\\virumque\\cano.trojae') eq 'A:\\virumque' ?
72 '' : 'not '),"ok 19\n";
73print +(dirname('A:\\') eq 'A:\\' ? '' : 'not '),"ok 20\n";
74print +(dirname('arma\\') eq '.' ? '' : 'not '),"ok 21\n";
75
76# Yes "/" is a legal path separator under MSDOS
77basename("lib/File/Basename.pm") eq "Basename.pm" or print "not ";
78print "ok 22\n";
79
80
81
82# set fstype -- should replace non-null default
83print +(File::Basename::fileparse_set_fstype('MacOS') eq 'MSDOS' ?
84 '' : 'not '),"ok 23\n";
85
86# MacOS syntax tests
87($base,$path,$type) = fileparse('virgil:aeneid:draft.book7','\.book\d+');
88if ($base eq 'draft' and $path eq 'virgil:aeneid:' and $type eq '.book7') {
89 print "ok 24\n";
90}
91else {
92 print "not ok 24 |$base|$path|$type|\n";
93}
94print +(basename(':arma:virumque:cano.trojae') eq 'cano.trojae' ?
95 '' : 'not '),"ok 25\n";
96print +(dirname(':arma:virumque:cano.trojae') eq ':arma:virumque:' ?
97 '' : 'not '),"ok 26\n";
98print +(dirname('arma:') eq 'arma:' ? '' : 'not '),"ok 27\n";
99print +(dirname(':') eq ':' ? '' : 'not '),"ok 28\n";
100
101
102# Check quoting of metacharacters in suffix arg by basename()
103print +(basename(':arma:virumque:cano.trojae','.trojae') eq 'cano' ?
104 '' : 'not '),"ok 29\n";
105print +(basename(':arma:virumque:cano_trojae','.trojae') eq 'cano_trojae' ?
106 '' : 'not '),"ok 30\n";
107
42568e28
RB
108# extra tests for a few specific bugs
109
110File::Basename::fileparse_set_fstype 'MSDOS';
111# perl5.003_18 gives C:/perl/.\
112print +((fileparse 'C:/perl/lib')[1] eq 'C:/perl/' ? '' : 'not '), "ok 31\n";
113# perl5.003_18 gives C:\perl\
114print +(dirname('C:\\perl\\lib\\') eq 'C:\\perl' ? '' : 'not '), "ok 32\n";
115
116File::Basename::fileparse_set_fstype 'UNIX';
117# perl5.003_18 gives '.'
118print +(dirname('/perl/') eq '/' ? '' : 'not '), "ok 33\n";
119# perl5.003_18 gives '/perl/lib'
120print +(dirname('/perl/lib//') eq '/perl' ? '' : 'not '), "ok 34\n";