Skip to content

Commit 9180ec5

Browse files
committed
Merge branches 'fix/configuration-instance-isolation' and 'test/allow-support-library-to-provide-a-configuration-object' into baseline/cat
2 parents fad081a + 3944a44 commit 9180ec5

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

tests/support/__init__.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import sys
4141
from unittest import TestCase, main as testmain
4242

43+
from tests.support.configsupp import FakeConfiguration
4344
from tests.support.suppconst import MIG_BASE, TEST_BASE, TEST_FIXTURE_DIR, \
4445
TEST_OUTPUT_DIR
4546

@@ -48,6 +49,20 @@
4849
# force defaults to a local environment
4950
os.environ['MIG_ENV'] = 'local'
5051

52+
# expose the configuraed environment as a constant
53+
MIG_ENV = os.environ['MIG_ENV']
54+
55+
if MIG_ENV == 'local':
56+
# force testconfig as the conig file path
57+
is_py2 = PY2
58+
_conf_dir_suffix = "-py%s" % ('2' if is_py2 else '3',)
59+
_conf_dir = "testconfs%s" % (_conf_dir_suffix,)
60+
_local_conf = os.path.join(
61+
MIG_BASE, 'envhelp/output', _conf_dir, 'MiGserver.conf')
62+
_config_file = os.getenv('MIG_CONF', None)
63+
if _config_file is None:
64+
os.environ['MIG_CONF'] = _local_conf
65+
5166
# All MiG related code will at some point include bits from the mig module
5267
# namespace. Rather than have this knowledge spread through every test file,
5368
# make the sole responsbility of test files to find the support file and
@@ -103,6 +118,7 @@ def __init__(self, *args):
103118
super(MigTestCase, self).__init__(*args)
104119
self._cleanup_checks = list()
105120
self._cleanup_paths = set()
121+
self._configuration = None
106122
self._logger = None
107123
self._skip_logging = False
108124

@@ -153,6 +169,31 @@ def _reset_logging(self, stream):
153169
root_handler = root_logger.handlers[0]
154170
root_handler.stream = stream
155171

172+
# testcase defaults
173+
174+
@staticmethod
175+
def _make_configuration_instance(configuration_to_make):
176+
if configuration_to_make == 'fakeconfig':
177+
return FakeConfiguration()
178+
elif configuration_to_make == 'testconfig':
179+
from mig.shared.conf import get_configuration_object
180+
return get_configuration_object(skip_log=True, disable_auth_log=True)
181+
else:
182+
raise AssertionError(
183+
"MigTestCase: unknown configuration %r" % (configuration_to_make,))
184+
185+
def _provide_configuration(self):
186+
return 'fakeconfig'
187+
188+
@property
189+
def configuration(self):
190+
"""Init a fake configuration if not already done"""
191+
if self._configuration is None:
192+
configuration_to_make = self._provide_configuration()
193+
self._configuration = self._make_configuration_instance(
194+
configuration_to_make)
195+
return self._configuration
196+
156197
@property
157198
def logger(self):
158199
"""Init a fake logger if not already done"""
@@ -199,6 +240,10 @@ def assertPathExists(self, relative_path):
199240
assert not os.path.isabs(
200241
relative_path), "expected relative path within output folder"
201242
absolute_path = os.path.join(TEST_OUTPUT_DIR, relative_path)
243+
return MigTestCase._absolute_path_kind(absolute_path)
244+
245+
@staticmethod
246+
def _absolute_path_kind(absolute_path):
202247
stat_result = os.lstat(absolute_path)
203248
if stat.S_ISLNK(stat_result.st_mode):
204249
return "symlink"

tests/test_support.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
import unittest
55

66
from tests.support import MigTestCase, PY2, testmain, temppath, \
7-
AssertOver
7+
AssertOver, FakeConfiguration
8+
9+
from mig.shared.conf import get_configuration_object
10+
from mig.shared.configuration import Configuration
811

912

1013
class InstrumentedAssertOver(AssertOver):
@@ -39,6 +42,17 @@ def _class_attribute(self, name, **kwargs):
3942
else:
4043
return getattr(cls, name, None)
4144

45+
def test_provides_a_fake_configuration(self):
46+
configuration = self.configuration
47+
48+
self.assertIsInstance(configuration, FakeConfiguration)
49+
50+
def test_provides_a_fake_configuration_for_the_duration_of_the_test(self):
51+
c1 = self.configuration
52+
c2 = self.configuration
53+
54+
self.assertIs(c2, c1)
55+
4256
@unittest.skipIf(PY2, "Python 3 only")
4357
def test_unclosed_files_are_recorded(self):
4458
tmp_path = temppath("support-unclosed", self)
@@ -88,5 +102,23 @@ def test_when_asserting_over_multiple_values_after(self):
88102
self.assertTrue(attempt_wrapper.was_check_callable_called())
89103

90104

105+
class SupportTestCase_overridden_configuration(MigTestCase):
106+
def _provide_configuration(self):
107+
return 'testconfig'
108+
109+
def test_provides_the_test_configuration(self):
110+
expected_last_dir = 'testconfs-py2' if PY2 else 'testconfs-py3'
111+
112+
configuration = self.configuration
113+
114+
# check we have a real config object
115+
self.assertIsInstance(configuration, Configuration)
116+
# check for having loaded a config file from a test config dir
117+
config_file_path_parts = configuration.config_file.split(os.path.sep)
118+
config_file_path_parts.pop() # discard file part
119+
config_file_last_dir = config_file_path_parts.pop()
120+
self.assertTrue(config_file_last_dir, expected_last_dir)
121+
122+
91123
if __name__ == '__main__':
92124
testmain()

0 commit comments

Comments
 (0)