| 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
Condition100%3/3
$cmd and $cmd ne "assert"
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"
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 3 | ▶ | if ( !assert_env( %{$args} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
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"}
| |||||||||||||||||||||||||||||
| 47 | 3 | ▶ | my $exact = $args{'exact'} // 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{"exact"} // 0
MC/DC0%0/1
$args{"exact"} // 0
$args{"exact"}
Truth table50%1/2
A
A= $args{"exact"}
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 3 | my @env_desc_rows; | |||||||||||||||||||||||||||
| 50 | 3 | ▶ | if ( $args{'envdesc'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 1 | my $content = $args{'envdesc'}; | |||||||||||||||||||||||||||
| 52 | 1 | ▶ | open my $fh, q{<}, \$content | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | else { | ||||||||||||||||||||||||||||
| 58 | 2 | ▶ | my $env_desc_filename = $args{'envdesc_file'} // ENV_DESC_FILENAME; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{"envdesc_file"} // ".envdesc"
MC/DC100%1/1
$args{"envdesc_file"} // ".envdesc"
$args{"envdesc_file"}
Truth table100%2/2
A
A= $args{"envdesc_file"}
| |||||||||||||||||||||||||||||
| 59 | 2 | ▶ | open my $fh, q{<}, $env_desc_filename or croak "Cannot open file '$env_desc_filename'"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 2 | @env_desc_rows = <$fh>; | |||||||||||||||||||||||||||
| 61 | 2 | ▶ | close $fh or croak "Cannot close file '$env_desc_filename'"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 67 | if defined $break_at_first_error; | ||||||||||||||||||||||||||||
| 68 | 3 | ▶ | $desc->{'options'}->{'exact'} = $exact | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | if defined $exact; | ||||||||||||||||||||||||||||
| 70 | 3 | my $r = assert( \%ENV, $desc, \%parameters ); | |||||||||||||||||||||||||||
| 71 | 3 | ▶ | if ( !$r->{'success'} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | print {*STDERR} report_errors( $r->{'errors'} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||