8 use Test::More tests => 60;
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/'), '.');
34 is(fileparse_set_fstype('VMS'), 'Unix', 'set fstype to VMS');
36 my($base,$path,$type) = fileparse('virgil:[aeneid]draft.book7',
39 is($path, 'virgil:[aeneid]');
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:');
48 local $ENV{DEFAULT} = '' unless exists $ENV{DEFAULT};
49 is(dirname('virumque.cano'), $ENV{DEFAULT});
50 is(dirname('arma/'), '.');
57 is(fileparse_set_fstype('DOS'), 'VMS', 'set fstype to DOS');
59 my($base,$path,$type) = fileparse('C:\\virgil\\aeneid\\draft.book7',
62 is($path, 'C:\\virgil\\aeneid\\');
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\\'), '.');
70 # Yes "/" is a legal path separator under DOS
71 is(basename("lib/File/Basename.pm"), "Basename.pm");
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" );
82 is(fileparse_set_fstype('MacOS'), 'MSDOS', 'set fstype to MacOS');
84 my($base,$path,$type) = fileparse('virgil:aeneid:draft.book7',
87 is($path, 'virgil:aeneid:');
90 is(basename(':arma:virumque:cano.trojae'), 'cano.trojae');
91 is(dirname(':arma:virumque:cano.trojae'), ':arma:virumque:');
92 is(dirname(':arma:virumque:'), ':arma:');
93 is(dirname(':arma:virumque'), ':arma:');
94 is(dirname(':arma:'), ':');
95 is(dirname(':arma'), ':');
96 is(dirname('arma:'), 'arma:');
97 is(dirname('arma'), ':');
98 is(dirname(':'), ':');
101 # Check quoting of metacharacters in suffix arg by basename()
102 is(basename(':arma:virumque:cano.trojae','.trojae'), 'cano');
103 is(basename(':arma:virumque:cano_trojae','.trojae'), 'cano_trojae');
107 ### extra tests for a few specific bugs
109 fileparse_set_fstype 'DOS';
110 # perl5.003_18 gives C:/perl/.\
111 is((fileparse 'C:/perl/lib')[1], 'C:/perl/');
112 # perl5.003_18 gives C:\perl\
113 is(dirname('C:\\perl\\lib\\'), 'C:\\perl');
115 fileparse_set_fstype 'UNIX';
116 # perl5.003_18 gives '.'
117 is(dirname('/perl/'), '/');
118 # perl5.003_18 gives '/perl/lib'
119 is(dirname('/perl/lib//'), '/perl');
122 ### rt.perl.org 22236
124 is(basename('a/'), 'a');
125 is(basename('/usr/lib//'), 'lib');
127 fileparse_set_fstype 'MSWin32';
128 is(basename('a\\'), 'a');
129 is(basename('\\usr\\lib\\\\'), 'lib');
133 ### rt.cpan.org 36477
135 fileparse_set_fstype('Unix');
136 is(dirname('/'), '/');
137 is(basename('/'), '/');
139 fileparse_set_fstype('DOS');
140 is(dirname('\\'), '\\');
141 is(basename('\\'), '\\');
147 # The empty tainted value, for tainting strings
148 my $TAINT = substr($^X, 0, 0);
150 # How to identify taint when you see it
151 sub any_tainted (@) {
152 return ! eval { eval("#" . substr(join("", @_), 0, 0)); 1 };
159 sub all_tainted (@) {
160 for (@_) { return 0 unless tainted $_ }
164 fileparse_set_fstype 'Unix';
165 ok tainted(dirname($TAINT.'/perl/lib//'));
166 ok all_tainted(fileparse($TAINT.'/dir/draft.book7','\.book\d+'));