Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing with `use-ok` on modules with a `MAIN` definition

Tags:

testing

raku

I am writing a new Perl 6 project for work, and would like to be able to test whether all parts can be used correctly. For this, I'm using the use-ok subroutine from the Test module. I'm trying to easily test all module files using the following code:

"META6.json".IO.slurp.&from-json<provides>
.grep(*.value.starts-with("lib")).Hash.keys
.map({ use-ok $_ })

My issue here is that there are a few files that contain a definition for a MAIN subroutine. From the output I see when running prove -e 'perl6 -Ilib' t, it looks like one of the files is having their MAIN executed, and then the testing stops.

I want to test whether these files can be used correctly, without actually running the MAIN subs that are defined within them. How would I do this?

like image 886
Tyil Avatar asked Oct 09 '18 10:10

Tyil


1 Answers

The MAIN of a file is only executed if it is in the top level of the mainline of a program. So:

sub MAIN() is export { }     # this will be executed when the mainline executes

However, if you move the MAIN sub out of the toplevel, it will not get executed. But you can still export it.

{
    sub MAIN() is export { } # will *not* execute
}

Sorry for it taking so long to answer: it took a while for me to figure what the question was :-)

like image 119
Elizabeth Mattijsen Avatar answered Dec 04 '22 02:12

Elizabeth Mattijsen