File: 02-test.t

package info (click to toggle)
libdist-zilla-plugin-makemaker-fallback-perl 0.033-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 316 kB
  • sloc: perl: 349; makefile: 2
file content (134 lines) | stat: -rw-r--r-- 4,604 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use strict;
use warnings;

use Test::More 0.88;
use if $ENV{AUTHOR_TESTING}, 'Test::Warnings';
use Test::DZil;
use Path::Tiny;
use Test::Deep;

use lib 't/lib';
use RunTests;

# build a dist with MBT and Fallback
# include a simple test in the dist
# run 'dzil test'
#   - only Build test run; make test is not; environment variables not set
# run 'dzil test --release'
#   - Build test and make test are both run; variables are set properly

{
    package BuildMunger;
    use Moose;
    with 'Dist::Zilla::Role::InstallTool';

    sub setup_installer {
        my $self = shift;

        my @mfpl = grep +($_->name eq 'Makefile.PL' or $_->name eq 'Build.PL'), @{ $self->zilla->files };
        $self->log_fatal('No Makefile.PL or Build.PL was found!') unless @mfpl == 2;

        # munges build scripts to add a line that saves the *TESTING
        # variables' value to 'environment-<builder>'
        for my $mfpl ( @mfpl ) {
            my $filename = $mfpl->name;
            $mfpl->content($mfpl->content . <<"LOG_ENVIRONMENT");

use Path::Tiny;
my \$file = path('../../environment-${filename}');
\$file->append_utf8('\$ENV{' . \$_ . '} = ' . (\$ENV{\$_} || '0') . "\\n")
    foreach qw(RELEASE_TESTING AUTHOR_TESTING);
LOG_ENVIRONMENT
      }
      return;
    }
}

foreach my $extra_testing (undef, 1) {
    note '------------ performing a test, with extra testing variables '
        . ($extra_testing ? '' : 'un') . 'set';

    local $ENV{RELEASE_TESTING}; $ENV{RELEASE_TESTING} = $extra_testing if defined $extra_testing;
    local $ENV{AUTHOR_TESTING}; $ENV{AUTHOR_TESTING} = $extra_testing if defined $extra_testing;

    my $tzil = Builder->from_config(
        { dist_root => 'does-not-exist' },
        {
            add_files => {
                path(qw(source dist.ini)) => simple_ini(
                    [ 'MetaJSON' ],
                    [ 'GatherDir' ],
                    [ 'MakeMaker::Fallback' ],
                    [ 'ModuleBuildTiny' => { version => 0 } ],
                    [ '=BuildMunger' ],
                ),
                path(qw(source lib Foo.pm)) => "package Foo;\n1;\n",
            },
        },
    );

    $tzil->chrome->logger->set_debug(1);

    # $tzil->test, printing diagnostics on failure
    runtests($tzil) || next;

    # I'm not really sure why the build seems to be getting run inside the
    # 'source' dir, rather than 'build' -- seems rather odd...
    my $source_dir = path($tzil->tempdir)->child('source');

    if (not $extra_testing) {
        # confirm that just Build.PL ran, with no environment set
        my $env_build = $source_dir->child('environment-Build.PL');
        ok(-e $env_build, 'Build.PL ran and saved some data for us');
        is(
            $env_build->slurp_utf8,
            "\$ENV{RELEASE_TESTING} = 0\n\$ENV{AUTHOR_TESTING} = 0\n",
            'when test variables are unset, Build.PL ran with variables unset',
        );

        my $env_makefile = $source_dir->child('environment-Makefile.PL');
        ok(!-e $env_makefile, 'Makefile.PL did not run');

        cmp_deeply(
            $tzil->log_messages,
            superbagof(
                re(qr/\Q[MakeMaker::Fallback] doing nothing during test...\E/),
                re(qr/all's well/),
            ),
            'the test method does not die; correct diagnostics printed',
        );
    }
    else {
        # confirm that both Build.PL and Makefile.PL ran, with env set and not set.

        my $env_build = $source_dir->child('environment-Build.PL');
        ok(-e $env_build, 'Build.PL ran and saved some data for us');
        is(
            $env_build->slurp_utf8,
            "\$ENV{RELEASE_TESTING} = 1\n\$ENV{AUTHOR_TESTING} = 1\n",
            'when test variables are set, Build.PL ran with variables set',
        );

        my $env_makefile = $source_dir->child('environment-Makefile.PL');
        ok(-e $env_makefile, 'Makefile.PL ran and saved some data for us');
        is(
            $env_makefile->slurp_utf8,
            "\$ENV{RELEASE_TESTING} = 0\n\$ENV{AUTHOR_TESTING} = 0\n",
            'when test variables are set, Makefile.PL ran with variables unset',
        );

        cmp_deeply(
            $tzil->log_messages,
            superbagof(
                re(qr/\[MakeMaker::Fallback\] performing test with RELEASE_TESTING, AUTHOR_TESTING unset/),
                re(qr/all's well/),
            ),
            'the test method does not die; correct diagnostics printed',
        )
    }

    diag 'got log messages: ', explain $tzil->log_messages
        if not Test::Builder->new->is_passing;
}

done_testing;