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.016';
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 {
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ENV_DESC_FILENAME => '.envdesc',
25 1 };
26  
27 # Handle exports
28 {
29 1 ▶ no warnings 'redefine'; ## no critic [TestingAndDebugging::ProhibitNoWarnings]
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 4 ▶ sub import {
Subroutine import: called CC=4 SCAR=14.0
32 4 my ($class, $cmd, $args) = @_;
33  
34 # We also allow only: 'use Env::Assert;'
35 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
36  
37 3 ▶ if( ! assert_env( %{ $args } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (assert_env(%{$args;})) 0 3 3
38 0 croak 'Errors in environment detected.';
39 }
40 3 return;
41 }
42 }
43  
44 =pod
45  
46 =encoding utf8
47  
48 =for :stopwords env filepath filepaths
49  
50 =head1 NAME
51  
52 Env::Assert - Ensure that the environment variables match what you need, or abort
53  
54 =head1 VERSION
55  
56 version 0.016
57  
58 =head1 SYNOPSIS
59  
60 =for test_synopsis BEGIN { die 'SKIP: no .envdesc file here' }
61  
62 use Env::Assert 'assert';
63 # or:
64 use Env::Assert assert => {
65 envdesc_file => 'another-envdesc',
66 break_at_first_error => 1,
67 };
68  
69 # .envdesc file:
70 # MY_VAR=.+
71  
72 # use any verified environment variable
73 say $ENV{MY_VAR};
74  
75 # You can inline the envdesc file:
76 use Env::Assert assert => {
77 exact => 1,
78 envdesc => <<'EOF'
79 NUMERIC_VAR=^[[:digit:]]+$
80 TIME_VAR=^\d{2}:\d{2}:\d{2}$
81 EOF
82 };
83  
84 =head1 STATUS
85  
86 Package Env::Assert is currently being developed so changes in the API are possible,
87 though not likely.
88  
89 =head1 NOTES
90  
91 Functionality of L<Env::Assert> has been moved to module L<Env::Assert::Functions> since version 0.013.
92 L<Env::Assert> has a different API now.
93  
94 =head1 METHODS
95  
96 =head2 assert_env
97  
98 Read environment description, F<.envdesc> by default,
99 and compare current environment.
100  
101 =cut
102  
103 1 ▶sub assert_env {
Subroutine assert_env: called CC=13 SCAR=28.3
Pod documented
104 3 my (%args) = @_;
105 3 local $OUTPUT_AUTOFLUSH = 1;
106  
107 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
108 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
109  
110 3 my @env_desc_rows;
111 3 ▶ if( $args{'envdesc'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'envdesc'}) { } 1 2 3
112 1 my $content = $args{'envdesc'};
113 1 ▶ open my $fh, q{<}, \$content
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", \$content or croak("Cannot open scalar envdesc content") 1 0 1
114 or croak 'Cannot open scalar envdesc content';
115 1 @env_desc_rows = <$fh>;
116 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
117 } else {
118 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
119 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
120 2 @env_desc_rows = <$fh>;
121 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
122 }
123  
124 3 my $desc = file_to_desc( @env_desc_rows );
125 3 my %parameters;
126 3 ▶ $parameters{'break_at_first_error'} = $break_at_first_error
Branch: 1 branch
BranchTrueFalseTotal
if defined $break_at_first_error 3 0 3
127 if defined $break_at_first_error;
128 3 ▶ $desc->{'options'}->{'exact'} = $exact
Branch: 1 branch
BranchTrueFalseTotal
if defined $exact 3 0 3
129 if defined $exact;
130 3 my $r = assert( \%ENV, $desc, \%parameters );
131 3 ▶ if( ! $r->{'success'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r->{"success"}) 0 3 3
132 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
133 or croak 'Cannot print errors to STDERR';
134 0 return 0;
135 }
136 3 return 1;
137 }
138  
139 =head1 DEPENDENCIES
140  
141 No external dependencies outside Perl's standard distribution.
142  
143 =head1 SEE ALSO
144  
145 L<Env::Dot> is a "sister" to Env::Assert.
146 Read environment variables from a F<.env> file directly into you program.
147 There is also script F<envdot> which can turn F<.env> file's content
148 into environment variables for different shells.
149  
150 =head1 AUTHOR
151  
152 Mikko Koivunalho <mikkoi@cpan.org>
153  
154 =head1 COPYRIGHT AND LICENSE
155  
156 This software is copyright (c) 2023 by Mikko Koivunalho.
157  
158 This is free software; you can redistribute it and/or modify it under
159 the same terms as the Perl 5 programming language system itself.
160  
161 =cut
162  
163 1 1;
164 __END__
« blib/lib/Env/Assert/Functions.pm ↑ Summary