The following little script should illustrate the point in various ways depending on whether the file present is indeed present and depending on whether the croak call is live or commented-out.
#!/usr/local/bin/perl use strict; use warnings; use File::Copy; use Carp; use Test::More (qw|no_plan|); my $test = "/Users/jimk/present"; my $hide = $test . '.hidden'; my $status; if (-f $test) { $status = 1; move($test, $hide); ok(! -f $test, "original file no longer present"); ok( -f $hide, "hidden file present"); } else { open FH, ">$test"; print FH "1\n"; close FH; ok(-f $test, "file created where it didn't previously exist"); ok(1, "test not relevant"); } my $trick = 0; if (! $trick) { croak "Trick is false"; } END { if ($status) { move($hide, $test); ok(! -f $hide, "hidden file no longer present"); ok( -f $test, "original file once again present"); } else { unlink $test; ok(! -f $test, "specially created file has been removed"); ok(1, "test not relevant"); } }
Update: While the solution above guarantees that the testing process is (more) non-destructive, it does have one drawback, AFACIT. When the croak occurs earlier in the file, (1) the total number of tests run is reduced by the quantity lying between the croak and the END block and (2) the numbers associated with the tests inside the END block are no longer at the end of the range predicted by the plan.
Revised example:
#!/usr/local/bin/perl use strict; use warnings; use File::Copy; use Carp; use Test::More tests => 7; # (qw|no_plan|); my $test = "/Users/jimk/present"; my $hide = $test . '.hidden'; my $status; my $trick = 0; if (-f $test) { $status = 1; move($test, $hide); ok(! -f $test, "original file no longer present"); ok( -f $hide, "hidden file present"); } else { open FH, ">$test"; print FH "1\n"; close FH; ok(-f $test, "file created where it didn't previously exist"); ok(1, "test not relevant"); } croak "Trick is false" unless $trick; ok('alpha', "alpha reached"); ok('beta', "beta reached"); ok('gamma', "gamma reached"); # croak "Trick is false" unless $trick; END { if ($status) { move($hide, $test); ok(! -f $hide, "hidden file no longer present"); ok( -f $test, "original file once again present"); } else { unlink $test; ok(! -f $test, "specially created file has been removed"); ok(1, "test not relevant"); } }
... produces (assuming that present was indeed present at the outset):
Tests 3 and 4 above are not the third and fourth as you would intuitively predict from eyeballing the file. You become more dependent on the croak message in locating the error within the file.1..7 ok 1 - original file no longer present ok 2 - hidden file present Trick is false at end.pl line 28 ok 3 - hidden file no longer present ok 4 - original file once again present # Looks like you planned 7 tests but only ran 4. # Looks like your test died just after 4.
Thanks!
Jim Keenan
In reply to Re^2: BEGIN and END blocks, use strict and scoping
by jkeenan1
in thread BEGIN and END blocks, use strict and scoping
by jkeenan1
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |