xref: /openbsd-src/gnu/usr.bin/perl/t/op/glob.t (revision e068048151d29f2562a32185e21a8ba885482260)
1#!./perl
2
3BEGIN {
4    chdir 't' if -d 't';
5    require './test.pl';
6    set_up_inc( qw(. ../lib) );
7}
8
9plan( tests => 18 );
10
11@oops = @ops = <op/*>;
12
13if ($^O eq 'MSWin32') {
14  map { $files{lc($_)}++ } <op/*>;
15  map { delete $files{"op/$_"} } split /[\s\n]/, `dir /b /l op & dir /b /l /ah op 2>nul`,
16}
17elsif ($^O eq 'VMS') {
18  map { $files{lc($_)}++ } <[.op]*>;
19  map { s/;.*$//; delete $files{lc($_)}; } split /[\n]/, `directory/noheading/notrailing/versions=1 [.op]`,
20}
21else {
22  local %ENV = %ENV;
23  # disable any env vars that might cause ls or dir to add colors or
24  # otherwise modify the output.
25  /COLOR|LS|CLI/i and delete $ENV{$_} for keys %ENV;
26
27  map { $files{$_}++ } <op/*>;
28  map { delete $files{"op/$_"} } split /\n/, `ls op/ | cat`;
29}
30ok( !(keys(%files)),'glob and directory listing agree' )
31    or diag(join(' ',sort keys %files));
32
33cmp_ok($/,'eq',"\n",'sane input record separator');
34
35$not = '';
36while (<jskdfjskdfj* op/* jskdjfjkosvk*>) {
37    $not = "not " unless $_ eq shift @ops;
38    $not = "not at all " if $/ eq "\0";
39}
40ok(!$not,"glob amid garbage [$not]");
41
42cmp_ok($/,'eq',"\n",'input record separator still sane');
43
44$_ = "op/*";
45@glops = glob $_;
46cmp_ok("@glops",'eq',"@oops",'glob operator 1');
47
48@glops = glob;
49cmp_ok("@glops",'eq',"@oops",'glob operator 2');
50
51# glob should still work even after the File::Glob stash has gone away
52# (this used to dump core)
53my $i = 0;
54for (1..2) {
55    eval "<.>";
56    ok(!length($@),"eval'ed a glob $_");
57    local %File::Glob::;
58    ++$i;
59}
60cmp_ok($i,'==',2,'remove File::Glob stash');
61
62# a more sinister version of the same test (crashes from 5.8 to 5.13.1)
63{
64    local %File::Glob::;
65    local %CORE::GLOBAL::;
66    eval "<.>";
67    ok(!length($@),"remove File::Glob stash *and* CORE::GLOBAL::glob");
68}
69# Also try undeffing the typeglob itself, instead of hiding it
70{
71    local *CORE::GLOBAL::glob;
72    ok eval  { glob("0"); 1 },
73	'undefined *CORE::GLOBAL::glob{CODE} at run time';
74}
75# And hide the typeglob without hiding File::Glob (crashes from 5.8
76# to 5.15.4)
77{
78    local %CORE::GLOBAL::;
79    ok eval q{ glob("0"); 1 },
80	'undefined *CORE::GLOBAL::glob{CODE} at compile time';
81}
82
83# ... while ($var = glob(...)) should test definedness not truth
84
85SKIP: {
86    skip('no File::Glob to emulate Unix-ism', 1)
87	unless $INC{'File/Glob.pm'};
88    my $ok = 0;
89    $ok = 1 while my $var = glob("0");
90    ok($ok,'define versus truth');
91}
92
93# The formerly-broken test for the situation above would accidentally
94# test definedness for an assignment with a LOGOP on the right:
95{
96    my $f = 0;
97    my $ok = 1;
98    $ok = 0, undef $f while $x = $f||$f;
99    ok($ok,'test definedness with LOGOP');
100}
101
102cmp_ok(scalar(@oops),'>',0,'glob globbed something');
103
104SKIP: {
105    skip "~ globbing returns nothing on VMS", 1 if $^O eq 'VMS';
106    # This test exists mainly for miniperl, to test that external calls to
107    # csh, which clear %ENV first, leave $ENV{HOME}.
108    # On Windows, external glob uses File::DosGlob which returns "~", so
109    # this should pass anyway.
110    ok <~>, '~ works';
111}
112
113{
114    my $called;
115    local *CORE::GLOBAL::glob = sub { ++$called };
116    eval 'CORE::glob("0")';
117    ok !$called, 'CORE::glob bypasses overrides';
118}
119
120######## glob() bug Mon, 01 Sep 2003 02:25:41 -0700 <200309010925.h819Pf0X011457@smtp3.ActiveState.com>
121
122SKIP: {
123    use Config;
124    skip("glob() works when cross-compiling, but this test doesn't", 1)
125        if $Config{usecrosscompile};
126
127    my $switches = [qw(-lw)];
128    my $expected = "ok1\nok2\n";
129    my $name     = "Make sure the presence of the CORE::GLOBAL::glob typeglob does not affect whether File::Glob::csh_glob is called.";
130
131    fresh_perl_is(<<'EOP', $expected, { switches => $switches }, $name);
132    if ($^O eq 'VMS') {
133        # A pattern with a double quote in it is a syntax error to LIB$FIND_FILE
134        # Should we strip quotes in Perl_vms_start_glob the way csh_glob() does?
135        print "ok1\nok2\n";
136    }
137    else {
138        ++$INC{"File/Glob.pm"}; # prevent it from loading
139        my $called1 = 0;
140        my $called2 = 0;
141        *File::Glob::csh_glob = sub { ++$called1 };
142        my $output1 = eval q{ glob(q(./"TEST")) };
143        undef *CORE::GLOBAL::glob; # but leave the typeglob itself there
144        ++$CORE::GLOBAL::glob if 0; # "used only once"
145        undef *File::Glob::csh_glob; # avoid redefinition warnings
146        *File::Glob::csh_glob = sub { ++$called2 };
147        my $output2 = eval q{ glob(q(./"TEST")) };
148        print "ok1" if $called1 eq $called2;
149        print "ok2" if $output1 eq $output2;
150    }
151EOP
152}
153