|
1
|
|
|
# |
|
2
|
|
|
# Copyright (C) 2015 - 2017 Satoru SATOH <ssato @ redhat.com> |
|
3
|
|
|
# License: MIT |
|
4
|
|
|
# |
|
5
|
|
|
# pylint: disable=missing-docstring, unused-variable, invalid-name |
|
6
|
|
|
import os.path |
|
7
|
|
|
import os |
|
8
|
|
|
import unittest |
|
9
|
|
|
import mock |
|
10
|
|
|
|
|
11
|
|
|
import anyconfig.template as TT |
|
12
|
|
|
import tests.common |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
C_1 = """A char is 'a'. |
|
16
|
|
|
A char is 'b'. |
|
17
|
|
|
A char is 'c'. |
|
18
|
|
|
""" |
|
19
|
|
|
|
|
20
|
|
|
TMPLS = [('00.j2', "{% include '10.j2' %}" + os.linesep, C_1), |
|
21
|
|
|
('10.j2', """{% for c in ['a', 'b', 'c'] -%} |
|
22
|
|
|
A char is '{{ c }}'. |
|
23
|
|
|
{% endfor %} |
|
24
|
|
|
""", C_1)] |
|
25
|
|
|
|
|
26
|
|
|
C_2 = "-1" |
|
27
|
|
|
TMPL_WITH_FILTER = ('11.j2', "{{ 1|negate }}", C_2) |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
def negate(value): |
|
31
|
|
|
return -value |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
class Test(unittest.TestCase): |
|
35
|
|
|
|
|
36
|
|
|
templates = TMPLS |
|
37
|
|
|
|
|
38
|
|
|
def setUp(self): |
|
39
|
|
|
self.workdir = tests.common.setup_workdir() |
|
40
|
|
|
for fname, tmpl_s, _ctx in self.templates + [TMPL_WITH_FILTER]: |
|
41
|
|
|
fpath = os.path.join(self.workdir, fname) |
|
42
|
|
|
open(fpath, 'w').write(tmpl_s) |
|
43
|
|
|
|
|
44
|
|
|
def tearDown(self): |
|
45
|
|
|
tests.common.cleanup_workdir(self.workdir) |
|
46
|
|
|
|
|
47
|
|
|
def test_10_render_impl__wo_paths(self): |
|
48
|
|
|
if TT.SUPPORTED: |
|
49
|
|
|
for fname, _str, ctx in self.templates: |
|
50
|
|
|
fpath = os.path.join(self.workdir, fname) |
|
51
|
|
|
c_r = TT.render_impl(fpath) |
|
52
|
|
|
self.assertEqual(c_r, ctx) |
|
53
|
|
|
|
|
54
|
|
|
def test_12_render_impl__w_paths(self): |
|
55
|
|
|
if TT.SUPPORTED: |
|
56
|
|
|
for fname, _str, ctx in self.templates: |
|
57
|
|
|
fpath = os.path.join(self.workdir, fname) |
|
58
|
|
|
c_r = TT.render_impl(os.path.basename(fpath), |
|
59
|
|
|
paths=[os.path.dirname(fpath)]) |
|
60
|
|
|
self.assertEqual(c_r, ctx) |
|
61
|
|
|
|
|
62
|
|
|
def test_20_render__wo_paths(self): |
|
63
|
|
|
if TT.SUPPORTED: |
|
64
|
|
|
for fname, _str, ctx in self.templates: |
|
65
|
|
|
fpath = os.path.join(self.workdir, fname) |
|
66
|
|
|
c_r = TT.render(fpath) |
|
67
|
|
|
self.assertEqual(c_r, ctx) |
|
68
|
|
|
|
|
69
|
|
|
def test_22_render__w_wrong_tpath(self): |
|
70
|
|
|
if TT.SUPPORTED: |
|
71
|
|
|
mpt = "anyconfig.compat.raw_input" |
|
72
|
|
|
|
|
73
|
|
|
ng_t = os.path.join(self.workdir, "ng.j2") |
|
74
|
|
|
ok_t = os.path.join(self.workdir, "ok.j2") |
|
75
|
|
|
ok_t_content = "a: {{ a }}" |
|
76
|
|
|
ok_content = "a: aaa" |
|
77
|
|
|
ctx = dict(a="aaa", ) |
|
78
|
|
|
|
|
79
|
|
|
open(ok_t, 'w').write(ok_t_content) |
|
80
|
|
|
|
|
81
|
|
|
with mock.patch(mpt, return_value=ok_t): |
|
82
|
|
|
c_r = TT.render(ng_t, ctx, ask=True) |
|
83
|
|
|
self.assertEqual(c_r, ok_content) |
|
84
|
|
|
try: |
|
85
|
|
|
TT.render(ng_t, ctx, ask=False) |
|
86
|
|
|
assert False # force raising an exception. |
|
87
|
|
|
except TT.TemplateNotFound: |
|
88
|
|
|
pass |
|
89
|
|
|
|
|
90
|
|
|
def test_24_render__wo_paths(self): |
|
91
|
|
|
if TT.SUPPORTED: |
|
92
|
|
|
fname = self.templates[0][0] |
|
93
|
|
|
assert os.path.exists(os.path.join(self.workdir, fname)) |
|
94
|
|
|
|
|
95
|
|
|
subdir = os.path.join(self.workdir, "a/b/c") |
|
96
|
|
|
os.makedirs(subdir) |
|
97
|
|
|
|
|
98
|
|
|
tmpl = os.path.join(subdir, fname) |
|
99
|
|
|
open(tmpl, 'w').write("{{ a|default('aaa') }}") |
|
100
|
|
|
|
|
101
|
|
|
c_r = TT.render(tmpl) |
|
102
|
|
|
self.assertEqual(c_r, "aaa") |
|
103
|
|
|
|
|
104
|
|
|
def test_25_render__w_prefer_paths(self): |
|
105
|
|
|
if TT.SUPPORTED: |
|
106
|
|
|
fname = self.templates[0][0] |
|
107
|
|
|
assert os.path.exists(os.path.join(self.workdir, fname)) |
|
108
|
|
|
|
|
109
|
|
|
subdir = os.path.join(self.workdir, "a/b/c") |
|
110
|
|
|
os.makedirs(subdir) |
|
111
|
|
|
|
|
112
|
|
|
tmpl = os.path.join(subdir, fname) |
|
113
|
|
|
open(tmpl, 'w').write("{{ a|default('aaa') }}") |
|
114
|
|
|
|
|
115
|
|
|
c_r = TT.render(tmpl, paths=[self.workdir]) |
|
116
|
|
|
self.assertNotEqual(c_r, "aaa") |
|
117
|
|
|
self.assertEqual(c_r, self.templates[0][-1]) |
|
118
|
|
|
|
|
119
|
|
|
def test_30_try_render_with_empty_filepath_and_content(self): |
|
120
|
|
|
if TT.SUPPORTED: |
|
121
|
|
|
try: |
|
122
|
|
|
TT.try_render() |
|
123
|
|
|
except ValueError: |
|
124
|
|
|
exc_was_raised = True |
|
125
|
|
|
self.assertTrue(exc_was_raised) |
|
126
|
|
|
|
|
127
|
|
|
def test_40_render__w_filter(self): |
|
128
|
|
|
if TT.SUPPORTED: |
|
129
|
|
|
fname, _, ctx = TMPL_WITH_FILTER |
|
130
|
|
|
fpath = os.path.join(self.workdir, fname) |
|
131
|
|
|
c_r = TT.render(fpath, filters={"negate": negate}) |
|
132
|
|
|
self.assertEqual(c_r, ctx) |
|
133
|
|
|
|
|
134
|
|
|
# vim:sw=4:ts=4:et: |
|
135
|
|
|
|