Michael Merickel
2018-10-26 035f6cf8238211d097c991677fde6b5bc046a57b
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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_render_template_success(self):
        inst = self._makeOne()
        result = inst.render_template('{{a}} {{b}}', {'a': '1', 'b': '2'})
        self.assertEqual(result, bytes_('1 2'))
 
    def test_render_template_expr_failure(self):
        inst = self._makeOne()
        self.assertRaises(
            AttributeError,
            inst.render_template,
            '{{a.foo}}',
            {'a': '1', 'b': '2'},
        )
 
    def test_render_template_expr_success(self):
        inst = self._makeOne()
        result = inst.render_template('{{a.lower()}}', {'a': 'A'})
        self.assertEqual(result, b'a')
 
    def test_render_template_expr_success_via_pipe(self):
        inst = self._makeOne()
        result = inst.render_template('{{b|c|a.lower()}}', {'a': 'A'})
        self.assertEqual(result, b'a')
 
    def test_render_template_expr_success_via_pipe2(self):
        inst = self._makeOne()
        result = inst.render_template('{{b|a.lower()|c}}', {'a': 'A'})
        self.assertEqual(result, b'a')
 
    def test_render_template_expr_value_is_None(self):
        inst = self._makeOne()
        result = inst.render_template('{{a}}', {'a': None})
        self.assertEqual(result, b'')
 
    def test_render_template_with_escaped_double_braces(self):
        inst = self._makeOne()
        result = inst.render_template(
            '{{a}} {{b}} \\{\\{a\\}\\} \\{\\{c\\}\\}', {'a': '1', 'b': '2'}
        )
        self.assertEqual(result, bytes_('1 2 {{a}} {{c}}'))
 
    def test_render_template_with_breaking_escaped_braces(self):
        inst = self._makeOne()
        result = inst.render_template(
            '{{a}} {{b}} \\{\\{a\\} \\{b\\}\\}', {'a': '1', 'b': '2'}
        )
        self.assertEqual(result, bytes_('1 2 \\{\\{a\\} \\{b\\}\\}'))
 
    def test_render_template_with_escaped_single_braces(self):
        inst = self._makeOne()
        result = inst.render_template(
            '{{a}} {{b}} \\{a\\} \\{b', {'a': '1', 'b': '2'}
        )
        self.assertEqual(result, bytes_('1 2 \\{a\\} \\{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.render_template,
                'indent': 1,
                'verbosity': 1,
                'simulate': False,
                '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'])
 
    def test_check_vars(self):
        inst = self._makeOne()
        self.assertRaises(RuntimeError, inst.check_vars, 'one', 'two')
 
 
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 DummyArgs(object):
    simulate = False
    overwrite = False
    interactive = False
 
 
class DummyCommand(object):
    args = DummyArgs()
    verbosity = 1