summaryrefslogtreecommitdiffstats
path: root/lib/Test/Lintian/Hooks.pm
blob: 4c8d848d99744df985ee86d2371c30772eda37fe (plain)
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
# Copyright (C) 2018 Felix Lechner
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, you can find it on the World Wide
# Web at https://www.gnu.org/copyleft/gpl.html, or write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301, USA

package Test::Lintian::Hooks;

=head1 NAME

Test::Lintian::Hooks -- hook routines for the test runners

=head1 SYNOPSIS

  use Test::Lintian::Hooks qw(sed_hook);
  sed_hook('script.sed', 'input.file');

=head1 DESCRIPTION

Various hook routines for the test runners.

=cut

use v5.20;
use warnings;
use utf8;

use Exporter qw(import);

BEGIN {
    our @EXPORT_OK = qw(
      sed_hook
      sort_lines
      calibrate
      find_missing_prerequisites
    );
}

use Capture::Tiny qw(capture_merged);
use Carp;
use Const::Fast;
use Cwd qw(getcwd);
use File::Basename;
use File::Find::Rule;
use File::Path;
use File::stat;
use IPC::Run3;
use List::SomeUtils qw(any);
use Path::Tiny;
use Unicode::UTF8 qw(encode_utf8 decode_utf8);

const my $NEWLINE => qq{\n};
const my $WAIT_STATUS_SHIFT => 8;

=head1 FUNCTIONS

=over 4

=item sed_hook(SCRIPT, SUBJECT, OUTPUT)

Runs the parser sed on file SUBJECT using the instructions in SCRIPT
and places the result in the file OUTPUT.

=cut

sub sed_hook {
    my ($script, $path, $output) = @_;

    croak encode_utf8("Parser script $script does not exist.")
      unless -e $script;

    my @command = (qw{sed -r -f}, $script, $path);
    my $bytes;
    run3(\@command, \undef, \$bytes);
    my $status = ($? >> $WAIT_STATUS_SHIFT);

    croak encode_utf8("Hook failed: sed -ri -f $script $path > $output: $!")
      if $status;

    # already in bytes
    path($output)->spew($bytes);

    croak encode_utf8("Did not create parser output file $output.")
      unless -e $output;

    return $output;
}

=item sort_lines(UNSORTED, SORTED)

Sorts the file UNSORTED line by line and places the result into the
file SORTED.

=cut

sub sort_lines {
    my ($path, $sorted) = @_;

    open(my $rfd, '<', $path)
      or croak encode_utf8("Could not open pre-sort file $path: $!");
    my @lines = sort map { decode_utf8($_) } <$rfd>;
    close $rfd
      or carp encode_utf8("Could not close open pre-sort file $path: $!");

    open(my $wfd, '>', $sorted)
      or croak encode_utf8("Could not open sorted file $sorted: $!");
    print {$wfd} encode_utf8($_) for @lines;
    close $wfd
      or carp encode_utf8("Could not close sorted file $sorted: $!");

    return $sorted;
}

=item calibrate(SCRIPT, ACTUAL, EXPECTED, CALIBRATED)

Executes calibration script SCRIPT with the three arguments EXPECTED,
ACTUAL and CALIBRATED, all of which are file paths. Please note that
the order of arguments in this function corresponds to the
bookkeeping logic of ACTUAL vs EXPECTED. The order for the script is
different.

=cut

sub calibrate {
    my ($hook, $actual, $expected, $calibrated) = @_;

    if (-x $hook) {
        system($hook, $expected, $actual, $calibrated) == 0
          or croak encode_utf8("Hook $hook failed on $actual: $!");
        croak encode_utf8("No calibrated hints created in $calibrated")
          unless -e $calibrated;
        return $calibrated;
    }
    return $expected;
}

=item find_missing_prerequisites(TEST_CASE)

Returns a string with missing dependencies, if applicable, that would
be necessary to run the test described by hash DESC.

=cut

sub find_missing_prerequisites {
    my ($testcase) = @_;

    # without prerequisites, no need to look
    return undef
      unless any { $testcase->declares($_) }
      qw(Build-Depends Build-Conflicts Test-Depends Test-Conflicts);

    # create a temporary file
    my $temp = Path::Tiny->tempfile(
        TEMPLATE => 'lintian-test-build-depends-XXXXXXXXX');
    my @lines;

    # dpkg-checkbuilddeps requires a Source: field
    push(@lines, 'Source: bd-test-pkg');

    my $build_depends = join(
        ', ',
        grep { length }(
            $testcase->value('Build-Depends'),$testcase->value('Test-Depends')
        )
    );

    push(@lines, "Build-Depends: $build_depends")
      if length $build_depends;

    my $build_conflicts = join(
        ', ',
        grep { length }(
            $testcase->value('Build-Conflicts'),
            $testcase->value('Test-Conflicts')
        )
    );
    push(@lines, "Build-Conflicts: $build_conflicts")
      if length $build_conflicts;

    $temp->spew_utf8(join($NEWLINE, @lines) . $NEWLINE);

    # run dpkg-checkbuilddeps
    my $command = "dpkg-checkbuilddeps $temp";
    my ($missing, $status) = capture_merged { system($command); };
    $status >>= $WAIT_STATUS_SHIFT;

    $missing = decode_utf8($missing)
      if length $missing;

    die encode_utf8("$command failed: $missing")
      if !$status && length $missing;

    # parse for missing prerequisites
    if ($missing =~ s{\A dpkg-checkbuilddeps: [ ] (?:error: [ ])? }{}xsm) {
        $missing =~ s{Unmet build dependencies}{Unmet}gi;
        chomp($missing);
        # expect exactly one line.
        die encode_utf8("Unexpected output from dpkg-checkbuilddeps: $missing")
          if $missing =~ s{\n}{\\n}gxsm;
        return $missing;
    }

    return undef;
}

=back

=cut

1;

# Local Variables:
# indent-tabs-mode: nil
# cperl-indent-level: 4
# End:
# vim: syntax=perl sw=4 sts=4 sr et