This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add plain ~ expansion for Windows system in File::Glob
[perl5.git] / ext / File-Glob / t / basic.t
CommitLineData
72b16652
GS
1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
1d89eaef 5 @INC = '../lib';
d2a01882
GS
6 require Config; import Config;
7 if ($Config{'extensions'} !~ /\bFile\/Glob\b/i) {
8 print "1..0\n";
9 exit 0;
10 }
72b16652 11}
06494c4c 12use strict;
528bd3ce 13use Test::More tests => 18;
06494c4c 14BEGIN {use_ok('File::Glob', ':glob')};
a4cf958a 15use Cwd ();
72b16652 16
6a164b5b
JM
17my $vms_unix_rpt = 0;
18my $vms_efs = 0;
19my $vms_mode = 0;
20if ($^O eq 'VMS') {
21 if (eval 'require VMS::Feature') {
22 $vms_unix_rpt = VMS::Feature::current("filename_unix_report");
23 $vms_efs = VMS::Feature::current("efs_charset");
24 } else {
25 my $unix_rpt = $ENV{'DECC$FILENAME_UNIX_REPORT'} || '';
26 my $efs_charset = $ENV{'DECC$EFS_CHARSET'} || '';
27 $vms_unix_rpt = $unix_rpt =~ /^[ET1]/i;
28 $vms_efs = $efs_charset =~ /^[ET1]/i;
29 }
30 $vms_mode = 1 unless ($vms_unix_rpt);
31}
32
33
72b16652
GS
34# look for the contents of the current directory
35$ENV{PATH} = "/bin";
06494c4c
NC
36delete @ENV{qw(BASH_ENV CDPATH ENV IFS)};
37my @correct = ();
1d89eaef 38if (opendir(D, ".")) {
94a371ee 39 @correct = grep { !/^\./ } sort readdir(D);
72b16652
GS
40 closedir D;
41}
06494c4c 42my @a = File::Glob::glob("*", 0);
72b16652 43@a = sort @a;
06494c4c
NC
44if (GLOB_ERROR) {
45 fail(GLOB_ERROR);
46} else {
47 is_deeply(\@a, \@correct);
72b16652 48}
72b16652
GS
49
50# look up the user's home directory
51# should return a list with one item, and not set ERROR
06494c4c
NC
52SKIP: {
53 my ($name, $home);
c491a205 54 skip $^O, 1 if $^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'VMS'
06494c4c 55 || $^O eq 'os2' || $^O eq 'beos';
c491a205 56 skip "Can't find user for $>: $@", 1 unless eval {
06494c4c
NC
57 ($name, $home) = (getpwuid($>))[0,7];
58 1;
59 };
c491a205 60 skip "$> has no home directory", 1
06494c4c
NC
61 unless defined $home && defined $name && -d $home;
62
63 @a = bsd_glob("~$name", GLOB_TILDE);
64
65 if (GLOB_ERROR) {
66 fail(GLOB_ERROR);
67 } else {
68 is_deeply (\@a, [$home]);
72b16652
GS
69 }
70}
528bd3ce
DCW
71# check plain tilde expansion
72{
73 my $tilde_check = sub {
74 my @a = bsd_glob('~');
75
76 if (GLOB_ERROR) {
77 fail(GLOB_ERROR);
78 } else {
79 is_deeply (\@a, [$_[0]], join ' - ', 'tilde expansion', @_ > 1 ? $_[1] : ());
80 }
81 };
82 my $passwd_home = eval { (getpwuid($>))[7] };
83
84 {
85 local %ENV = %ENV;
86 delete $ENV{HOME};
87 delete $ENV{USERPROFILE};
88 $tilde_check->(defined $passwd_home ? $passwd_home : q{~}, 'no environment');
89 }
90
91 SKIP: {
92 skip 'MSWin32 only', 1 if $^O ne 'MSWin32';
93 local %ENV = %ENV;
94 delete $ENV{HOME};
95 $ENV{USERPROFILE} = 'sweet win32 home';
96 $tilde_check->(defined $passwd_home ? $passwd_home : $ENV{USERPROFILE}, 'USERPROFILE');
97 }
98
99 my $home = exists $ENV{HOME} ? $ENV{HOME}
100 : eval { getpwuid($>); 1 } ? (getpwuid($>))[7]
101 : $^O eq 'MSWin32' && exists $ENV{USERPROFILE} ? $ENV{USERPROFILE}
102 : q{~};
103 $tilde_check->($home);
104}
72b16652
GS
105
106# check backslashing
107# should return a list with one item, and not set ERROR
00c80938 108@a = bsd_glob('TEST', GLOB_QUOTE);
06494c4c
NC
109if (GLOB_ERROR) {
110 fail(GLOB_ERROR);
111} else {
112 is_deeply(\@a, ['TEST']);
72b16652 113}
72b16652
GS
114
115# check nonexistent checks
116# should return an empty list
117# XXX since errfunc is NULL on win32, this test is not valid there
00c80938 118@a = bsd_glob("asdfasdf", 0);
06494c4c 119SKIP: {
c491a205 120 skip $^O, 1 if $^O eq 'MSWin32' || $^O eq 'NetWare';
06494c4c 121 is_deeply(\@a, []);
72b16652 122}
72b16652
GS
123
124# check bad protections
125# should return an empty list, and set ERROR
06494c4c 126SKIP: {
c491a205 127 skip $^O, 2 if $^O eq 'mpeix' or $^O eq 'MSWin32' or $^O eq 'NetWare'
06494c4c 128 or $^O eq 'os2' or $^O eq 'VMS' or $^O eq 'cygwin';
c491a205
SH
129 skip "AFS", 2 if Cwd::cwd() =~ m#^$Config{'afsroot'}#s;
130 skip "running as root", 2 if not $>;
06494c4c
NC
131
132 my $dir = "pteerslo";
1cf742e9 133 mkdir $dir, 0;
00c80938 134 @a = bsd_glob("$dir/*", GLOB_ERR);
1cf742e9 135 rmdir $dir;
06494c4c
NC
136 local $TODO = 'hit VOS bug posix-956' if $^O eq 'vos';
137
138 isnt(GLOB_ERROR, 0);
139 is_deeply(\@a, []);
72b16652 140}
72b16652
GS
141
142# check for csh style globbing
00c80938 143@a = bsd_glob('{a,b}', GLOB_BRACE | GLOB_NOMAGIC);
06494c4c 144is_deeply(\@a, ['a', 'b']);
72b16652 145
00c80938 146@a = bsd_glob(
72b16652 147 '{TES*,doesntexist*,a,b}',
3eba041e 148 GLOB_BRACE | GLOB_NOMAGIC | ($^O eq 'VMS' ? GLOB_NOCASE : 0)
72b16652 149);
5d9a6404 150
8ac1de08
MS
151# Working on t/TEST often causes this test to fail because it sees Emacs temp
152# and RCS files. Filter them out, and .pm files too, and patch temp files.
153@a = grep !/(,v$|~$|\.(pm|ori?g|rej)$)/, @a;
c065ed18 154@a = (grep !/test.pl/, @a) if $^O eq 'VMS';
8ac1de08
MS
155
156print "# @a\n";
5d9a6404 157
6a164b5b 158is_deeply(\@a, [($vms_mode ? 'test.' : 'TEST'), 'a', 'b']);
72b16652
GS
159
160# "~" should expand to $ENV{HOME}
161$ENV{HOME} = "sweet home";
00c80938 162@a = bsd_glob('~', GLOB_TILDE | GLOB_NOMAGIC);
1d89eaef 163is_deeply(\@a, [$ENV{HOME}]);
ab3ed403
BS
164
165# GLOB_ALPHASORT (default) should sort alphabetically regardless of case
8f562d3a
NA
166mkdir "pteerslo", 0777;
167chdir "pteerslo";
93782ce2 168
06494c4c
NC
169my @f_names = qw(Ax.pl Bx.pl Cx.pl aY.pl bY.pl cY.pl);
170my @f_alpha = qw(Ax.pl aY.pl Bx.pl bY.pl Cx.pl cY.pl);
93782ce2
BS
171if ('a' lt 'A') { # EBCDIC char sets sort lower case before UPPER
172 @f_names = sort(@f_names);
d2f5bb60 173}
89f4ed55 174if ($^O eq 'VMS') { # VMS is happily caseignorant
16421035
PP
175 @f_alpha = qw(ax.pl ay.pl bx.pl by.pl cx.pl cy.pl);
176 @f_names = @f_alpha;
177}
93782ce2
BS
178
179for (@f_names) {
ab3ed403
BS
180 open T, "> $_";
181 close T;
182}
93782ce2 183
06494c4c 184my $pat = "*.pl";
93782ce2 185
06494c4c 186my @g_names = bsd_glob($pat, 0);
93782ce2
BS
187print "# f_names = @f_names\n";
188print "# g_names = @g_names\n";
06494c4c 189is_deeply(\@g_names, \@f_names);
93782ce2 190
06494c4c 191my @g_alpha = bsd_glob($pat);
584c1423
JH
192print "# f_alpha = @f_alpha\n";
193print "# g_alpha = @g_alpha\n";
06494c4c 194is_deeply(\@g_alpha, \@f_alpha);
93782ce2
BS
195
196unlink @f_names;
ab3ed403 197chdir "..";
8f562d3a 198rmdir "pteerslo";
e0e8a4dc
MHM
199
200# this can panic if PL_glob_index gets passed as flags to bsd_glob
201<*>; <*>;
06494c4c 202pass("Don't panic");
77348331
SR
203
204{
205 use File::Temp qw(tempdir);
206 use File::Spec qw();
207
208 my($dir) = tempdir(CLEANUP => 1)
209 or die "Could not create temporary directory";
210 for my $file (qw(a_dej a_ghj a_qej)) {
211 open my $fh, ">", File::Spec->catfile($dir, $file)
212 or die "Could not create file $dir/$file: $!";
213 close $fh;
214 }
215 my $cwd = Cwd::cwd();
216 chdir $dir
217 or die "Could not chdir to $dir: $!";
218 my(@glob_files) = glob("a*{d[e]}j");
77348331
SR
219 chdir $cwd
220 or die "Could not chdir back to $cwd: $!";
946b2ae5
CB
221 local $TODO = "home-made glob doesn't do regexes" if $^O eq 'VMS';
222 is_deeply(\@glob_files, ['a_dej']);
77348331 223}
3c97495f
CB
224
225# This used to segfault.
226my $i = bsd_glob('*', GLOB_ALTDIRFUNC);
227is(&File::Glob::GLOB_ERROR, 0, "Successfuly ignored unsupported flag");