The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

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

VERSION

Version v0.4.1, released on 2015-10-29 22:06 UTC.

SYNOPSIS

    package ManifestTester1;

    use Test::Deep qw{ cmp_deeply re };
    use Test::More;
    use Test::Routine;
    use Test::Routine::Util;

    with 'Test::Dist::Zilla::Build';

    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 (C) 2015 Van de Bugger

License GPLv3+: The GNU General Public License version 3 or later <http://www.gnu.org/licenses/gpl-3.0.txt>.

This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.