Skip to content

Commit 11692ab

Browse files
haargLeont
authored andcommitted
fix -M option to call import rather than load
The -M option to prove should load modules basically the same way perl does. If given arguments, it should be calling the import method, not trying to call load like it is a plugin. Before 692c19d, import and load would be called both for plugins (-P) and modules (-M). This was causing warnings and possibly in the future errors for the calls to import with extraneous arguments. When attempting to fix this, the call to import was entirely elimintated. That was not correct for the -M option. Instead, -M should call import and -P should call load.
1 parent 8b86956 commit 11692ab

File tree

3 files changed

+111
-38
lines changed

3 files changed

+111
-38
lines changed

lib/App/Prove.pm

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ sub _find_module {
403403
}
404404

405405
sub _load_extension {
406-
my ( $self, $name, @search ) = @_;
406+
my ( $self, $cb, $name, @search ) = @_;
407407

408408
my @args = ();
409409
if ( $name =~ /^(.*?)=(.*)/ ) {
@@ -412,18 +412,16 @@ sub _load_extension {
412412
}
413413

414414
if ( my $class = $self->_find_module( $name, @search ) ) {
415-
if ( $class->can('load') ) {
416-
$class->load( { app_prove => $self, args => [@args] } );
417-
}
415+
$class->$cb(@args);
418416
}
419417
else {
420418
croak "Can't load module $name";
421419
}
422420
}
423421

424422
sub _load_extensions {
425-
my ( $self, $ext, @search ) = @_;
426-
$self->_load_extension( $_, @search ) for @$ext;
423+
my ( $self, $cb, $ext, @search ) = @_;
424+
$self->_load_extension( $cb, $_, @search ) for @$ext;
427425
}
428426

429427
sub _parse_source {
@@ -497,8 +495,16 @@ sub run {
497495
}
498496
else {
499497

500-
$self->_load_extensions( $self->modules );
501-
$self->_load_extensions( $self->plugins, PLUGINS );
498+
$self->_load_extensions( sub {
499+
my ($ext, @args) = @_;
500+
$ext->import(@args);
501+
}, $self->modules );
502+
$self->_load_extensions( sub {
503+
my ($ext, @args) = @_;
504+
if ( $ext->can('load') ) {
505+
$ext->load( { app_prove => $self, args => [@args] } );
506+
}
507+
}, $self->plugins, PLUGINS );
502508

503509
local $ENV{TEST_VERBOSE} = 1 if $self->verbose;
504510

t/lib/App/Prove/Plugin/Dummy2.pm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package App::Prove::Plugin::Dummy2;
33
use strict;
44
use warnings;
55

6-
sub load {
7-
main::test_log_plugin_load(@_);
6+
sub import {
7+
main::test_log_import(@_);
88
}
99

1010
1;

t/prove.t

Lines changed: 95 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ sub mabs {
5353
}
5454

5555
{
56+
my @import_log = ();
57+
sub test_log_import { push @import_log, [@_] }
58+
59+
sub get_import_log {
60+
my @log = @import_log;
61+
@import_log = ();
62+
return @log;
63+
}
64+
5665
my @plugin_load_log = ();
5766
sub test_log_plugin_load { push @plugin_load_log, [@_] }
5867

@@ -1112,11 +1121,44 @@ BEGIN { # START PLAN
11121121
},
11131122
extra => sub {
11141123
my @loaded = get_plugin_load_log();
1115-
ok @loaded == 1 && $loaded[0][0] eq 'App::Prove::Plugin::Dummy',
1116-
"Plugin loaded OK";
1117-
my $args = $loaded[0][1]{args};
1118-
is_deeply $args, [ 'cracking', 'cheese', 'gromit' ],
1119-
"Plugin args OK";
1124+
is scalar @loaded, 1, 'Plugin->load called OK';
1125+
1126+
my ( $inv, @args ) = @{ shift @loaded };
1127+
is $inv, 'App::Prove::Plugin::Dummy', 'correct plugin passed';
1128+
is scalar @args, 1, 'one argument';
1129+
my $args = shift @args;
1130+
1131+
isa_ok(
1132+
$args->{app_prove}, 'App::Prove',
1133+
'app_prove object passed'
1134+
);
1135+
is_deeply(
1136+
$args->{args}, [qw( cracking cheese gromit )],
1137+
'expected args passed'
1138+
);
1139+
},
1140+
plan => 5,
1141+
runlog => [
1142+
[ '_runtests',
1143+
{ show_count => 1,
1144+
},
1145+
$dummy_test
1146+
]
1147+
],
1148+
},
1149+
1150+
{ name => 'Load plugin (args, no load method)',
1151+
switches => [ '-P', 'Dummy2=cracking,cheese,gromit', $dummy_test ],
1152+
args => {
1153+
argv => [qw( one two three )],
1154+
},
1155+
expect => {
1156+
plugins => ['Dummy2'],
1157+
},
1158+
extra => sub {
1159+
my @import = get_import_log();
1160+
1161+
is scalar @import, 0, 'import not called';
11201162
},
11211163
plan => 1,
11221164
runlog => [
@@ -1151,31 +1193,26 @@ BEGIN { # START PLAN
11511193
],
11521194
},
11531195

1154-
{ name => 'Load plugin (args + call load method)',
1155-
switches => [ '-P', 'Dummy2=fou,du,fafa', $dummy_test ],
1196+
{ name => 'Load module',
1197+
switches => [ '-M', 'App::Prove::Plugin::Dummy2', $dummy_test ],
11561198
args => {
11571199
argv => [qw( one two three )],
11581200
},
11591201
expect => {
1160-
plugins => ['Dummy2'],
1202+
plugins => [],
11611203
},
11621204
extra => sub {
1163-
my @loaded = get_plugin_load_log();
1164-
is( scalar @loaded, 1, 'Plugin->load called OK' );
1165-
my ( $plugin_class, $args ) = @{ shift @loaded };
1166-
is( $plugin_class, 'App::Prove::Plugin::Dummy2',
1167-
'plugin_class passed'
1168-
);
1169-
isa_ok(
1170-
$args->{app_prove}, 'App::Prove',
1171-
'app_prove object passed'
1172-
);
1173-
is_deeply(
1174-
$args->{args}, [qw( fou du fafa )],
1175-
'expected args passed'
1176-
);
1205+
my @import = get_import_log();
1206+
1207+
is scalar @import, 1, 'import called once';
1208+
1209+
my ($inv, @args) = @{ $import[0] };
1210+
1211+
is $inv, 'App::Prove::Plugin::Dummy2', 'Module loaded OK';
1212+
1213+
is scalar @args, 0, 'no extra args passed';
11771214
},
1178-
plan => 5,
1215+
plan => 3,
11791216
runlog => [
11801217
[ '_runtests',
11811218
{ show_count => 1,
@@ -1185,18 +1222,48 @@ BEGIN { # START PLAN
11851222
],
11861223
},
11871224

1188-
{ name => 'Load module',
1225+
{ name => 'Load module (args)',
1226+
switches => [ '-M', 'App::Prove::Plugin::Dummy2=cracking,cheese,gromit', $dummy_test ],
1227+
args => {
1228+
argv => [qw( one two three )],
1229+
},
1230+
expect => {
1231+
plugins => [],
1232+
},
1233+
extra => sub {
1234+
my @import = get_import_log();
1235+
1236+
is scalar @import, 1, 'import called once';
1237+
1238+
my ($inv, @args) = @{ $import[0] };
1239+
1240+
is $inv, 'App::Prove::Plugin::Dummy2', 'Module loaded OK';
1241+
1242+
is_deeply \@args, [qw(cracking cheese gromit)],
1243+
'correct args passed';
1244+
},
1245+
plan => 3,
1246+
runlog => [
1247+
[ '_runtests',
1248+
{ show_count => 1,
1249+
},
1250+
$dummy_test
1251+
]
1252+
],
1253+
},
1254+
1255+
{ name => 'Load module (plugin)',
11891256
switches => [ '-M', 'App::Prove::Plugin::Dummy', $dummy_test ],
11901257
args => {
11911258
argv => [qw( one two three )],
11921259
},
11931260
expect => {
1194-
plugins => ['Dummy'],
1261+
plugins => [],
11951262
},
11961263
extra => sub {
1197-
my @loaded = get_plugin_load_log();
1198-
ok @loaded == 1 && $loaded[0][0] eq 'App::Prove::Plugin::Dummy',
1199-
"Plugin loaded OK";
1264+
my @load = get_plugin_load_log();
1265+
1266+
is scalar @load, 0, 'load not called';
12001267
},
12011268
plan => 1,
12021269
runlog => [

0 commit comments

Comments
 (0)