File Coverage

blib/lib/Dist/Zilla/Plugin/Test/ChangesHasContent.pm
Criterion Covered Total %
statement 38 38 100.0
branch 1 2 50.0
condition 1 3 33.3
subroutine 11 11 100.0
pod 0 2 0.0
total 51 56 91.0


line stmt bran cond sub pod time code
1 1     1   616164 use strict;
  1         1  
  1         29  
2 1     1   3 use warnings;
  1         1  
  1         52  
3             package Dist::Zilla::Plugin::Test::ChangesHasContent;
4             # ABSTRACT: Release test to ensure Changes has content
5             our $VERSION = '0.009';
6              
7             # Dependencies
8 1     1   3 use Dist::Zilla;
  1         1  
  1         27  
9 1     1   4 use autodie 2.00;
  1         25  
  1         5  
10 1     1   2857 use Moose 0.99;
  1         16  
  1         8  
11 1     1   4481 use Sub::Exporter::ForMethods;
  1         2  
  1         8  
12             use Data::Section 0.200002 # encoding and bytes
13 1         4 { installer => Sub::Exporter::ForMethods::method_installer },
14 1     1   129 '-setup' => { encoding => 'bytes' };
  1         19  
15 1     1   556 use Moose::Util::TypeConstraints 'role_type';
  1         2  
  1         8  
16 1     1   263 use namespace::autoclean 0.09;
  1         15  
  1         6  
