NAME

Test::Dist::Zilla::Build - Test your Dist::Zilla plugin in build action

VERSION

Version v0.3.0, released on 2015-09-19 23:00 UTC.

SYNOPSIS

package ManifestTester;

use Moose;
with 'Test::Dist::Zilla::Build';
use Test::Deep qw{ cmp_deeply re };
use Test::More;

has options => (
    is          => 'ro',
    isa         => 'HashRef',
    default     => sub { {} },
);

sub _build_message_filter {
    sub { grep( { $_ =~ qr{^\[Manifest\] } @_ ) };
};

sub _build_plugins {
    my ( $self ) = @_;
    [
        'GatherDir',
        [ 'Manifest' => $self->options ],
        'MetaJSON',
    ];
};

sub _build_files { {
    'lib/Dummy.pm' => 'package Dummy; 1;',
} };

test 'Manifest' => sub {

    my ( $self ) = @_;
    my $expected = $self->{ expected };

    if ( $self->exception ) {
        plan skip_all => 'exception occurred';
    };
    if ( not exists( $expected->{ manifest } ) ) {
        plan skip_all => 'no expected manifest';
    };

    my $name = $self->options->{ filename };
    my $built_in = path( $self->tzil->built_in );
    my @manifest = $built_in->child( $name )->lines;
    cmp_deeply( \@manifest, $expected->{ manifest } );

};

run_me 'Positive test' => {
    options => {
        filename => 'Manifest',
    },
    expected => {
        manifest => [
            'lib/Dunny.pm',
            'MANIFEST',
            'META.json',
        ],
    };
};

run_me 'Negative test' => {
    options => {
        filename = '/dev/null',
    },
    expected => {
        exception => re( qr{^Abort\.\.\.} ),
        messages => [
            '[Manifest] Input file /dev/null is empty',
        ],
    };
};

exit( 0 );

DESCRIPTION

This is a Test::Routine-based role for testing Dist::Zilla and its plugins. It creates dist.ini file with specified content in a temporary directory, populates the directory with specified files, runs "build" command with testing version of Dist::Zilla in the temporary directory, checks actual exception and log messages do match expected ones, and let you write other checks specific for your plugin.

OBJECT METHODS

Build

It is a test routine. It runs "build" command, then checks actual exception and log messages match expected ones. Expected exception and log messages should be specified as keys in expected hash, e. g.:

run_me {
    …
    expected => {
        exception => $exception,
        messages => [
            $message,
            …
        ],
    },
};

If exception key is not specified (or exception value is undef), build is expected to complete successfully (i. e. with no exception), otherwise build is expected to fail with the specified exception.

If messages key is not specified, log messages are not checked. Actual log messages retrieved with messages method so you can filter them before comparison with expected messages by defining message_filter attribute and/or by overriding messages method.

Exception (if not undef) and log messages are compared expected counterparts with cmp_deeply (from Test::Deep module).

SEE ALSO

Test::Dist::Zilla
"$ok = cmp_deeply($got, $expected, $name)" in Test::Deep
Test::Routine

AUTHOR

Van de Bugger <van.de.bugger@gmail.com>

COPYRIGHT AND LICENSE

Copyright © 2015 Van de Bugger

This file is part of perl-Test-Dist-Zilla.

perl-Test-Dist-Zilla is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

perl-Test-Dist-Zilla is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with perl-Test-Dist-Zilla. If not, see <http://www.gnu.org/licenses/>.