Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: optionally tell "ok" module to BAIL_OUT if module cannot be loaded #929

Open
XSven opened this issue Apr 29, 2024 · 4 comments

Comments

@XSven
Copy link

XSven commented Apr 29, 2024

I am looking for a way to replace this idiom

BEGIN {
  use_ok( 'Some::Module' ) or BAIL_OUT( "Cannot load module 'Some::Module'!" );
}

with something that uses the ok module?!

@exodist
Copy link
Member

exodist commented May 3, 2024

I think this feature request should be a new module. I do not want to complicate ok.pm with this functionality.

I would suggest subclassing https://metacpan.org/pod/Test2::Require::Module in Test2::Require::ModuleOrBail that implements the additional behavior.

If you write it as a PR against Test2-Suite I will likely merge it. But it is also perfectly reasonable as a stand alone cpan dist.

@briandfoy
Copy link

I do this with a subroutine in a module that I share among my tests. Part of that is I want better control of the error reporting, and I already have a utility module for tests to access application specific things they need in a common way. Since I already have that utility module, it's easy to add functions.

Typically I have some other checks besides the use because I've made a lot of dumb errors (Some:Class) that I also want to check. My general strategy in any test is to catch myself being dumb and report the most specific sort of dumbness I can based on my general past idiocy. If I can catch dumb errors before the use, I can make better error messages. It looks something like this:

sub load (@classes) {
    subtest 'sanity' => sub {
        foreach my $class ( @classes ) {
            ok valid_package_name($class), "$class is a valid package name";
            ok file_exists($class), ....;
            use_ok $class;
        }
}

These tests may seem overly targeted, but while developing, sometimes a module name changes because I'm an idiot at naming things and often the team comes up with a better name. Along with that, the module name changes but I forget which tests use that name. Sure, grep could find those, but did I mention how dumb I am (especially in the middle of the night after several hours working on a problem)? :)

Note a drawback I recently had. Things like $TODO are based on the current package, so a $TODO in a subtest in a separate package won't respect the TODO: label in a calling package that is different. Again, completely understandable once I stop being an idiot.

@exodist
Copy link
Member

exodist commented Nov 20, 2024

@briandfoy $TODO is essentially deprecated in favor of this from Test2: https://metacpan.org/pod/Test2::Todo it plays nicely with Test::Builder based tools, so you can start using that. Or even the todo() function from https://metacpan.org/pod/Test2::Tools::Basic#$todo-=-todo($reason) Once againm, plays nicely with older tools, and does not suffer from the package variable problem.

@briandfoy
Copy link

Indeed Test2 does some things better. But, Test2 was only just recently added to core so the old ways will persist for several more years.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants