File: TestAPI.pm

package info (click to toggle)
libtest-needs-perl 0.002010-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 144 kB
  • sloc: perl: 551; makefile: 2
file content (268 lines) | stat: -rw-r--r-- 6,413 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
256
257
258
259
260
261
262
263
264
265
266
267
268
package TestAPI;
use strict;
use warnings;
use Test::More;
use IPC::Open3;
use Config ();

my @perl = $^X =~ /(.*)/s;

{
  my $taint;
  local $^W = 0;
  local $@;
  local $SIG{__DIE__};
  local $SIG{__WARN__} = sub {
    $taint = 1;
    push @perl, '-t';
  };
  if (!eval { eval substr($ENV{PATH}, 0, 0); 1 }) {
    $taint = 1;
    push @perl, '-T';
  }
  if ($taint) {
    $ENV{PATH} = '/';
    delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
  }
}

my %inc;
{
  my $cmd = join ' ', map qq{"$_"}, @perl, '-le', 'print for @INC';
  my @inc = `$cmd`;
  chomp @inc;
  @inc{@inc} = ();
}
my @need_inc;
my $i = -1;
push @need_inc, $INC[$i]
  while ++$i < @INC && !exists $inc{$INC[$i]};

my $inc_extension = qr/[\\\/]\Q$Config::Config{archname}\E|[\\\/]\Q$Config::Config{version}\E(?:[\\\/]\Q$Config::Config{archname}\E)?/;

my $bare;
for my $path (@need_inc) {
  unless (defined $bare and $path =~ /\A\Q$bare\E$inc_extension\z/) {
    $bare = $path;
    push @perl, "-I$path";
  }
}

push @perl, "-It/lib";

my $missing = "Module::Does::Not::Exist::".time;

sub capture {
  my $pid = open3 my $stdin, my $stdout, undef, @_
    or die "can't run @_: $!";
  my $out = do { local $/; <$stdout> };
  close $stdout;
  waitpid $pid, 0;
  my $exit = $?;
  $out =~ s{^Possible precedence issue with control flow operator at .*Test/Builder\.pm.*\n}{};
  return wantarray ? ($exit, $out) : $exit;
}

BEGIN {
  *note = sub { print '# '.join('', @_)."\n" }
    if !defined &note;
}

sub check {
  my ($load, $args, $match, $name) = @_;
  my ($label, @load) = @$load;
  my @args = ((map "--load=$_", @load), @$args);
  my ($exit, $out)
    = capture @perl, '-MTestScript' . (@args ? '='.join(',', @args) : '');
  $name = "$label: $name";
  my $want_exit;
  my $unmatch;
  if (ref $match eq 'HASH') {
    $want_exit = $match->{exit};
    $unmatch = $match->{unmatch};
    $match = $match->{match};
  }
  $match   = !defined $match    ? [] : ref $match eq 'ARRAY'    ? $match    : [$match];
  $unmatch = !defined $unmatch  ? [] : ref $unmatch eq 'ARRAY'  ? $unmatch  : [$unmatch];
  if (!is $exit == 0, !$want_exit, "$name - exit status") {
    ok 0, $name
      for 1, 0 .. $#$match, 0 .. $#$unmatch;
    diag "Exit status $exit\nOutput:\n$out";
    return;
  }

  my $plan_count = () = $out =~ /^[0-9]+\.\.[0-9]+(?: |$)/mg;
  if (!ok $plan_count <= 1, "$name - no excess plans") {
    ok 0, $name
      for 0 .. $#$match, 0 .. $#$unmatch;
    diag "Output:\n$out";
    return;
  }

  for my $m (@$match) {
    like $out, $m, $name;
  }
  for my $um (@$unmatch) {
    unlike $out, $um, $name;
  }
}

