I'm collecting a bunch of subroutines that are common to a bunch of my scripts into a module. (I should've done this way earlier, but started out with inherited scripts.) I'm modelling my work on the very helpful example here, using Test::More and Module::Build
All of the subroutines that read or write from files all include a line open() or die "errmsg"
. I'm in the process of writing a test for the module and ran across this problem. One of the subroutines checks whether a path points to something or not, dying on fail. In the inherited scripts, the subroutine looks like this:
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'm testing this with the following line:
is(HomeBrew::IO::checkExist('f', $0), '', "can checkExist find file $0 ?");
which works fine unless I pick a path which doesn't doesn't exist, in which case the test script dies, but the test succeeds, producing the following output:
# Looks like your test exited with 2 just after 5.
Dubious, test returned 2 (wstat 512, 0x200)
All 5 subtests passed
I would prefer if this were a failing test (rather than a dubious pass), but since this is legacy code, I also want this subroutine to halt execution on fail. What to do? Is it stupid to write a test on a function this simple?
I've already written a checkExist2 function that I'll be using in the future that returns undef on success else a non-zero error (so I can write die if checkExist2()
elsewhere). Other suggestions that do not maintain the functionality of checkExist are welcome.