Ovid has asked for the wisdom of the Perl Monks concerning the following question:

I've found a problem with Test::Harness. It appears to affect both the old and new harnesses and I've no idea how to work around it.

#!/usr/bin/perl use strict; use warnings; use Test::More 'no_plan'; ok 1, 'this works'; $SIG{__DIE__} = sub { die @_ }; ok no_such_sub(), '... no such sub';

Both the old and new test harnesses report this as a passing program with one test. Apparently, the $SIG{__DIE__} causes the program to exit with a status of zero. Is there some way to work around this?

Test-Harness-2.65_02 $ perl -Ilib bin/prove -v test.pl test....ok 1 - this works Undefined subroutine &main::no_such_sub called at test.pl line 9. 1..1 ok All tests successful. Files=1, Tests=1, 1 wallclock secs ( 0.22 cusr + 0.02 csys = 0.24 +CPU)

Basically, we'd like the new test harness to identify when a program does this and respond appropriately. Of course, we get an error if we actually have a plan, but that's not enough:

#!/usr/bin/perl use strict; use warnings; use Test::More tests => 1; ok 1, 'this works'; $SIG{__DIE__} = sub { die @_ }; cleanup();

In the above example we have all tests running and passing, but the cleanup() subroutine isn't defined. Short of trying to parse all error messages which might be generated (um, no, we're not going to try that), this seems like a fairly significant limitation and enough CPAN modules have $SIG{__DIE__} handlers that it's not just a matter of saying "don't do that".

Cheers,
Ovid

New address of my CGI Course.

Replies are listed 'Best First'.
Re: $SIG{__DIE__} overrides exit status?
by ikegami (Patriarch) on Oct 17, 2007 at 13:33 UTC

    Apparently, the $SIG{__DIE__} causes the program to exit with a status of zero.

    No, it doesn't

    >type script.pl #!/usr/bin/perl $SIG{__DIE__} = sub { die @_ }; no_such_sub(); >echo %ERRORLEVEL% 0 >perl script.pl Undefined subroutine &main::no_such_sub called at script.pl line 4. >echo %ERRORLEVEL% 255

    It's whatever prints '1..1' that causes the program to exist with a status of zero.

    >type script.pl #!/usr/bin/perl use strict; use warnings; use Test::More 'no_plan'; ok 1, 'this works'; $SIG{__DIE__} = sub { die @_ }; ok no_such_sub(), '... no such sub'; >echo %ERRORLEVEL% 0 >perl script.pl ok 1 - this works Undefined subroutine &main::no_such_sub called at script.pl line 9. 1..1 >echo %ERRORLEVEL% 0

    You'll get very similar results if you take out the $SIG{__DIE__}. That's why you should use a plan.

      We can't rely on a plan because we can't force authors what to do. Also, if the failure occurs after all tests have run, the plan is still correct. However, you've put me on th right track. I think there's a bug in Test::Builder and not with $SIG{__DIE__}.

      $ perl -le '$SIG{__DIE__} = sub { die }; foo()'; echo $? Died at -e line 1. 255

      Cheers,
      Ovid

      New address of my CGI Course.

Re: $SIG{__DIE__} overrides exit status?
by jdporter (Paladin) on Oct 17, 2007 at 13:16 UTC
    $SIG{__DIE__} = sub { $! = $real_exit_code; die @_ };

      Test::Harness doesn't have control over the signal handlers that the tested code might implement. When you install Class::Does::Everything via cpan or cpanplus, you can't just expect they've written it correctly.

      What we could do is have the test programs invoked like this:

        perl -MTest::Harness::SIGDIE t/some_test.t

      Then we'd just have to figure out the Test::Harness::SIGDIE magic. Actually, now that I stop to think about it, that might not work either. It still requires the signal handler to be written correctly.

      Cheers,
      Ovid

      New address of my CGI Course.

        Test::Harness doesn't have control over the signal handlers that the tested code might implement.

        Ah, sorry; I misunderstood the situation.