File: envassert-script.t

package info (click to toggle)
env-assert 0.015-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 424 kB
  • sloc: perl: 1,877; makefile: 8; sh: 7
file content (93 lines) | stat: -rw-r--r-- 3,732 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!perl
# no critic (ValuesAndExpressions::ProhibitMagicNumbers)

use strict;
use warnings;

use Carp;
use Cwd;
use English    qw( -no_match_vars );    # Avoids regex performance penalty in perl 5.18 and earlier
use FindBin    qw( $RealBin );
use File::Spec ();
my $lib_path;

BEGIN {
    $lib_path = File::Spec->catdir( ( $RealBin =~ /(.+)/msx )[0], q{..}, 'lib' );
}
use lib "$lib_path";

use Test2::V0;
use Test::Script 1.28;

use Test2::Deny::Platform::OS::DOSOrDerivative;

# use Test2::Deny::Platform::CI::GitHubCI;

my $path1 = File::Spec->rel2abs( File::Spec->catfile( File::Spec->curdir(), 't', 'three' ) );

# FIXME: Why does GitHub CI not manage to run test! The file .envdesc is not in its place!
# subtest 'Script fails with missing env variables' => sub {
#     chdir($path1) || croak "Cannot chdir($path1): $OS_ERROR";
#
#     my $stderr;
#     my $stderr_result = <<'EOF';
# Environment Assert: ERRORS:
#     variables:
#         A_DIGIT: Variable A_DIGIT is missing from environment
#         A_MISSING_VAR: Variable A_MISSING_VAR is missing from environment
# Errors in environment detected. at bin/using-script.pl line 9.
# BEGIN failed--compilation aborted at bin/using-script.pl line 9.
# EOF
#     script_fails(['bin/using-script.pl', ], { stderr => \$stderr, exit => 255, }, 'Verify errors in output');
#     is( $stderr, $stderr_result, 'Correct stderr' );
#
#     done_testing;
# };
#
# subtest 'Script succeeds' => sub {
#     chdir($path1) || croak "Cannot chdir($path1): $OS_ERROR";
#
#     ## no critic (Variables::RequireLocalizedPunctuationVars)
#     local %ENV = map { $_ => $ENV{$_} } keys %ENV;
#     $ENV{A_DIGIT} = '123';
#     $ENV{A_MISSING_VAR} = 'is_no_longer_missing';
#     my $stdout = 'Control should not reach this point!';
#     script_runs(['bin/using-script.pl', ], { stdout => \$stdout, }, 'Verify no errors in output');
#
#     done_testing;
# };

subtest 'Script fails with other envdesc file with missing env variables' => sub {
    chdir($path1) || croak "Cannot chdir($path1): $OS_ERROR";

    my ( $stdout, $stderr );
    script_fails( [ 'bin/using-another.pl', ], { stdout => \$stdout, stderr => \$stderr, exit => 255, },
        'Verify errors in output' );
    ## no critic (RegularExpressions::RequireDotMatchAnything,RegularExpressions::RequireLineBoundaryMatching,RegularExpressions::ProhibitComplexRegexes,RegularExpressions::RequireExtendedFormatting)
    is( $stdout, q{},                                                                                     'Correct stdout' );
    is( $stderr, match qr/ANOTHER_MISSING_VAR: Variable ANOTHER_MISSING_VAR is missing from environment/, 'Correct stderr' );
    is( $stderr, match qr/A_DIGIT: Variable A_DIGIT is missing from environment/,                         'Correct stderr' );
    is( $stderr, match qr/A_MISSING_VAR: Variable A_MISSING_VAR is missing from environment/,             'Correct stderr' );

    done_testing;
};

subtest 'Script succeeds with other envdesc file because env requirements are fullfilled' => sub {
    chdir($path1) || croak "Cannot chdir($path1): $OS_ERROR";

    local %ENV = (
        ( map { $_ => $ENV{$_} } keys %ENV ),
        A_DIGIT             => '123',
        A_MISSING_VAR       => 'is_no_longer_missing',
        ANOTHER_MISSING_VAR => 'is_no_longer_missing',
    );

    my ( $stdout, $stderr );
    script_runs( [ 'bin/using-another.pl', ], { stdout => \$stdout, stderr => \$stderr, }, 'Verify no errors in output' );
    is( $stdout, qq{Control will reach this point if env requirements are fulfilled!\n}, 'Correct stdout' );
    is( $stderr, q{},                                                                    'Correct stderr' );

    done_testing;
};

done_testing;