Add register_tempfile() to t/test.pl
authorBrad Gilbert <b2gills@gmail.com>
Tue, 4 Feb 2014 16:37:02 +0000 (10:37 -0600)
committerTony Cook <tony@develop-help.com>
Thu, 13 Feb 2014 02:53:28 +0000 (13:53 +1100)
This convenience function causes files to be removed in the same
way that tempfile() does.

It uses the same variable that tempfile() does, to catch and
warn on any collisions.

t/test.pl
t/test_pl/tempfile.t

index 6808127..fc64f4a 100644 (file)
--- a/t/test.pl
+++ b/t/test.pl
@@ -869,6 +869,28 @@ sub tempfile {
     die "Can't find temporary file name starting \"tmp$$\"";
 }
 
+# register_tempfile - Adds a list of files to be removed at the end of the current test file
+# Arguments :
+#   a list of files to be removed later
+
+# returns a count of how many file names were actually added
+
+# Reuses %tmpfiles so that tempfile() will also skip any files added here
+# even if the file doesn't exist yet.
+
+sub register_tempfile {
+    my $count = 0;
+    for( @_ ){
+       if( $tmpfiles{$_} ){
+           _print_stderr "# Temporary file '$_' already added\n";
+       }else{
+           $tmpfiles{$_} = 1;
+           $count = $count + 1;
+       }
+    }
+    return $count;
+}
+
 # This is the temporary file for _fresh_perl
 my $tmpfile = tempfile();
 
index 5c0e76e..881ecbf 100644 (file)
@@ -55,7 +55,10 @@ is( tempfile(), "${prefix}C");
     }
 }
 
-skip_files(20,'Y','Z');
+ok( register_tempfile("${prefix}F"), 'registered the next file with register_tempfile' );
+is( tempfile(), "${prefix}G", 'tempfile() properly skips files added with register_tempfile()' );
+
+skip_files(18,'Y','Z');
 
 is( tempfile(), "${prefix}Z", 'Last single letter filename');
 is( tempfile(), "${prefix}AA", 'First double letter filename');