This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix CORE::glob
[perl5.git] / t / op / glob.t
old mode 100755 (executable)
new mode 100644 (file)
index 1dd806e..3c64353
@@ -2,10 +2,11 @@
 
 BEGIN {
     chdir 't' if -d 't';
-    @INC = '../lib';
+    @INC = qw(. ../lib);
+    require 'test.pl';
 }
 
-print "1..13\n";
+plan( tests => 17 );
 
 @oops = @ops = <op/*>;
 
@@ -21,52 +22,88 @@ else {
   map { $files{$_}++ } <op/*>;
   map { delete $files{$_} } split /[\s\n]/, `echo op/*`;
 }
-if (keys %files) {
-       print "not ok 1\t(",join(' ', sort keys %files),"\n";
-} else { print "ok 1\n"; }
+ok( !(keys(%files)),'leftover op/* files' ) or diag(join(' ',sort keys %files));
 
-print $/ eq "\n" ? "ok 2\n" : "not ok 2\n";
+cmp_ok($/,'eq',"\n",'sane input record separator');
 
+$not = '';
 while (<jskdfjskdfj* op/* jskdjfjkosvk*>) {
     $not = "not " unless $_ eq shift @ops;
     $not = "not at all " if $/ eq "\0";
 }
-print "${not}ok 3\n";
+ok(!$not,"glob amid garbage [$not]");
 
-print $/ eq "\n" ? "ok 4\n" : "not ok 4\n";
+cmp_ok($/,'eq',"\n",'input record separator still sane');
 
-# test the "glob" operator
 $_ = "op/*";
 @glops = glob $_;
-print "@glops" eq "@oops" ? "ok 5\n" : "not ok 5\n";
+cmp_ok("@glops",'eq',"@oops",'glob operator 1');
 
 @glops = glob;
-print "@glops" eq "@oops" ? "ok 6\n" : "not ok 6\n";
+cmp_ok("@glops",'eq',"@oops",'glob operator 2');
 
 # glob should still work even after the File::Glob stash has gone away
 # (this used to dump core)
 my $i = 0;
 for (1..2) {
     eval "<.>";
-    undef %File::Glob::;
+    ok(!length($@),"eval'ed a glob $_");
+    local %File::Glob::;
     ++$i;
 }
-print $i == 2 ? "ok 7\n" : "not ok 7\n";
+cmp_ok($i,'==',2,'remove File::Glob stash');
 
-# [ID 20010526.001] localized glob loses value when assigned to
+# a more sinister version of the same test (crashes from 5.8 to 5.13.1)
+{
+    local %File::Glob::;
+    local %CORE::GLOBAL::;
+    eval "<.>";
+    ok(!length($@),"remove File::Glob stash *and* CORE::GLOBAL::glob");
+}
+# Also try undeffing the typeglob itself, instead of hiding it
+{
+    local *CORE::GLOBAL::glob;
+    ok eval  { glob("0"); 1 },
+       'undefined *CORE::GLOBAL::glob{CODE} at run time';
+}
+# And hide the typeglob without hiding File::Glob (crashes from 5.8
+# to 5.15.4)
+{
+    local %CORE::GLOBAL::;
+    ok eval q{ glob("0"); 1 },
+       'undefined *CORE::GLOBAL::glob{CODE} at compile time';
+}
+
+# ... while ($var = glob(...)) should test definedness not truth
 
-$j=1; %j=(a=>1); @j=(1); local *j=*j; *j = sub{};
+SKIP: {
+    skip('no File::Glob to emulate Unix-ism', 1)
+       unless $INC{'File/Glob.pm'};
+    my $ok = 0;
+    $ok = 1 while my $var = glob("0");
+    ok($ok,'define versus truth');
+}
 
-print $j    == 1 ? "ok 8\n"  : "not ok 8\n";
-print $j{a} == 1 ? "ok 9\n"  : "not ok 9\n";
-print $j[0] == 1 ? "ok 10\n" : "not ok 10\n";
+# The formerly-broken test for the situation above would accidentally
+# test definedness for an assignment with a LOGOP on the right:
+{
+    my $f = 0;
+    my $ok = 1;
+    $ok = 0, undef $f while $x = $f||$f;
+    ok($ok,'test definedness with LOGOP');
+}
 
-# ... while ($var = glob(...)) should test definedness not truth
+cmp_ok(scalar(@oops),'>',0,'glob globbed something');
 
-# Create a file called "0"
-print open(F, ">0") ? "ok 11\n" : "not ok 11 # $!\n";
-my $ok = "not ok 12\n";
-$ok = "ok 12\n" while my $var = glob("0");
-print $ok;
+# This test exists mainly for miniperl, to test that external calls to
+# csh, which clear %ENV first, leave $ENV{HOME}.
+# On Windows, external glob uses File::DosGlob which returns "~", so this
+# should pass anyway.
+ok <~>, '~ works';
 
-print unlink("0") ? "ok 13\n" : "not ok 13\n";
+{
+    my $called;
+    local *CORE::GLOBAL::glob = sub { ++$called };
+    eval 'CORE::glob("0")';
+    ok !$called, 'CORE::glob bypasses overrides';
+}