Tyil
Tyil

Reputation: 1807

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

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?

Upvotes: 8

Views: 173

Answers (1)

Elizabeth Mattijsen
Elizabeth Mattijsen

Reputation: 26977

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 :-)

Upvotes: 5

Related Questions