Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guard %ENV access with exists() checks and some minor edge case bugs I found while doing so #908

Closed
wants to merge 3 commits into from

Conversation

demerphq
Copy link
Contributor

@demerphq demerphq commented Mar 13, 2023

In Perl/perl5#20928 I discovered that if %ENV is locked while testing and a test fails then Test::More blows up horribly. You can argue this issue several ways, including that it is a Hash::Util testing bug. But I think Test::More can harden itself pretty reasonably against this issue by simply checking for key existence before reading a key. I did not attempt to deal with the places where there are writes to ENV, obviously that is going to end badly if %ENV is locked. But reading from a locked %ENV during testing is easy to guard against and obviously at least possible to happen since code doing so has been in perl core for a long time. :-)

Related tickets:
#907
Perl/perl5#20929

Along the way I noticed a subtle error in the logic for parsing the T2_FORMATTER env var. If the string is set to "+" it would end up setting the formatter to the empty string. I also realized it was possible for the logic to not set it at all, and step past the defaulting logic in Test2::API::Instance. So I fixed it. Since part of this logic reads from the %ENV I dealt with both issues in the same commit. Hope that is ok.

t/Legacy/More.t Outdated Show resolved Hide resolved
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.
_env_get() ensures that if %ENV is locked we wont throw an exception
accessing a missing key. Tests might lock %ENV.
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.
@demerphq
Copy link
Contributor Author

Closing as per #907 (comment)

@demerphq demerphq closed this Mar 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants