| 1 | package Dist::Zilla::Plugin::DBIO::VersionLockstepTest; | ||
| 2 | # ABSTRACT: Generate xt/author/version-lockstep.t, a gate that every module carries the main module's version | ||
| 3 | 5 | our $VERSION = '0.900007'; | |
| 4 | 5 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 5 | ▶ | use Dist::Zilla::File::InMemory; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 5 | with 'Dist::Zilla::Role::FileGatherer'; | |
| 7 | |||
| 8 | |||
| 9 | 2 | ▶ | sub gather_files { |
|
Subroutine
gather_files: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 10 | 2 | my ($self) = @_; | |
| 11 | 2 | my $main = $self->zilla->main_module->name; | |
| 12 | 2 | $self->add_file(Dist::Zilla::File::InMemory->new( | |
| 13 | name => 'xt/author/version-lockstep.t', | ||
| 14 | content => $self->_template($main), | ||
| 15 | )); | ||
| 16 | 2 | $self->log_debug('gathered xt/author/version-lockstep.t'); | |
| 17 | } | ||
| 18 | |||
| 19 | 2 | ▶ | sub _template { |
|
Subroutine
_template: called CC=1 SCAR=0.0
| |||
| 20 | 2 | my ($self, $main) = @_; | |
| 21 | 2 | (my $quoted = $main) =~ s/([\\'])/\\$1/g; | |
| 22 | 2 | my $tpl = <<'PERL'; | |
| 23 | use strict; | ||
| 24 | use warnings; | ||
| 25 | use File::Find (); | ||
| 26 | use Test::More; | ||
| 27 | |||
| 28 | # Every module under lib/ carries the version of the main module. The | ||
| 29 | # release rewrites all of them; this checks the result. Paths are relative to | ||
| 30 | # the distribution root. | ||
| 31 | my $main = '@@MAIN@@'; | ||
| 32 | |||
| 33 | # The literal $VERSION assignment of a file, or undef when it has none. | ||
| 34 | sub version_of { | ||
| 35 | my ($file) = @_; | ||
| 36 | open my $fh, '<:encoding(UTF-8)', $file or die "cannot read $file: $!"; | ||
| 37 | while (my $line = <$fh>) { | ||
| 38 | return $1 if $line =~ /^\s*(?:our\s+)?\$VERSION\s*=\s*['"]?([0-9][0-9._]*)['"]?\s*;/; | ||
| 39 | } | ||
| 40 | return undef; | ||
| 41 | } | ||
| 42 | |||
| 43 | my $want = -f $main ? version_of($main) : undef; | ||
| 44 | ok defined $want, "main module $main carries a literal \$VERSION" | ||
| 45 | or BAIL_OUT 'no main module version to compare against -- run this test from the distribution root'; | ||
| 46 | |||
| 47 | my @files; | ||
| 48 | File::Find::find({ | ||
| 49 | no_chdir => 1, | ||
| 50 | wanted => sub { push @files, $_ if -f && /\.pm\z/ }, | ||
| 51 | }, 'lib'); | ||
| 52 | ok scalar(@files), 'found modules under lib/'; | ||
| 53 | |||
| 54 | for my $file (sort @files) { | ||
| 55 | my $have = version_of($file); | ||
| 56 | ok defined $have && $have eq $want, | ||
| 57 | "$file carries version $want" | ||
| 58 | or diag defined $have | ||
| 59 | ? "$file has \$VERSION $have, the main module has $want" | ||
| 60 | : "$file has no literal \$VERSION, the main module has $want"; | ||
| 61 | } | ||
| 62 | |||
| 63 | done_testing; | ||
| 64 | PERL | ||
| 65 | 2 | $tpl =~ s/\@\@MAIN\@\@/$quoted/; | |
| 66 | 2 | return $tpl; | |
| 67 | } | ||
| 68 | |||
| 69 | 5 | __PACKAGE__->meta->make_immutable; | |
| 70 | 5 | ▶ | no Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 71 | 5 | 1; | |
| 72 | |||
| 73 | __END__ | ||