mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-14 17:22:17 -05:00
fix weird test isolation errors with os.environ and boto3
test_config manipulates os.environ. os.environ is an `environ` object, which acts like a dict but isn't in some subtle unknowable ways. The `reload_config` fixture would create a dict copy of the env, and then just call `os.environ = old_env` afterwards. Boto3 would then complain that it couldn't load credentials (despite us using the mock_s3 fixture and also not having creds in the environment in the first place). Not entirely sure why this happens, but it does. For some reason, it being a `dict` instead of an `environ` object causes the mocking of boto3 to fail. The solution is to not overwrite os.environ entirely, rather, use the standard dictionary setitem syntax to update the values to their previous values. Use `clear` to empty the environment too.
This commit is contained in:
@@ -21,7 +21,9 @@ def reload_config():
|
||||
|
||||
yield
|
||||
|
||||
os.environ = old_env
|
||||
for k, v in old_env.items():
|
||||
os.environ[k] = v
|
||||
|
||||
importlib.reload(config)
|
||||
|
||||
|
||||
|
||||
@@ -136,9 +136,10 @@ def os_environ():
|
||||
assert type(value) == str
|
||||
super().__setitem__(key, value)
|
||||
|
||||
os.environ = EnvironDict()
|
||||
os.environ.clear()
|
||||
yield
|
||||
os.environ = old_env
|
||||
for k, v in old_env.items():
|
||||
os.environ[k] = v
|
||||
|
||||
|
||||
def pytest_generate_tests(metafunc):
|
||||
|
||||
Reference in New Issue
Block a user