in reply to Unit test - check file created using test::directory

This is how i would do that:

use strict; use warnings; use File::Temp qw( tempdir ); use Test::More tests => 2; my $fname = 'foo.txt'; my $dir = tempdir( CLEANUP => 1 ); is -f "$dir/$fname", undef, "file does not exit"; create_file( $dir, $fname ); is -f "$dir/$fname", 1, "file now exists"; sub create_file { my ($path, $fname) = @_; open FH, '>', "$path/$fname" or die "$!\n"; close FH; }

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re^2: Unit test - check file created using test::directory
by APGRMF (Novice) on Jul 04, 2014 at 15:57 UTC

    Thanks for this.

    I think I have a better understanding now.

    Your code worked straight away and the test was successful. The significant difference as far as I can see was that the original code in my sub just opened a file/filehandle but never closed it - this was being done by another subroutine. I think this was why the cleanup process failed. If I close the file/filehandle (or call the subroutine that does this from the test script) the test passes and the directories created by the test are all cleaned up.

    Many thanks for all your efforts.

Re^2: Unit test - check file created using test::directory
by GotToBTru (Prior) on Jul 03, 2014 at 18:27 UTC

    Need to add unlink $path/$fname; at the end.

    1 Peter 4:10

      File::Temp does that for you. Run the code and see for yourself.

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)