changeset 6341:6a9f82696750

tests: introduce test_context to handle internationalization As preparation to the migration to Turbogears2, introduce a test_context context manager to wrap certain tests. The context manager provides a translator for tests that need it. This translator was previously provided globally to all tests via the TestController. When swapping Pylons with Turbogears2, the new file kallithea/tests/test_context.py can be removed, and the test_context from tg2/tg/util/webtest.py should be used.
author Thomas De Schampheleire <thomas.de.schampheleire@gmail.com>
date Sun, 20 Nov 2016 20:51:40 +0100
parents 70d3a838a9f4
children 8e3137064ab6
files kallithea/tests/test_context.py
diffstat 1 files changed, 18 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/kallithea/tests/test_context.py	Sun Nov 20 20:51:40 2016 +0100
@@ -0,0 +1,18 @@
+# Based on Turbogears2 2.3.8: util/webtest.py but almost entirely stripped
+# and translator bits added.
+
+import pylons
+from pylons.i18n.translation import _get_translator
+
+class test_context(object):
+    def __init__(self, app):
+        self._app = app
+
+    def __enter__(self):
+        # Initialize a translator for tests that utilize i18n
+        translator = _get_translator(pylons.config.get('lang'))
+        pylons.translator._push_object(translator)
+        return self._app
+
+    def __exit__(self, exc_type, exc_val, exc_tb):
+        pylons.translator._pop_object()