This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
t/porting/cmp_version.t: add version skip facility
[perl5.git] / t / porting / cmp_version.t
1 #!./perl -w
2
3 #
4 # Compare the current Perl source tree and a given tag for modules that
5 # have identical version numbers but different contents.
6 #
7 # Original by slaven@rezic.de, modified by jhi and matt.w.johnson@gmail.com
8 #
9 # Adapted from Porting/cmpVERSION.pl by Abigail
10 #
11
12 BEGIN {
13     chdir '..' unless -d 't';
14     unshift @INC, 'lib', 'Porting';
15 }
16
17 use strict;
18 use warnings;
19 use version;
20 use ExtUtils::MakeMaker;
21 use File::Compare;
22 use File::Find;
23 use File::Spec::Functions qw(rel2abs abs2rel catfile catdir curdir);
24 use Getopt::Std;
25 use Maintainers;
26
27 if (! -d '.git' ) {
28     print "1..0 # SKIP: not being run from a git checkout\n";
29     exit 0;
30 }
31
32 #
33 # Thanks to David Golden for this suggestion.
34 #
35 my $tag_to_compare = `git describe --abbrev=0`;
36 chomp $tag_to_compare;
37 my $source_dir = '.';
38
39 my $null = $^O eq 'MSWin32' ? 'nul' : '/dev/null';
40
41 my $tag_exists = `git --no-pager tag -l $tag_to_compare 2>$null`;
42 chomp $tag_exists;
43
44
45 if ($tag_exists ne $tag_to_compare) {
46     print "1..0 # SKIP: '$tag_to_compare' is not a known Git tag\n";
47     exit 0;
48 }
49
50
51 my %dual_files;
52 for my $m (grep $Maintainers::Modules {$_} {CPAN}, keys %Maintainers::Modules) {
53     $dual_files{$_} = 1 for Maintainers::get_module_files ($m);
54 }
55
56
57 # Files to skip from the check for one reason or another,
58 # usually because they pull in their version from some other file.
59 my %skip;
60 @skip{
61     'lib/Carp/Heavy.pm',
62     'lib/Config.pm',            # no version number but contents will vary
63     'lib/Exporter/Heavy.pm',
64     'win32/FindExt.pm',
65 } = ();
66
67 # Files to skip just for particular version(s),
68 # usually due to some # mix-up
69
70 my %skip_versions = (
71     # 'some/sample/file.pm' => [ '1.23', '1.24' ],
72 );
73
74 my $skip_dirs = qr{^(?:t/lib|cpan)};
75
76 my @all_diffs = `git --no-pager diff --name-only $tag_to_compare`;
77 chomp @all_diffs;
78
79 my @tmp_diffs = grep {
80     my $this_dir;
81     $this_dir = $1 if m/^(.*)\//;
82     /\.pm$/ &&
83     (!defined($this_dir) || ($this_dir !~ $skip_dirs)) &&
84     !exists $skip{$_};
85 } @all_diffs;
86 my   @module_diffs =  grep {!exists $dual_files {$_}} @tmp_diffs;
87 push @module_diffs => grep { exists $dual_files {$_}} @tmp_diffs;
88
89 unless (@module_diffs) {
90     print "1..1\n";
91     print "ok 1 - No difference found\n";
92     exit;
93 }
94
95 my (@output_files, @output_diffs);
96
97 printf "1..%d\n" => scalar @module_diffs;
98
99 my $count = 0;
100 my @diff;
101 foreach my $pm_file (@module_diffs) {
102     @diff = ();
103     (my $xs_file = $pm_file) =~ s/\.pm$/.xs/;
104     my $pm_eq = compare_git_file($pm_file, $tag_to_compare);
105     next unless defined $pm_eq;
106     my $xs_eq = 1;
107     if (-e $xs_file) {
108         $xs_eq = compare_git_file($xs_file, $tag_to_compare);
109         next unless defined $xs_eq;
110     }
111     next if ($pm_eq && $xs_eq);
112     my $pm_version = eval {MM->parse_version($pm_file)};
113     my $orig_pm_content = get_file_from_git($pm_file, $tag_to_compare);
114     my $orig_pm_version = eval {MM->parse_version(\$orig_pm_content)};
115     next if ( ! defined $pm_version || ! defined $orig_pm_version );
116     next if ( $pm_version eq 'undef' || $orig_pm_version eq 'undef' ); # sigh
117     next if $pm_version ne $orig_pm_version;
118     next if exists $skip_versions{$pm_file}
119          and grep $pm_version eq $_, @{$skip_versions{$pm_file}};
120     push @diff => $pm_file unless $pm_eq;
121     push @diff => $xs_file unless $xs_eq;
122 }
123 continue {
124     if (@diff) {
125         foreach my $diff (@diff) {
126             print "# $_" for `git --no-pager diff $tag_to_compare '$diff'`;
127         }
128         printf "not ok %d - %s\n" => ++ $count, $pm_file;
129     }
130     else {
131         printf "ok %d - %s\n" => ++ $count, $pm_file;
132     }
133 }
134
135 exit;
136
137 sub compare_git_file {
138     my ($file, $tag) = @_;
139     open(my $orig_fh, "-|", "git --no-pager show $tag:$file 2>$null");
140     return undef if eof($orig_fh);
141     my $is_eq = compare($file, $orig_fh) == 0;
142     close($orig_fh);
143     return $is_eq;
144 }
145
146 sub get_file_from_git {
147     my ($file, $tag) = @_;
148     local $/ = undef;
149     my $file_content = `git --no-pager show $tag:$file 2>$null`;
150     return $file_content;
151 }