File Coverage

blib/lib/Test/Dist/Zilla/Build.pm
Criterion Covered Total %
statement 34 34 100.0
branch 5 6 83.3
condition 2 3 66.6
subroutine 8 8 100.0
pod n/a
total 49 51 96.0


line stmt bran cond sub pod time code
1             # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2             #
3             # file: lib/Test/Dist/Zilla/Build.pm
4             #
5             # Copyright © 2015 Van de Bugger
6             #
7             # This file is part of perl-Test-Dist-Zilla.
8             #
9             # perl-Test-Dist-Zilla is free software: you can redistribute it and/or modify it under the terms
10             # of the GNU General Public License as published by the Free Software Foundation, either version
11             # 3 of the License, or (at your option) any later version.
12             #
13             # perl-Test-Dist-Zilla is distributed in the hope that it will be useful, but WITHOUT ANY
14             # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15             # PURPOSE. See the GNU General Public License for more details.
16             #
17             # You should have received a copy of the GNU General Public License along with
18             # perl-Test-Dist-Zilla. If not, see <http://www.gnu.org/licenses/>.
19             #
20             # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
21              
22             #pod =head1 SYNOPSIS
23             #pod
24             #pod package ManifestTester1;
25             #pod
26             #pod use Test::Deep qw{ cmp_deeply re };
27             #pod use Test::More;
28             #pod use Test::Routine;
29             #pod use Test::Routine::Util;
30             #pod
31             #pod with 'Test::Dist::Zilla::Build';
32             #pod
33             #pod has options => (
34             #pod is => 'ro',
35             #pod isa => 'HashRef',
36             #pod default => sub { {} },
37             #pod );
38             #pod
39             #pod sub _build_message_filter {
40             #pod sub { grep( { $_ =~ qr{^\[Manifest\] } } @_ ) };
41             #pod };
42             #pod
43             #pod sub _build_plugins {
44             #pod my ( $self ) = @_;
45             #pod [
46             #pod 'GatherDir',
47             #pod [ 'Manifest' => $self->options ],
48             #pod 'MetaJSON',
49             #pod ];
50             #pod };
51             #pod
52             #pod sub _build_files { {
53             #pod 'lib/Dummy.pm' => 'package Dummy; 1;',
54             #pod } };
55             #pod
56             #pod test 'Manifest' => sub {
57             #pod
58             #pod my ( $self ) = @_;
59             #pod my $expected = $self->{ expected };
60             #pod
61             #pod if ( $self->exception ) {
62             #pod plan skip_all => 'exception occurred';
63             #pod };
64             #pod if ( not exists( $expected->{ manifest } ) ) {
65             #pod plan skip_all => 'no expected manifest';
66             #pod };
67             #pod
68             #pod my $name = $self->options->{ filename };
69             #pod my $built_in = path( $self->tzil->built_in );
70             #pod my @manifest = $built_in->child( $name )->lines;
71             #pod cmp_deeply( \@manifest, $expected->{ manifest } );
72             #pod
73             #pod };
74             #pod
75             #pod run_me 'Positive test' => {
76             #pod options => {
77             #pod filename => 'Manifest',
78             #pod },
79             #pod expected => {
80             #pod manifest => [
81             #pod 'lib/Dunny.pm',
82             #pod 'MANIFEST',
83             #pod 'META.json',
84             #pod ],
85             #pod },
86             #pod };
87             #pod
88             #pod run_me 'Negative test' => {
89             #pod options => {
90             #pod filename => '/dev/null',
91             #pod },
92             #pod expected => {
93             #pod exception => re( qr{^Abort\.\.\.} ),
94             #pod messages => [
95             #pod '[Manifest] Input file /dev/null is empty',
96             #pod ],
97             #pod },
98             #pod };
99             #pod
100             #pod exit( 0 );
101             #pod
102             #pod =head1 DESCRIPTION
103             #pod
104             #pod This is a C<Test::Routine>-based role for testing C<Dist::Zilla> and its plugins. It creates
105             #pod F<dist.ini> file with specified content in a temporary directory, populates the directory with
106             #pod specified files, runs "build" command with testing version of C<Dist::Zilla> in the temporary
107             #pod directory, checks actual exception and log messages do match expected ones, and let you write other
108             #pod checks specific for your plugin.
109             #pod
110             #pod =cut
111              
112             package Test::Dist::Zilla::Build;
113              
114 2     2   1515 use namespace::autoclean;
  2         4  
  2         18  
