Skip to content

Commit

Permalink
Test2::API - harden T2_FORMATTER env access.
Browse files Browse the repository at this point in the history
There were two problems. The first is accessing the %ENV hash when it
might locked. Using Test2::Util::_env_get() solves that problem.

The second issue is that the patterns might match "+" with no extra string,
leaving the formatter as the empty string. The third problem is the
defaulting for the formatter could end up not being triggered and the
formatter being left as the empty string.

This fixes all three issues. I noticed the second two when I fixing the
first and did it wrong accidentally.
  • Loading branch information
demerphq committed Mar 14, 2023
1 parent 692606e commit 862e459
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
5 changes: 3 additions & 2 deletions lib/Test2/API.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use strict;
use warnings;

use Time::HiRes qw/time/;
use Test2::Util qw/USE_THREADS/;
use Test2::Util qw/USE_THREADS _env_get/;

BEGIN {
$ENV{TEST_ACTIVE} ||= 1;
Expand Down Expand Up @@ -287,7 +287,8 @@ sub test2_ipc_get_timeout { $INST->ipc_timeout() }
sub test2_ipc_enable_shm { 0 }

sub test2_formatter {
if ($ENV{T2_FORMATTER} && $ENV{T2_FORMATTER} =~ m/^(\+)?(.*)$/) {
my $env_var = _env_get('T2_FORMATTER', '');
if ($env_var =~ m/^(\+)?(.+)$/) {
my $formatter = $1 ? $2 : "Test2::Formatter::$2";
my $file = pkg_to_file($formatter);
require $file;
Expand Down
7 changes: 4 additions & 3 deletions lib/Test2/API/Instance.pm
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,10 @@ sub _finalize {

unless ($self->{+FORMATTER}) {
my ($formatter, $source);
if ($ENV{T2_FORMATTER}) {
if (my $env_val = _env_get('T2_FORMATTER')) {
$source = "set by the 'T2_FORMATTER' environment variable";

if ($ENV{T2_FORMATTER} =~ m/^(\+)?(.*)$/) {
if ($env_val =~ m/^(\+)?(.+)$/) {
$formatter = $1 ? $2 : "Test2::Formatter::$2"
}
else {
Expand All @@ -177,7 +177,8 @@ sub _finalize {
($formatter) = @{$self->{+FORMATTERS}};
$source = "Most recently added";
}
else {

if (!$formatter) {
$formatter = 'Test2::Formatter::TAP';
$source = 'default formatter';
}
Expand Down

0 comments on commit 862e459

Please sign in to comment.