File: git.t

package info (click to toggle)
libmath-bigint-gmp-perl 1.7003-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,016 kB
  • sloc: pascal: 3,998; perl: 280; makefile: 9; sh: 1
file content (284 lines) | stat: -rw-r--r-- 7,909 bytes parent folder | download | duplicates (3)
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# -*- mode: perl; -*-

use strict;
use warnings;

use IO::Pipe;
use IO::File;
use IO::Dir;

use File::Which    qw< which >;
use Sort::Versions qw< versioncmp >;

$| = 1;

my $testno = 0;
my $failno = 0;

END {
    print "1..$testno\n";
    exit $failno == 0 ? 0 : 1;
}

################################################################################
# See if we can find a "git" program.
################################################################################

my $git = which('git');

print "not " unless $git;
print "ok ", ++$testno, " - found 'git' program";
print " ('$git')" if $git;
print "\n";

# There is no point in continuing without the 'git' program.

unless ($git) {
    print STDERR "#   can't continue testing without a 'git' program\n";
    exit 1;
}

################################################################################
# See if we can get the "git" version number.
################################################################################

my $gitver;
{
    my $pipe = IO::Pipe -> new();
    my @args = ('git --version 2>/dev/null');
    $pipe -> reader(@args);
    my $output = <$pipe>;
    ($gitver) = $output =~ /^git version (\S+)/
      if defined $output;
}

unless ($gitver) {
    print "not ";
    $failno++;
}
print "ok ", ++$testno, " - found 'git' version number";
print " ('$gitver')" if $gitver;
print "\n";

################################################################################
# See if the working directory is clean.
################################################################################

{
    my $pipe = IO::Pipe -> new();
    my @args = ('git', 'status', '--porcelain');
    $pipe -> reader(@args);
    my @output = <$pipe>;
    my $ok = @output == 0;
    unless ($ok) {
        print "not ";
        $failno++;
    }
    print "ok ", ++$testno, " - working directory is clean\n";
    unless ($ok) {
        print STDERR "#   expected no output from '@args', but got:\n\n",
          map("    $_", @output), "\n";
    }
}

################################################################################
# Search for changelog files.
################################################################################

my @files;

my $dh = IO::Dir -> new('.')
  or die "can't open the current directory for reading: $!";

for my $filename ($dh -> read()) {

    # changelog, changelog.txt, changes, changes.log, changes.txt changes.log
    next unless $filename =~ / ^
                               (
                                   changelog ( \. txt )?
                               |
                                   changes ( \. ( log | txt ) )?
                               )
                               $
                             /ix && -f $filename;
    my @info = stat(_);
    my ($dev, $ino, $size) = @info[0, 1, 7];
    push @files, $filename;
}

$dh -> close()
  or die "can't close directory after reading: $!";

unless (@files) {
    print "not ";
    $failno++;
}
print "ok ", ++$testno, " - found changelog file(s)";
print " (", join(", ", map("'$_'", @files)), ")" if @files;
print "\n";

################################################################################
# Read all changelog files, and sort all version numbers.
################################################################################

my @vers = ();

for (my $i = 0 ; $i <= $#files ; $i++) {
    my $filename = $files[$i];

    my $fh = IO::File -> new($filename)
      or die "$filename: can't open file for reading: $!\n";

    while (defined(my $line = <$fh>)) {
        if ($line =~ /^(\S+)/) {
            my $verstr = $1;
            if ($verstr =~ / ^ v? ( \d+ ( \. \d+ ( _ \d+ )* )? ) $ /ix) {
                my $vernum = $1;
                $vernum =~ tr/_//d;
                push @vers, [ $verstr, $vernum ];
                last;                   # only get the first one
            } else {
                printf STDERR "  Ignoring version number '%s' in %s line %u\n",
                  $verstr, $filename, $.;
            }
        }
    }

    $fh -> close()
      or die "$filename: can't close file after reading: $!\n";
}

# Sort the versions.

#@vers = sort { versioncmp($a, $b) } @vers;

unless (@vers) {
    print "not ";
    $failno++;
}
print "ok ", ++$testno, " - found version number(s) in changelog file(s)";
print " ('$vers[0][0]')" if @vers;
print "\n";

################################################################################
# Get the all the git tags that look like version numbers.
################################################################################

my @tags;
{
    my $pipe = IO::Pipe -> new();
    my @args = ('git', 'tag', '-l');
    $pipe -> reader(@args);
    while (defined(my $tag = <$pipe>)) {
        $tag =~ s/\s+\z//;
        if ($tag =~ / ^ v? ( \d+ ( \. \d+ ( _ \d+ )* )? ) /ix) {
            my $vernum = $1;
            $vernum =~ tr/_//d;
            push @tags, [ $tag, $vernum ];
        }
    }
    $pipe -> close() or die "can't close pipe after reading: $!";
}

# Sort the tags.

#@tags = sort { versioncmp($b, $b) } @tags;
@tags = sort { $b -> [1] <=> $a -> [1] } @tags;

unless (@tags) {
    print "not ";
    $failno++;
}
print "ok ", ++$testno, " - found git tag(s)";
print " ('", $tags[0][0], "')" if @tags;
print "\n";

################################################################################
# Compare version number with git tag.
################################################################################

++$testno;
if (@vers and @tags) {
    my $ok = $vers[0][0] eq $tags[0][0];
    unless ($ok) {
        print "not ";
        $failno++;
    }
    print "ok ", $testno, " - changelog version matches git tag\n";
    print STDERR <<"EOF" unless $ok;
#   latest version in changelog(s): $vers[0][0]
#                   latest git tag: $tags[0][0]
EOF
} else {
    print "ok ", $testno, " - skipped (missing version number or git tag)\n";
}

################################################################################
# See if the commit corresponding to the most recent tag is also the most
# recent commit.
################################################################################

# Get the commit corresponding to the most recent tag.

my $commit_tagged;

++$testno;
if (@tags) {
    my $pipe = IO::Pipe -> new();
    my @args = ('git', 'rev-parse', $tags[0][0]);
    $pipe -> reader(@args);
    $commit_tagged = <$pipe>;
    chomp $commit_tagged if defined $commit_tagged;

    unless ($commit_tagged) {
        print "not ";
        $failno++;
    }
    print "ok ", $testno, " - tag ('$tags[0][0]') refers to a commit";
    print " ('$commit_tagged')" if $commit_tagged;
    print "\n";
} else {
    print "ok ", $testno, " - skipped (no tags found)\n";
}

# Get the most recent commit.

my $commit_newest;

{
    my $pipe = IO::Pipe -> new();
    my @args = ('git', 'log', '-n', '1', '--pretty=format:%H');
    $pipe -> reader(@args);
    $commit_newest = <$pipe>;
    chomp $commit_newest if defined $commit_newest;;
}

unless ($commit_newest) {
    print "not ";
    $failno++;
}
print "ok ", ++$testno, " - found most recent commit";
print " ('$commit_newest')" if $commit_newest;
print "\n";

print STDERR "#   no commits found\n" unless $commit_newest;

################################################################################
# Compare the two commits.
################################################################################

++$testno;
if (defined $commit_tagged and defined $commit_newest) {
    my $ok = $commit_tagged eq $commit_newest;
    unless ($ok) {
        print "not ";
        $failno++;
    }
    print "ok ", $testno, " - the tagged commit is also the most recent commit\n";
    print STDERR <<"EOF" unless $ok;
#   newest commit: $commit_newest
#   tagged commit: $commit_tagged
EOF
} else {
    print "ok ", $testno, " - skipped (missing commit(s))\n";
}