From 46d0d97b59c86a8f8a930055fd81486e74c5b5b5 Mon Sep 17 00:00:00 2001 From: Alexandre Pion Date: Thu, 25 May 2023 09:31:52 +0200 Subject: [PATCH] Extend --test option to pass only a test case 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. --- lib/Zonemaster/CLI.pm | 21 ++++++++++++++++----- script/zonemaster-cli | 6 ++++-- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/lib/Zonemaster/CLI.pm b/lib/Zonemaster/CLI.pm index 40887375..22c84fd6 100755 --- a/lib/Zonemaster/CLI.pm +++ b/lib/Zonemaster/CLI.pm @@ -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.' ) ); @@ -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 ); } } } diff --git a/script/zonemaster-cli b/script/zonemaster-cli index 1ccffed9..e8d22799 100755 --- a/script/zonemaster-cli +++ b/script/zonemaster-cli @@ -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