File Coverage

blib/lib/Env/Assert.pm
Criterion Covered Total %
statement 52 56 92.8
branch 12 22 54.5
condition 7 9 77.7
subroutine 11 11 100.0
pod 1 1 100.0
total 83 99 83.8


line stmt bran cond sub pod time code
1             ## no critic (ControlStructures::ProhibitPostfixControls)
2             ## no critic (ValuesAndExpressions::ProhibitConstantPragma)
3             package Env::Assert;
4 1     1   564354 use strict;
  1         2  
  1         47  
5 1     1   6 use warnings;
  1         2  
  1         82  
6 1     1   25 use 5.010;
  1         9  
7              
8             # ABSTRACT: Ensure that the environment variables match what you need, or abort.
9              
10             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     1   8 use Carp;
  1         3  
  1         109  
17              
18 1     1   8 use English qw( -no_match_vars ); # Avoids regex performance penalty in perl 5.18 and earlier
  1         2  
  1         9  
19 1     1   1464 use open ':std', IO => ':encoding(UTF-8)';
  1         1674  
  1         6  
20              
21 1     1   3301 use Env::Assert::Functions qw( :all );
  1         4  
  1         250  
22              
23 1     1   9 use constant { ENV_DESC_FILENAME => '.envdesc', };
  1         2  
  1         102  
24              
25             # Handle exports
26             {
27 1     1   8 no warnings 'redefine'; ## no critic [TestingAndDebugging::ProhibitNoWarnings]
  1         22  
  1         658  
28              
29             sub import {
30 4     4   12577 my ( $class, $cmd, $args ) = @_;
31              
32             # We also allow only: 'use Env::Assert;'
33 4 100 100     271 croak "Unknown argument '$cmd'" if ( $cmd && $cmd ne 'assert' );
34              
35 3 50       6 if ( !assert_env( %{$args} ) ) {
  3         12  
36 0         0 croak 'Errors in environment detected.';
37             }
38 3         124 return;
39             }
40             }
41              
42             sub assert_env {
43 3     3 1 8 my (%args) = @_;
44 3         17 local $OUTPUT_AUTOFLUSH = 1;
45              
46 3   50     16 my $break_at_first_error = $args{'break_at_first_error'} // 0;
47 3   50     12 my $exact = $args{'exact'} // 0;
48              
49 3         6 my @env_desc_rows;
50 3 100       9 if ( $args{'envdesc'} ) {
51 1         3 my $content = $args{'envdesc'};
52 1 50       23 open my $fh, q{<}, \$content
53             or croak 'Cannot open scalar envdesc content';
54 1         86 @env_desc_rows = <$fh>;
55 1 50       22 close $fh or croak 'Cannot close scalar envdesc content';
56             }
57             else {
58 2   100     8 my $env_desc_filename = $args{'envdesc_file'} // ENV_DESC_FILENAME;
59 2 50       146 open my $fh, q{<}, $env_desc_filename or croak "Cannot open file '$env_desc_filename'";
60 2         270 @env_desc_rows = <$fh>;
61 2 50       87 close $fh or croak "Cannot close file '$env_desc_filename'";
62             }
63              
64 3         16 my $desc = file_to_desc(@env_desc_rows);
65 3         5 my %parameters;
66 3 50       11 $parameters{'break_at_first_error'} = $break_at_first_error
67             if defined $break_at_first_error;
68 3 50       10 $desc->{'options'}->{'exact'} = $exact
69             if defined $exact;
70 3         15 my $r = assert( \%ENV, $desc, \%parameters );
71 3 50       10 if ( !$r->{'success'} ) {
72 0 0       0 print {*STDERR} report_errors( $r->{'errors'} )
  0         0  
73             or croak 'Cannot print errors to STDERR';
74 0         0 return 0;
75             }
76 3         60 return 1;
77             }
78              
79             1;
80              
81             __END__