This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
add File::BSDGlob as File::Glob and load it at compile-time
[perl5.git] / t / lib / glob-basic.t
CommitLineData
72b16652
GS
1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
5 unshift @INC, '../lib';
6
7 print "1..9\n";
8}
9END {
10 print "not ok 1\n" unless $loaded;
11}
12use File::Glob ':glob';
13$loaded = 1;
14print "ok 1\n";
15
16sub array {
17 return '(', join(", ", map {defined $_ ? "\"$_\"" : "undef"} @a), ")\n";
18}
19
20# look for the contents of the current directory
21$ENV{PATH} = "/bin";
22delete @ENV{BASH_ENV, CDPATH, ENV, IFS};
23@correct = ();
24if (opendir(D, ".")) {
25 @correct = grep { !/^\.\.?$/ } sort readdir(D);
26 closedir D;
27}
28@a = File::Glob::glob("*", 0);
29@a = sort @a;
30if ("@a" ne "@correct" || GLOB_ERROR) {
31 print "# |@a| ne |@correct|\nnot ";
32}
33print "ok 2\n";
34
35# look up the user's home directory
36# should return a list with one item, and not set ERROR
37if ($^O ne 'MSWin32') {
38 ($name, $home) = (getpwuid($>))[0,7];
39 @a = File::Glob::glob("~$name", GLOB_TILDE);
40 if (scalar(@a) != 1 || $a[0] ne $home || GLOB_ERROR) {
41 print "not ";
42 }
43}
44print "ok 3\n";
45
46# check backslashing
47# should return a list with one item, and not set ERROR
48@a = File::Glob::glob('TEST', GLOB_QUOTE);
49if (scalar @a != 1 || $a[0] ne 'TEST' || GLOB_ERROR) {
50 local $/ = "][";
51 print "# [@a]\n";
52 print "not ";
53}
54print "ok 4\n";
55
56# check nonexistent checks
57# should return an empty list
58# XXX since errfunc is NULL on win32, this test is not valid there
59@a = File::Glob::glob("asdfasdf", 0);
60if ($^O ne 'MSWin32' and scalar @a != 0) {
61 print "# |@a|\nnot ";
62}
63print "ok 5\n";
64
65# check bad protections
66# should return an empty list, and set ERROR
67$dir = "PtEeRsLt.dir";
68mkdir $dir, 0;
69@a = File::Glob::glob("$dir/*", GLOB_ERR);
70#print "\@a = ", array(@a);
71rmdir $dir;
72if (scalar(@a) != 0 || ($^O ne 'MSWin32' && GLOB_ERROR == 0)) {
73 print "not ";
74}
75print "ok 6\n";
76
77# check for csh style globbing
78@a = File::Glob::glob('{a,b}', GLOB_BRACE | GLOB_NOMAGIC);
79unless (@a == 2 and $a[0] eq 'a' and $a[1] eq 'b') {
80 print "not ";
81}
82print "ok 7\n";
83
84@a = File::Glob::glob(
85 '{TES*,doesntexist*,a,b}',
86 GLOB_BRACE | GLOB_NOMAGIC
87);
88unless (@a == 3
89 and $a[0] eq 'TEST'
90 and $a[1] eq 'a'
91 and $a[2] eq 'b')
92{
93 print "not ";
94}
95print "ok 8\n";
96
97# "~" should expand to $ENV{HOME}
98$ENV{HOME} = "sweet home";
99@a = File::Glob::glob('~', GLOB_TILDE | GLOB_NOMAGIC);
100unless (@a == 1 and $a[0] eq $ENV{HOME}) {
101 print "not ";
102}
103print "ok 9\n";