This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make ExtUtils::CBuilder reset ccflags on compile for VMS.
[perl5.git] / lib / File / Basename.t
CommitLineData
e7204fba 1#!./perl -Tw
78201403 2
3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
78201403 6}
7
e713b737 8use Test::More tests => 49;
78201403 9
e7204fba 10BEGIN { use_ok 'File::Basename' }
78201403 11
12# import correctly?
e7204fba
MS
13can_ok( __PACKAGE__, qw( basename fileparse dirname fileparse_set_fstype ) );
14
15### Testing Unix
16{
17 ok length fileparse_set_fstype('unix'), 'set fstype to unix';
3291253b 18 is( fileparse_set_fstype(), 'Unix', 'get fstype' );
e7204fba
MS
19
20 my($base,$path,$type) = fileparse('/virgil/aeneid/draft.book7',
21 qr'\.book\d+');
22 is($base, 'draft');
23 is($path, '/virgil/aeneid/');
24 is($type, '.book7');
25
26 is(basename('/arma/virumque.cano'), 'virumque.cano');
27 is(dirname ('/arma/virumque.cano'), '/arma');
28 is(dirname('arma/'), '.');
e7204fba 29}
78201403 30
78201403 31
e7204fba
MS
32### Testing VMS
33{
3291253b 34 is(fileparse_set_fstype('VMS'), 'Unix', 'set fstype to VMS');
78201403 35
e7204fba
MS
36 my($base,$path,$type) = fileparse('virgil:[aeneid]draft.book7',
37 qr{\.book\d+});
38 is($base, 'draft');
39 is($path, 'virgil:[aeneid]');
40 is($type, '.book7');
78201403 41
e7204fba
MS
42 is(basename('arma:[virumque]cano.trojae'), 'cano.trojae');
43 is(dirname('arma:[virumque]cano.trojae'), 'arma:[virumque]');
44 is(dirname('arma:<virumque>cano.trojae'), 'arma:<virumque>');
45 is(dirname('arma:virumque.cano'), 'arma:');
78201403 46
e7204fba
MS
47 {
48 local $ENV{DEFAULT} = '' unless exists $ENV{DEFAULT};
49 is(dirname('virumque.cano'), $ENV{DEFAULT});
50 is(dirname('arma/'), '.');
51 }
78201403 52}
78201403 53
78201403 54
3291253b 55### Testing DOS
e7204fba 56{
3291253b 57 is(fileparse_set_fstype('DOS'), 'VMS', 'set fstype to DOS');
78201403 58
e7204fba
MS
59 my($base,$path,$type) = fileparse('C:\\virgil\\aeneid\\draft.book7',
60 '\.book\d+');
61 is($base, 'draft');
62 is($path, 'C:\\virgil\\aeneid\\');
63 is($type, '.book7');
78201403 64
e7204fba
MS
65 is(basename('A:virumque\\cano.trojae'), 'cano.trojae');
66 is(dirname('A:\\virumque\\cano.trojae'), 'A:\\virumque');
67 is(dirname('A:\\'), 'A:\\');
68 is(dirname('arma\\'), '.');
78201403 69
3291253b 70 # Yes "/" is a legal path separator under DOS
e7204fba 71 is(basename("lib/File/Basename.pm"), "Basename.pm");
3291253b
MS
72
73 # $^O for DOS is "dos" not "MSDOS" but "MSDOS" is left in for
74 # backward bug compat.
75 is(fileparse_set_fstype('MSDOS'), 'DOS');
76 is( dirname("\\foo\\bar\\baz"), "\\foo\\bar" );
78201403 77}
e7204fba 78
e7204fba
MS
79### extra tests for a few specific bugs
80{
3291253b 81 fileparse_set_fstype 'DOS';
e7204fba
MS
82 # perl5.003_18 gives C:/perl/.\
83 is((fileparse 'C:/perl/lib')[1], 'C:/perl/');
84 # perl5.003_18 gives C:\perl\
85 is(dirname('C:\\perl\\lib\\'), 'C:\\perl');
86
87 fileparse_set_fstype 'UNIX';
88 # perl5.003_18 gives '.'
89 is(dirname('/perl/'), '/');
90 # perl5.003_18 gives '/perl/lib'
91 is(dirname('/perl/lib//'), '/perl');
b3eb6a9b
GS
92}
93
e586b3eb
MS
94### rt.perl.org 22236
95{
96 is(basename('a/'), 'a');
97 is(basename('/usr/lib//'), 'lib');
98
99 fileparse_set_fstype 'MSWin32';
100 is(basename('a\\'), 'a');
101 is(basename('\\usr\\lib\\\\'), 'lib');
102}
103
e7204fba 104
08bc7695
MS
105### rt.cpan.org 36477
106{
107 fileparse_set_fstype('Unix');
108 is(dirname('/'), '/');
109 is(basename('/'), '/');
110
111 fileparse_set_fstype('DOS');
112 is(dirname('\\'), '\\');
113 is(basename('\\'), '\\');
114}
115
116
08ea998e
MS
117### basename(1) sez: "The suffix is not stripped if it is identical to the
118### remaining characters in string"
119{
120 fileparse_set_fstype('Unix');
121 is(basename('.foo'), '.foo');
122 is(basename('.foo', '.foo'), '.foo');
123 is(basename('.foo.bar', '.foo'), '.foo.bar');
124 is(basename('.foo.bar', '.bar'), '.foo');
125}
126
127
e7204fba
MS
128### Test tainting
129{
130 # The empty tainted value, for tainting strings
131 my $TAINT = substr($^X, 0, 0);
132
133 # How to identify taint when you see it
134 sub any_tainted (@) {
135 return ! eval { eval("#" . substr(join("", @_), 0, 0)); 1 };
136 }
137
138 sub tainted ($) {
139 any_tainted @_;
140 }
141
142 sub all_tainted (@) {
143 for (@_) { return 0 unless tainted $_ }
144 1;
145 }
146
08bc7695 147 fileparse_set_fstype 'Unix';
e7204fba
MS
148 ok tainted(dirname($TAINT.'/perl/lib//'));
149 ok all_tainted(fileparse($TAINT.'/dir/draft.book7','\.book\d+'));
150}