Skip to content

Commit 08c2a77

Browse files
committed
Update after review - refactoring in --test
Move input checks earlier in the code Add input check for multiple slash characters Refactoring
1 parent a66d559 commit 08c2a77

File tree

1 file changed

+41
-19
lines changed

1 file changed

+41
-19
lines changed

lib/Zonemaster/CLI.pm

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,39 @@ sub run {
407407
Zonemaster::Engine::Profile->effective->merge( $profile );
408408
}
409409

410+
my @testing_suite;
411+
if ( $self->test and @{ $self->test } > 0 ) {
412+
foreach my $t ( @{ $self->test } ) {
413+
# There should be at most one slash character
414+
if ( $t =~ tr/\/// > 1 ) {
415+
die __( "Error: --test must have at most one slash ('/') character: $t");
416+
}
417+
418+
# The case does not matter
419+
$t = lc( $t );
420+
421+
my ( $module, $method );
422+
# Fully qualified module and test case (e.g. Example/example12), or just a test case (e.g. example12). Note the different capturing order.
423+
if ( ( ($module, $method) = $t =~ m#^ ( [a-z]+ ) / ( [a-z]+[0-9]{2} ) $#ix )
424+
or
425+
( ($method, $module) = $t =~ m#^ ( ( [a-z]+ ) [0-9]{2} ) $#ix ) )
426+
{
427+
push @testing_suite, "$module/$method";
428+
if ( not grep( /^$method$/, @{ Zonemaster::Engine::Profile->effective->get( 'test_cases' ) } ) ) {
429+
# Actual forcing will be done later in the code, i.e. just before calling Zonemaster::Engine->test_method()
430+
say $fh_diag __x( "Notice: Engine does not have test case '$method' enabled in the profile. Forcing...");
431+
}
432+
}
433+
# Just a module name (e.g. Example) or something invalid.
434+
# TODO: in case of invalid input, print a proper error message
435+
# suggesting to use --list-tests for valid choices.
436+
else {
437+
$t =~ s{/$}{};
438+
push @testing_suite, $t;
439+
}
440+
}
441+
}
442+
410443
# These two must come after any profile from command line has been loaded
411444
# to make any IPv4/IPv6 option override the profile setting.
412445
if ( defined ($self->ipv4) ) {
@@ -630,35 +663,24 @@ sub run {
630663
# Actually run tests!
631664
eval {
632665
if ( $self->test and @{ $self->test } > 0 ) {
633-
foreach my $t ( @{ $self->test } ) {
634-
# The case does not matter
635-
$t = lc( $t );
636-
# Fully qualified module and test case (e.g. Example/example12), or just a test case (e.g. example12). Note the different capturing order.
637-
my ( $module, $method );
638-
if ( ( ($module, $method) = $t =~ m#^ ( [a-z]+ ) / ( [a-z]+[0-9]{2} ) $#ix )
639-
or
640-
( ($method, $module) = $t =~ m#^ ( ( [a-z]+ ) [0-9]{2} ) $#ix ) )
641-
{
642-
if ( not grep( /^$method$/, @{ Zonemaster::Engine::Profile->effective->get( 'test_cases' ) } ) ) {
643-
say $fh_diag __x( "Notice: Engine does not have test case '$method' enabled in the profile. Forcing...");
644-
Zonemaster::Engine::Profile->effective->set( 'test_cases', [ "$method" ] );
645-
}
646-
666+
foreach my $t ( @testing_suite ) {
667+
# Either a module/method, or just a module
668+
my ( $module, $method ) = split('/', $t);
669+
if ( $method ) {
670+
# Force Engine to include the requested test case in its profile
671+
Zonemaster::Engine::Profile->effective->set( 'test_cases', [ "$method" ] );
647672
Zonemaster::Engine->test_method( $module, $method, $domain );
648673
}
649-
# Just a module name (e.g. Example) or something invalid.
650-
# TODO: in case of invalid input, print a proper error message
651-
# suggesting to use --list-tests for valid choices.
652674
else {
653-
$t =~ s{/$}{};
654-
Zonemaster::Engine->test_module( $t, $domain );
675+
Zonemaster::Engine->test_module( $module, $domain );
655676
}
656677
}
657678
}
658679
else {
659680
Zonemaster::Engine->test_zone( $domain );
660681
}
661682
};
683+
662684
if ( not $self->raw and not $self->json ) {
663685
if ( not $printed_something ) {
664686
say __( "Looks OK." );

0 commit comments

Comments
 (0)