This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Disallow weakening of read-only references
[perl5.git] / ext / File-Glob / t / basic.t
1 #!./perl
2
3 BEGIN {
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 }
12 use strict;
13 use Test::More tests => 18;
14 BEGIN {use_ok('File::Glob', ':glob')};
15 use Cwd ();
16
17 my $vms_unix_rpt = 0;
18 my $vms_efs = 0;
19 my $vms_mode = 0;
20 if ($^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 $ENV{PATH} = "/bin";
36 delete @ENV{qw(BASH_ENV CDPATH ENV IFS)};
37 my @correct = ();
38 if (opendir(D, ".")) {
39    @correct = grep { !/^\./ } sort readdir(D);
40    closedir D;
41 }
42 my @a = File::Glob::glob("*", 0);
43 @a = sort @a;
44 if (GLOB_ERROR) {
45     fail(GLOB_ERROR);
46 } else {
47     is_deeply(\@a, \@correct);
48 }
49
50 # look up the user's home directory
51 # should return a list with one item, and not set ERROR
52 SKIP: {
53     my ($name, $home);
54     skip $^O, 1 if $^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'VMS'
55         || $^O eq 'os2' || $^O eq 'beos';
56     skip "Can't find user for $>: $@", 1 unless eval {
57         ($name, $home) = (getpwuid($>))[0,7];
58         1;
59     };
60     skip "$> has no home directory", 1
61         unless defined $home && defined $name && -d $home;
62
63     @a = bsd_glob("~$name", GLOB_TILDE);
64
65     if (GLOB_ERROR) {
66         fail(GLOB_ERROR);
67     } else {
68         is_deeply (\@a, [$home]);
69     }
70 }
71 # check plain tilde expansion
72 {
73     my $tilde_check = sub {
74     my @a = bsd_glob('~');
75
76     if (GLOB_ERROR) {
77         fail(GLOB_ERROR);
78     } else {
79         is_deeply (\@a, [$_[0]], join ' - ', 'tilde expansion', @_ > 1 ? $_[1] : ());
80     }
81     };
82     my $passwd_home = eval { (getpwuid($>))[7] };
83
84     {
85     local %ENV = %ENV;
86     delete $ENV{HOME};
87     delete $ENV{USERPROFILE};
88     $tilde_check->(defined $passwd_home ? $passwd_home : q{~}, 'no environment');
89     }
90
91     SKIP: {
92     skip 'MSWin32 only', 1 if $^O ne 'MSWin32';
93     local %ENV = %ENV;
94     delete $ENV{HOME};
95     $ENV{USERPROFILE} = 'sweet win32 home';
96     $tilde_check->(defined $passwd_home ? $passwd_home : $ENV{USERPROFILE}, 'USERPROFILE');
97     }
98
99     my $home = exists $ENV{HOME} ? $ENV{HOME}
100     : eval { getpwuid($>); 1 } ? (getpwuid($>))[7]
101     : $^O eq 'MSWin32' && exists $ENV{USERPROFILE} ? $ENV{USERPROFILE}
102     : q{~};
103     $tilde_check->($home);
104 }
105
106 # check backslashing
107 # should return a list with one item, and not set ERROR
108 @a = bsd_glob('TEST', GLOB_QUOTE);
109 if (GLOB_ERROR) {
110     fail(GLOB_ERROR);
111 } else {
112     is_deeply(\@a, ['TEST']);
113 }
114
115 # check nonexistent checks
116 # should return an empty list
117 # XXX since errfunc is NULL on win32, this test is not valid there
118 @a = bsd_glob("asdfasdf", 0);
119 SKIP: {
120     skip $^O, 1 if $^O eq 'MSWin32' || $^O eq 'NetWare';
121     is_deeply(\@a, []);
122 }
123
124 # check bad protections
125 # should return an empty list, and set ERROR
126 SKIP: {
127     skip $^O, 2 if $^O eq 'mpeix' or $^O eq 'MSWin32' or $^O eq 'NetWare'
128         or $^O eq 'os2' or $^O eq 'VMS' or $^O eq 'cygwin';
129     skip "AFS", 2 if Cwd::cwd() =~ m#^$Config{'afsroot'}#s;
130     skip "running as root", 2 if not $>;
131
132     my $dir = "pteerslo";
133     mkdir $dir, 0;
134     @a = bsd_glob("$dir/*", GLOB_ERR);
135     rmdir $dir;
136     local $TODO = 'hit VOS bug posix-956' if $^O eq 'vos';
137
138     isnt(GLOB_ERROR, 0);
139     is_deeply(\@a, []);
140 }
141
142 # check for csh style globbing
143 @a = bsd_glob('{a,b}', GLOB_BRACE | GLOB_NOMAGIC);
144 is_deeply(\@a, ['a', 'b']);
145
146 @a = bsd_glob(
147     '{TES*,doesntexist*,a,b}',
148     GLOB_BRACE | GLOB_NOMAGIC | ($^O eq 'VMS' ? GLOB_NOCASE : 0)
149 );
150
151 # Working on t/TEST often causes this test to fail because it sees Emacs temp
152 # and RCS files.  Filter them out, and .pm files too, and patch temp files.
153 @a = grep !/(,v$|~$|\.(pm|ori?g|rej)$)/, @a;
154 @a = (grep !/test.pl/, @a) if $^O eq 'VMS';
155
156 print "# @a\n";
157
158 is_deeply(\@a, [($vms_mode ? 'test.' : 'TEST'), 'a', 'b']);
159
160 # "~" should expand to $ENV{HOME}
161 $ENV{HOME} = "sweet home";
162 @a = bsd_glob('~', GLOB_TILDE | GLOB_NOMAGIC);
163 is_deeply(\@a, [$ENV{HOME}]);
164
165 # GLOB_ALPHASORT (default) should sort alphabetically regardless of case
166 mkdir "pteerslo", 0777;
167 chdir "pteerslo";
168
169 my @f_names = qw(Ax.pl Bx.pl Cx.pl aY.pl bY.pl cY.pl);
170 my @f_alpha = qw(Ax.pl aY.pl Bx.pl bY.pl Cx.pl cY.pl);
171 if ('a' lt 'A') { # EBCDIC char sets sort lower case before UPPER
172     @f_names = sort(@f_names);
173 }
174 if ($^O eq 'VMS') { # VMS is happily caseignorant
175     @f_alpha = qw(ax.pl ay.pl bx.pl by.pl cx.pl cy.pl);
176     @f_names = @f_alpha;
177 }
178
179 for (@f_names) {
180     open T, "> $_";
181     close T;
182 }
183
184 my $pat = "*.pl";
185
186 my @g_names = bsd_glob($pat, 0);
187 print "# f_names = @f_names\n";
188 print "# g_names = @g_names\n";
189 is_deeply(\@g_names, \@f_names);
190
191 my @g_alpha = bsd_glob($pat);
192 print "# f_alpha = @f_alpha\n";
193 print "# g_alpha = @g_alpha\n";
194 is_deeply(\@g_alpha, \@f_alpha);
195
196 unlink @f_names;
197 chdir "..";
198 rmdir "pteerslo";
199
200 # this can panic if PL_glob_index gets passed as flags to bsd_glob
201 <*>; <*>;
202 pass("Don't panic");
203
204 {
205     use File::Temp qw(tempdir);
206     use File::Spec qw();
207
208     my($dir) = tempdir(CLEANUP => 1)
209         or die "Could not create temporary directory";
210     for my $file (qw(a_dej a_ghj a_qej)) {
211         open my $fh, ">", File::Spec->catfile($dir, $file)
212             or die "Could not create file $dir/$file: $!";
213         close $fh;
214     }
215     my $cwd = Cwd::cwd();
216     chdir $dir
217         or die "Could not chdir to $dir: $!";
218     my(@glob_files) = glob("a*{d[e]}j");
219     chdir $cwd
220         or die "Could not chdir back to $cwd: $!";
221     local $TODO = "home-made glob doesn't do regexes" if $^O eq 'VMS';
222     is_deeply(\@glob_files, ['a_dej']);
223 }
224
225 # This used to segfault.
226 my $i = bsd_glob('*', GLOB_ALTDIRFUNC);
227 is(&File::Glob::GLOB_ERROR, 0, "Successfuly ignored unsupported flag");