Debian Codegraph @

xt/99-test-prerequisites.t

raw
1 #!perl -w
2
3 use warnings;
4 use strict;
5 use Test::More;
6 use Data::Dumper;
7 use File::Find;
8
9 =head1 DESCRIPTION
10
11 This test checks whether all tests still pass when the optional test
12 prerequisites for the test are not present.
13
14 This is done by using L<Test::Without::Module> to rerun the test while excluding
15 the optional prerequisite.
16
17 =cut
18
19 BEGIN {
20 eval {
21 require CPAN::Meta::Prereqs;
22 require Parse::CPAN::Meta;
23 require Perl::PrereqScanner::Lite;
24 require Module::CoreList;
25 require Test::Without::Module;
26 require Capture::Tiny;
27 Capture::Tiny->import('capture');
28 require Path::Class;
29 Path::Class->import('dir');
30 };
31 if (my $err = $@) {
32 warn "# $err";
33 plan skip_all => "Prerequisite needed for testing is missing";
34 exit 0;
35 };
36 };
37
38 my @tests;
39 if( @ARGV ) {
40 @tests = @ARGV;
41 } else {
42 open my $manifest, '<', 'MANIFEST'
43 or die "Couldn't read MANIFEST: $!";
44 @tests = grep { -f $_ } grep { m!^(t/.*\.t|scripts/.*\.pl)$! } map { s!\s*$!!; $_ } <$manifest>
45 }
46 plan tests => 0+@tests;
47
48 my $meta = Parse::CPAN::Meta->load_file('META.json');
49
50 # Find what META.* declares
51 my $explicit_test_prereqs = CPAN::Meta::Prereqs->new( $meta->{prereqs} )->merged_requirements->as_string_hash;
52 my $minimum_perl = $meta->{prereqs}->{runtime}->{requires}->{perl} || 5.006;
53
54 sub distributed_packages {
55 my @modules;
56 for( @_ ) {
57 dir($_)->recurse( callback => sub {
58 my( $child ) = @_;
59 if( !$child->is_dir and $child =~ /\.pm$/) {
60 push @modules, ((scalar $child->slurp()) =~ m/^\s*package\s+(?:#.*?\n\s+)*(\w+(?:::\w+)*)\b/msg);
61 }
62 });
63 };
64 map { $_ => $_ } @modules;
65 }
66
67 # Find what we distribute:
68 my %distribution = distributed_packages('blib','t');
69
70 my $scanner = Perl::PrereqScanner::Lite->new;
71 for my $test_file (@tests) {
72 my $implicit_test_prereqs = $scanner->scan_file($test_file)->as_string_hash;
73 my %missing = %{ $implicit_test_prereqs };
74 #warn Dumper \%missing;
75
76 for my $p ( keys %missing ) {
77 # remove core modules
78 if( Module::CoreList::is_core( $p, undef, $minimum_perl)) {
79 delete $missing{ $p };
80 #diag "$p is core for $minimum_perl";
81 } else {
82 #diag "$p is not in core for $minimum_perl";
83 };
84
85 # remove explicit (test) prerequisites
86 for my $k (keys %$explicit_test_prereqs) {
87 delete $missing{ $k };
88 };
89 #warn Dumper $explicit_test_prereqs->as_string_hash;
90
91 # Remove stuff from our distribution
92 for my $k (keys %distribution) {
93 delete $missing{ $k };
94 };
95 }
96
97 # If we have no apparent missing prerequisites, we're good
98 my @missing = sort keys %missing;
99
100 # Rerun the test without these modules and see whether it crashes
101 my @failed;
102 for my $candidate (@missing) {
103 diag "Checking that $candidate is not essential";
104 my @cmd = ($^X, "-MTest::Without::Module=$candidate", "-Mblib", '-w', $test_file);
105 my $cmd = join " ", @cmd;
106
107 my ($stdout, $stderr, $exit) = capture {
108 system( @cmd );
109 };
110 if( $exit != 0 ) {
111 push @failed, [ $candidate, [@cmd]];
112 } elsif( $? != 0 ) {
113 push @failed, [ $candidate, [@cmd]];
114 };
115 };
116 is 0+@failed, 0, $test_file
117 or diag Dumper \@failed;
118
119 };
120
121 done_testing;