8 use Test::More tests => 57;
10 BEGIN { use_ok 'File::Basename' }
13 can_ok( __PACKAGE__, qw( basename fileparse dirname fileparse_set_fstype ) );
17 ok length fileparse_set_fstype('unix'), 'set fstype to unix';
18 is( fileparse_set_fstype(), 'Unix', 'get fstype' );
20 my($base,$path,$type) = fileparse('/virgil/aeneid/draft.book7',
23 is($path, '/virgil/aeneid/');
26 is(basename('/arma/virumque.cano'), 'virumque.cano');
27 is(dirname ('/arma/virumque.cano'), '/arma');
28 is(dirname('arma/'), '.');
29 is(dirname('/'), '/');
35 is(fileparse_set_fstype('VMS'), 'Unix', 'set fstype to VMS');
37 my($base,$path,$type) = fileparse('virgil:[aeneid]draft.book7',
40 is($path, 'virgil:[aeneid]');
43 is(basename('arma:[virumque]cano.trojae'), 'cano.trojae');
44 is(dirname('arma:[virumque]cano.trojae'), 'arma:[virumque]');
45 is(dirname('arma:<virumque>cano.trojae'), 'arma:<virumque>');
46 is(dirname('arma:virumque.cano'), 'arma:');
49 local $ENV{DEFAULT} = '' unless exists $ENV{DEFAULT};
50 is(dirname('virumque.cano'), $ENV{DEFAULT});
51 is(dirname('arma/'), '.');
58 is(fileparse_set_fstype('DOS'), 'VMS', 'set fstype to DOS');
60 my($base,$path,$type) = fileparse('C:\\virgil\\aeneid\\draft.book7',
63 is($path, 'C:\\virgil\\aeneid\\');
66 is(basename('A:virumque\\cano.trojae'), 'cano.trojae');
67 is(dirname('A:\\virumque\\cano.trojae'), 'A:\\virumque');
68 is(dirname('A:\\'), 'A:\\');
69 is(dirname('arma\\'), '.');
71 # Yes "/" is a legal path separator under DOS
72 is(basename("lib/File/Basename.pm"), "Basename.pm");
74 # $^O for DOS is "dos" not "MSDOS" but "MSDOS" is left in for
75 # backward bug compat.
76 is(fileparse_set_fstype('MSDOS'), 'DOS');
77 is( dirname("\\foo\\bar\\baz"), "\\foo\\bar" );
83 is(fileparse_set_fstype('MacOS'), 'MSDOS', 'set fstype to MacOS');
85 my($base,$path,$type) = fileparse('virgil:aeneid:draft.book7',
88 is($path, 'virgil:aeneid:');
91 is(basename(':arma:virumque:cano.trojae'), 'cano.trojae');
92 is(dirname(':arma:virumque:cano.trojae'), ':arma:virumque:');
93 is(dirname(':arma:virumque:'), ':arma:');
94 is(dirname(':arma:virumque'), ':arma:');
95 is(dirname(':arma:'), ':');
96 is(dirname(':arma'), ':');
97 is(dirname('arma:'), 'arma:');
98 is(dirname('arma'), ':');
99 is(dirname(':'), ':');
102 # Check quoting of metacharacters in suffix arg by basename()
103 is(basename(':arma:virumque:cano.trojae','.trojae'), 'cano');
104 is(basename(':arma:virumque:cano_trojae','.trojae'), 'cano_trojae');
108 ### extra tests for a few specific bugs
110 fileparse_set_fstype 'DOS';
111 # perl5.003_18 gives C:/perl/.\
112 is((fileparse 'C:/perl/lib')[1], 'C:/perl/');
113 # perl5.003_18 gives C:\perl\
114 is(dirname('C:\\perl\\lib\\'), 'C:\\perl');
116 fileparse_set_fstype 'UNIX';
117 # perl5.003_18 gives '.'
118 is(dirname('/perl/'), '/');
119 # perl5.003_18 gives '/perl/lib'
120 is(dirname('/perl/lib//'), '/perl');
123 ### rt.perl.org 22236
125 is(basename('a/'), 'a');
126 is(basename('/usr/lib//'), 'lib');
128 fileparse_set_fstype 'MSWin32';
129 is(basename('a\\'), 'a');
130 is(basename('\\usr\\lib\\\\'), 'lib');
136 # The empty tainted value, for tainting strings
137 my $TAINT = substr($^X, 0, 0);
139 # How to identify taint when you see it
140 sub any_tainted (@) {
141 return ! eval { eval("#" . substr(join("", @_), 0, 0)); 1 };
148 sub all_tainted (@) {
149 for (@_) { return 0 unless tainted $_ }
153 ok tainted(dirname($TAINT.'/perl/lib//'));
154 ok all_tainted(fileparse($TAINT.'/dir/draft.book7','\.book\d+'));