This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Configure sanity checks:
[perl5.git] / t / lib / glob-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 }
ab3ed403 16 print "1..11\n";
72b16652
GS
17}
18END {
19 print "not ok 1\n" unless $loaded;
20}
21use File::Glob ':glob';
a4cf958a 22use Cwd ();
72b16652
GS
23$loaded = 1;
24print "ok 1\n";
25
26sub array {
27 return '(', join(", ", map {defined $_ ? "\"$_\"" : "undef"} @a), ")\n";
28}
29
30# look for the contents of the current directory
31$ENV{PATH} = "/bin";
32delete @ENV{BASH_ENV, CDPATH, ENV, IFS};
33@correct = ();
7369a524 34if (opendir(D, $^O eq "MacOS" ? ":" : ".")) {
94a371ee 35 @correct = grep { !/^\./ } sort readdir(D);
72b16652
GS
36 closedir D;
37}
38@a = File::Glob::glob("*", 0);
39@a = sort @a;
40if ("@a" ne "@correct" || GLOB_ERROR) {
41 print "# |@a| ne |@correct|\nnot ";
42}
43print "ok 2\n";
44
45# look up the user's home directory
46# should return a list with one item, and not set ERROR
4b1e50d9 47if ($^O ne 'MSWin32' && $^O ne 'VMS') {
0eb4ebd7 48 eval {
72b16652 49 ($name, $home) = (getpwuid($>))[0,7];
0eb4ebd7
GS
50 1;
51 } and do {
00c80938 52 @a = bsd_glob("~$name", GLOB_TILDE);
72b16652
GS
53 if (scalar(@a) != 1 || $a[0] ne $home || GLOB_ERROR) {
54 print "not ";
55 }
0eb4ebd7 56 };
72b16652
GS
57}
58print "ok 3\n";
59
60# check backslashing
61# should return a list with one item, and not set ERROR
00c80938 62@a = bsd_glob('TEST', GLOB_QUOTE);
72b16652
GS
63if (scalar @a != 1 || $a[0] ne 'TEST' || GLOB_ERROR) {
64 local $/ = "][";
65 print "# [@a]\n";
66 print "not ";
67}
68print "ok 4\n";
69
70# check nonexistent checks
71# should return an empty list
72# XXX since errfunc is NULL on win32, this test is not valid there
00c80938 73@a = bsd_glob("asdfasdf", 0);
72b16652
GS
74if ($^O ne 'MSWin32' and scalar @a != 0) {
75 print "# |@a|\nnot ";
76}
77print "ok 5\n";
78
79# check bad protections
80# should return an empty list, and set ERROR
a4cf958a
GS
81if ($^O eq 'mpeix' or $^O eq 'MSWin32' or $^O eq 'os2' or $^O eq 'VMS'
82 or $^O eq 'cygwin' or Cwd::cwd() =~ m#^/afs#s or not $>)
83{
1cf742e9
GS
84 print "ok 6 # skipped\n";
85}
86else {
ab3ed403 87 $dir = "pteerslt";
1cf742e9 88 mkdir $dir, 0;
00c80938 89 @a = bsd_glob("$dir/*", GLOB_ERR);
1cf742e9
GS
90 #print "\@a = ", array(@a);
91 rmdir $dir;
92 if (scalar(@a) != 0 || GLOB_ERROR == 0) {
93 print "not ";
94 }
95 print "ok 6\n";
72b16652 96}
72b16652
GS
97
98# check for csh style globbing
00c80938 99@a = bsd_glob('{a,b}', GLOB_BRACE | GLOB_NOMAGIC);
72b16652
GS
100unless (@a == 2 and $a[0] eq 'a' and $a[1] eq 'b') {
101 print "not ";
102}
103print "ok 7\n";
104
00c80938 105@a = bsd_glob(
72b16652 106 '{TES*,doesntexist*,a,b}',
3eba041e 107 GLOB_BRACE | GLOB_NOMAGIC | ($^O eq 'VMS' ? GLOB_NOCASE : 0)
72b16652 108);
5d9a6404 109
8ac1de08
MS
110# Working on t/TEST often causes this test to fail because it sees Emacs temp
111# and RCS files. Filter them out, and .pm files too, and patch temp files.
112@a = grep !/(,v$|~$|\.(pm|ori?g|rej)$)/, @a;
113
114print "# @a\n";
5d9a6404 115
72b16652 116unless (@a == 3
f0963acb 117 and $a[0] eq ($^O eq 'VMS'? 'test.' : 'TEST')
72b16652
GS
118 and $a[1] eq 'a'
119 and $a[2] eq 'b')
120{
121 print "not ";
122}
123print "ok 8\n";
124
125# "~" should expand to $ENV{HOME}
126$ENV{HOME} = "sweet home";
00c80938 127@a = bsd_glob('~', GLOB_TILDE | GLOB_NOMAGIC);
7369a524 128unless ($^O eq "MacOS" || (@a == 1 and $a[0] eq $ENV{HOME})) {
72b16652
GS
129 print "not ";
130}
131print "ok 9\n";
ab3ed403
BS
132
133# GLOB_ALPHASORT (default) should sort alphabetically regardless of case
134mkdir "pteerslt", 0777;
135chdir "pteerslt";
93782ce2
BS
136
137@f_names = qw(Ax.pl Bx.pl Cx.pl aY.pl bY.pl cY.pl);
138@f_alpha = qw(Ax.pl aY.pl Bx.pl bY.pl Cx.pl cY.pl);
139if ('a' lt 'A') { # EBCDIC char sets sort lower case before UPPER
140 @f_names = sort(@f_names);
141 @f_alpha = qw(aY.pl Ax.pl bY.pl Bx.pl cY.pl Cx.pl);
d2f5bb60 142}
93782ce2
BS
143
144for (@f_names) {
ab3ed403
BS
145 open T, "> $_";
146 close T;
147}
93782ce2
BS
148
149$pat = "*.pl";
150
ab3ed403 151$ok = 1;
93782ce2
BS
152@g_names = bsd_glob($pat, 0);
153print "# f_names = @f_names\n";
154print "# g_names = @g_names\n";
155for (@f_names) {
156 $ok = 0 unless $_ eq shift @g_names;
ab3ed403
BS
157}
158print $ok ? "ok 10\n" : "not ok 10\n";
93782ce2 159
ab3ed403
BS
160$ok = 1;
161@g_alpha = bsd_glob($pat);
584c1423
JH
162print "# f_alpha = @f_alpha\n";
163print "# g_alpha = @g_alpha\n";
93782ce2
BS
164for (@f_alpha) {
165 $ok = 0 unless $_ eq shift @g_alpha;
ab3ed403
BS
166}
167print $ok ? "ok 11\n" : "not ok 11\n";
93782ce2
BS
168
169unlink @f_names;
ab3ed403
BS
170chdir "..";
171rmdir "pteerslt";