blib/lib/Test/Smoke/Policy.pm

filter: Statementstmt 66.6% 52 / 78 Branchbran 61.5% 16 / 26 Conditioncond 50.0% 7 / 14 MC/DCmcdc 33.3% 3 / 9 Subroutinesub 90.9% 10 / 11 Podpod 100% 6 / 6 totaltotal 65.2% 94 / 144 CC 20 SCAR 24.3
CC
20
Cov
64%
write
30.0
_read_Policy
15.0
_do_subst
5.2
CRAP 11.4
« blib/lib/Test/Smoke/App/W32Configure.pm ↑ Summary blib/lib/Test/Smoke/App/SendReport.pm »
1 package Test::Smoke::Policy;
2 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 14 our $VERSION = '0.004';
5  
6 14 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 14 ▶use Test::Smoke::LogMixin;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Test::Smoke::Policy - OO interface to handle the Policy.sh stuff.
12  
13 =head1 SYNOPSIS
14  
15 use Test::Smoke::Policy;
16  
17 my $srcpath = File::Spec->updir;
18 my $policy = Test::Smoke::Policy->new( $srcpath );
19  
20 $policy->substitute( [] );
21 $policy->write;
22  
23 =head1 DESCRIPTION
24  
25 I wish I understood what Merijn is doing in the original code.
26  
27 =head1 METHODS
28  
29 =head2 Test::Smoke::Policy->new( $srcpath )
30  
31 Create a new instance of the Policy object.
32 Read the file or take data from the DATA section.
33  
34 =cut
35  
36 4 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
37 4 my $proto = shift;
38 4 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
39  
40 4 my $self = bless { }, $class;
41 4 $self->reset_rules;
42 4 $self->_read_Policy( @_ );
43 4 $self;
44 }
45  
46 =head2 $policy->verbose
47  
48 Get verbosity.
49  
50 =cut
51  
52 8 ▶sub verbose { $_[0]->{v} }
Subroutine verbose: called CC=1 SCAR=0.0
Pod documented
53  
54 =head2 $policy->set_rules( $rules )
55  
56 Set the rules for substitutions.
57  
58 =cut
59  
60 46 ▶sub set_rules {
Subroutine set_rules: called CC=1 SCAR=0.0
Pod documented
61 46 my( $self, $rules ) = @_;
62  
63 46 push @{ $self->{_rules} }, $rules;
64 }
65  
66 =head2 $policy->reset_rules( )
67  
68 Reset the C<_rules> property.
69  
70 =cut
71  
72 56 ▶sub reset_rules {
Subroutine reset_rules: called CC=1 SCAR=0.0
Pod documented
73 56 $_[0]->{_rules} = [ ];
74 56 $_[0]->{_new_policy} = undef;
75 }
76  
77 =head2 $policy->_do_subst( )
78  
79 C<_do_subst()> does the substitutions and stores the substituted version
80 as the B<_new_policy> attribute.
81  
82 =cut
83  
84 32 ▶sub _do_subst {
Subroutine _do_subst: called CC=5 SCAR=16.5
85 32 my $self = shift;
86  
87 32 my %substs;
88 32 foreach my $rule ( @{ $self->{_rules} } ) {
89 40 push @{ $substs{ $rule->[0] } }, $rule->[1];
90 }
91 32 my $policy = $self->{_policy};
92 32 ▶ while ( my( $target, $values ) = each %substs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %substs) 40 32 72
93 40 ▶ unless ( $policy =~ s{^(\s*ccflags=.*?)$target}
Branch: 1 branch
BranchTrueFalseTotal
unless ($policy =~ s/^(\s*ccflags=.*?)$target/$1 . join(" ", grep(($_ && length $_), @$values));/egm) 0 40 40
94 40 ▶ {$1 . join " ",
Condition66%2/3
$_ && length $_
!ll&&!rl&&r
20020
MC/DC50%1/2
$_ && length $_
$_ length $_
Truth table66%2/3
A && B
A= $_B= length $_
ABresult
0X0
100
111
95 grep $_ && length $_ => @$values}meg ) {
96 0 require Carp;
97 0 Carp::carp( "Policy target '$target' failed to match" );
98 }
99 }
100 32 $self->{_new_policy} = $policy;
101 }
102  
103 =head2 $policy->write( )
104  
105 =cut
106  
107 0 ▶sub write {
Subroutine write: not called CC=5 SCAR=34.0
Pod documented
108 0 my $self = shift;
109  
110 0 ▶ defined $self->{_new_policy} or $self->_do_subst;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"_new_policy"} or $self->_do_subst 0 0 0
111  
112 0 local *POL;
113 0 ▶ my $p_name = shift || 'Policy.sh';
Condition0%0/2
shift() || 'Policy.sh'
l!l
00
MC/DC0%0/1
shift() || 'Policy.sh'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
114 0 unlink $p_name; # or carp "Can't unlink '$p_name': $!";
115 0 ▶ if ( open POL, "> $p_name" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open POL, "> $p_name") { } 0 0 0
116 0 print POL $self->{_new_policy};
117 0 ▶ close POL or do {
Branch: 1 branch
BranchTrueFalseTotal
close POL or do { require Carp; Carp::carp("Error rewriting '${p_name}': $!") } 0 0 0
118 0 require Carp;
119 0 Carp::carp( "Error rewriting '$p_name': $!" );
120 };
121 } else {
122 0 require Carp;
123 0 Carp::carp( "Unable to rewrite '$p_name': $!" );
124 }
125 }
126  
127 =head2 $policy->_read_Policy( $srcpath[, $verbose[, @ccflags]] )
128  
129 C<_read_Policy()> checks the C<< $srcpath >> for these conditions:
130  
131 =over 4
132  
133 =item B<Reference to a SCALAR> Policy is in C<$$srcpath>
134  
135 =item B<Reference to an ARRAY> Policy is in C<@$srcpath>
136  
137 =item B<Reference to a GLOB> Policy is read from the filehandle
138  
139 =item B<Other values> are taken as the base path for F<Policy.sh>
140  
141 =back
142  
143 The C<@ccflags> are passed to C<< $self->default_Policy() >>
144  
145 =cut
146  
147 4 ▶sub _read_Policy {
Subroutine _read_Policy: called CC=10 SCAR=27.1
148 4 my( $self, $srcpath, $verbose, @ccflags ) = @_;
149 4 ▶ $srcpath = '' unless defined $srcpath;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $srcpath 2 2 4
150  
151 4 ▶ $self->{v} ||= defined $verbose ? $verbose : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $verbose ? : 2 2 4
Condition66%2/3
$self->{"v"} ||= defined $verbose ? $verbose : 0
l!l&&r!l&&!r
013
MC/DC50%1/2
$self->{"v"} ||= defined $verbose ? $verbose : 0
$self->{"v"} defined $verbose ? $verbose : 0
Truth table66%2/3
A ||= B
A= $self->{"v"}B= defined $verbose ? $verbose : 0
ABresult
1X1
011
000
152 4 my $vmsg = "";
153 4 local *POLICY;
154 4 ▶ if ( ref $srcpath eq 'SCALAR' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $srcpath eq 'SCALAR') { } 1 3 4
elsif (ref $srcpath eq 'ARRAY') { } 0 3 3
elsif (ref $srcpath eq 'GLOB') { } 0 3 3
155  
156 1 $self->{_policy} = $$srcpath;
157 1 $vmsg = "internal content";
158  
159 } elsif ( ref $srcpath eq 'ARRAY' ) {
160  
161 0 $self->{_policy} = join "", @$srcpath;
162 0 $vmsg = "internal content";
163  
164 } elsif ( ref $srcpath eq 'GLOB' ) {
165  
166 0 *POLICY = *$srcpath;
167 0 $self->{_policy} = do { local $/; <POLICY> };
168 0 $vmsg = "anonymous filehandle";
169  
170 } else {
171 3 ▶ $srcpath = File::Spec->curdir
Branch: 1 branch
BranchTrueFalseTotal
unless defined $srcpath and length $srcpath 2 1 3
Condition66%2/3
defined $srcpath and length $srcpath
!ll&&!rl&&r
021
MC/DC50%1/2
defined $srcpath and length $srcpath
defined $srcpath length $srcpath
Truth table66%2/3
A and B
A= defined $srcpathB= length $srcpath
ABresult
0X0
100
111
172 unless defined $srcpath && length $srcpath;
173 3 my $p_name = File::Spec->catfile( $srcpath, 'Policy.sh' );
174  
175 3 ▶ unless ( open POLICY, $p_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open POLICY, $p_name) { } 0 3 3
176 3 $self->{_policy} = $self->default_Policy( @ccflags );
177 3 $vmsg = "default content";
178 } else {
179 0 $self->{_policy} = do { local $/; <POLICY> };
180 0 close POLICY;
181 0 $vmsg = $p_name;
182 }
183  
184 }
185 4 $self->log_info("Reading 'Policy.sh' from %s (v=%d)", $vmsg, $self->verbose);
186 }
187  
188 =head2 $policy->default_Policy( [@ccflags] )
189  
190 Generate the default F<Policy.sh> from a set of ccflags, but be
191 backward compatible.
192  
193 =cut
194  
195 3 ▶sub default_Policy {
Subroutine default_Policy: called CC=2 SCAR=6.9
Pod documented
196 3 my $self = shift;
197 3 ▶ my @ccflags = @_ ? @_ : qw( -DDEBUGGING );
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 2 1 3
198  
199 3 local $" = " ";
200 3 return <<__EOPOLICY__;
201 #!/bin/sh
202  
203 # Default Policy.sh from Test::Smoke
204  
205 # Be sure to define -DDEBUGGING by default, it's easier to remove
206 # it from Policy.sh than it is to add it in on the correct places
207  
208 ccflags='@ccflags'
209 __EOPOLICY__
210 }
211  
212 14 1;
213  
214 =head1 COPYRIGHT
215  
216 (c) 2001-2015, All rights reserved.
217  
218 * H.Merijn Brand <hmbrand@hccnet.nl>
219 * Nicholas Clark <nick@unfortu.net>
220 * Abe Timmerman <abeltje@cpan.org>
221  
222 This library is free software; you can redistribute it and/or modify
223 it under the same terms as Perl itself.
224  
225 See:
226  
227 * <http://www.perl.com/perl/misc/Artistic.html>,
228 * <http://www.gnu.org/copyleft/gpl.html>
229  
230 This program is distributed in the hope that it will be useful,
231 but WITHOUT ANY WARRANTY; without even the implied warranty of
232 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
233  
234 =cut
« blib/lib/Test/Smoke/App/W32Configure.pm ↑ Summary blib/lib/Test/Smoke/App/SendReport.pm »