xref: /openbsd-src/gnu/usr.bin/perl/ext/File-Glob/t/basic.t (revision c90a81c56dcebd6a1b73fe4aff9b03385b8e63b3)
1#!./perl
2
3BEGIN {
4    chdir 't' if -d 't';
5    @INC = '../lib';
6    require Config; import Config;
7    if ($Config{'extensions'} !~ /\bFile\/Glob\b/i) {
8        print "1..0\n";
9        exit 0;
10    }
11}
12use strict;
13use Test::More tests => 49;
14BEGIN {use_ok('File::Glob', ':glob')};
15use Cwd ();
16
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
34# look for the contents of the current directory
35# try it in a directory that doesn't get modified during testing,
36# so parallel testing won't give us race conditions. t/base/ seems
37# fairly static
38
39chdir 'base' or die "chdir base: $!";
40$ENV{PATH} = "/bin";
41delete @ENV{qw(BASH_ENV CDPATH ENV IFS)};
42my @correct = ();
43if (opendir(D, ".")) {
44   @correct = grep { !/^\./ } sort readdir(D);
45   closedir D;
46}
47my @a = File::Glob::glob("*", 0);
48@a = sort @a;
49if (GLOB_ERROR) {
50    fail(GLOB_ERROR);
51} else {
52    is_deeply(\@a, \@correct);
53}
54chdir '..' or die "chdir .. $!";
55
56# look up the user's home directory
57# should return a list with one item, and not set ERROR
58SKIP: {
59    my ($name, $home);
60    skip $^O, 1 if $^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'VMS'
61	|| $^O eq 'os2';
62    skip "Can't find user for $>: $@", 1 unless eval {
63	($name, $home) = (getpwuid($>))[0,7];
64	1;
65    };
66    skip "$> has no home directory", 1
67	unless defined $home && defined $name && -d $home;
68
69    @a = bsd_glob("~$name", GLOB_TILDE);
70
71    if (GLOB_ERROR) {
72	fail(GLOB_ERROR);
73    } else {
74	is_deeply (\@a, [$home]);
75    }
76}
77# check plain tilde expansion
78{
79    my $tilde_check = sub {
80        my @a = bsd_glob('~');
81
82        if (GLOB_ERROR) {
83            fail(GLOB_ERROR);
84        } else {
85            is_deeply (\@a, [$_[0]], join ' - ', 'tilde expansion', @_ > 1 ? $_[1] : ());
86        }
87    };
88    my $passwd_home = eval { (getpwuid($>))[7] };
89
90    TODO: {
91        local $TODO = 'directory brackets look like pattern brackets to glob' if $^O eq 'VMS';
92        local $ENV{HOME};
93        delete $ENV{HOME};
94        local $ENV{USERPROFILE};
95        delete $ENV{USERPROFILE};
96        $tilde_check->(defined $passwd_home ? $passwd_home : q{~}, 'no environment');
97    }
98
99    SKIP: {
100        skip 'MSWin32 only', 1 if $^O ne 'MSWin32';
101        local $ENV{HOME};
102        delete $ENV{HOME};
103        local $ENV{USERPROFILE};
104        $ENV{USERPROFILE} = 'sweet win32 home';
105        $tilde_check->(defined $passwd_home ? $passwd_home : $ENV{USERPROFILE}, 'USERPROFILE');
106    }
107
108    TODO: {
109        local $TODO = 'directory brackets look like pattern brackets to glob' if $^O eq 'VMS';
110        my $home = exists $ENV{HOME} ? $ENV{HOME}
111        : eval { getpwuid($>); 1 } ? (getpwuid($>))[7]
112        : $^O eq 'MSWin32' && exists $ENV{USERPROFILE} ? $ENV{USERPROFILE}
113        : q{~};
114        $tilde_check->($home);
115    }
116}
117
118# check backslashing
119# should return a list with one item, and not set ERROR
120@a = bsd_glob('TEST', GLOB_QUOTE);
121if (GLOB_ERROR) {
122    fail(GLOB_ERROR);
123} else {
124    is_deeply(\@a, ['TEST']);
125}
126
127# check nonexistent checks
128# should return an empty list
129# XXX since errfunc is NULL on win32, this test is not valid there
130@a = bsd_glob("asdfasdf", 0);
131SKIP: {
132    skip $^O, 1 if $^O eq 'MSWin32' || $^O eq 'NetWare';
133    is_deeply(\@a, []);
134}
135
136# check bad protections
137# should return an empty list, and set ERROR
138SKIP: {
139    skip $^O, 2 if $^O eq 'MSWin32' or $^O eq 'NetWare'
140	or $^O eq 'os2' or $^O eq 'VMS' or $^O eq 'cygwin';
141    skip "AFS", 2 if Cwd::cwd() =~ m#^$Config{'afsroot'}#s;
142    skip "running as root", 2 if not $>;
143
144    my $dir = "pteerslo";
145    mkdir $dir, 0;
146    @a = bsd_glob("$dir/*", GLOB_ERR);
147    rmdir $dir;
148    local $TODO = 'hit VOS bug posix-956' if $^O eq 'vos';
149
150    isnt(GLOB_ERROR, 0);
151    is_deeply(\@a, []);
152}
153
154# check for csh style globbing
155@a = bsd_glob('{a,b}', GLOB_BRACE | GLOB_NOMAGIC);
156is_deeply(\@a, ['a', 'b']);
157
158@a = bsd_glob(
159    '{TES*,doesntexist*,a,b}',
160    GLOB_BRACE | GLOB_NOMAGIC | ($^O eq 'VMS' ? GLOB_NOCASE : 0)
161);
162
163# Working on t/TEST often causes this test to fail because it sees Emacs temp
164# and RCS files.  Filter them out, and .pm files too, and patch temp files.
165@a = grep !/(,v$|~$|\.(pm|ori?g|rej)$)/, @a;
166@a = (grep !/test.pl/, @a) if $^O eq 'VMS';
167
168map { $_  =~ s/test\.?/TEST/i } @a if $^O eq 'VMS';
169print "# @a\n";
170
171is_deeply(\@a, ['TEST', 'a', 'b']);
172
173# "~" should expand to $ENV{HOME}
174{
175    local $ENV{HOME} = "sweet home";
176    @a = bsd_glob('~', GLOB_TILDE | GLOB_NOMAGIC);
177    is_deeply(\@a, [$ENV{HOME}]);
178}
179
180# GLOB_ALPHASORT (default) should sort alphabetically regardless of case
181mkdir "pteerslo", 0777;
182chdir "pteerslo";
183
184my @f_names = qw(Ax.pl Bx.pl Cx.pl aY.pl bY.pl cY.pl);
185my @f_alpha = qw(Ax.pl aY.pl Bx.pl bY.pl Cx.pl cY.pl);
186if ('a' lt 'A') { # EBCDIC char sets sort lower case before UPPER
187    @f_names = sort(@f_names);
188}
189if ($^O eq 'VMS') { # VMS is happily caseignorant
190    @f_alpha = qw(ax.pl ay.pl bx.pl by.pl cx.pl cy.pl);
191    @f_names = @f_alpha;
192}
193
194for (@f_names) {
195    open T, "> $_";
196    close T;
197}
198
199my $pat = "*.pl";
200
201my @g_names = bsd_glob($pat, 0);
202print "# f_names = @f_names\n";
203print "# g_names = @g_names\n";
204is_deeply(\@g_names, \@f_names);
205
206my @g_alpha = bsd_glob($pat);
207print "# f_alpha = @f_alpha\n";
208print "# g_alpha = @g_alpha\n";
209is_deeply(\@g_alpha, \@f_alpha);
210
211unlink @f_names;
212chdir "..";
213rmdir "pteerslo";
214
215# this can panic if PL_glob_index gets passed as flags to bsd_glob
216<*>; <*>;
217pass("Don't panic");
218
219{
220    use File::Temp qw(tempdir);
221    use File::Spec qw();
222
223    my($dir) = tempdir(CLEANUP => 1)
224	or die "Could not create temporary directory";
225    for my $file (qw(a_dej a_ghj a_qej)) {
226	open my $fh, ">", File::Spec->catfile($dir, $file)
227	    or die "Could not create file $dir/$file: $!";
228	close $fh;
229    }
230    my $cwd = Cwd::cwd();
231    chdir $dir
232	or die "Could not chdir to $dir: $!";
233    my(@glob_files) = glob("a*{d[e]}j");
234    chdir $cwd
235	or die "Could not chdir back to $cwd: $!";
236    local $TODO = "home-made glob doesn't do regexes" if $^O eq 'VMS';
237    is_deeply(\@glob_files, ['a_dej']);
238}
239
240# This used to segfault.
241my $i = bsd_glob('*', GLOB_ALTDIRFUNC);
242is(&File::Glob::GLOB_ERROR, 0, "Successfuly ignored unsupported flag");
243
244package frimpy; # get away from the glob override, so we can test csh_glob,
245use Test::More;  # which is perl's default
246
247# In case of PERL_EXTERNAL_GLOB:
248use subs 'glob';
249BEGIN { *glob = \&File::Glob::csh_glob }
250
251is +(glob "a'b'")[0], (<a'b' c>)[0], "a'b' with and without spaces";
252is <a"b">, 'ab', 'a"b" without spaces';
253is_deeply [<a"b" c>], [qw<ab c>], 'a"b" without spaces';
254is_deeply [<\\* .\\*>], [<\\*>,<.\\*>], 'backslashes with(out) spaces';
255like <\\ >, qr/^\\? \z/, 'final escaped space';
256is <a"b>, 'a"b', 'unmatched quote';
257is < a"b >, 'a"b', 'unmatched quote with surrounding spaces';
258is glob('a\"b'), 'a"b', '\ before quote *only* escapes quote';
259is glob(q"a\'b"), "a'b", '\ before single quote *only* escapes quote';
260is glob('"a\"b c\"d"'), 'a"b c"d', 'before \" within "..."';
261is glob(q"'a\'b c\'d'"), "a'b c'd", q"before \' within '...'";
262
263
264package bsdglob;  # for testing the :bsd_glob export tag
265
266use File::Glob ':bsd_glob';
267use Test::More;
268for (qw[
269        GLOB_ABEND
270	GLOB_ALPHASORT
271        GLOB_ALTDIRFUNC
272        GLOB_BRACE
273        GLOB_CSH
274        GLOB_ERR
275        GLOB_ERROR
276        GLOB_LIMIT
277        GLOB_MARK
278        GLOB_NOCASE
279        GLOB_NOCHECK
280        GLOB_NOMAGIC
281        GLOB_NOSORT
282        GLOB_NOSPACE
283        GLOB_QUOTE
284        GLOB_TILDE
285        bsd_glob
286    ]) {
287    ok (exists &$_, qq':bsd_glob exports $_');
288}
289is <a b>, 'a b', '<a b> under :bsd_glob';
290is <"a" "b">, '"a" "b"', '<"a" "b"> under :bsd_glob';
291is_deeply [<a b>], [q<a b>], '<> in list context under :bsd_glob';
292