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:
Leo Hemsted
2020-01-06 16:08:33 +00:00
parent c3b1766220
commit d244146638
2 changed files with 6 additions and 3 deletions

View File

@@ -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)

View File

@@ -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):