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