Skip to content

Commit

Permalink
use tr/_//d instead of eval on $VERSION
Browse files Browse the repository at this point in the history
This way, trailing zeroes are preserved in the version. This might be
important in some cases e.g. in metadata.
  • Loading branch information
karenetheridge committed Apr 2, 2018
1 parent 4ab0d44 commit f7c3778
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 18 deletions.
10 changes: 5 additions & 5 deletions lib/Dist/Zilla/Plugin/BumpVersionAfterRelease.pm
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ sub rewrite_version {
Path::Tiny::path( $file->_original_name )->slurp( { binmode => $iolayer } );

my $code = "our \$VERSION = '$version';";
$code .= "\n\$VERSION = eval \$VERSION;"
$code .= "\n\$VERSION =~ tr/_//d;"
if $version =~ /_/ and scalar( $version =~ /\./g ) <= 1;

my $assign_regex = $self->assign_re();
Expand Down Expand Up @@ -339,7 +339,7 @@ use underscores in decimal versions. In this case, the following line will
be added after the C<$VERSION> assignment to ensure the underscore is
removed at runtime:
$VERSION = eval $VERSION;
$VERSION =~ tr/_//d;
Despite their long history on CPAN, the author does not recommend the use
of decimal underscore versions with Dist::Zilla, as Dist::Zilla supports
Expand All @@ -352,19 +352,19 @@ line, it's obvious in the source that the module is a development release.
With both source and tarball obviously marked "TRIAL", most of the
historical need for underscore in a version is taken care of.
Using decimal underscores (with the "eval" hack ) introduces a subtle
Using decimal underscores (with the "tr" hack ) introduces a subtle
difference between what the C<< MM->parse_version >> thinks the version is
(and what is in META) and what Perl thinks the version is at runtime.
Foo->VERSION eq MM->parse_version( $INC{"Foo.pm"} )
This would be false for the version "1.002_003" with
C<$VERSION = eval $VERSION>. Much of the toolchain has heuristics to
C<$VERSION =~ tr/_//d>. Much of the toolchain has heuristics to
deal with this, but it may be an issue depending on exactly what
version of toolchain modules you have installed. You can avoid all of
it by just not using underscores.
On the other hand, using underscores and B<not> using C<eval $VERSION>
On the other hand, using underscores and B<not> using the "tr" hack
leads to even worse problems trying to specify a version number with
C<use>:
Expand Down
2 changes: 1 addition & 1 deletion lib/Dist/Zilla/Plugin/RewriteVersion.pm
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ sub rewrite_version {
$code .= ( $self->zilla->is_trial ? "" : " #" ) . " from $tarball";
}

$code .= "\n\$VERSION = eval \$VERSION;"
$code .= "\n\$VERSION =~ tr/_//d;"
if $version =~ /_/ and scalar( $version =~ /\./g ) <= 1;

my $assign_regex = $self->assign_re();
Expand Down
81 changes: 69 additions & 12 deletions t/underscore_version.t
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ subtest "with allow_decimal_underscore" => sub {
"package Foo::Eval;\n\nour \$VERSION = '0.004_002';\n\$VERSION = eval \$VERSION;\n\n1;\n",
path(qw(source lib Foo Eval Trial.pm)) =>
"package Foo::Eval::Trial;\n\nour \$VERSION = '0.004_002'; # TRIAL\n\$VERSION = eval \$VERSION;\n\n1;\n",
path(qw(source lib Foo Transliterate.pm)) =>
"package Foo::Transliterate;\n\nour \$VERSION = '0.004_002';\n\$VERSION =~ tr/_//d;\n\n1;\n",
path(qw(source lib Foo Transliterate Trial.pm)) =>
"package Foo::Transliterate::Trial;\n\nour \$VERSION = '0.004_002'; # TRIAL\n\$VERSION =~ tr/_//d;\n\n1;\n",
path(qw(source lib Foo Substitute.pm)) =>
"package Foo::Substitute;\n\nour \$VERSION = '0.004_002';\n\$VERSION =~ s/_//g;\n\n1;\n",
path(qw(source lib Foo Substitute Trial.pm)) =>
"package Foo::Substitute::Trial;\n\nour \$VERSION = '0.004_002'; # TRIAL\n\$VERSION =~ s/_//g;\n\n1;\n",
},
},
);
Expand All @@ -119,38 +127,87 @@ subtest "with allow_decimal_underscore" => sub {

is(
path( $tzil->tempdir, qw(build lib Foo.pm) )->slurp_utf8,
"package Foo;\n\nour \$VERSION = '0.004_002'; # TRIAL\n\$VERSION = eval \$VERSION;\n\n1;\n",
'TRIAL comment and eval line are added',
"package Foo;\n\nour \$VERSION = '0.004_002'; # TRIAL\n\$VERSION =~ tr/_//d;\n\n1;\n",
'TRIAL comment and tr line are added',
);

is(
path( $tzil->tempdir, qw(build lib Foo Eval.pm) )->slurp_utf8,
"package Foo::Eval;\n\nour \$VERSION = '0.004_002'; # TRIAL\n\$VERSION = eval \$VERSION;\n\n1;\n",
'TRIAL comment is added; eval line is retained',
"package Foo::Eval;\n\nour \$VERSION = '0.004_002'; # TRIAL\n\$VERSION =~ tr/_//d;\n\n1;\n",
'TRIAL comment is added; eval line is changed to tr',
);

is(
path( $tzil->tempdir, qw(build lib Foo Eval Trial.pm) )->slurp_utf8,
"package Foo::Eval::Trial;\n\nour \$VERSION = '0.004_002'; # TRIAL\n\$VERSION = eval \$VERSION;\n\n1;\n",
'TRIAL comment and eval line are retained',
"package Foo::Eval::Trial;\n\nour \$VERSION = '0.004_002'; # TRIAL\n\$VERSION =~ tr/_//d;\n\n1;\n",
'TRIAL comment is retained; eval is changed to tr',
);

is(
path( $tzil->tempdir, qw(build lib Foo Transliterate.pm) )->slurp_utf8,
"package Foo::Transliterate;\n\nour \$VERSION = '0.004_002'; # TRIAL\n\$VERSION =~ tr/_//d;\n\n1;\n",
'TRIAL comment is added; tr line is retained',
);

is(
path( $tzil->tempdir, qw(build lib Foo Transliterate Trial.pm) )->slurp_utf8,
"package Foo::Transliterate::Trial;\n\nour \$VERSION = '0.004_002'; # TRIAL\n\$VERSION =~ tr/_//d;\n\n1;\n",
'TRIAL comment and tr line are retained',
);

is(
path( $tzil->tempdir, qw(build lib Foo Substitute.pm) )->slurp_utf8,
"package Foo::Substitute;\n\nour \$VERSION = '0.004_002'; # TRIAL\n\$VERSION =~ tr/_//d;\n\n1;\n",
'TRIAL comment is added; substitution is changed to tr',
);

is(
path( $tzil->tempdir, qw(build lib Foo Substitute Trial.pm) )->slurp_utf8,
"package Foo::Substitute::Trial;\n\nour \$VERSION = '0.004_002'; # TRIAL\n\$VERSION =~ tr/_//d;\n\n1;\n",
'TRIAL comment is retained; substitution is changed to tr',
);


is(
path( $tzil->tempdir, qw(source lib Foo.pm) )->slurp_utf8,
"package Foo;\n\nour \$VERSION = '0.004_003';\n\$VERSION = eval \$VERSION;\n\n1;\n",
'.pm contents in source saw the underscore version incremented, and eval added',
"package Foo;\n\nour \$VERSION = '0.004_003';\n\$VERSION =~ tr/_//d;\n\n1;\n",
'.pm contents in source saw the underscore version incremented, and tr added',
);

is(
path( $tzil->tempdir, qw(source lib Foo Eval.pm) )->slurp_utf8,
"package Foo::Eval;\n\nour \$VERSION = '0.004_003';\n\$VERSION = eval \$VERSION;\n\n1;\n",
'.pm contents in source saw the underscore version incremented and eval retained',
"package Foo::Eval;\n\nour \$VERSION = '0.004_003';\n\$VERSION =~ tr/_//d;\n\n1;\n",
'.pm contents in source saw the underscore version incremented and eval changed to tr',
);

is(
path( $tzil->tempdir, qw(source lib Foo Eval Trial.pm) )->slurp_utf8,
"package Foo::Eval::Trial;\n\nour \$VERSION = '0.004_003';\n\$VERSION = eval \$VERSION;\n\n1;\n",
'.pm contents in source saw the underscore version incremented, TRIAL comment removed and eval retained',
"package Foo::Eval::Trial;\n\nour \$VERSION = '0.004_003';\n\$VERSION =~ tr/_//d;\n\n1;\n",
'.pm contents in source saw the underscore version incremented, TRIAL comment removed and eval changed to tr',
);

is(
path( $tzil->tempdir, qw(source lib Foo Transliterate.pm) )->slurp_utf8,
"package Foo::Transliterate;\n\nour \$VERSION = '0.004_003';\n\$VERSION =~ tr/_//d;\n\n1;\n",
'.pm contents in source saw the underscore version incremented and tr retained',
);

is(
path( $tzil->tempdir, qw(source lib Foo Transliterate Trial.pm) )->slurp_utf8,
"package Foo::Transliterate::Trial;\n\nour \$VERSION = '0.004_003';\n\$VERSION =~ tr/_//d;\n\n1;\n",
'.pm contents in source saw the underscore version incremented, TRIAL comment removed and tr retained',
);

is(
path( $tzil->tempdir, qw(source lib Foo Substitute.pm) )->slurp_utf8,
"package Foo::Substitute;\n\nour \$VERSION = '0.004_003';\n\$VERSION =~ tr/_//d;\n\n1;\n",
'.pm contents in source saw the underscore version incremented and substitution changed to tr',
);

is(
path( $tzil->tempdir, qw(source lib Foo Substitute Trial.pm) )->slurp_utf8,
"package Foo::Substitute::Trial;\n\nour \$VERSION = '0.004_003';\n\$VERSION =~ tr/_//d;\n\n1;\n",
'.pm contents in source saw the underscore version incremented, TRIAL comment removed and substitution changed to tr',
);

diag 'got log messages: ', explain $tzil->log_messages
Expand Down

0 comments on commit f7c3778

Please sign in to comment.