BEGIN { use_ok } keeps biting

ferreira on 2007-12-10T15:59:17

The fearless Michael Schwern kept his word and brought back the Test-Simple fix for BEGIN { use_ok } bug. (If you don't know what I am saying, read this).

Some CPAN dists remain unfixed and those who upgraded the basic test modules will see an error while testing. Outstanding examples are:

Notice that the fix called for in this distributions is a real test bug. A contrived example is of the form:

use Test::More;

if ( $exp_which_tells_test_should_be_skipped ) {
    plan tests => 1; # this will happen at runtime
} else {
    plan 'skip_all' => 1; # that too
}

BEGIN { use_ok 'File::Temp' } # at compile-time

ok(1);

When Test::Builder had the bug, it swallowed the exception during BEGIN { use_ok } which announced a test before planning. Also, the test count should not include the use_ok to get pass this point. Yes, for some time, we authors have introduced not one, but two bugs in this type of code to make it work. Now it is time to write it down correctly.

use Test::More;

BEGIN {
    if ( $exp_which_tells_test_should_be_skipped )     {
        plan tests => 2; 
    } else {
        plan 'skip_all' => 1;
    }
}

BEGIN { use_ok 'File::Temp' } # at compile-time

ok(1);

Bonus point: The fixed version works ok with the old (buggy) and the new Test-Simple.