Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Add optional modules to TEST_REQUIRES?

by dkechag (Acolyte)
on Nov 21, 2019 at 14:26 UTC ( [id://11108983]=perlquestion: print w/replies, xml ) Need Help??

dkechag has asked for the wisdom of the Perl Monks concerning the following question:

Hello. I am preparing to upload a new version of my Test2::Aggregate module to CPAN. I don't like dependencies myself, so when I want to add something like an optional function that not many would use and which would require a dependency, I load the module in runtime making a note in the documentation. So, PREREQ_PM does not contain these extra modules, however I do have tests around them that just skip if you don't have the module. The thing is, cpantesters and cpancover won't actually install the extra modules hence won't actually test these features (unless they happen to have them - but from cpancover at least I see they might not). But, if I add the modules to TEST_REQUIRES, then any user installing the module will have the extra modules installed anyway, which is what I tried to avoid in the first place.

Comments? Should I just add them to TEST_REQUIRES and not worry about it? I bet I am being a bit too OCD, but I don't like installing things I don't use.

Thanks!

Replies are listed 'Best First'.
Re: Add optional modules to TEST_REQUIRES?
by choroba (Cardinal) on Nov 21, 2019 at 14:44 UTC
    You are probably more interested in "suggested" or "recommended" dependencies. See Specifying the type of your CPAN dependencies. Unfortunately, not all tools support all types of dependencies.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: Add optional modules to TEST_REQUIRES?
by Tux (Canon) on Nov 21, 2019 at 15:32 UTC
Re: Add optional modules to TEST_REQUIRES? (updated)
by haukex (Archbishop) on Nov 21, 2019 at 15:03 UTC

    Personally, I am no longer a fan of optional dependencies*, because they tend to complicate things in exactly the way you describe. I would suggest thinking about alternatives, such as just deciding to either declare the module as a dependency, or removing it, perhaps a replacement is possible with core modules - for example, if I'm only using Path::Class in one or two places, I might as well use File::Spec instead. Or, if this optional functionality is sufficiently large, consider splitting it out into a separate release in the same namespace.

    Having said that, of course there are exceptions, for example if there are different backends that are OS-dependent. I have a module on CPAN myself with optional dependencies. My suggestion would be: Don't include the optional dependencies in TEST_REQUIRES, use META_MERGE => { prereqs => { runtime => { recommends => { ... } } } } (example). Then skip the corresponding tests if the modules aren't installed, but when you do your author tests before release, verify that the modules are installed and the tests pass. (There are of course other possible approaches, this is just one suggestion.)

    * Update: Just to clarify: I mean when writing new modules, I wasn't trying to hate on modules that already have them :-) And even when writing new modules, as I said, there are exceptions. But optional dependencies add a bunch complexity, especially in testing: it really would be best to run the entire test suite once when the optional dependency isn't installed, and once when it is, and AFAIK there's no way to do that on CPAN Testers, which means you have to rely on your own local testing, on multiple platforms. And if there's more than one dependency, the number of times one should run the test suite grows quickly.

Re: Add optional modules to TEST_REQUIRES?
by dkechag (Acolyte) on Nov 21, 2019 at 19:13 UTC
    Thanks all, I missed the META_MERGE options which seem to be appropriate and Spreadsheet::Read even has a workaround for cpancover (using $ENV{EXTENDED_TESTING}).
Re: Add optional modules to TEST_REQUIRES?
by jcb (Parson) on Nov 21, 2019 at 23:49 UTC

    The build processes for Perl modules are driven by Perl scripts and CPAN test smokers are supposed to set the environment variable AUTOMATED_TESTING. The simple answer is to add the extra modules to TEST_REQUIRES iff $ENV{AUTOMATED_TESTING} is set.

      Hi

      cpan testers test cpan modules :)

      perl smokers test builds of perl ;)

      AUTOMATED_TESTING is not the time to decide you want to smoke an extra 15k worth of test files/dependencies...

      If OP wants cpantesters to see what happens with an extra 14k worth of dependencies,

      create a Task (replacement for Bundle) module distribution which requires all 14k depends

      But even cpantesters place time limits on testing distributions, so be prepared to adjust

Re: Add optional modules to TEST_REQUIRES?
by dkechag (Acolyte) on Nov 22, 2019 at 09:35 UTC
    Thanks for the AUTOMATED_TESTING tip. No, I am not talking about 14k dependencies, I said I may be a bit OCD, it is just Sub::Override and a Test2 plugin. But it was the first module I released on CPAN and should figure out these things for the future ;)
Re: Add optional modules to TEST_REQUIRES?
by dkechag (Acolyte) on Nov 25, 2019 at 08:29 UTC
    So, I confirm the EXTENDED_TESTING works with cpancover, but you do need AUTOMATED_TESTING for cpantesters, as they don't install "recommends" options, so I went with something like:
    my $extra_test = ( $ENV{EXTENDED_TESTING} || $ENV{AUTOMATED_TESTING} ) ? 'requires' : 'recommends'; $WriteMakefileArgs{META_MERGE}->{prereqs}->{test} = { $extra_test => { 'Test2::Plugin::BailOnFail' => '0', 'Sub::Override' => '0' } };
      Test2::Plugin::BailOnFail, Sub::Override

      I know this goes against the grain of this thread, and I'm glad you got it working, but: Test2::Plugin::BailOnFail is basically just five lines of code, and Sub::Override is a more sophisticated version of local *subname = sub { print "overridden" };. As I said, personally, in a case like this I wouldn't depend on those modules at all.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11108983]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-03-29 00:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found