summaryrefslogtreecommitdiff
path: root/pyramid/tests/test_scaffolds/test_template.py
blob: 88a31e802b84934ef7cd0d137fb5358cdbf09aff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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, b'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, b'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, b'a')

    def test_template_renderer_expr_value_is_None(self):
        inst = self._makeOne()
        result = inst.template_renderer('{{a}}', {'a':None})
        self.assertEqual(result, b'')

    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