summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris McDonough <chrism@plope.com>2011-10-06 05:11:28 -0400
committerChris McDonough <chrism@plope.com>2011-10-06 05:11:28 -0400
commit71260a1b55f07c44f5a6275f34b6601cf66158db (patch)
tree0a2ff0867aa248e21073c4859b71c40d7da5eebd
parentf8869cb0664506204b22aa791003a6d5f8ded58c (diff)
downloadpyramid-71260a1b55f07c44f5a6275f34b6601cf66158db.tar.gz
pyramid-71260a1b55f07c44f5a6275f34b6601cf66158db.tar.bz2
pyramid-71260a1b55f07c44f5a6275f34b6601cf66158db.zip
add tests for template.py
-rw-r--r--pyramid/scaffolds/template.py43
-rw-r--r--pyramid/tests/test_scaffolds/__init__.py1
-rw-r--r--pyramid/tests/test_scaffolds/test_template.py137
3 files changed, 166 insertions, 15 deletions
diff --git a/pyramid/scaffolds/template.py b/pyramid/scaffolds/template.py
index f9af8010a..4ffa946d3 100644
--- a/pyramid/scaffolds/template.py
+++ b/pyramid/scaffolds/template.py
@@ -16,6 +16,8 @@ from pyramid.scaffolds import copydir
fsenc = sys.getfilesystemencoding()
class Template(object):
+ copydir = copydir # for testing
+ _template_dir = None
def __init__(self, name):
self.name = name
@@ -47,13 +49,13 @@ class Template(object):
self.write_files(command, output_dir, vars)
self.post(command, output_dir, vars)
- def pre(self, command, output_dir, vars):
+ def pre(self, command, output_dir, vars): # pragma: no cover
"""
Called before template is applied.
"""
pass
- def post(self, command, output_dir, vars):
+ def post(self, command, output_dir, vars): # pragma: no cover
"""
Called after template is applied.
"""
@@ -61,21 +63,32 @@ class Template(object):
def write_files(self, command, output_dir, vars):
template_dir = self.template_dir()
- if not os.path.exists(output_dir):
- print("Creating directory %s" % output_dir)
+ if not self.exists(output_dir):
+ self.out("Creating directory %s" % output_dir)
if not command.simulate:
# Don't let copydir create this top-level directory,
# since copydir will svn add it sometimes:
- os.makedirs(output_dir)
- copydir.copy_dir(template_dir, output_dir,
- vars,
- verbosity=command.verbose,
- simulate=command.options.simulate,
- interactive=command.interactive,
- overwrite=command.options.overwrite,
- indent=1,
- template_renderer=self.template_renderer)
-
+ self.makedirs(output_dir)
+ self.copydir.copy_dir(
+ template_dir,
+ output_dir,
+ vars,
+ verbosity=command.verbose,
+ simulate=command.options.simulate,
+ interactive=command.interactive,
+ overwrite=command.options.overwrite,
+ indent=1,
+ template_renderer=self.template_renderer
+ )
+
+ def makedirs(self, dir): # pragma: no cover
+ return os.makedirs(dir)
+
+ def exists(self, path): # pragma: no cover
+ return os.path.exists(path)
+
+ def out(self, msg): # pragma: no cover
+ print(msg)
class TypeMapper(dict):
@@ -109,7 +122,7 @@ def substitute_double_braces(content, values):
return values[value]
return double_brace_pattern.sub(double_bracerepl, content)
-def _add_except(exc, info):
+def _add_except(exc, info): # pragma: no cover
if not hasattr(exc, 'args') or exc.args is None:
return
args = list(exc.args)
diff --git a/pyramid/tests/test_scaffolds/__init__.py b/pyramid/tests/test_scaffolds/__init__.py
new file mode 100644
index 000000000..5bb534f79
--- /dev/null
+++ b/pyramid/tests/test_scaffolds/__init__.py
@@ -0,0 +1 @@
+# package
diff --git a/pyramid/tests/test_scaffolds/test_template.py b/pyramid/tests/test_scaffolds/test_template.py
new file mode 100644
index 000000000..2943a0057
--- /dev/null
+++ b/pyramid/tests/test_scaffolds/test_template.py
@@ -0,0 +1,137 @@
+import unittest
+
+from pyramid.compat import bytes_
+
+class TestTemplate(unittest.TestCase):
+ def _makeOne(self, name='whatever'):
+ from pyramid.scaffolds.template import Template
+ return Template(name)
+
+ def test_template_renderer_success(self):
+ inst = self._makeOne()
+ result = inst.template_renderer('{{a}} {{b}}', {'a':'1', 'b':'2'})
+ self.assertEqual(result, bytes_('1 2'))
+
+ def test_template_renderer_expr_failure(self):
+ inst = self._makeOne()
+ self.assertRaises(AttributeError, inst.template_renderer,
+ '{{a.foo}}', {'a':'1', 'b':'2'})
+
+ def test_template_renderer_expr_success(self):
+ inst = self._makeOne()
+ result = inst.template_renderer('{{a.lower()}}', {'a':'A'})
+ self.assertEqual(result, 'a')
+
+ def test_template_renderer_expr_success_via_pipe(self):
+ inst = self._makeOne()
+ result = inst.template_renderer('{{b|c|a.lower()}}', {'a':'A'})
+ self.assertEqual(result, 'a')
+
+ def test_template_renderer_expr_success_via_pipe2(self):
+ inst = self._makeOne()
+ result = inst.template_renderer('{{b|a.lower()|c}}', {'a':'A'})
+ self.assertEqual(result, 'a')
+
+ def test_template_renderer_expr_value_is_None(self):
+ inst = self._makeOne()
+ result = inst.template_renderer('{{a}}', {'a':None})
+ self.assertEqual(result, '')
+
+ def test_module_dir(self):
+ import sys
+ import pkg_resources
+ package = sys.modules['pyramid.scaffolds.template']
+ path = pkg_resources.resource_filename(package.__name__, '')
+ inst = self._makeOne()
+ result = inst.module_dir()
+ self.assertEqual(result, path)
+
+ def test_template_dir__template_dir_is_None(self):
+ inst = self._makeOne()
+ self.assertRaises(AssertionError, inst.template_dir)
+
+ def test_template_dir__template_dir_is_tuple(self):
+ inst = self._makeOne()
+ inst._template_dir = ('a', 'b')
+ self.assertEqual(inst.template_dir(), ('a', 'b'))
+
+ def test_template_dir__template_dir_is_not_None(self):
+ import os
+ import sys
+ import pkg_resources
+ package = sys.modules['pyramid.scaffolds.template']
+ path = pkg_resources.resource_filename(package.__name__, '')
+ inst = self._makeOne()
+ inst._template_dir ='foo'
+ result = inst.template_dir()
+ self.assertEqual(result, os.path.join(path, 'foo'))
+
+ def test_write_files_path_exists(self):
+ import os
+ import sys
+ import pkg_resources
+ package = sys.modules['pyramid.scaffolds.template']
+ path = pkg_resources.resource_filename(package.__name__, '')
+ inst = self._makeOne()
+ inst._template_dir = 'foo'
+ inst.exists = lambda *arg: True
+ copydir = DummyCopydir()
+ inst.copydir = copydir
+ command = DummyCommand()
+ inst.write_files(command, 'output dir', {'a':1})
+ self.assertEqual(copydir.template_dir, os.path.join(path, 'foo'))
+ self.assertEqual(copydir.output_dir, 'output dir')
+ self.assertEqual(copydir.vars, {'a':1})
+ self.assertEqual(copydir.kw,
+ {'template_renderer':inst.template_renderer,
+ 'indent':1,
+ 'verbosity':True,
+ 'simulate':True,
+ 'overwrite':False,
+ 'interactive':False,
+ })
+
+ def test_write_files_path_missing(self):
+ L = []
+ inst = self._makeOne()
+ inst._template_dir = 'foo'
+ inst.exists = lambda *arg: False
+ inst.out = lambda *arg: None
+ inst.makedirs = lambda dir: L.append(dir)
+ copydir = DummyCopydir()
+ inst.copydir = copydir
+ command = DummyCommand()
+ inst.write_files(command, 'output dir', {'a':1})
+ self.assertEqual(L, ['output dir'])
+
+ def test_run(self):
+ L = []
+ inst = self._makeOne()
+ inst._template_dir = 'foo'
+ inst.exists = lambda *arg: False
+ inst.out = lambda *arg: None
+ inst.makedirs = lambda dir: L.append(dir)
+ copydir = DummyCopydir()
+ inst.copydir = copydir
+ command = DummyCommand()
+ inst.run(command, 'output dir', {'a':1})
+ self.assertEqual(L, ['output dir'])
+
+class DummyCopydir(object):
+ def copy_dir(self, template_dir, output_dir, vars, **kw):
+ self.template_dir = template_dir
+ self.output_dir = output_dir
+ self.vars = vars
+ self.kw = kw
+
+class DummyOptions(object):
+ simulate = True
+ overwrite = False
+
+class DummyCommand(object):
+ options = DummyOptions()
+ verbose = True
+ interactive = False
+ simulate = False
+
+