#!perl
use
if
$ENV
{AUTOMATED_TESTING},
'Test::DiagINC'
;
sub
hook($$) {
my
(
$name
,
$text
) =
@_
;
$text
=~ s{\A\n}{};
$text
=~ s{\n +\z}{\n};
return
[
$name
, {
'hook'
=> [
split
(
"\n"
,
$text
) ] } ];
};
sub
skip_if_missed($) {
my
(
$module
) =
@_
;
local
$@;
eval
"require $module;"
;
if
( $@ ) {
note( $@ );
if
( not
$ENV
{ AUTHOR_TESTING } ) {
skip
"Can't load $module"
, 1;
};
};
};
my
$abort
= isa(
'Dist::Zilla::Role::ErrorLogger::Exception::Abort'
);
run_me
'$self and $zilla'
=> {
plugins
=> [
hook(
'Hook::Init'
,
q{
$self->log( $zilla->name );
}
),
'GatherDir'
,
],
expected
=> {
messages
=> [
'[Hook::Init] Dummy'
],
}
};
run_me
'$plugin == $self'
=> {
plugins
=> [
hook(
'Hook::Init'
,
q{
$self->log( $plugin == $self ? "OK" : "NOT OK" );
}
),
'GatherDir'
,
],
expected
=> {
messages
=> [
'[Hook::Init] OK'
,
],
},
};
run_me
'$dist == $zilla'
=> {
plugins
=> [
hook(
'Hook::Init'
,
q{
$self->log( $dist == $zilla ? "OK" : "NOT OK" );
}
),
'GatherDir'
,
],
expected
=> {
messages
=> [
'[Hook::Init] OK'
,
],
},
};
run_me
'$arg defined'
=> {
plugins
=> [
hook(
'Hook::LicenseProvider'
,
q{
$plugin->log( [
"Copyright (C) %d %s", $arg->{ copyright_year }
,
$arg
->{ copyright_holder }
] );
} ),
'GatherDir'
,
],
expected
=> {
messages
=> [
'[Hook::LicenseProvider] Copyright (C) 2007 John Doe'
,
],
},
};
SKIP: {
skip
'Not yet decided'
, 1;
run_me
'$arg not defined'
=> {
plugins
=> [
hook(
'Hook::BeforeBuild'
,
q{
$plugin->log( $arg );
}
),
'GatherDir'
,
],
expected
=> {
exception
=>
$abort
,
messages
=> [
re(
qr{^\[Hook::BeforeBuild\] Global symbol "\$arg" requires explicit package name.* at Hook::BeforeBuild line 1\b}
),
],
},
};
};
run_me
'@_'
=> {
plugins
=> [
hook(
'Hook::LicenseProvider'
,
q{
my ( $args ) = @_;
my ( $holder, $year ) = map( $args->{ "copyright_$_" }
,
qw{ holder year }
);
$plugin
->
log
( [
"Copyright (C) %d %s"
,
$year
,
$holder
] );
} ),
'GatherDir'
,
],
expected
=> {
messages
=> [
'[Hook::LicenseProvider] Copyright (C) 2007 John Doe'
,
],
},
};
run_me
'"use strict;" is in effect'
=> {
plugins
=> [
hook(
'Hook::BeforeBuild'
,
q{
$assa = 123;
}
),
'GatherDir'
,
],
expected
=> {
exception
=>
$abort
,
messages
=> [
re(
qr{^\[Hook::BeforeBuild\] Global symbol "\$assa" requires explicit package name.* at Hook::BeforeBuild line 1\b}
),
],
},
};
run_me
'"use warnings;" is in effect'
=> {
plugins
=> [
hook(
'Hook::BeforeBuild'
,
q{
my $assa;
my $qwerty = $assa + 1;
}
),
'GatherDir'
,
],
expected
=> {
messages
=> [
'[Hook::BeforeBuild] Use of uninitialized value $assa in addition (+) at Hook::BeforeBuild line 2.'
,
],
},
};
run_me
'semicolon not preceeded by space'
=> {
plugins
=> [
hook(
'Hook::Init'
,
q{
$plugin->log( "Assa" ); $plugin->log( "Qwerty" );
}
),
'GatherDir'
,
],
expected
=> {
messages
=> [
'[Hook::Init] Assa'
,
'[Hook::Init] Qwerty'
,
],
},
};
run_me
'semicolon preceeded by space'
=> {
plugins
=> [
hook(
'Hook::Init'
,
q{
$plugin->log( "Assa" ) ; $plugin->log( "Qwerty" );
}
),
'GatherDir'
,
],
expected
=> {
messages
=> [
'[Hook::Init] Assa'
,
],
},
};
my
$hook
= '
$plugin
->
log
(
"hook"
);
if
(
$plugin
->plugin_name eq
"Hook::MetaProvider"
) {
return
{};
}
else
{
return
undef
;
};
';
SKIP: {
skip_if_missed(
'Dist::Zilla::Plugin::ReportPhase'
);
run_me
'Phases'
=> {
message_filter
=>
sub
{
grep
( {
$_
=~ m{^\[(?:Hook::|Phase)} }
@_
) },
plugins
=> [
'ReportPhase/Phase_Begins'
,
hook(
'Hook::AfterBuild'
,
$hook
),
hook(
'Hook::AfterMint'
,
$hook
),
hook(
'Hook::AfterRelease'
,
$hook
),
hook(
'Hook::BeforeArchive'
,
$hook
),
hook(
'Hook::BeforeBuild'
,
$hook
),
hook(
'Hook::BeforeMint'
,
$hook
),
hook(
'Hook::BeforeRelease'
,
$hook
),
hook(
'Hook::FileGatherer'
,
$hook
),
hook(
'Hook::FileMunger'
,
$hook
),
hook(
'Hook::FilePruner'
,
$hook
),
hook(
'Hook::Init'
,
$hook
),
hook(
'Hook::InstallTool'
,
$hook
),
hook(
'Hook::LicenseProvider'
,
$hook
),
hook(
'Hook::MetaProvider'
,
$hook
),
hook(
'Hook::ModuleMaker'
,
$hook
),
hook(
'Hook::NameProvider'
,
$hook
),
hook(
'Hook::PrereqSource'
,
$hook
),
hook(
'Hook::Releaser'
,
$hook
),
hook(
'Hook::ReleaseStatusProvider'
,
$hook
),
hook(
'Hook::VersionProvider'
,
$hook
),
'GatherDir'
,
'ReportPhase/Phase_Ends'
,
],
message_grepper
=>
sub
{
return
$_
=~ m{^\[(?:Phase_(?:Begins|Ends)|Hook::.+?)\] };
},
expected
=> {
messages
=> [
'[Hook::Init] hook'
,
'[Phase_Begins] ########## Before Build ##########'
,
'[Hook::BeforeBuild] hook'
,
'[Phase_Ends] ########## Before Build ##########'
,
'[Phase_Begins] ########## Gather Files ##########'
,
'[Hook::FileGatherer] hook'
,
'[Phase_Ends] ########## Gather Files ##########'
,
'[Phase_Begins] ########## Prune Files ##########'
,
'[Hook::FilePruner] hook'
,
'[Phase_Ends] ########## Prune Files ##########'
,
'[Phase_Begins] ########## Provide Version ##########'
,
'[Hook::VersionProvider] hook'
,
'[Phase_Ends] ########## Provide Version ##########'
,
'[Phase_Begins] ########## Munge Files ##########'
,
'[Hook::FileMunger] hook'
,
'[Phase_Ends] ########## Munge Files ##########'
,
'[Phase_Begins] ########## Bundle Config ##########'
, # TODO: Support `PluginBundle`?
'[Hook::PrereqSource] hook'
,
'[Phase_Ends] ########## Bundle Config ##########'
,
'[Hook::LicenseProvider] hook'
,
'[Hook::ReleaseStatusProvider] hook'
,
'[Phase_Begins] ########## Metadata ##########'
,
'[Hook::MetaProvider] hook'
,
'[Phase_Ends] ########## Metadata ##########'
,
'[Phase_Begins] ########## Setup Installer ##########'
,
'[Hook::InstallTool] hook'
,
'[Phase_Ends] ########## Setup Installer ##########'
,
'[Phase_Begins] ########## After Build ##########'
,
'[Hook::AfterBuild] hook'
,
'[Phase_Ends] ########## After Build ##########'
,
],
},
};
};
run_me
'die in hook'
=> {
plugins
=> [
hook(
'Hook::Init'
,
q{
# this is line 1
die "oops"; # line 2
# this is line 3
}
),
'GatherDir'
,
],
expected
=> {
exception
=>
$abort
,
messages
=> [
re(
qr{^\[Hook::Init\] oops at Hook::Init line 2\b}
),
],
},
};
SKIP: {
skip_if_missed(
'Throwable'
);
run_me
'die with object'
=> {
plugins
=> [
hook(
'Hook::Init'
,
q{
use strict;
{ package Exception;
use Moose;
with 'Throwable';
has message => ( is => 'ro' );
sub string { shift->message }
;
}
Exception->throw( {
message
=>
'Assa'
} );
} ),
'GatherDir'
,
],
expected
=> {
exception
=>
$abort
,
messages
=> [
re(
qr{^\[Hook::Init\] Assa\b}
),
],
},
};
};
run_me
'die in named hook'
=> {
plugins
=> [
hook(
'Hook::Init/HookName'
,
q{
# this is line 1
# this is line 2
die "oops"; # line 3
}
),
'GatherDir'
,
],
expected
=> {
exception
=>
$abort
,
messages
=> [
re(
qr{^\[HookName\] oops at HookName line 3\b}
),
],
},
};
run_me
'hook name contains space'
=> {
plugins
=> [
hook(
'Hook::Init/hook name'
,
q{
# this is line 1
# this is line 2
die "oops"; # line 3
}
),
'GatherDir'
,
],
expected
=> {
exception
=>
$abort
,
messages
=> [
re(
qr{^\[hook name\] oops at hook name line 3\b}
),
],
},
};
run_me
'hook name contains quote'
=> {
plugins
=> [
hook(
'Hook::Init/hook "name"'
,
q{
# this is line 1
# this is line 2
die "oops"; # line 3
}
),
'GatherDir'
,
],
expected
=> {
exception
=>
$abort
,
messages
=> [
re(
qr{^\[hook "name"\] oops at hook 'name' line 3\b}
),
],
},
};
run_me
'prologue'
=> {
plugins
=> [
hook(
'Hook/prologue'
,
'$self->log( "prologue" );'
),
hook(
'Hook::Init'
,
'$self->log( "hook" );'
),
hook(
'Hook::BeforeBuild'
,
'$self->log( "hook" );'
),
'GatherDir'
,
],
expected
=> {
messages
=> [
'[Hook::Init] prologue'
,
'[Hook::Init] hook'
,
'[Hook::BeforeBuild] prologue'
,
'[Hook::BeforeBuild] hook'
,
],
},
};
run_me
'prologue dies'
=> {
plugins
=> [
hook(
'Hook/prologue'
,
q{
$self->log( "prologue" );
die "oops";
}
),
hook(
'Hook::Init'
,
q{
$self->log( "init" );
}
),
'GatherDir'
,
],
expected
=> {
exception
=>
$abort
,
messages
=> [
'[Hook::Init] prologue'
,
re(
qr{\[Hook::Init\] oops at prologue line 2\b}
),
],
},
};
run_me
'prologue + body dies'
=> {
plugins
=> [
hook(
'Hook/prologue'
,
q{
$self->log( "prologue" );
}
),
hook(
'Hook::Init'
,
q{
$self->log( "init" );
die "oops";
}
),
'GatherDir'
,
],
expected
=> {
exception
=>
$abort
,
messages
=> [
'[Hook::Init] prologue'
,
'[Hook::Init] init'
,
re(
qr{\[Hook::Init\] oops at Hook::Init line 2\b}
),
],
},
};
done_testing;
exit
( 0 );