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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
|
#!perl
# no critic (ValuesAndExpressions::ProhibitMagicNumbers)
use strict;
use warnings;
use Test2::V0;
use Carp qw( croak );
use English qw( -no_match_vars ); # Avoids regex performance
use FileHandle ();
use File::Path qw( make_path );
use File::Spec ();
use File::Temp ();
use Cwd qw( getcwd abs_path );
use FindBin qw( $RealBin );
my $lib_path;
BEGIN {
$lib_path = File::Spec->catdir( ( $RealBin =~ /(.+)/msx )[0], q{.}, 'lib' );
}
use lib "$lib_path";
use Test2::Require::Platform::Unix;
# $File::Temp::KEEP_ALL = 1;
# $File::Temp::DEBUG = 1;
sub create_subtest_files {
my ( $root_env, $dir_env, $subdir_env ) = @_;
my $dir = File::Temp->newdir(
TEMPLATE => 'temp-envdot-test-XXXXX',
CLEANUP => 1,
DIR => File::Spec->tmpdir,
);
my $dir_path = abs_path( $dir->dirname );
make_path( File::Spec->catdir( $dir_path, 'root', 'dir', 'subdir' ) );
if ($root_env) {
my $fh_root_env = FileHandle->new( File::Spec->catfile( $dir_path, 'root', '.env' ), 'w' );
print {$fh_root_env} $root_env || croak;
$fh_root_env->close;
}
if ($dir_env) {
my $fh_dir_env = FileHandle->new( File::Spec->catfile( $dir_path, 'root', 'dir', '.env' ), 'w' );
print {$fh_dir_env} $dir_env || croak;
$fh_dir_env->close;
}
if ($subdir_env) {
my $fh_subdir_env = FileHandle->new( File::Spec->catfile( $dir_path, 'root', 'dir', 'subdir', '.env' ), 'w' );
print {$fh_subdir_env} $subdir_env || croak;
$fh_subdir_env->close;
}
return $dir, $dir_path;
}
my $CASE_ONE_ROOT_ENV = <<"END_OF_FILE";
ROOT_VAR="root"
COMMON_VAR="root"
DIR_COMMON_VAR="root"
END_OF_FILE
my $CASE_ONE_DIR_ENV = <<"END_OF_FILE";
# envdot (read:from_parent)
DIR_VAR="dir"
COMMON_VAR="dir"
DIR_COMMON_VAR="dir"
SUBDIR_COMMON_VAR="dir"
END_OF_FILE
my $CASE_ONE_SUBDIR_ENV = <<"END_OF_FILE";
# envdot (file:type=shell,read:from_parent)
SUBDIR_VAR="subdir"
COMMON_VAR="subdir"
SUBDIR_COMMON_VAR="subdir"
END_OF_FILE
subtest 'One dotenv, two parent files' => sub {
my ( $dir, $dir_path ) = create_subtest_files( $CASE_ONE_ROOT_ENV, $CASE_ONE_DIR_ENV, $CASE_ONE_SUBDIR_ENV, );
# Do not use __FILE__ because its value is not absolute and not updated
# when chdir is done.
my $this = getcwd;
($this) = $this =~ /(.+)/msx; # Make it non-tainted
my $subdir_path = File::Spec->catdir( $dir_path, 'root', 'dir', 'subdir' );
# CD to subdir, the bottom in the hierarcy.
chdir $subdir_path || croak;
my %new_env;
## no critic (ControlStructures::ProhibitPostfixControls)
$new_env{$_} = $ENV{$_} foreach ( keys %ENV );
delete $new_env{'ENVDOT_FILEPATHS'} if exists $new_env{'ENVDOT_FILEPATHS'};
# We need to replace the current %ENV, not change individual values.
local %ENV = %new_env;
local $EVAL_ERROR = undef;
my $r = eval 'use Env::Dot; 1'; ## no critic [BuiltinFunctions::ProhibitStringyEval]
is( $EVAL_ERROR, q{}, 'use Env::Dot failed' );
is( $r, 1, 'evaled okay' );
is( $ENV{'ROOT_VAR'}, 'root', 'Interface works' );
is( $ENV{'DIR_VAR'}, 'dir', 'Interface works' );
is( $ENV{'SUBDIR_VAR'}, 'subdir', 'Interface works' );
is( $ENV{'COMMON_VAR'}, 'subdir', 'Interface works' );
is( $ENV{'DIR_COMMON_VAR'}, 'dir', 'Interface works' );
is( $ENV{'SUBDIR_COMMON_VAR'}, 'subdir', 'Interface works' );
chdir $this;
done_testing;
};
# my $CASE_TWO_ROOT_ENV = <<"END_OF_FILE";
# # envdot (read:from_parent)
# ROOT_VAR="root"
# COMMON_VAR="root"
# DIR_COMMON_VAR="root"
# END_OF_FILE
#
# my $CASE_TWO_DIR_ENV = <<"END_OF_FILE";
# # envdot (read:from_parent)
# DIR_VAR="dir"
# COMMON_VAR="dir"
# DIR_COMMON_VAR="dir"
# SUBDIR_COMMON_VAR="dir"
# END_OF_FILE
#
my $CASE_TWO_SUBDIR_ENV = <<"END_OF_FILE";
# envdot (file:type=shell,read:from_parent)
SUBDIR_VAR="subdir"
COMMON_VAR="subdir"
SUBDIR_COMMON_VAR="subdir"
END_OF_FILE
subtest 'Missing parent file, not okay' => sub {
# N.B. This test will fail if there is a .env file in a parent dir of the tempdir.
my ( $dir, $dir_path ) = create_subtest_files( undef, undef, $CASE_TWO_SUBDIR_ENV, );
# Do not use __FILE__ because its value is not absolute and not updated
# when chdir is done.
my $this = getcwd;
($this) = $this =~ /(.+)/msx; # Make it non-tainted
my $subdir_path = File::Spec->catdir( $dir_path, 'root', 'dir', 'subdir' );
my $subdir_env = File::Spec->catdir( $dir_path, 'root', 'dir', 'subdir', '.env' );
# CD to subdir, the bottom in the hierarcy.
chdir $subdir_path || croak;
my %new_env;
## no critic (ControlStructures::ProhibitPostfixControls)
$new_env{$_} = $ENV{$_} foreach ( keys %ENV );
delete $new_env{'ENVDOT_FILEPATHS'} if exists $new_env{'ENVDOT_FILEPATHS'};
# We need to replace the current %ENV, not change individual values.
local %ENV = %new_env;
local $EVAL_ERROR = undef;
my $r = eval 'use Env::Dot; 1'; ## no critic [BuiltinFunctions::ProhibitStringyEval]
## no critic (RegularExpressions::ProhibitComplexRegexes)
like(
$EVAL_ERROR,
qr/^Error: \s No \s parent \s [.]env \s file \s found \s for \s child \s file \s '$subdir_env' .* $/msx,
'use Env::Dot failed'
);
is( $r, U(), 'eval failed' );
chdir $this;
done_testing;
};
# my $CASE_TWO_ROOT_ENV = <<"END_OF_FILE";
# # envdot (read:from_parent)
# ROOT_VAR="root"
# COMMON_VAR="root"
# DIR_COMMON_VAR="root"
# END_OF_FILE
#
my $CASE_THREE_DIR_ENV = <<"END_OF_FILE";
# envdot (read:from_parent=true,read:allow_missing_parent=false)
DIR_VAR="dir"
COMMON_VAR="dir"
DIR_COMMON_VAR="dir"
SUBDIR_COMMON_VAR="dir"
END_OF_FILE
my $CASE_THREE_SUBDIR_ENV = <<"END_OF_FILE";
# envdot (file:type=shell,read:from_parent)
SUBDIR_VAR="subdir"
COMMON_VAR="subdir"
SUBDIR_COMMON_VAR="subdir"
END_OF_FILE
subtest 'Missing parent file 2, not okay' => sub {
# N.B. This test will fail if there is a .env file in a parent dir of the tempdir.
my ( $tmp_dir, $tmp_dir_path ) = create_subtest_files( undef, $CASE_THREE_DIR_ENV, $CASE_TWO_SUBDIR_ENV, );
# Do not use __FILE__ because its value is not absolute and not updated
# when chdir is done.
my $this = getcwd;
($this) = $this =~ /(.+)/msx; # Make it non-tainted
my $dir_path = File::Spec->catdir( $tmp_dir_path, 'root', 'dir' );
my $dir_env = File::Spec->catdir( $tmp_dir_path, 'root', 'dir', '.env' );
my $subdir_path = File::Spec->catdir( $tmp_dir_path, 'root', 'dir', 'subdir' );
# CD to subdir, the bottom in the hierarcy.
chdir $subdir_path || croak;
my %new_env;
## no critic (ControlStructures::ProhibitPostfixControls)
$new_env{$_} = $ENV{$_} foreach ( keys %ENV );
delete $new_env{'ENVDOT_FILEPATHS'} if exists $new_env{'ENVDOT_FILEPATHS'};
# We need to replace the current %ENV, not change individual values.
local %ENV = %new_env;
local $EVAL_ERROR = undef;
my $r = eval 'use Env::Dot; 1'; ## no critic [BuiltinFunctions::ProhibitStringyEval]
## no critic (RegularExpressions::ProhibitComplexRegexes)
like(
$EVAL_ERROR,
qr/^Error: \s No \s parent \s [.]env \s file \s found \s for \s child \s file \s '$dir_env' .* $/msx,
'use Env::Dot failed'
);
is( $r, U(), 'eval failed' );
chdir $this;
done_testing;
};
my $CASE_FOUR_SUBDIR_ENV = <<"END_OF_FILE";
# envdot (file:type=shell,read:from_parent=true,read:allow_missing_parent=true)
SUBDIR_VAR="subdir"
COMMON_VAR="subdir"
SUBDIR_COMMON_VAR="subdir"
END_OF_FILE
subtest 'Missing parent file, okay' => sub {
# N.B. This test will fail if there is a .env file in a parent dir of the tempdir.
my ( $dir, $dir_path ) = create_subtest_files( undef, undef, $CASE_FOUR_SUBDIR_ENV, );
# Do not use __FILE__ because its value is not absolute and not updated
# when chdir is done.
my $this = getcwd;
($this) = $this =~ /(.+)/msx; # Make it non-tainted
my $subdir_path = File::Spec->catdir( $dir_path, 'root', 'dir', 'subdir' );
# CD to subdir, the bottom in the hierarcy.
chdir $subdir_path || croak;
my %new_env;
## no critic (ControlStructures::ProhibitPostfixControls)
$new_env{$_} = $ENV{$_} foreach ( keys %ENV );
delete $new_env{'ENVDOT_FILEPATHS'} if exists $new_env{'ENVDOT_FILEPATHS'};
# We need to replace the current %ENV, not change individual values.
local %ENV = %new_env;
local $EVAL_ERROR = undef;
my $r = eval 'use Env::Dot; 1'; ## no critic [BuiltinFunctions::ProhibitStringyEval]
is( $r, 1, 'evaled okay' );
is( $EVAL_ERROR, q{}, 'use Env::Dot successful' );
is( $ENV{'ROOT_VAR'}, U(), 'Interface works' );
is( $ENV{'DIR_VAR'}, U(), 'Interface works' );
is( $ENV{'SUBDIR_VAR'}, 'subdir', 'Interface works' );
is( $ENV{'COMMON_VAR'}, 'subdir', 'Interface works' );
is( $ENV{'DIR_COMMON_VAR'}, U(), 'Interface works' );
is( $ENV{'SUBDIR_COMMON_VAR'}, 'subdir', 'Interface works' );
chdir $this;
done_testing;
};
done_testing;
|