115 2     2   145 use strict;
  2         4  
  2         53  
116 2     2   1419 use version 0.77;
  2         4093  
  2         13  
117 2     2   122 use warnings;
  2         4  
  2         81  
118              
119             # ABSTRACT: Test your Dist::Zilla plugin in I<build> action
120             our $VERSION = 'v0.4.2'; # VERSION
121              
122 2     2   72 use Test::Routine;
  2         3  
  2         16  
123              
124             with 'Test::Dist::Zilla';
125              
126 2     2   15803 use Test::Deep qw{ cmp_deeply };
  2         4  
  2         19  
127 2     2   222 use Test::More;
  2         4  
  2         23  
128              
129             # --------------------------------------------------------------------------------------------------
130              
131             #pod =method Build
132             #pod
133             #pod It is a test routine. It runs "build" command, then checks actual exception and log messages match
134             #pod expected ones. Expected exception and log messages should be specified as keys in C<expected> hash,
135             #pod e. g.:
136             #pod
137             #pod run_me {
138             #pod …
139             #pod expected => {
140             #pod exception => $exception,
141             #pod messages => [
142             #pod $message,
143             #pod …
144             #pod ],
145             #pod },
146             #pod };
147             #pod
148             #pod If C<exception> key is not specified (or exception value is C<undef>), build is expected to
149             #pod complete successfully (i. e. with no exception), otherwise build is expected to fail with the
150             #pod specified exception.
151             #pod
152             #pod If C<messages> key is not specified, log messages are not checked. Actual log messages retrieved
153             #pod with C<messages> method so you can filter them before comparison with expected messages by defining
154             #pod C<message_filter> attribute and/or by overriding C<messages> method.
155             #pod
156             #pod Exception (if not C<undef>) and log messages are compared expected counterparts with C<cmp_deeply>
157             #pod (from C<Test::Deep> module).
158             #pod
159             #pod =cut
160              
161             test 'Build' => sub {
162              
163 4     4   47974 my ( $self ) = @_;
164 4         182 my $expected = $self->expected;
165              
166 4         16 plan 'no_plan'; # Some checks may be run inside `$self->build()`, I do not know how many.
167              
168 4         127 $self->build();
169 4         11 $self->_anno_text( 'Full log', @{ $self->tzil->log_messages } );
  4         177  
170 4 100       182 if ( $self->exception ) {
171 1         40 $self->_anno_line( 'Exception: ' . $self->exception );
172             };
173              
174 4 100 66     28 if ( exists( $expected->{ exception } ) and defined( $expected->{ exception } ) ) {
175 1         40 cmp_deeply( $self->exception, $expected->{ exception }, 'build must fail' );
176             } else {
177 3         127 is( $self->exception, undef, 'build must pass' );
178             };
179 4 50       2632 if ( exists( $expected->{ messages } ) ) {
180 4         22 cmp_deeply( $self->messages, $expected->{ messages }, 'messages' );
181             };
182              
183             };
184              
185             # --------------------------------------------------------------------------------------------------
186              
187             1;
188              
189             # --------------------------------------------------------------------------------------------------
190              
191             #pod =head1 SEE ALSO
192             #pod
193             #pod =for :list
194             #pod = L<Test::Dist::Zilla>
195             #pod = L<Test::Deep/"$ok = cmp_deeply($got, $expected, $name)">
196             #pod = L<Test::Routine>
197             #pod
198             #pod =head1 COPYRIGHT AND LICENSE
199             #pod
200             #pod Copyright (C) 2015 Van de Bugger
201             #pod
202             #pod License GPLv3+: The GNU General Public License version 3 or later
203             #pod <http://www.gnu.org/licenses/gpl-3.0.txt>.
204             #pod
205             #pod This is free software: you are free to change and redistribute it. There is
206             #pod NO WARRANTY, to the extent permitted by law.
207             #pod
208             #pod
209             #pod =cut
210              
211             # end of file #
212              
213             __END__
214              
215             =pod
216              
217             =encoding UTF-8
218              
219             =head1 NAME
220              
221             Test::Dist::Zilla::Build - Test your Dist::Zilla plugin in I<build> action
222              
223             =head1 VERSION
224              
225             Version v0.4.2, released on 2015-10-31 08:49 UTC.
226              
227             =head1 SYNOPSIS
228              
229             package ManifestTester1;
230              
231             use Test::Deep qw{ cmp_deeply re };
232             use Test::More;
233             use Test::Routine;
234             use Test::Routine::Util;
235              
236             with 'Test::Dist::Zilla::Build';
237              
238             has options => (
239             is => 'ro',
240             isa => 'HashRef',
241             default => sub { {} },
242             );
243              
244             sub _build_message_filter {
245             sub { grep( { $_ =~ qr{^\[Manifest\] } } @_ ) };
246             };
247              
248             sub _build_plugins {
249             my ( $self ) = @_;
250             [
251             'GatherDir',
252             [ 'Manifest' => $self->options ],
253             'MetaJSON',
254             ];
255             };
256              
257             sub _build_files { {
258             'lib/Dummy.pm' => 'package Dummy; 1;',
259             } };
260              
261             test 'Manifest' => sub {
262              
263             my ( $self ) = @_;
264             my $expected = $self->{ expected };
265              
266             if ( $self->exception ) {
267             plan skip_all => 'exception occurred';
268             };
269             if ( not exists( $expected->{ manifest } ) ) {
270             plan skip_all => 'no expected manifest';
271             };
272              
273             my $name = $self->options->{ filename };
274             my $built_in = path( $self->tzil->built_in );
275             my @manifest = $built_in->child( $name )->lines;
276             cmp_deeply( \@manifest, $expected->{ manifest } );
277              
278             };
279              
280             run_me 'Positive test' => {
281             options => {
282             filename => 'Manifest',
283             },
284             expected => {
285             manifest => [
286             'lib/Dunny.pm',
287             'MANIFEST',
288             'META.json',
289             ],
290             },
291             };
292              
293             run_me 'Negative test' => {
294             options => {
295             filename => '/dev/null',
296             },
297             expected => {
298             exception => re( qr{^Abort\.\.\.} ),
299             messages => [
300             '[Manifest] Input file /dev/null is empty',
301             ],
302             },
303             };
304              
305             exit( 0 );
306              
307             =head1 DESCRIPTION
308              
309             This is a C<Test::Routine>-based role for testing C<Dist::Zilla> and its plugins. It creates
310             F<dist.ini> file with specified content in a temporary directory, populates the directory with
311             specified files, runs "build" command with testing version of C<Dist::Zilla> in the temporary
312             directory, checks actual exception and log messages do match expected ones, and let you write other
313             checks specific for your plugin.
314              
315             =head1 OBJECT METHODS
316              
317             =head2 Build
318              
319             It is a test routine. It runs "build" command, then checks actual exception and log messages match
320             expected ones. Expected exception and log messages should be specified as keys in C<expected> hash,
321             e. g.:
322              
323             run_me {
324             …
325             expected => {
326             exception => $exception,
327             messages => [
328             $message,
329             …
330             ],
331             },
332             };
333              
334             If C<exception> key is not specified (or exception value is C<undef>), build is expected to
335             complete successfully (i. e. with no exception), otherwise build is expected to fail with the
336             specified exception.
337              
338             If C<messages> key is not specified, log messages are not checked. Actual log messages retrieved
339             with C<messages> method so you can filter them before comparison with expected messages by defining
340             C<message_filter> attribute and/or by overriding C<messages> method.
341              
342             Exception (if not C<undef>) and log messages are compared expected counterparts with C<cmp_deeply>
343             (from C<Test::Deep> module).
344              
345             =head1 SEE ALSO
346              
347             =over 4
348              
349             =item L<Test::Dist::Zilla>
350              
351             =item L<Test::Deep/"$ok = cmp_deeply($got, $expected, $name)">
352              
353             =item L<Test::Routine>
354              
355             =back
356              
357             =head1 AUTHOR
358              
359             Van de Bugger <van.de.bugger@gmail.com>
360              
361             =head1 COPYRIGHT AND LICENSE
362              
363             Copyright (C) 2015 Van de Bugger
364              
365             License GPLv3+: The GNU General Public License version 3 or later
366             <http://www.gnu.org/licenses/gpl-3.0.txt>.
367              
368             This is free software: you are free to change and redistribute it. There is
369             NO WARRANTY, to the extent permitted by law.
370              
371             =cut