sub test_api {
  my ($label, @load) = @_;
  local $ENV{RELEASE_TESTING};
  delete $ENV{RELEASE_TESTING};

  my @using = map {
    my ($e, $o) = capture @perl, "-m$_", "-eprint+$_->VERSION";
    $e ? () : "$_ $o";
  } @load;

  if (@using != @load) {
    plan skip_all => "$label not available";
    return;
  }

  plan tests => ( 14*3 + 1*4 ) * !!@load + (7*3 + 1*4);

  note "Checking against ".join(', ', @using)."\n"
    if @using;

  check([@_],
    [$missing],
    qr/^1\.\.0 # SKIP/i,
    'Missing module SKIPs',
  );
  check([@_],
    ['BrokenModule'],
    { match => qr/syntax error/, exit => 1 },
    'Broken module dies',
  );
  check([@_],
    ['ModuleWithVersion'],
    qr/^(?!1\.\.0 # SKIP)/i,
    'Working module runs',
  );
  check([@_],
    ['ModuleWithVersion', 2],
    qr/^1\.\.0 # SKIP/i,
    'Outdated module SKIPs',
  );

  {
    local $ENV{RELEASE_TESTING} = 1;
    check([@_],
      [$missing],
      { match => qr/^not ok/m, exit => 1 },
      'Missing module fails with RELEASE_TESTING',
    );
    check([@_],
      ['BrokenModule'],
      { match => qr/syntax error/, exit => 1 },
      'Broken module dies with RELEASE_TESTING',
    );
    check([@_],
      ['ModuleWithVersion'],
      qr/^(?!1\.\.0 # SKIP)/i,
      'Working module runs with RELEASE_TESTING',
    );
    check([@_],
      ['ModuleWithVersion', 2],
      { match => qr/^not ok/m, unmatch => qr/Cleaning up the CONTEXT stack/, exit => 1 },
      'Outdated module fails with RELEASE_TESTING',
    );
  }

  return
    unless @load;

  check([@_],
    [$missing, '--plan'],
    qr/# skip/,
    'Missing module skips with plan',
  );
  check([@_],
    [$missing, '--no_plan'],
    qr/# skip/,
    'Missing module skips with no_plan',
  );
  SKIP: {
    skip 'Test::More too old to run tests without plan', 3
      if !Test::More->can('done_testing');
    check([@_],
      [$missing, '--tests'],
      qr/# skip/,
      'Missing module skips with tests',
    );
  }
  check([@_],
    [$missing, '--plan', '--tests'],
    qr/# skip/,
    'Missing module passes with plan and tests',
  );
  check([@_],
    [$missing, '--no_plan', '--tests'],
    qr/# skip/,
    'Missing module passes with no_plan and tests',
  );

  SKIP: {
    skip 'Test::More too old to run subtests', 9*3 + 1*4
      if !Test::More->can('subtest');

    check([@_],
      [$missing, '--subtest'],
      qr/^ +1\.\.0 # SKIP/mi,
      'Missing module skips in subtest',
    );
    check([@_],
      ['BrokenModule', '--subtest'],
      { match => qr/syntax error/, exit => 1 },
      'Broken module dies in subtest',
    );
    check([@_],
      ['ModuleWithVersion', '--subtest'],
      [ qr/^ +1\.\.(?!0 # SKIP)/mi, qr/^ok[^\n#]+(?!# skip)/m ],
      'Working module runs in subtest',
    );
    check([@_],
      ['ModuleWithVersion', 2, '--subtest'],
      qr/^ +1\.\.0 # SKIP/mi,
      'Outdated module skips in subtest',
    );

    check([@_],
      [$missing, '--subtest', '--plan'],
      qr/# skip/,
      'Missing module skips with plan in subtest',
    );
    check([@_],
      [$missing, '--subtest', '--no_plan'],
      qr/# skip/,
      'Missing module skips with no_plan in subtest',
    );
    check([@_],
      [$missing, '--subtest', '--tests'],
      qr/# skip/,
      'Missing module skips with tests in subtest',
    );
    check([@_],
      [$missing, '--subtest', '--plan', '--tests'],
      qr/# skip/,
      'Missing module passes with plan and tests in subtest',
    );
    check([@_],
      [$missing, '--subtest', '--no_plan', '--tests'],
      qr/# skip/,
      'Missing module passes with no_plan and tests in subtest',
    );

    local $ENV{RELEASE_TESTING} = 1;
    check([@_],
      [$missing, '--subtest'],
      { match => qr/^ +not ok/m, exit => 1 },
      'Missing module fails in subtest with RELEASE_TESTING',
    );
  }
}

1;