Skip to content

Commit

Permalink
Test2::Util - add _env_get() to wrap %ENV access.
Browse files Browse the repository at this point in the history
A test might lock %ENV, so we should always check for key existence
before reading a key, or the lookup might trigger an exception. This
function wraps the check up and provides a default facility to make
logic that needs to consult %ENV more robust.
  • Loading branch information
demerphq committed Mar 14, 2023
1 parent b30fa0d commit 3d3fc82
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/Test2/Util.pm
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,26 @@ our @EXPORT_OK = qw{
try_sig_mask
clone_io
_env_get
};
BEGIN { require Exporter; our @ISA = qw(Exporter) }

BEGIN {
*IS_WIN32 = ($^O eq 'MSWin32') ? sub() { 1 } : sub() { 0 };
}

# check for key existence before fetching from %ENV to avoid
# locked hash issues. If it does not exist, or it does and the
# value is undefined return $_[1] instead, thus allowing
# a default value to be supplied if required.
sub _env_get {
my ($key,$default) = @_;
my $got = exists($ENV{$key}) ? $ENV{$key} : undef;
$got = $default unless defined $got;
return $got;
}

sub _can_thread {
return 0 unless $] >= 5.008001;
return 0 unless $Config{'useithreads'};
Expand Down

0 comments on commit 3d3fc82

Please sign in to comment.