NAME

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

VERSION

Version v0.4.4, released on 2016-12-28 19:48 UTC.

SYNOPSIS

    # Let's test Manifest Dist::Zilla plugin:

    use strict;
    use warnings;

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

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

    sub _build_message_filter {
        return sub {
            map(
                { $_ =~ s{^\[.*?\] }{}; $_; }   # Drop plugin name from messages.
                grep( { $_ =~ qr{^\Q[Manifest]\E } } @_ )
                    # We are interested only in messages printed by the plugin.
            );
        };
    };

    test Manifest => sub {
        my ( $self ) = @_;
        my $expected = $self->{ expected };
        $self->skip_if_exception;
        if ( not exists( $expected->{ manifest } ) ) {
            plan skip_all => 'no expected manifest';
        };
        my $built_in = path( $self->tzil->built_in );
        my @manifest = $built_in->child( 'MANIFEST' )->lines( { chomp => 1 } );
        my $comment = shift( @manifest );
        like(
            $comment,
            qr{
                ^ \# \Q This file was automatically generated by \E
                Dist::Zilla::Plugin::Manifest
            }x,
            'first line is a comment',
        );
        cmp_deeply( \@manifest, $expected->{ manifest }, 'manifest body' )
            or do { diag( "MANIFEST:" ); diag( "    $_" ) for @manifest; };
    };

    run_me 'Positive test' => {
        # exception and messages are checked by Build test (defined in
        # Test::Dist::Zilla::Build), manifest content is checked by
        # Manifest test defined above.
        plugins => [                        # Plugins to use.
            'GatherDir',
            'Manifest',
            'MetaJSON',
        ],
        files => {                          # Files to add.
            'lib/Dummy.pm' => 'package Dummy; 1;',
        },
        expected => {                       # Expected outcome.
            # exception is not specified => successful build is expected.
            messages => [],                 # No messages from the plugin expected.
            manifest => [                   # Expected content of MANIFEST.
                'MANIFEST',
                'META.json',
                'dist.ini',
                'lib/Dummy.pm',
            ],
        },
    };

    done_testing;

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 are 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 with expected counterparts by using 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, 2016 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.