Skip to content

Commit

Permalink
Fix environment manipulation in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sondreso committed Feb 18, 2020
1 parent f6cea47 commit 6245b68
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
18 changes: 12 additions & 6 deletions tests/all/plugins/test_plugin_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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"]
Expand All @@ -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"]
Expand All @@ -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())
Expand All @@ -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:
Expand All @@ -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"
)
Expand Down
19 changes: 18 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import pytest


@pytest.fixture()
def source_root():
path_list = os.path.dirname(os.path.abspath(__file__)).split("/")
Expand All @@ -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"
)

0 comments on commit 6245b68

Please sign in to comment.