Skip to content

Commit

Permalink
Extend --test option to pass only a test case
Browse files Browse the repository at this point in the history
Following commands are identical:
  zonemaster-cli --test delegation/delegation01
  zonemaster-cli --test delegation01

Passing a trailing '/' assumes a module should be run.

Credits to @marc-vanderwal for most of this code.
  • Loading branch information
Alexandre Pion committed Nov 22, 2023
1 parent be1ae5e commit 46d0d97
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
21 changes: 16 additions & 5 deletions lib/Zonemaster/CLI.pm
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ has 'test' => (
isa => 'ArrayRef',
required => 0,
documentation => __(
'Specify test to run case-insensitively. Should be either the name of a module, or the name of a module and the name of a method in that module separated by a "/" character (Example: "Basic/basic01"). This switch can be repeated.'
'Specify one or several test cases to be run. Should be the case-insensitive name of a test module and/or a test case (examples: "Delegation", "delegation01", "Delegation/delegation01"). This switch can be repeated.'
)
);

Expand Down Expand Up @@ -621,13 +621,24 @@ sub run {
# Actually run tests!
eval {
if ( $self->test and @{ $self->test } > 0 ) {
my $zone = Zonemaster::Engine->zone( $domain );
foreach my $t ( @{ $self->test } ) {
my ( $module, $method ) = split( '/', lc($t), 2 );
if ( $method ) {
Zonemaster::Engine->test_method( $module, $method, Zonemaster::Engine->zone( $domain ) );
# The case does not matter
$t = lc( $t );
# Fully qualified module and test case (e.g. Example/example12)
if (my ($module, $method) = $t =~ m#^ ( [a-z]+ ) / ( [a-z]+[0-9]{2} ) $#ix) {
Zonemaster::Engine->test_method( $module, $method, $zone );
}
# Just a test case (e.g. example12). Note the different capturing order.
elsif (($method, $module) = $t =~ m#^ ( ( [a-z]+ ) [0-9]{2} ) $#ix) {
Zonemaster::Engine->test_method( $module, $method, $zone );
}
# Just a module name (e.g. Example) or something invalid.
# TODO: in case of invalid input, print a proper error message
# suggesting to use --list-tests for valid choices.
else {
Zonemaster::Engine->test_module( $module, $domain );
$t =~ s/\/$//;
Zonemaster::Engine->test_module( $t, $zone );
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions script/zonemaster-cli
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,14 @@ Default: on

Print all test cases listed in the test modules, then exit.

=item --test=MODULE, --test=MODULE/TESTCASE
=item --test=MODULE, --test=MODULE/TESTCASE, --test=TESTCASE

Limit the testing suite to run only the specified tests.
This can be the name of a testing module, in which case all test cases from
that module will be run, or the name of a module followed by a slash and the
name of a test case (test case identifier) in that module.
name of a test case (test case identifier) in that module, or the name of the
test case.
Can be specified multiple times.
This option is case-insensitive.

=item --stop_level=LEVEL, --stop-level=LEVEL
Expand Down

0 comments on commit 46d0d97

Please sign in to comment.