17              
18             # extends, roles, attributes, etc.
19              
20             with qw/Dist::Zilla::Role::FileGatherer
21             Dist::Zilla::Role::FileMunger
22             Dist::Zilla::Role::TextTemplate
23             /;
24              
25             has changelog => (
26             is => 'ro',
27             isa => 'Str',
28             default => 'Changes'
29             );
30              
31             has trial_token => (
32             is => 'ro',
33             isa => 'Str',
34             default => '-TRIAL'
35             );
36              
37             has _file => (
38             is => 'rw', isa => role_type('Dist::Zilla::Role::File'),
39             );
40              
41             # methods
42              
43             sub gather_files
44             {
45 4     4 0 120143 my $self = shift;
46              
47 4         23 my $data = $self->merged_section_data;
48 4 50 33     558 return unless $data and %$data;
49              
50 4         14 my ($name, $contentref) = %$data;
51              
52 4         588 require Dist::Zilla::File::InMemory;
53              
54 4         58838 $self->add_file( $self->_file(
55             Dist::Zilla::File::InMemory->new({
56             name => $name,
57             content => $$contentref,
58             }))
59             );
60              
61 4         1245 return;
62             }
63              
64             sub munge_files
65             {
66 4     4 0 83898 my $self = shift;
67 4         130 my $file = $self->_file;
68              
69 4         18 $file->content(
70             $self->fill_in_string(
71             $file->content,
72             {
73             changelog => $self->changelog,
74             trial_token => $self->trial_token,
75             newver => $self->zilla->version
76             }
77             )
78             );
79              
80 4         4496 return;
81             }
82              
83             __PACKAGE__->meta->make_immutable;
84              
85             1;
86              
87             # Pod must be before DATA
88              
89             #pod =for Pod::Coverage gather_files munge_files
90             #pod
91             #pod =head1 SYNOPSIS
92             #pod
93             #pod # in dist.ini
94             #pod
95             #pod [Test::ChangesHasContent]
96             #pod
97             #pod =head1 DESCRIPTION
98             #pod
99             #pod This plugin provides C<xt/release/changes_has_content.t>.
100             #pod
101             #pod This test ensures ensures that your Changes file actually has some content
102             #pod since the last release.
103             #pod
104             #pod This can be contrasted to L<Dist::Zilla::Plugin::CheckChangesHasContent>, which
105             #pod performs the check at release time, halting the release process if content is
106             #pod missing. Performing the check as a test makes it possible to check more
107             #pod frequently, and closer to the point of development.
108             #pod
109             #pod The algorithm is very naive. It looks for an unindented line starting with
110             #pod the version to be released. It then looks for any text from that line until
111             #pod the next unindented line (or the end of the file), ignoring whitespace.
112             #pod
113             #pod For example, in the file below, algorithm will find "- blah blah blah":
114             #pod
115             #pod Changes file for Foo-Bar
116             #pod
117             #pod {{$NEXT}}
118             #pod
119             #pod - blah blah blah
120             #pod
121             #pod 0.001 Wed May 12 13:49:13 EDT 2010
122             #pod
123             #pod - the first release
124             #pod
125             #pod If you had nothing but whitespace between C<{{$NEXT}}> and C<0.001>,
126             #pod the release would be halted.
127             #pod
128             #pod If you name your change log something other than "Changes", you can configure
129             #pod the name with the C<changelog> argument:
130             #pod
131             #pod [Test::ChangesHasContent]
132             #pod changelog = ChangeLog
133             #pod
134             #pod =head1 SEE ALSO
135             #pod
136             #pod * L<Dist::Zilla::Plugin::CheckChangesHasContent>
137             #pod * L<Dist::Zilla>
138             #pod
139             #pod =cut
140              
141             =pod
142              
143             =encoding UTF-8
144              
145             =head1 NAME
146              
147             Dist::Zilla::Plugin::Test::ChangesHasContent - Release test to ensure Changes has content
148              
149             =head1 VERSION
150              
151             version 0.009
152              
153             =head1 SYNOPSIS
154              
155             # in dist.ini
156              
157             [Test::ChangesHasContent]
158              
159             =head1 DESCRIPTION
160              
161             This plugin provides C<xt/release/changes_has_content.t>.
162              
163             This test ensures ensures that your Changes file actually has some content
164             since the last release.
165              
166             This can be contrasted to L<Dist::Zilla::Plugin::CheckChangesHasContent>, which
167             performs the check at release time, halting the release process if content is
168             missing. Performing the check as a test makes it possible to check more
169             frequently, and closer to the point of development.
170              
171             The algorithm is very naive. It looks for an unindented line starting with
172             the version to be released. It then looks for any text from that line until
173             the next unindented line (or the end of the file), ignoring whitespace.
174              
175             For example, in the file below, algorithm will find "- blah blah blah":
176              
177             Changes file for Foo-Bar
178              
179             {{$NEXT}}
180              
181             - blah blah blah
182              
183             0.001 Wed May 12 13:49:13 EDT 2010
184              
185             - the first release
186              
187             If you had nothing but whitespace between C<{{$NEXT}}> and C<0.001>,
188             the release would be halted.
189              
190             If you name your change log something other than "Changes", you can configure
191             the name with the C<changelog> argument:
192              
193             [Test::ChangesHasContent]
194             changelog = ChangeLog
195              
196             =for Pod::Coverage gather_files munge_files
197              
198             =head1 SEE ALSO
199              
200             * L<Dist::Zilla::Plugin::CheckChangesHasContent>
201             * L<Dist::Zilla>
202              
203             =head1 AUTHORS
204              
205             =over 4
206              
207             =item *
208              
209             David Golden <dagolden@cpan.org>
210              
211             =item *
212              
213             Karen Etheridge <ether@cpan.org>
214              
215             =back
216              
217             =head1 COPYRIGHT AND LICENSE
218              
219             This software is Copyright (c) 2016 by David Golden.
220              
221             This is free software, licensed under:
222              
223             The Apache License, Version 2.0, January 2004
224              
225             =cut
226              
227             __DATA__
228             ___[ xt/release/changes_has_content.t ]___
229             #!perl
230              
231             use Test::More tests => 2;
232              
233             note 'Checking Changes';
234             my $changes_file = '{{$changelog}}';
235             my $newver = '{{$newver}}';
236             my $trial_token = '{{$trial_token}}';
237              
238             SKIP: {
239             ok(-e $changes_file, "$changes_file file exists")
240             or skip 'Changes is missing', 1;
241              
242             ok(_get_changes($newver), "$changes_file has content for $newver");
243             }
244              
245             done_testing;
246              
247             # _get_changes copied and adapted from Dist::Zilla::Plugin::Git::Commit
248             # by Jerome Quelin
249             sub _get_changes
250             {
251             my $newver = shift;
252              
253             # parse changelog to find commit message
254             open(my $fh, '<', $changes_file) or die "cannot open $changes_file: $!";
255             my $changelog = join('', <$fh>);
256             close $fh;
257              
258             my @content =
259             grep { /^$newver(?:$trial_token)?(?:\s+|$)/ ... /^\S/ } # from newver to un-indented
260             split /\n/, $changelog;
261             shift @content; # drop the version line
262              
263             # drop unindented last line and trailing blank lines
264             pop @content while ( @content && $content[-1] =~ /^(?:\S|\s*$)/ );
265              
266             # return number of non-blank lines
267             return scalar @content;
268             }
269              
270             __END__
271