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