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; }

In reply to Unit test - check file created using test::directory by APGRMF

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.