Skip to content

Commit f718ae4

Browse files
committed
separate the test templates from the mainline stuff and tweak paths
1 parent 21a5ef0 commit f718ae4

File tree

6 files changed

+48
-20
lines changed

6 files changed

+48
-20
lines changed

mig/assets/templates/.gitkeep

Whitespace-only changes.

mig/shared/templates/__init__.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,20 @@
4040
else:
4141
from collections import ChainMap
4242

43-
TEMPLATES_DIR = os.path.abspath(os.path.dirname(__file__))
43+
TEMPLATES_DIR = os.path.join(MIG_BASE, 'mig/assets/templates')
4444
TEMPLATES_CACHE_DIR = os.path.join(TEMPLATES_DIR, '__jinja__')
4545

4646
_all_template_dirs = [
47-
os.path.join(TEMPLATES_DIR, 'pages'),
48-
os.path.join(TEMPLATES_DIR, 'partials')
47+
TEMPLATES_DIR,
4948
]
5049
_global_store = None
5150

5251

52+
def _clear_global_store():
53+
global _global_store
54+
_global_store = None
55+
56+
5357
def cache_dir():
5458
return TEMPLATES_CACHE_DIR
5559

@@ -114,7 +118,7 @@ def grab_template(self, template_name, template_group, output_format, template_g
114118
return self._template_environment.get_template(template_fqname, globals=template_globals)
115119

116120
def list_templates(self):
117-
return self._template_environment.list_templates()
121+
return [t for t in self._template_environment.list_templates() if t.endswith('.jinja')]
118122

119123
def extract_variables(self, template_fqname):
120124
template = self._template_environment.get_template(template_fqname)
@@ -142,14 +146,14 @@ def populated(template_dirs, cache_dir=None, context=None):
142146
return store
143147

144148

145-
def init_global_templates(configuration):
149+
def init_global_templates(configuration, _templates_dirs=template_dirs):
146150
global _global_store
147151

148152
if _global_store is not None:
149153
return _global_store
150154

151155
_global_store = TemplateStore.populated(
152-
template_dirs(),
156+
_templates_dirs(),
153157
cache_dir=cache_dir(),
154158
context=_FormatContext(configuration)
155159
)

tests/test_mig_shared_templates.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,52 @@
1+
import os
12
import shutil
23

3-
from tests.support import MigTestCase, testmain
4+
from tests.support import MigTestCase, testmain, \
5+
MIG_BASE, TEST_DATA_DIR, TEST_OUTPUT_DIR
46

5-
from mig.shared.templates import TemplateStore, cache_dir, template_dirs
7+
from mig.shared.templates import TemplateStore, template_dirs, \
8+
init_global_templates
69

10+
TEST_CACHE_DIR = os.path.join(TEST_OUTPUT_DIR, '__template_cache__')
11+
TEST_TMPL_DIR = os.path.join(TEST_DATA_DIR, 'templates')
712

8-
class TestMigSharedTemplates(MigTestCase):
9-
@classmethod
10-
def tearDownClass(cls):
11-
shutil.rmtree(cache_dir(), ignore_errors=True)
13+
14+
class TestMigSharedTemplates_instance(MigTestCase):
15+
def after_each(self):
16+
shutil.rmtree(TEST_CACHE_DIR, ignore_errors=True)
1217

1318
def _provide_configuration(self):
1419
return 'testconfig'
1520

1621
def test_the_creation_of_a_template_store(self):
17-
store = TemplateStore.populated(template_dirs(), cache_dir=cache_dir())
22+
store = TemplateStore.populated(TEST_TMPL_DIR, cache_dir=TEST_CACHE_DIR)
1823
self.assertIsInstance(store, TemplateStore)
1924

2025
def test_a_listing_all_templates(self):
21-
store = TemplateStore.populated(template_dirs(), cache_dir=cache_dir())
26+
store = TemplateStore.populated(TEST_TMPL_DIR, cache_dir=TEST_CACHE_DIR)
2227
self.assertEqual(len(store.list_templates()), 2)
2328

2429
def test_grab_template(self):
25-
store = TemplateStore.populated(template_dirs(), cache_dir=cache_dir())
26-
template = store.grab_template('other', 'partial', 'html')
30+
store = TemplateStore.populated(TEST_TMPL_DIR, cache_dir=TEST_CACHE_DIR)
31+
template = store.grab_template('other', 'test', 'html')
2732
pass
2833

2934
def test_variables_for_remplate_ref(self):
30-
store = TemplateStore.populated(template_dirs(), cache_dir=cache_dir())
31-
template_vars = store.extract_variables('partial_something.html.jinja')
35+
store = TemplateStore.populated(TEST_TMPL_DIR, cache_dir=TEST_CACHE_DIR)
36+
template_vars = store.extract_variables('test_something.html.jinja')
3237
self.assertEqual(template_vars, set(['content']))
3338

3439

40+
class TestMigSharedTemplates_global(MigTestCase):
41+
def _provide_configuration(self):
42+
return 'testconfig'
43+
44+
def test_cache_location(self):
45+
store = init_global_templates(self.configuration)
46+
47+
relative_cache_dir = os.path.relpath(store.cache_dir, MIG_BASE)
48+
self.assertEqual(relative_cache_dir, 'mig/assets/templates/__jinja__')
49+
50+
3551
if __name__ == '__main__':
3652
testmain()

tests/test_mig_wsgibin.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import stat
3535
import sys
3636

37-
from tests.support import PY2, MIG_BASE, MigTestCase, testmain, is_path_within
37+
from tests.support import PY2, MIG_BASE, TEST_DATA_DIR, MigTestCase, testmain
3838
from tests.support.snapshotsupp import SnapshotAssertMixin
3939
from tests.support.wsgisupp import prepare_wsgi, WsgiAssertMixin
4040

@@ -49,6 +49,13 @@
4949
from html.parser import HTMLParser
5050

5151

52+
def _force_test_templates(configuration):
53+
from mig.shared.templates import init_global_templates, _clear_global_store
54+
_clear_global_store()
55+
test_tmpl_dir = os.path.join(TEST_DATA_DIR, 'templates')
56+
init_global_templates(configuration, _templates_dirs=lambda: [test_tmpl_dir])
57+
58+
5259
class DocumentBasicsHtmlParser(HTMLParser):
5360
"""An HTML parser using builtin machinery to check basic html structure."""
5461

@@ -313,13 +320,14 @@ def test_objects_with_type_template(self):
313320
{
314321
'object_type': 'template',
315322
'template_name': 'something',
316-
'template_group': 'partial',
323+
'template_group': 'test',
317324
'template_args': {
318325
'content': 'here!!'
319326
}
320327
}
321328
]
322329
self.fake_backend.set_response(output_objects, returnvalues.OK)
330+
_force_test_templates(self.configuration)
323331

324332
wsgi_result = migwsgi.application(
325333
*self.application_args,

0 commit comments

Comments
 (0)