blib/lib/Env/Assert.pm

filter: Statementstmt 93.2% 55 / 59 Branchbran 54.5% 12 / 22 Conditioncond 77.7% 7 / 9 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 11 / 11 Podpod 100% 1 / 1 totaltotal 83.1% 89 / 107 CC 16 SCAR 22.5
CC
16
Cov
82%
assert_env
16.9
import
4.1
BEGIN
1.0
CRAP 9.4
« blib/lib/Env/Assert/Functions.pm ↑ Summary
1 ## no critic (ControlStructures::ProhibitPostfixControls)
2 ## no critic (ValuesAndExpressions::ProhibitConstantPragma)
3 package Env::Assert;
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # ABSTRACT: Ensure that the environment variables match what you need, or abort.
9  
10 1 our $VERSION = '0.015';
11  
12 # We define our own import routine because
13 # this is the point (when `use Env::Assert` is called)
14 # when we do our magic.
15  
16 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶use English qw( -no_match_vars ); # Avoids regex performance penalty in perl 5.18 and earlier
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use open ':std', IO => ':encoding(UTF-8)';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶use Env::Assert::Functions qw( :all );
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 ▶use constant { ENV_DESC_FILENAME => '.envdesc', };
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 # Handle exports
26 {
27 1 ▶ no warnings 'redefine'; ## no critic [TestingAndDebugging::ProhibitNoWarnings]
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 4 ▶ sub import {
Subroutine import: called CC=4 SCAR=14.0
30 4 my ( $class, $cmd, $args ) = @_;
31  
32 # We also allow only: 'use Env::Assert;'
33 4 ▶ croak "Unknown argument '$cmd'" if ( $cmd && $cmd ne 'assert' );
Branch: 1 branch
BranchTrueFalseTotal
if $cmd and $cmd ne "assert" 1 3 4
Condition100%3/3
$cmd and $cmd ne "assert"
!ll&&!rl&&r
121
MC/DC100%2/2
$cmd and $cmd ne "assert"
$cmd $cmd ne "assert"
Truth table100%3/3
A and B
A= $cmdB= $cmd ne "assert"
ABresult
0X0
100
111
34  
35 3 ▶ if ( !assert_env( %{$args} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (assert_env(%{$args;})) 0 3 3
36 0 croak 'Errors in environment detected.';
37 }
38 3 return;
39 }
40 }
41  
42 1 ▶sub assert_env {
Subroutine assert_env: called CC=13 SCAR=28.3
Pod documented
43 3 my (%args) = @_;
44 3 local $OUTPUT_AUTOFLUSH = 1;
45  
46 3 ▶ my $break_at_first_error = $args{'break_at_first_error'} // 0;
Condition50%1/2
$args{"break_at_first_error"} // 0
l!l
03
MC/DC0%0/1
$args{"break_at_first_error"} // 0
$args{"break_at_first_error"}
Truth table50%1/2
A
A= $args{"break_at_first_error"}
Aresult
11
00
47 3 ▶ my $exact = $args{'exact'} // 0;
Condition50%1/2
$args{"exact"} // 0
l!l
03
MC/DC0%0/1
$args{"exact"} // 0
$args{"exact"}
Truth table50%1/2
A
A= $args{"exact"}
Aresult
11
00
48  
49 3 my @env_desc_rows;
50 3 ▶ if ( $args{'envdesc'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'envdesc'}) { } 1 2 3
51 1 my $content = $args{'envdesc'};
52 1 ▶ open my $fh, q{<}, \$content
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", \$content or croak("Cannot open scalar envdesc content") 1 0 1
53 or croak 'Cannot open scalar envdesc content';
54 1 @env_desc_rows = <$fh>;
55 1 ▶ close $fh or croak 'Cannot close scalar envdesc content';
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("Cannot close scalar envdesc content") 1 0 1
56 }
57 else {
58 2 ▶ my $env_desc_filename = $args{'envdesc_file'} // ENV_DESC_FILENAME;
Condition100%2/2
$args{"envdesc_file"} // ".envdesc"
l!l
11
MC/DC100%1/1
$args{"envdesc_file"} // ".envdesc"
$args{"envdesc_file"}
Truth table100%2/2
A
A= $args{"envdesc_file"}
Aresult
11
00
59 2 ▶ open my $fh, q{<}, $env_desc_filename or croak "Cannot open file '$env_desc_filename'";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $env_desc_filename or croak("Cannot open file '${env_desc_filename}'") 2 0 2
60 2 @env_desc_rows = <$fh>;
61 2 ▶ close $fh or croak "Cannot close file '$env_desc_filename'";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("Cannot close file '${env_desc_filename}'") 2 0 2
62 }
63  
64 3 my $desc = file_to_desc(@env_desc_rows);
65 3 my %parameters;
66 3 ▶ $parameters{'break_at_first_error'} = $break_at_first_error
Branch: 1 branch
BranchTrueFalseTotal
if defined $break_at_first_error 3 0 3
67 if defined $break_at_first_error;
68 3 ▶ $desc->{'options'}->{'exact'} = $exact
Branch: 1 branch
BranchTrueFalseTotal
if defined $exact 3 0 3
69 if defined $exact;
70 3 my $r = assert( \%ENV, $desc, \%parameters );
71 3 ▶ if ( !$r->{'success'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r->{"success"}) 0 3 3
72 0 ▶ print {*STDERR} report_errors( $r->{'errors'} )
Branch: 1 branch
BranchTrueFalseTotal
print {*STDERR;} report_errors($r->{"errors"}) or croak("Cannot print errors to STDERR") 0 0 0
73 or croak 'Cannot print errors to STDERR';
74 0 return 0;
75 }
76 3 return 1;
77 }
78  
79 1 1;
80  
81 __END__
« blib/lib/Env/Assert/Functions.pm ↑ Summary