This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
In bisect-runner.pl, default to 'cc' not 'gcc'.
[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 {
592808d8 74 my @a = bsd_glob('~');
528bd3ce 75
592808d8
CB
76 if (GLOB_ERROR) {
77 fail(GLOB_ERROR);
78 } else {
79 is_deeply (\@a, [$_[0]], join ' - ', 'tilde expansion', @_ > 1 ? $_[1] : ());
80 }
528bd3ce
DCW
81 };
82 my $passwd_home = eval { (getpwuid($>))[7] };
83
592808d8
CB
84 TODO: {
85 local $TODO = 'directory brackets look like pattern brackets to glob' if $^O eq 'VMS';
86 local $ENV{HOME};
87 delete $ENV{HOME};
88 local $ENV{USERPROFILE};
89 delete $ENV{USERPROFILE};
90 $tilde_check->(defined $passwd_home ? $passwd_home : q{~}, 'no environment');
528bd3ce
DCW
91 }
92
93 SKIP: {
592808d8
CB
94 skip 'MSWin32 only', 1 if $^O ne 'MSWin32';
95 local $ENV{HOME};
96 delete $ENV{HOME};
97 local $ENV{USERPROFILE};
98 $ENV{USERPROFILE} = 'sweet win32 home';
99 $tilde_check->(defined $passwd_home ? $passwd_home : $ENV{USERPROFILE}, 'USERPROFILE');
528bd3ce
DCW
100 }
101
592808d8
CB
102 TODO: {
103 local $TODO = 'directory brackets look like pattern brackets to glob' if $^O eq 'VMS';
104 my $home = exists $ENV{HOME} ? $ENV{HOME}
105 : eval { getpwuid($>); 1 } ? (getpwuid($>))[7]
106 : $^O eq 'MSWin32' && exists $ENV{USERPROFILE} ? $ENV{USERPROFILE}
107 : q{~};
108 $tilde_check->($home);
109 }
528bd3ce 110}
72b16652
GS
111
112# check backslashing
113# should return a list with one item, and not set ERROR
00c80938 114@a = bsd_glob('TEST', GLOB_QUOTE);
06494c4c
NC
115if (GLOB_ERROR) {
116 fail(GLOB_ERROR);
117} else {
118 is_deeply(\@a, ['TEST']);
72b16652 119}
72b16652
GS
120
121# check nonexistent checks
122# should return an empty list
123# XXX since errfunc is NULL on win32, this test is not valid there
00c80938 124@a = bsd_glob("asdfasdf", 0);
06494c4c 125SKIP: {
c491a205 126 skip $^O, 1 if $^O eq 'MSWin32' || $^O eq 'NetWare';
06494c4c 127 is_deeply(\@a, []);
72b16652 128}
72b16652
GS
129
130# check bad protections
131# should return an empty list, and set ERROR
06494c4c 132SKIP: {
c491a205 133 skip $^O, 2 if $^O eq 'mpeix' or $^O eq 'MSWin32' or $^O eq 'NetWare'
06494c4c 134 or $^O eq 'os2' or $^O eq 'VMS' or $^O eq 'cygwin';
c491a205
SH
135 skip "AFS", 2 if Cwd::cwd() =~ m#^$Config{'afsroot'}#s;
136 skip "running as root", 2 if not $>;
06494c4c
NC
137
138 my $dir = "pteerslo";
1cf742e9 139 mkdir $dir, 0;
00c80938 140 @a = bsd_glob("$dir/*", GLOB_ERR);
1cf742e9 141 rmdir $dir;
06494c4c
NC
142 local $TODO = 'hit VOS bug posix-956' if $^O eq 'vos';
143
144 isnt(GLOB_ERROR, 0);
145 is_deeply(\@a, []);
72b16652 146}
72b16652
GS
147
148# check for csh style globbing
00c80938 149@a = bsd_glob('{a,b}', GLOB_BRACE | GLOB_NOMAGIC);
06494c4c 150is_deeply(\@a, ['a', 'b']);
72b16652 151
00c80938 152@a = bsd_glob(
72b16652 153 '{TES*,doesntexist*,a,b}',
3eba041e 154 GLOB_BRACE | GLOB_NOMAGIC | ($^O eq 'VMS' ? GLOB_NOCASE : 0)
72b16652 155);
5d9a6404 156
8ac1de08
MS
157# Working on t/TEST often causes this test to fail because it sees Emacs temp
158# and RCS files. Filter them out, and .pm files too, and patch temp files.
159@a = grep !/(,v$|~$|\.(pm|ori?g|rej)$)/, @a;
c065ed18 160@a = (grep !/test.pl/, @a) if $^O eq 'VMS';
8ac1de08
MS
161
162print "# @a\n";
5d9a6404 163
6a164b5b 164is_deeply(\@a, [($vms_mode ? 'test.' : 'TEST'), 'a', 'b']);
72b16652
GS
165
166# "~" should expand to $ENV{HOME}
592808d8
CB
167{
168 local $ENV{HOME} = "sweet home";
169 @a = bsd_glob('~', GLOB_TILDE | GLOB_NOMAGIC);
170 is_deeply(\@a, [$ENV{HOME}]);
171}
ab3ed403
BS
172
173# GLOB_ALPHASORT (default) should sort alphabetically regardless of case
8f562d3a
NA
174mkdir "pteerslo", 0777;
175chdir "pteerslo";
93782ce2 176
06494c4c
NC
177my @f_names = qw(Ax.pl Bx.pl Cx.pl aY.pl bY.pl cY.pl);
178my @f_alpha = qw(Ax.pl aY.pl Bx.pl bY.pl Cx.pl cY.pl);
93782ce2
BS
179if ('a' lt 'A') { # EBCDIC char sets sort lower case before UPPER
180 @f_names = sort(@f_names);
d2f5bb60 181}
89f4ed55 182if ($^O eq 'VMS') { # VMS is happily caseignorant
16421035
PP
183 @f_alpha = qw(ax.pl ay.pl bx.pl by.pl cx.pl cy.pl);
184 @f_names = @f_alpha;
185}
93782ce2
BS
186
187for (@f_names) {
ab3ed403
BS
188 open T, "> $_";
189 close T;
190}
93782ce2 191
06494c4c 192my $pat = "*.pl";
93782ce2 193
06494c4c 194my @g_names = bsd_glob($pat, 0);
93782ce2
BS
195print "# f_names = @f_names\n";
196print "# g_names = @g_names\n";
06494c4c 197is_deeply(\@g_names, \@f_names);
93782ce2 198
06494c4c 199my @g_alpha = bsd_glob($pat);
584c1423
JH
200print "# f_alpha = @f_alpha\n";
201print "# g_alpha = @g_alpha\n";
06494c4c 202is_deeply(\@g_alpha, \@f_alpha);
93782ce2
BS
203
204unlink @f_names;
ab3ed403 205chdir "..";
8f562d3a 206rmdir "pteerslo";
e0e8a4dc
MHM
207
208# this can panic if PL_glob_index gets passed as flags to bsd_glob
209<*>; <*>;
06494c4c 210pass("Don't panic");
77348331
SR
211
212{
213 use File::Temp qw(tempdir);
214 use File::Spec qw();
215
216 my($dir) = tempdir(CLEANUP => 1)
217 or die "Could not create temporary directory";
218 for my $file (qw(a_dej a_ghj a_qej)) {
219 open my $fh, ">", File::Spec->catfile($dir, $file)
220 or die "Could not create file $dir/$file: $!";
221 close $fh;
222 }
223 my $cwd = Cwd::cwd();
224 chdir $dir
225 or die "Could not chdir to $dir: $!";
226 my(@glob_files) = glob("a*{d[e]}j");
77348331
SR
227 chdir $cwd
228 or die "Could not chdir back to $cwd: $!";
946b2ae5
CB
229 local $TODO = "home-made glob doesn't do regexes" if $^O eq 'VMS';
230 is_deeply(\@glob_files, ['a_dej']);
77348331 231}
3c97495f
CB
232
233# This used to segfault.
234my $i = bsd_glob('*', GLOB_ALTDIRFUNC);
235is(&File::Glob::GLOB_ERROR, 0, "Successfuly ignored unsupported flag");