From 6245b684210c51f26adad5316e8486f3edc6c76c Mon Sep 17 00:00:00 2001 From: Sondre Sortland Date: Mon, 17 Feb 2020 10:06:31 +0100 Subject: [PATCH] Fix environment manipulation in tests --- tests/all/plugins/test_plugin_context.py | 18 ++++++++++++------ tests/conftest.py | 19 ++++++++++++++++++- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/tests/all/plugins/test_plugin_context.py b/tests/all/plugins/test_plugin_context.py index 2d47ae3ba0e..7c740b8e8fc 100644 --- a/tests/all/plugins/test_plugin_context.py +++ b/tests/all/plugins/test_plugin_context.py @@ -2,9 +2,9 @@ import os import sys import unittest +from _pytest.monkeypatch import MonkeyPatch from ert_shared.plugins import ErtPluginContext - import tests.all.plugins.dummy_plugins as dummy_plugins @@ -18,8 +18,16 @@ class PluginContextTest(unittest.TestCase): + def setUp(self): + self.monkeypatch = MonkeyPatch() + pass + + def tearDown(self): + self.monkeypatch.undo() + @unittest.skipIf(sys.version_info.major < 3, "Plugin Manager is Python 3 only") def test_no_plugins(self): + self.monkeypatch.delenv("ERT_SITE_CONFIG", raising=False) with ErtPluginContext(plugins=[]) as c: with self.assertRaises(KeyError): os.environ["ECL100_SITE_CONFIG"] @@ -42,6 +50,7 @@ def test_no_plugins(self): @unittest.skipIf(sys.version_info.major < 3, "Plugin Manager is Python 3 only") def test_with_plugins(self): + self.monkeypatch.delenv("ERT_SITE_CONFIG", raising=False) with ErtPluginContext(plugins=[dummy_plugins]) as c: with self.assertRaises(KeyError): os.environ["ECL100_SITE_CONFIG"] @@ -51,7 +60,7 @@ def test_with_plugins(self): os.environ["FLOW_SITE_CONFIG"] with self.assertRaises(KeyError): os.environ["RMS_SITE_CONFIG"] - + self.assertTrue(os.path.isfile(os.environ["ERT_SITE_CONFIG"])) with open(os.environ["ERT_SITE_CONFIG"]) as f: self.assertEqual(f.read(), c.plugin_manager.get_site_config_content()) @@ -65,7 +74,7 @@ def test_with_plugins(self): @unittest.skipIf(sys.version_info.major < 3, "Plugin Manager is Python 3 only") def test_already_set(self): for var in env_vars: - os.environ[var] = "TEST" + self.monkeypatch.setenv(var, "TEST") with ErtPluginContext(plugins=[dummy_plugins]) as c: for var in env_vars: @@ -74,9 +83,6 @@ def test_already_set(self): for var in env_vars: self.assertEqual("TEST", os.environ[var]) - for var in env_vars: - del os.environ[var] - @unittest.skipIf( sys.version_info.major > 2, "Skipping Plugin Manager Python 2 test" ) diff --git a/tests/conftest.py b/tests/conftest.py index 6b515214717..5aa741f922a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,6 +1,7 @@ import os import pytest + @pytest.fixture() def source_root(): path_list = os.path.dirname(os.path.abspath(__file__)).split("/") @@ -9,4 +10,20 @@ def source_root(): if os.path.isdir(git_path): return os.path.join(os.sep, *path_list) path_list.pop() - raise RuntimeError('Cannot find the source folder') + raise RuntimeError("Cannot find the source folder") + + +@pytest.fixture(autouse=True) +def env_save(): + environment_pre = [ + (key, val) for key, val in os.environ.items() if key != "PYTEST_CURRENT_TEST" + ] + yield + environment_post = [ + (key, val) for key, val in os.environ.items() if key != "PYTEST_CURRENT_TEST" + ] + if set(environment_pre) != set(environment_post): + raise EnvironmentError( + "Your environment has changed after that test, please reset" + ) +