Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test::More doesn't know if test dies - so how do I test?

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.

like image 914
flies Avatar asked Aug 26 '10 21:08

flies


People also ask

What does a faint T line on Covid test mean?

Any faint visible reddish/pink test (T) line with the control line (C) should be read as positive. A positive test result means that the virus that causes COVID-19 was detected in your sample and it is very likely you have COVID-19 and are contagious.

How do I read Intelliswab?

The test is POSITIVE if: there is a reddish-purple line next to the "T" and NO reddish-purple line next to the "C" there is a reddish-purple line next to the "T" and a reddish-purple line next to the "C", even if the "C" line is faint there is a faint reddish-purple line next to the "T" and a reddish-purple line next ...

What does the C mean on a Covid test?

One coloured line should be in the control line region (C), and another coloured line should be in the test line region (T). Two lines, one next to C and one next to T, even faint lines, show the test is positive. If you test positive, you and your. household should self-isolate following.

How long are you contagious with Omicron?

We know that people tend to be most infectious early in the course of their infection. With Omicron, most transmission occurs during the one to two days before onset of symptoms, and in the two to three days afterwards.


2 Answers

The proper way to test if code lives, or dies with a proper error, is with Test::Exception. You can wrap this test around other test cases, since it just takes a coderef:

use Test::More;
use Test::Exception;

lives_ok {
    is(HomeBrew::IO::checkExist('f', $0), '',
    "can checkExist find file $0 ?")
} '...and code does not die';
like image 121
Ether Avatar answered Nov 10 '22 06:11

Ether


Why not have a helper subroutine in your test module which wraps an eval{} around HomeBrew::IO::checkExist call and checks for a fail via $@?

sub runcheckExist {
   my $res = eval { HomeBrew::IO::checkExist('f', $0) };
   # May want more logic here 
   # for checking $@ for specific error text pattern
   # or $res
   return $@ ? 1 : 0;
}
my $expect_to_die = 1;
is(runcheckExist(), $expect_to_die, "can checkExist find file $0 ?");
like image 20
DVK Avatar answered Nov 10 '22 04:11

DVK