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