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::Release - 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 ArchiveRelease 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::Release';

    has options => (                    # Options for the plugin.
        isa         => 'HashRef',
        is          => 'ro',
        default     => sub { {} },      # No options by default,
                                        # but can be specified in test.
    );

    sub _build_plugins {    # All the tests use the same set of plugins.
        my ( $self ) = @_;  # Let's define builder to avoid repetition.
        return [            # See "plugins" in Test::Dist::Zilla.
            'GatherDir',
            'Manifest',
            'MetaJSON',
            [ 'ArchiveRelease' => $self->options ], # Pass options to the plugin.
        ];
    };

    sub _build_files {      # Source file.
        return {            # See "files" in Test::Dist::Zilla.
            'lib/Dummy.pm' => 'package Dummy; 1;',
        };
    };

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

    test Archive => sub {       # Test routine, is called after Release routine.
        my ( $self ) = @_;
        my $expected = $self->{ expected };
        $self->skip_if_exception;
        if ( not exists( $expected->{ archive } ) ) {
            plan skip_all => 'no expected archive';
        };
        my $root = path( $self->tzil->root );
        my $archive = $root->child( $expected->{ archive } );
        ok( -f $archive, "archive $archive exists" );
        # Archive content could also be tested...
    };

    run_me 'Default directory' => {
        expected => {
            messages => [
                'Created directory releases',
                re( qr{Moved to releases[/\\]Dummy-0\.003\.tar\.gz} ),
            ],
            archive => 'releases/Dummy-0.003.tar.gz',
        },
    };

    run_me 'Custom directory' => {
        options => {
            directory => '.archive',
        },
        expected => {
            messages => [
                'Created directory .archive',
                re( qr{Moved to \.archive[/\\]Dummy-0\.003\.tar\.gz} ),
            ],
            archive => '.archive/Dummy-0.003.tar.gz',
        },
    };

    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 "release" 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

Release

This is a test routine. It runs dzil release, and then:

  • If expected exception is specified (see "expected" in Test::Dist::Zilla) the routine checks release fails with the expected exception. If exception is not expected the routine checks release completes successfully.

  • If expected messages are specified (see "expected" in Test::Dist::Zilla) the routine compares (with cmd_deeply) actual messages and expected messages.

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.