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