Test-More / Test2-Harness

Alternative to Test::Harness
Other
23 stars 26 forks source link

$. is set when running yath #261

Closed Tux closed 1 year ago

Tux commented 1 year ago

When running a .t file with yath, $. is set (to 0) where it should still be undef

$ cat test.t
#!/usr/bin/perl

use 5.014002;
use warnings;

use Test::More;
use Data::Dumper;

ok (1);
diag Dumper ($.);

done_testing;
$ prove -w test.t
test.t .. 1/? # $VAR1 = undef;
test.t .. ok
All tests successful.
Files=1, Tests=1,  1 wallclock secs ( 0.02 usr  0.00 sys +  0.06 cusr  0.00 csys =  0.08 CPU)
Result: PASS
$ yath test.t

** Defaulting to the 'test' command **

(  DIAG  )  job  1    $VAR1 = 0;
( PASSED )  job  1    test.t

                                Yath Result Summary
-----------------------------------------------------------------------------------
     File Count: 1
Assertion Count: 1
      Wall Time: 0.48 seconds
       CPU Time: 0.72 seconds (usr: 0.23s | sys: 0.02s | cusr: 0.42s | csys: 0.05s)
      CPU Usage: 148%
    -->  Result: PASSED  <--
toddr commented 1 year ago

Good catch!

More simply, this passes on prove and fails on yath

$>cat t/test.t
#!/usr/bin/perl

use Test::More;

is ($., undef, '$. starts out undefined');

done_testing;
exodist commented 1 year ago

I can confirm this is happening. I cannot figure out how it is happening. When yath preloads+forks it uses goto::file to switch execution over to the test file. https://metacpan.org/dist/goto-file/source/lib/goto/file.pm#L59 If I add a debug statement to that line to print $., it is set to undef the first call, and then it is 0 by the next call and all following calls. If I set $. to a Scope::Guard it says it is destroyed by scripts/yath line 260, which is the end of the BEGIN block, a BEGIN block that has local $. at the very top. I am reaching the end of my ability to debug this, and no workaround of manually setting $. = undef at ANY pount fixes this.

exodist commented 1 year ago

ah, well it looks like setting $. = undef results in $. being 0. So that explains why I cannot reset it. But I am also at a loss as to what is setting it.

exodist commented 1 year ago

I created a .t file:

BEGIN { $. = undef }
$. = undef;
my $val = $.; 
print STDERR "|$.|$val|\n";

When run via yath I get this output:

( LAUNCH )  job  1    xxx.t
( STDERR )  job  1    |0|0|
( FAILED )  job  1    xxx.t
(  TIME  )  job  1    Total: 0.02369s
< REASON >  job  1    No plan was declared, and no assertions were made.

So it seems once $. is set to 0, even the test file cannot change it back. And apparently wrapping EVERYTHING that is done with a local $. can also not protect it.

exodist commented 1 year ago

ok, found it by chance, and part of the problem is that goto::file::filter was being monkeypatched. I will have a fix shortly.