I am collecting a bunch of routines that are common to a bunch of my scripts in a module. (I should have done this before, but started with legacy scripts.) I model my work on a very useful example here using Test :: More and Module :: Build
All routines that read or write from files include the line open() or die "errmsg" . I am in the process of writing a test for a module and ran into this problem. One of the routines checks whether the path points to something or not, dies on error. In legacy scripts, the routine is as follows:
sub checkExist { my ($type, $path) = @_; if ($type eq 'd') { if (! -d $path) { warn("dir not found: $path\n"); die $pathNotFound; } } elsif ($type eq 'f') { if (! -f $path) { warn("file not found: $path\n"); die $pathNotFound; } elsif (! -s $path) { warn("empty file: $path\n"); die $emptyFile; } } }
Now, I am testing this with the following line:
is(HomeBrew::IO::checkExist('f', $0), '', "can checkExist find file $0 ?");
which works fine if I do not choose a path that does not exist, in which case the test script dies, but the test succeeds , producing the following output:
I would prefer this to be an unsuccessful test (rather than a dubious pass), but since this is obsolete code, I also want this routine to stop execution on error. What to do? Is it stupid to write a function test, is it simple?
I already wrote a checkExist2 function, which I will use in the future, which returns undef if the non-zero error completes successfully (so I can write die if checkExist2() elsewhere). Other suggestions that do not support checkExist functionality are welcome.
module perl testing
flies
source share