summaryrefslogtreecommitdiff
path: root/t
diff options
context:
space:
mode:
authorNicholas Clark <nick@ccl4.org>2010-08-20 21:22:45 +0200
committerNicholas Clark <nick@ccl4.org>2010-08-20 21:22:45 +0200
commitb7781c7ebca16bcc1cb2792cc1754c7d44875050 (patch)
treecf8277d73acd6aa85a40d8d55a725a1ce87a9132 /t
parent26359cfa9ac0b8c63aa387ca92e7fa1b20d22d39 (diff)
downloadperl-b7781c7ebca16bcc1cb2792cc1754c7d44875050.tar.gz
while_readdir.t needs a private directory to run in.
It assumes that it can read the contents twice and get the same result. It's running in parallel with tests which do sometimes write in t/ so if it runs at that level, it will sometimes fail due to race conditions.
Diffstat (limited to 't')
-rw-r--r--t/op/while_readdir.t62
1 files changed, 56 insertions, 6 deletions
diff --git a/t/op/while_readdir.t b/t/op/while_readdir.t
index 3964158287..1473dada92 100644
--- a/t/op/while_readdir.t
+++ b/t/op/while_readdir.t
@@ -9,8 +9,49 @@ BEGIN {
use strict;
use warnings;
-open my $fh, ">", "0" or die "Can't open '0' for writing: $!\n";
-print $fh <<'FILE0';
+plan 10;
+
+# Need to run this in a quiet private directory as it assumes that it can read
+# the contents twice and get the same result.
+my $tempdir = tempfile;
+
+mkdir $tempdir, 0700 or die "Can't mkdir '$tempdir': $!";
+chdir $tempdir or die die "Can't chdir '$tempdir': $!";
+
+my $cleanup = 1;
+my %tempfiles;
+
+END {
+ if ($cleanup) {
+ foreach my $file (keys %tempfiles) {
+ # We only wrote each of these once so 1 delete should work:
+ if (unlink $file) {
+ warn "unlink tempfile '$file' passed but it's still there"
+ if -e $file;
+ } else {
+ warn "Couldn't unlink tempfile '$file': $!";
+ }
+ }
+ chdir '..' or die "Couldn't chdir .. for cleanup: $!";
+ rmdir $tempdir or die "Couldn't unlink tempdir '$tempdir': $!";
+ }
+}
+
+# This is intentionally not random (per run), but intentionally will try to
+# give different file names for different people running this test.
+srand $< * $];
+
+my @chars = ('A' .. 'Z', 'a' .. 'z', 0 .. 9);
+
+sub make_file {
+ my $name = shift;
+
+ return if $tempfiles{$name}++;
+
+ print "# Writing to $name in $tempdir\n";
+
+ open my $fh, '>', $name or die "Can't open '$name' for writing: $!\n";
+ print $fh <<'FILE0';
This file is here for testing
while(readdir $dir){...}
@@ -18,9 +59,20 @@ while(readdir $dir){...}
etc
FILE0
-close $fh;
+ close $fh or die "Can't close '$name': $!";
+}
-plan 10;
+sub make_some_files {
+ for (1..int rand 10) {
+ my $name;
+ $name .= $chars[rand $#chars] for 1..int(1 + rand 5);
+ make_file($name);
+ }
+}
+
+make_some_files();
+make_file('0');
+make_some_files();
ok(-f '0', "'0' file is here");
@@ -126,5 +178,3 @@ rewinddir $dirhandle;
}
closedir $dirhandle;
-
-END { 1 while unlink "0" }