dont reassign os.environ

os.environ is an `environ` object, not a dict. by only interacting with
it through builtin functions we can ensure it remains properly
accessible to third party libraries which might interact with it in
different ways.

See https://github.com/alphagov/notifications-api/commit/d2441466 for
more detail
This commit is contained in:
Leo Hemsted
2020-01-07 11:54:10 +00:00
parent 39ec952c45
commit 91adadfed0
3 changed files with 15 additions and 12 deletions

View File

@@ -12,21 +12,24 @@ def cf_conf():
@pytest.fixture
def reload_config():
def reload_config(os_environ):
"""
Reset config, by simply re-running config.py from a fresh environment
"""
old_env = os.environ.copy()
os.environ.clear()
yield
os.environ = old_env
os.environ.clear()
for k, v in old_env.items():
os.environ[k] = v
importlib.reload(config)
def test_load_cloudfoundry_config_if_available(monkeypatch, reload_config):
def test_load_cloudfoundry_config_if_available(reload_config):
os.environ['API_HOST_NAME'] = 'env'
monkeypatch.setenv('VCAP_APPLICATION', 'some json blob')
os.environ['VCAP_APPLICATION'] = 'some json blob'
with mock.patch('app.cloudfoundry_config.extract_cloudfoundry_config', side_effect=cf_conf) as cf_config:
# reload config so that its module level code (ie: all of it) is re-instantiated
@@ -38,10 +41,10 @@ def test_load_cloudfoundry_config_if_available(monkeypatch, reload_config):
assert config.Config.API_HOST_NAME == 'cf'
def test_load_config_if_cloudfoundry_not_available(monkeypatch, reload_config):
def test_load_config_if_cloudfoundry_not_available(reload_config):
os.environ['API_HOST_NAME'] = 'env'
monkeypatch.delenv('VCAP_APPLICATION', raising=False)
os.environ.pop('VCAP_APPLICATION', None)
with mock.patch('app.cloudfoundry_config.extract_cloudfoundry_config') as cf_config:
# reload config so that its module level code (ie: all of it) is re-instantiated