Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl6 generic code to test if modules load

This is a generic code code in /t to test if .pm6 modules in /lib load.

use lib $*PROGRAM.sibling('../lib');
use Test;

my @dir = dir($*PROGRAM.sibling('../lib'), test => { $_ ~~ /.*pm6/  } );
plan @dir.elems;

sub module( IO $dir ) {
  $dir.basename.Str ~~ /(\w+)\.pm6/;
  return $0.Str;
}

for  @dir.map(&module) -> $module {
  use-ok $module, "This module loads: $module";
}

Before going any further (recursively looking at lib sub-folders ), I wonder in this is the right approach.

Thanks!

like image 990
Mimosinnet Avatar asked May 01 '19 17:05

Mimosinnet


2 Answers

If you are testing a well-formed distribution then you should be using:

use lib $*PROGRAM.parent(2);

By pointing use lib at the directory containing your META6.json instead of the lib directory you help ensure that the provides entry of the META6.json file is up to date (since files not listed in the META6.json but that do exist inside lib won't be seen).

(I'd even take it one step further and say don't use use lib '...' at all, and instead run your tests using perl6 -I .... For instance -- what if you want to run these tests (for whatever reason) on the installed copy of some distribution?)

With that said you could skip the directory recursion by using the META6 data. One method would be to read the META6.json directly, but a better way of doing so would be to get the module names from the distribution itself:

# file: zef/t/my-test.t
# cwd: zef/

use lib $*PROGRAM.parent(2); # or better: perl6 -I. t/my-test.t
use Test;

my $known-module = CompUnit::DependencySpecification.new(short-name => "Zef");
my $comp-unit    = $*REPO.resolve($known-module);
my @module-names = $comp-unit.distribution.meta<provides>.keys;

use-ok($_) for @module-names;
like image 79
ugexe Avatar answered Oct 20 '22 08:10

ugexe


Using @ugexe feedback and META6 distribution, the following code in t/ tests that modules defined in META6.json load.

use META6;
use Test;

my $m = META6.new( file => $*PROGRAM.sibling('../META6.json') );
my @modules = $m<provides>.keys;
plan @modules.elems; 

for $m<provides>.keys -> $module {
  use-ok $module, "This module loads: $module";
}

This test has been pulled into the META6 distribution.

like image 2
Mimosinnet Avatar answered Oct 20 '22 08:10

Mimosinnet