Install/METArequires.pm

filter: Statementstmt 27.1% 22 / 81 Branchbran 0.0% 0 / 24 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 54.5% 6 / 11 Podpod 100% 5 / 5 totaltotal 27.2% 33 / 121 CC 13 SCAR 34.8
CC
13
Cov
21%
install
72.0
t
30.0
cpan_i_timeout
6.0
CRAP 32.5
↑ Summary iMETAr.pm »
1 package Test::Install::METArequires;
2  
3 =head1 NAME
4  
5 Test::Install::METArequires - TAP output of installing requires listed in META.yml
6  
7 =head1 SYNOPSIS
8  
9 use Test::Install::METArequires;
10 Test::Install::METArequires->t();
11  
12 prove -Ilib `perl -MTest::iMETAr -le 'print Test::iMETAr->t_file'` t/
13  
14 =head1 DESCRIPTION
15  
16 Reads F<META.yml> in current folder and tries to install any of the
17 C<configure_requires>, C<build_requires>, C<requires> modules listed
18 there. Output is TAP - one test line (OK/FAIL) per module with note output.
19  
20 =cut
21  
22 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 1 ▶use YAML::Syck ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use IPC::Run 'run', 'timeout';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use Test::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use File::Basename 'fileparse';
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 1 our $VERSION = '0.03';
31  
32 1 my $tb = Test::Builder->new();
33  
34 =head1 METHODS
35  
36 =head2 t
37  
38 Install requires listed in META.yml and output results as TAP.
39  
40 =cut
41  
42 0 ▶sub t {
Subroutine t: not called CC=5 SCAR=34.0
Pod documented
43 0 my $self = shift;
44
45 0 my $meta = eval { YAML::Syck::LoadFile('META.yml') };
46 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
47 0 $tb->plan( skip_all => $@ );
48 0 return;
49 }
50
51 0 my %configure_requires = eval { %{$meta->{'configure_requires'}} };
52 0 my %build_requires = eval { %{$meta->{'build_requires'}} };
53 0 my %requires = eval { %{$meta->{'requires'}} };
54
55 0 $tb->plan('tests' => ((keys %requires) + (keys %build_requires) + (keys %configure_requires)));
56
57 0 $tb->note('--- configure_requires ---');
58 0 ▶ while (my ($module, $version) = each %configure_requires) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %configure_requires) 0 0 0
59 0 $self->install($module, $version);
60 }
61 0 $tb->note('--- build_requires ---');
62 0 ▶ while (my ($module, $version) = each %build_requires) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %build_requires) 0 0 0
63 0 $self->install($module, $version);
64 }
65 0 $tb->note('--- requires ---');
66 0 ▶ while (my ($module, $version) = each %requires) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %requires) 0 0 0
67 0 $self->install($module, $version);
68 }
69 }
70  
71 =head2 install($module, $version)
72  
73 Do C<cpan -i $module> if the required C<$version> is not jet
74 installed.
75  
76 If L<dbedia::Debian> is loaded. It will install modules from Debian packages
77 if available. Or at least module dependencies if higher version is required
78 than it is available in Debian.
79  
80 Set C<$ENV{IMETAR_SUDO}> to true if `sudo` has to be executed before each
81 C<cpan> or C<apt-get> commands.
82  
83 =cut
84  
85 0 ▶sub install {
Subroutine install: not called CC=8 SCAR=42.8
Pod documented
86 0 my $self = shift;
87 0 my $module = shift;
88 0 my $version = shift;
89
90 0 my $test_name = $module.' version >='.$version;
91  
92 # skip installing modules already in the system
93 0 eval "use $module $version;";
94 0 ▶ if (not $@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 0 0
95 0 $tb->skip('already have '.$test_name);
96 0 return;
97 }
98
99 # use `sudo ...` if required
100 0 my @sudo = ();
101 push @sudo, 'sudo'
102 0 ▶ if $ENV{IMETAR_SUDO};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"IMETAR_SUDO"} 0 0 0
103
104 # install module
105 0 my ($in, $out, $dep_out) = ('', '', '');
106 0 my @run = (@sudo, 'cpan', '-i', $module);
107  
108 # install Debian package if there is one and dbedia::Debian is loaded
109 0 ▶ if ($INC{'dbedia/Debian.pm'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{"dbedia/Debian.pm"}) 0 0 0
110 0 my $package_name = dbedia::Debian->find_perl_module_package($module, $version);
111 0 ▶ if ($package_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package_name) { } 0 0 0
112 0 @run = (@sudo, 'apt-get', 'install', '--yes', $package_name);
113 }
114 # if not found for that version try to find an old version and install build dependecies
115 else {
116 0 $package_name = dbedia::Debian->find_perl_module_package($module);
117 0 ▶ run [ @sudo, 'apt-get', 'build-dep', '--yes', $package_name ], \$in, \$dep_out, \$dep_out, timeout( $self->cpan_i_timeout )
Branch: 1 branch
BranchTrueFalseTotal
if $package_name 0 0 0
118 if ($package_name);
119 }
120 }
121
122 0 ▶ if (run [ @run ], \$in, \$out, \$out, timeout( $self->cpan_i_timeout )) {
Branch: 1 branch
BranchTrueFalseTotal
if (run([@run], \$in, \$out, \$out, timeout($self->cpan_i_timeout))) { } 0 0 0
123 0 eval "use $module $version;";
124 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
125 0 $tb->ok(0, $test_name);
126 0 $tb->note($dep_out.$out);
127 0 $tb->note($@);
128 }
129 else {
130 0 $tb->ok(1, $test_name);
131 }
132 }
133 else {
134 0 $tb->ok(0, $test_name.' - timeout');
135 0 $tb->note($out);
136 }
137 }
138  
139 =head2 cpan_i_timeout
140  
141 Set/get timeout for one `cpan -i ...` execution. Default is 1h.
142  
143 =cut
144  
145 1 my $cpan_i_timeout = 60*60; # default timeout is 1h
146 0 ▶sub cpan_i_timeout {
Subroutine cpan_i_timeout: not called CC=2 SCAR=17.9
Pod documented
147 0 my $self = shift;
148  
149 0 ▶ $cpan_i_timeout = shift @_
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 0 0 0 0
150 if (@_ > 0);
151  
152 0 return $cpan_i_timeout;
153 }
154  
155 =head2 t_file
156  
157 Return the location of F<METArequires.t> file.
158  
159 =cut
160  
161 0 ▶sub t_file {
Subroutine t_file: not called CC=1 SCAR=6.9
Pod documented
162 0 my ($filename, $dirname) = fileparse($INC{"Test/Install/METArequires.pm"}, '.pm');
163 0 return $dirname.$filename.'.t';
164 }
165  
166 =head2 t_debian_file
167  
168 Return the location of F<METArequires-Debian.t> file.
169  
170 =cut
171  
172 0 ▶sub t_debian_file {
Subroutine t_debian_file: not called CC=1 SCAR=6.9
Pod documented
173 0 my ($filename, $dirname) = fileparse($INC{"Test/Install/METArequires.pm"}, '.pm');
174 0 return $dirname.$filename.'-Debian.t';
175 }
176  
177 1 1;
178  
179  
180 __END__
↑ Summary iMETAr.pm »