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 | |
by APGRMF (Novice) on Jul 04, 2014 at 15:57 UTC | |
by GotToBTru (Prior) on Jul 03, 2014 at 18:27 UTC | |
by jeffa (Bishop) on Jul 03, 2014 at 19:39 UTC | |
Re: Unit test - check file created using test::directory
by redbull2012 (Sexton) on Jul 04, 2014 at 01:27 UTC | |
by APGRMF (Novice) on Jul 04, 2014 at 07:33 UTC | |
Re: Unit test - check file created using test::directory
by Bloodnok (Vicar) on Jul 03, 2014 at 17:23 UTC | |
by APGRMF (Novice) on Jul 04, 2014 at 07:28 UTC | |
by Bloodnok (Vicar) on Jul 04, 2014 at 15:10 UTC |