Skip to content

Commit 337f775

Browse files
committed
Consolidate and fix Environ fixtures
1 parent ae84064 commit 337f775

File tree

3 files changed

+18
-42
lines changed

3 files changed

+18
-42
lines changed

tests/agent_unittests/test_utilization_settings.py

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from importlib import reload
1818

1919
import pytest
20+
from testing_support.fixtures import Environ
2021

2122
# these will be reloaded for each test
2223
import newrelic.config
@@ -65,25 +66,9 @@
6566
"NEW_RELIC_HEROKU_DYNO_NAME_PREFIXES_TO_SHORTEN": "meow wruff",
6667
}
6768

68-
INITIAL_ENV = os.environ
6969

7070
# Tests for loading settings and testing for values precedence
7171

72-
73-
class Environ:
74-
def __init__(self, env_dict):
75-
self.env_dict = {}
76-
for key in env_dict.keys():
77-
self.env_dict[key] = str(env_dict[key])
78-
79-
def __enter__(self):
80-
os.environ.update(self.env_dict)
81-
82-
def __exit__(self, *args, **kwargs):
83-
os.environ.clear()
84-
os.environ[:] = INITIAL_ENV[:]
85-
86-
8772
def reset_agent_config(ini_contents, env_dict):
8873
@function_wrapper
8974
def reset(wrapped, instance, args, kwargs):

tests/cross_agent/test_pcf_utilization_data.py

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
import os
1717

1818
import pytest
19+
from testing_support.fixtures import Environ
1920
from testing_support.validators.validate_internal_metrics import validate_internal_metrics
2021

2122
from newrelic.common.utilization import PCFUtilization
2223

2324
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
24-
INITIAL_ENV = os.environ
2525
FIXTURE = os.path.normpath(os.path.join(CURRENT_DIR, "fixtures", "utilization_vendor_specific", "pcf.json"))
2626

2727
_parameters_list = ["testname", "env_vars", "expected_vendors_hash", "expected_metrics"]
@@ -42,27 +42,6 @@ def _parametrize_test(test):
4242
_pcf_tests = [_parametrize_test(t) for t in _load_tests()]
4343

4444

45-
class Environ:
46-
def __init__(self, env_dict):
47-
env_dict = env_dict or {}
48-
cleaned_env_dict = {}
49-
for key, val in env_dict.items():
50-
if val is None:
51-
continue
52-
elif not isinstance(val, str):
53-
cleaned_env_dict[key] = val.encode("utf-8")
54-
else:
55-
cleaned_env_dict[key] = val
56-
self.env_dict = cleaned_env_dict
57-
58-
def __enter__(self):
59-
os.environ.update(self.env_dict)
60-
61-
def __exit__(self, *args, **kwargs):
62-
os.environ.clear()
63-
os.environ[:] = INITIAL_ENV[:]
64-
65-
6645
class MockResponse:
6746
def __init__(self, code, body):
6847
self.code = code

tests/testing_support/fixtures.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,17 +1335,29 @@ def check_error_attributes(
13351335
class Environ:
13361336
"""Context manager for setting environment variables temporarily."""
13371337

1338-
def __init__(self, **kwargs):
1339-
self._original_environ = os.environ
1340-
self._environ_dict = kwargs
1338+
def __init__(self, env_dict=None, **kwargs):
1339+
self._environ_dict = {}
1340+
1341+
env_dict = env_dict or kwargs
1342+
for key, val in env_dict.items():
1343+
if val is None:
1344+
continue
1345+
elif isinstance(val, str):
1346+
self._environ_dict[key] = val
1347+
elif isinstance(val, bytes):
1348+
self._environ_dict[key] = val.encode("utf-8")
1349+
else:
1350+
self._environ_dict[key] = str(val)
13411351

13421352
def __enter__(self):
1353+
self._original_environ = os.environ.copy()
1354+
13431355
for key, val in self._environ_dict.items():
13441356
os.environ[key] = str(val)
13451357

13461358
def __exit__(self, exc, val, tb):
13471359
os.environ.clear()
1348-
os.environ[:] = self._original_environ[:]
1360+
os.environ.update(self._original_environ)
13491361

13501362

13511363
class TerminatingPopen(subprocess.Popen):

0 commit comments

Comments
 (0)