This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Bump threads(::shared) versions again
[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
NC
12use strict;
13use Test::More tests => 14;
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}
72b16652
GS
71
72# check backslashing
73# should return a list with one item, and not set ERROR
00c80938 74@a = bsd_glob('TEST', GLOB_QUOTE);
06494c4c
NC
75if (GLOB_ERROR) {
76 fail(GLOB_ERROR);
77} else {
78 is_deeply(\@a, ['TEST']);
72b16652 79}
72b16652
GS
80
81# check nonexistent checks
82# should return an empty list
83# XXX since errfunc is NULL on win32, this test is not valid there
00c80938 84@a = bsd_glob("asdfasdf", 0);
06494c4c 85SKIP: {
c491a205 86 skip $^O, 1 if $^O eq 'MSWin32' || $^O eq 'NetWare';
06494c4c 87 is_deeply(\@a, []);
72b16652 88}
72b16652
GS
89
90# check bad protections
91# should return an empty list, and set ERROR
06494c4c 92SKIP: {
c491a205 93 skip $^O, 2 if $^O eq 'mpeix' or $^O eq 'MSWin32' or $^O eq 'NetWare'
06494c4c 94 or $^O eq 'os2' or $^O eq 'VMS' or $^O eq 'cygwin';
c491a205
SH
95 skip "AFS", 2 if Cwd::cwd() =~ m#^$Config{'afsroot'}#s;
96 skip "running as root", 2 if not $>;
06494c4c
NC
97
98 my $dir = "pteerslo";
1cf742e9 99 mkdir $dir, 0;
00c80938 100 @a = bsd_glob("$dir/*", GLOB_ERR);
1cf742e9 101 rmdir $dir;
06494c4c
NC
102 local $TODO = 'hit VOS bug posix-956' if $^O eq 'vos';
103
104 isnt(GLOB_ERROR, 0);
105 is_deeply(\@a, []);
72b16652 106}
72b16652
GS
107
108# check for csh style globbing
00c80938 109@a = bsd_glob('{a,b}', GLOB_BRACE | GLOB_NOMAGIC);
06494c4c 110is_deeply(\@a, ['a', 'b']);
72b16652 111
00c80938 112@a = bsd_glob(
72b16652 113 '{TES*,doesntexist*,a,b}',
3eba041e 114 GLOB_BRACE | GLOB_NOMAGIC | ($^O eq 'VMS' ? GLOB_NOCASE : 0)
72b16652 115);
5d9a6404 116
8ac1de08
MS
117# Working on t/TEST often causes this test to fail because it sees Emacs temp
118# and RCS files. Filter them out, and .pm files too, and patch temp files.
119@a = grep !/(,v$|~$|\.(pm|ori?g|rej)$)/, @a;
c065ed18 120@a = (grep !/test.pl/, @a) if $^O eq 'VMS';
8ac1de08
MS
121
122print "# @a\n";
5d9a6404 123
6a164b5b 124is_deeply(\@a, [($vms_mode ? 'test.' : 'TEST'), 'a', 'b']);
72b16652
GS
125
126# "~" should expand to $ENV{HOME}
127$ENV{HOME} = "sweet home";
00c80938 128@a = bsd_glob('~', GLOB_TILDE | GLOB_NOMAGIC);
1d89eaef 129is_deeply(\@a, [$ENV{HOME}]);
ab3ed403
BS
130
131# GLOB_ALPHASORT (default) should sort alphabetically regardless of case
8f562d3a
NA
132mkdir "pteerslo", 0777;
133chdir "pteerslo";
93782ce2 134
06494c4c
NC
135my @f_names = qw(Ax.pl Bx.pl Cx.pl aY.pl bY.pl cY.pl);
136my @f_alpha = qw(Ax.pl aY.pl Bx.pl bY.pl Cx.pl cY.pl);
93782ce2
BS
137if ('a' lt 'A') { # EBCDIC char sets sort lower case before UPPER
138 @f_names = sort(@f_names);
d2f5bb60 139}
89f4ed55 140if ($^O eq 'VMS') { # VMS is happily caseignorant
16421035
PP
141 @f_alpha = qw(ax.pl ay.pl bx.pl by.pl cx.pl cy.pl);
142 @f_names = @f_alpha;
143}
93782ce2
BS
144
145for (@f_names) {
ab3ed403
BS
146 open T, "> $_";
147 close T;
148}
93782ce2 149
06494c4c 150my $pat = "*.pl";
93782ce2 151
06494c4c 152my @g_names = bsd_glob($pat, 0);
93782ce2
BS
153print "# f_names = @f_names\n";
154print "# g_names = @g_names\n";
06494c4c 155is_deeply(\@g_names, \@f_names);
93782ce2 156
06494c4c 157my @g_alpha = bsd_glob($pat);
584c1423
JH
158print "# f_alpha = @f_alpha\n";
159print "# g_alpha = @g_alpha\n";
06494c4c 160is_deeply(\@g_alpha, \@f_alpha);
93782ce2
BS
161
162unlink @f_names;
ab3ed403 163chdir "..";
8f562d3a 164rmdir "pteerslo";
e0e8a4dc
MHM
165
166# this can panic if PL_glob_index gets passed as flags to bsd_glob
167<*>; <*>;
06494c4c 168pass("Don't panic");
77348331
SR
169
170{
171 use File::Temp qw(tempdir);
172 use File::Spec qw();
173
174 my($dir) = tempdir(CLEANUP => 1)
175 or die "Could not create temporary directory";
176 for my $file (qw(a_dej a_ghj a_qej)) {
177 open my $fh, ">", File::Spec->catfile($dir, $file)
178 or die "Could not create file $dir/$file: $!";
179 close $fh;
180 }
181 my $cwd = Cwd::cwd();
182 chdir $dir
183 or die "Could not chdir to $dir: $!";
184 my(@glob_files) = glob("a*{d[e]}j");
77348331
SR
185 chdir $cwd
186 or die "Could not chdir back to $cwd: $!";
946b2ae5
CB
187 local $TODO = "home-made glob doesn't do regexes" if $^O eq 'VMS';
188 is_deeply(\@glob_files, ['a_dej']);
77348331 189}