Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Unit test - check file created using test::directory

by APGRMF (Novice)
on Jul 03, 2014 at 16:49 UTC ( [id://1092197]=perlquestion: print w/replies, xml ) Need Help??

APGRMF has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I'm new to Perl and looking at it in terms of test-driven development and unit testing. I would like to write a test that that will confirm that a function has created a file on the file system.

I think I would like my unit test to:

- check the file doesn't exist

- call the function (passing in a path and file name as arguments)

- check the file now exists

- clean up after itself and delete any files or directories it created during the test

My attempts at this are failing. I'm using Test::Directory. Whilst I can confirm that the specified file gets created, I cannot cleanup up after the test. I've almost resorted to using rmtree but this too fails as permision is denied.

I'm using ActivePerl 5.16.3

I'm clearly missing something or approaching it the wrong way. Any thoughts would be greatly appreciated.

My test is of the form:

use strict; use warnings; use Test::More; use Test::Exception; use Test::Builder; use Test::Directory; my $builder = Test::More->builder->output('test-result.txt'); note "\nTest function: open_log_file"; subtest 'open_log_file' => sub { my $log_file = 'xyz.log'; my $log_dir = 'unit-test-temp'; my $dir = Test::Directory->new($log_dir); $dir->hasnt($log_file , "log file should not e +xist"); ok(open_log_file($log_dir . '/' . $log_file), "open_log_file shoul +d create a the log file"); $dir->has($log_file , "log file should now e +xist"); #cleanup after the test - this fails so far $dir->remove_files($log_file); $dir->remove_directories($log_dir); #another attempt to cleanup after the test - this also fails $dir->clean($log_dir); }; # doesn't work either #if (-e $log_dir) { # rmtree($log_dir); #}
###################################################################### +################################# use FileHandle; ... ... sub open_log_file { my $log_file = shift; # open the log $log_fh = FileHandle->new("> $log_file") || die "Failed to open log +file $log_file: $!"; return 1; }

Replies are listed 'Best First'.
Re: Unit test - check file created using test::directory
by jeffa (Bishop) on Jul 03, 2014 at 17:52 UTC

    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)
    

      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.

      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)
        
Re: Unit test - check file created using test::directory
by redbull2012 (Sexton) on Jul 04, 2014 at 01:27 UTC

    Beside the undeclared $log_fh, the code runs fine in my window, something else is causing this problem. Perhaps you should debug to see which specific line causing the problem. I usually use EPIC (Eclipse Perl integration), you can use other debug methods if Eclipse is not already in your system.

      Excellent, many thanks. I'll look along these lines

Re: Unit test - check file created using test::directory
by Bloodnok (Vicar) on Jul 03, 2014 at 17:23 UTC
    If I may make so bold, it would help (me at least) if you provided the error(s) that you're seeing.

    A user level that continues to overstate my experience :-))

      Sorry. Should have been clearer. The following lines do not actually throw an error as far as I can see. They just don't do what I was hoping/expecting they would do - ie clean up the files and directories after the test.

      #cleanup after the test - this fails so far $dir->remove_files($log_file); $dir->remove_directories($log_dir); $dir->clean($log_dir);

      The commented rmtree command (when commented in and in scope) returns "cannot unlink file for unit-test-temp\xyz.log".

        Aha, looking at Test::Direcory, I would infer that the remove_files() and remove_directories() methods are used to remove files &/or directories from within the directory created by the constructor i.e. personally, I would expect the call $dir->remove_directories($log_dir); to fail because $log_dir is not a subdirectory of itself, the clean() method takes no args and is/should be used to rmdir the directory created by the constructor i.e. $log_dir.

        HTH ,

        A user level that continues to overstate my experience :-))

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1092197]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-26 05:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found