File: 40.perl-reversion.t

package info (click to toggle)
libperl-version-perl 1.018-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 240 kB
  • sloc: perl: 2,327; makefile: 2
file content (255 lines) | stat: -rwxr-xr-x 5,705 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/usr/bin/perl

use strict;
use warnings;
use Test::More;
use File::Temp;
use File::Path qw(mkpath);
use File::Spec;
use Data::Dumper;

#if ( $^O =~ /MSWin32/ ) {
#  plan skip_all => 'cannot run on Windows';
#}

# -Mblib makes a lot of noise
my $libs = join " ",
 map { '-I' . File::Spec->catfile( 'blib', $_ ) } qw(lib arch);
my $RUN = "$^X $libs examples/perl-reversion";

if ( system( "$RUN -quiet" ) ) {
  plan skip_all => 'cannot run perl-reversion, skipping its tests';
}

my $dir = File::Temp::tempdir( CLEANUP => 1 );

sub find {
  my $rv = _run( @_ );
  if ( $rv->{output} =~ /version is (\S+)$/ ) {
    return { found => $1 };
  }
  else {
    return {};
  }
}

sub _run {
  my $cmd = "$RUN @_";
  #diag $cmd;
  my $output = readpipe( $cmd );

  #diag $output;
  return { output => $output };
}

sub with_file {
  my ( $name, $content, $code ) = @_;

  subtest $name => sub {
      my $path = File::Spec->catfile( $dir, $name );
      open my $fh, '>', $path or die "Can't open $path: $!";
      binmode $fh;
      print $fh $content;
      close $fh;
      $code->();
      unlink $path or die "Can't unlink $path: $!";
    };
}

sub count_newlines {
    my @newlines= ("\x{0d}\x{0a}","\x{0d}","\x{0a}");
    my %result;
    for my $name (@_) {
        local $/;
        open my $fh, '<:raw', $name;
        my $content = do { local $/; <$fh> };
        close $fh;

        $result{ $name }= +{
            map {
                my $key= unpack 'H*', $_;
                my $count =()= $content=~ /$_/g;
                $key=>$count
            } @newlines
        };
    };
    %result
};

sub ok_newlines {
    my( $name, %expected ) = @_;
    my %got= count_newlines( keys %expected );

    is_deeply \%got, \%expected,
        "$name - All newlines remain intact"
      or diag Dumper [ \%expected, \%got ];
};


sub runtests {
  my ( $name, $version ) = @_;

  # Check that we keep line endings consistent:
  my @files= (grep { -f } glob( "$dir/*" ), glob( "$dir/*/*" ) );
  my %newlines= count_newlines( @files );

  is_deeply( find( $dir ), { found => $version }, "found $version in $name" );
  is_deeply( find( $dir, "-current=1.2" ), {}, "partial does not match " );

  _run( $dir, '-set', '1.2' );
  ok_newlines( "$name -set", %newlines );
  _run( $dir, '-bump' );
  ok_newlines( "$name -bump", %newlines );
  is_deeply(
    find( $dir ),
    { found => '1.3', },
    "-bump did not extend version"
  );
  my $rv = _run( $dir, '-bump-subversion', '2>&1' );
  ok_newlines( "$name -bump-subversion", %newlines );
  like(
    $rv->{output},
    qr/version 1\.3 does not have 'subversion' component/,
    "-bump- with missing component has useful error",
  );
}

mkpath( File::Spec->catfile( $dir, "lib" ) );

with_file(
  "META.yml", <<'END',
---
bar: 2
version: 1.2.3
meta-spec:
  url: whatever
  version: 1.3
END
  sub { runtests( META => '1.2.3' ) },
);

# weirdly indented but still valid
with_file(
  "META.yml", <<'END',
---
   bar: 2
   version: 7.8.9
   meta-spec:
     url: whatever
     version: 1.3
END
  sub { runtests( META => '7.8.9' ) },
);

with_file(
  "lib/Foo_pod.pm", <<'END',
=head1 VERSION

Version 2.4.6

=cut
END
  sub { runtests( pod => "2.4.6" ) },
);

with_file(
  "Foo.pm", <<'END',
package Foo;
our $VERSION = '3.6.9';
1;
END
  sub { runtests( pm => "3.6.9" ) },
);

with_file(
  "Foo.pm", <<'END',
package Foo;
our $VERSION = version->declare('v7.6.5');
1;
END
  sub {
    is_deeply( find( $dir ), { found => 'v7.6.5' }, "found in pm" );
    _run( $dir, '-set', '7.7' );
    _run( $dir, '-bump' );
    is_deeply( find( $dir ), { found => 'v7.8' }, "bump subversion with v prefix" );
  },
);

with_file(
  "Foo.pm", <<'END',
package Foo v1.2.3;
1;
END
  sub {
    is_deeply( find( $dir ), { found => 'v1.2.3' }, "package-v-string found in pm" );
    _run( $dir, '-set', '1.2' );
    is_deeply( find( $dir ), { found => 'v1.2' }, "set version keeps v prefix" );
    _run( $dir, '-bump' );
    is_deeply( find( $dir ), { found => 'v1.3' }, "bump subversion with v prefix" );
  },
);

with_file(
  "Foo.pm", <<'END',
package Foo 1.0;
1;
END
  sub {
    is_deeply( find( $dir ), { found => '1.0' }, "package version found in pm" );
    _run( $dir, '-set', '1.2' );
    _run( $dir, '-bump' );
    is_deeply( find( $dir ), { found => '1.3' }, "bump version without v prefix" );
  },
);

with_file(
  README => <<'END',
This README describes version 5.4.6 of Flurble.
END
  sub { runtests( plain => "5.4.6" ) },
);

with_file(
  README => "This README describes\x{0d}\x{0a}version 1.2.3 of\x{0d}\x{0a}Flurble.\x{0a}",
  sub { runtests( newlines => "1.2.3" ) },
);

with_file(
  "FooBar.pm", <<'END',
package FooBar 1.0;
1;
END
  sub {
    is_deeply( find( $dir ), { found => '1.0' }, "package version found in pm" );

    my $previous = '1.0';
    foreach my $v ( qw(1.000005 1.000005_001 1.000005_01 1.000005 1.000005_02 1.000005_002 1.000005_003) ) {
        _run( $dir, '-set', $v, '-numify' );
        is_deeply( find( $dir ), { found => $v }, "set version from $previous to $v" );
        $previous = $v;
    }
  },
);

with_file(
  "FooBar.pm", <<'END',
package FooBar 1.0;
1;
END
  sub {
    is_deeply( find( $dir ), { found => '1.0' }, "package version found in pm" );
    my @table = (
        [ qw( 1.000005 1.000006 ) ],
        [ qw( 1.000005_001 1.000005_002 ) ],
        [ qw( 1.000005_01 1.000005_02 ) ],
    );

    foreach my $row ( @table ) {
        _run( $dir, '-set', $row->[0], '-numify' );
        is_deeply( find( $dir ), { found => $row->[0] }, "set version to $row->[0]" );
        _run( $dir, '-bump' );
        is_deeply( find( $dir ), { found => $row->[1] }, "bump version from $row->[0] to $row->[1]" );
    }
  },
);
done_testing();