Debian Codegraph @

debian/patches/nose-to-nose2

raw
1 Description: use nose2 instead of nose to test the package
2 Origin: vendor
3 Forwarded: not-needed
4 Last-Update: 2022-09-23
5
6 --- a/tests/render-test.py
7 +++ /dev/null
8 @@ -1,240 +0,0 @@
9 -# -*- coding: utf-8 -*-
10 -
11 -from __future__ import unicode_literals
12 -
13 -import unittest
14 -import os, sys, io, os.path, tempfile
15 -from copy import copy
16 -from contextlib import contextmanager
17 -from jinja2.exceptions import UndefinedError
18 -
19 -from j2cli.cli import render_command
20 -
21 -@contextmanager
22 -def mktemp(contents):
23 - """ Create a temporary file with the given contents, and yield its path """
24 - _, path = tempfile.mkstemp()
25 - fp = io.open(path, 'wt+', encoding='utf-8')
26 - fp.write(contents)
27 - fp.flush()
28 - try:
29 - yield path
30 - finally:
31 - fp.close()
32 - os.unlink(path)
33 -
34 -
35 -@contextmanager
36 -def mock_environ(new_env):
37 - old_env = copy(os.environ)
38 - os.environ.update(new_env)
39 - yield
40 - os.environ.clear()
41 - os.environ.update(old_env)
42 -
43 -
44 -class RenderTest(unittest.TestCase):
45 - def setUp(self):
46 - os.chdir(
47 - os.path.dirname(__file__)
48 - )
49 -
50 - def _testme(self, argv, expected_output, stdin=None, env=None):
51 - """ Helper test shortcut """
52 - with mock_environ(env or {}):
53 - result = render_command(os.getcwd(), env or {}, stdin, argv)
54 - if isinstance(result, bytes):
55 - result = result.decode('utf-8')
56 - self.assertEqual(result, expected_output)
57 -
58 - #: The expected output
59 - expected_output = """server {
60 - listen 80;
61 - server_name localhost;
62 -
63 - root /var/www/project;
64 - index index.htm;
65 -
66 - access_log /var/log/nginx//http.access.log combined;
67 - error_log /var/log/nginx//http.error.log;
68 -}
69 -"""
70 -
71 - def _testme_std(self, argv, stdin=None, env=None):
72 - self._testme(argv, self.expected_output, stdin, env)
73 -
74 - def test_ini(self):
75 - # Filename
76 - self._testme_std(['resources/nginx.j2', 'resources/data.ini'])
77 - # Format
78 - self._testme_std(['--format=ini', 'resources/nginx.j2', 'resources/data.ini'])
79 - # Stdin
80 - self._testme_std(['--format=ini', 'resources/nginx.j2'], stdin=open('resources/data.ini'))
81 - self._testme_std(['--format=ini', 'resources/nginx.j2', '-'], stdin=open('resources/data.ini'))
82 -
83 - def test_json(self):
84 - # Filename
85 - self._testme_std(['resources/nginx.j2', 'resources/data.json'])
86 - # Format
87 - self._testme_std(['--format=json', 'resources/nginx.j2', 'resources/data.json'])
88 - # Stdin
89 - self._testme_std(['--format=json', 'resources/nginx.j2'], stdin=open('resources/data.json'))
90 - self._testme_std(['--format=json', 'resources/nginx.j2', '-'], stdin=open('resources/data.json'))
91 -
92 - def test_yaml(self):
93 - try:
94 - import yaml
95 - except ImportError:
96 - raise unittest.SkipTest('Yaml lib not installed')
97 -
98 - # Filename
99 - self._testme_std(['resources/nginx.j2', 'resources/data.yml'])
100 - self._testme_std(['resources/nginx.j2', 'resources/data.yaml'])
101 - # Format
102 - self._testme_std(['--format=yaml', 'resources/nginx.j2', 'resources/data.yml'])
103 - # Stdin
104 - self._testme_std(['--format=yaml', 'resources/nginx.j2'], stdin=open('resources/data.yml'))
105 - self._testme_std(['--format=yaml', 'resources/nginx.j2', '-'], stdin=open('resources/data.yml'))
106 -
107 - def test_env(self):
108 - # Filename
109 - self._testme_std(['--format=env', 'resources/nginx-env.j2', 'resources/data.env'])
110 - self._testme_std([ 'resources/nginx-env.j2', 'resources/data.env'])
111 - # Format
112 - self._testme_std(['--format=env', 'resources/nginx-env.j2', 'resources/data.env'])
113 - self._testme_std([ 'resources/nginx-env.j2', 'resources/data.env'])
114 - # Stdin
115 - self._testme_std(['--format=env', 'resources/nginx-env.j2', '-'], stdin=open('resources/data.env'))
116 - self._testme_std([ 'resources/nginx-env.j2', '-'], stdin=open('resources/data.env'))
117 -
118 - # Environment!
119 - # In this case, it's not explicitly provided, but implicitly gotten from the environment
120 - env = dict(NGINX_HOSTNAME='localhost', NGINX_WEBROOT='/var/www/project', NGINX_LOGS='/var/log/nginx/')
121 - self._testme_std(['--format=env', 'resources/nginx-env.j2'], env=env)
122 - self._testme_std([ 'resources/nginx-env.j2'], env=env)
123 -
124 - def test_import_env(self):
125 - # Import environment into a variable
126 - with mktemp('{{ a }}/{{ env.B }}') as template:
127 - with mktemp('{"a":1}') as context:
128 - self._testme(['--format=json', '--import-env=env', template, context], '1/2', env=dict(B='2'))
129 - # Import environment into global scope
130 - with mktemp('{{ a }}/{{ B }}') as template:
131 - with mktemp('{"a":1,"B":1}') as context:
132 - self._testme(['--format=json', '--import-env=', template, context], '1/2', env=dict(B='2'))
133 -
134 - def test_env_file__equals_sign_in_value(self):
135 - # Test whether environment variables with "=" in the value are parsed correctly
136 - with mktemp('{{ A|default('') }}/{{ B }}/{{ C }}') as template:
137 - with mktemp('A\nB=1\nC=val=1\n') as context:
138 - self._testme(['--format=env', template, context], '/1/val=1')
139 -
140 - def test_unicode(self):
141 - # Test how unicode is handled
142 - # I'm using Russian language for unicode :)
143 - with mktemp('Проверка {{ a }} связи!') as template:
144 - with mktemp('{"a": "широкополосной"}') as context:
145 - self._testme(['--format=json', template, context], 'Проверка широкополосной связи!')
146 -
147 - # Test case from issue #17: unicode environment variables
148 - if sys.version_info[0] == 2:
149 - # Python 2: environment variables are bytes
150 - self._testme(['resources/name.j2'], u'Hello Jürgen!\n', env=dict(name=b'J\xc3\xbcrgen'))
151 - else:
152 - # Python 3: environment variables are unicode strings
153 - self._testme(['resources/name.j2'], u'Hello Jürgen!\n', env=dict(name=u'Jürgen'))
154 -
155 - def test_filters__env(self):
156 - with mktemp('user_login: kolypto') as yml_file:
157 - with mktemp('{{ user_login }}:{{ "USER_PASS"|env }}') as template:
158 - # Test: template with an env variable
159 - self._testme(['--format=yaml', template, yml_file], 'kolypto:qwerty123', env=dict(USER_PASS='qwerty123'))
160 -
161 - # environment cleaned up
162 - assert 'USER_PASS' not in os.environ
163 -
164 - # Test: KeyError
165 - with self.assertRaises(KeyError):
166 - self._testme(['--format=yaml', template, yml_file], 'kolypto:qwerty123', env=dict())
167 -
168 - # Test: default
169 - with mktemp('{{ user_login }}:{{ "USER_PASS"|env("-none-") }}') as template:
170 - self._testme(['--format=yaml', template, yml_file], 'kolypto:-none-', env=dict())
171 -
172 - # Test: using as a function
173 - with mktemp('{{ user_login }}:{{ env("USER_PASS") }}') as template:
174 - self._testme(['--format=yaml', template, yml_file], 'kolypto:qwerty123', env=dict(USER_PASS='qwerty123'))
175 -
176 - with self.assertRaises(KeyError):
177 - # Variable not set
178 - self._testme(['--format=yaml', template, yml_file], '', env=dict())
179 -
180 - # Test: using as a function, with a default
181 - with mktemp('{{ user_login }}:{{ env("USER_PASS", "-none-") }}') as template:
182 - self._testme(['--format=yaml', template, yml_file], 'kolypto:qwerty123', env=dict(USER_PASS='qwerty123'))
183 - self._testme(['--format=yaml', template, yml_file], 'kolypto:-none-', env=dict())
184 -
185 -
186 - def test_custom_filters(self):
187 - with mktemp('{{ a|parentheses }}') as template:
188 - self._testme(['--format=env', '--filters=resources/custom_filters.py', template], '(1)', env=dict(a='1'))
189 -
190 - def test_custom_tests(self):
191 - with mktemp('{% if a|int is custom_odd %}odd{% endif %}') as template:
192 - self._testme(['--format=env', '--tests=resources/custom_tests.py', template], 'odd', env=dict(a='1'))
193 -
194 - def test_output_file(self):
195 - with mktemp('{{ a }}') as template:
196 - try:
197 - self._testme(['-o', '/tmp/j2-out', template], '', env=dict(a='123'))
198 - self.assertEqual('123', io.open('/tmp/j2-out', 'r').read())
199 - finally:
200 - os.unlink('/tmp/j2-out')
201 -
202 - def test_undefined(self):
203 - """ Test --undefined """
204 - # `name` undefined: error
205 - self.assertRaises(UndefinedError, self._testme, ['resources/name.j2'], u'Hello !\n', env=dict())
206 - # `name` undefined: no error
207 - self._testme(['--undefined', 'resources/name.j2'], u'Hello !\n', env=dict())
208 -
209 - def test_jinja2_extensions(self):
210 - """ Test that an extension is enabled """
211 - with mktemp('{% do [] %}') as template:
212 - # `do` tag is an extension
213 - self._testme([template], '')
214 -
215 -
216 - def test_customize(self):
217 - """ Test --customize """
218 - # Test: j2_environment_params()
219 - # Custom tag start/end
220 - with mktemp('<% if 1 %>1<% else %>2<% endif %>') as template:
221 - self._testme(['--customize=resources/customize.py', template], '1')
222 -
223 - # Test: j2_environment()
224 - # custom function: my_function
225 - with mktemp('<< my_function("hey") >>') as template:
226 - self._testme(['--customize=resources/customize.py', template], 'my function says "hey"')
227 -
228 - # Test: alter_context()
229 - # Extra variable: ADD=127
230 - with mktemp('<< ADD >>') as template:
231 - self._testme(['--customize=resources/customize.py', template], '127')
232 -
233 - # Test: extra_filters()
234 - with mktemp('<< ADD|parentheses >>') as template:
235 - self._testme(['--customize=resources/customize.py', template], '(127)')
236 -
237 - # Test: extra_tests()
238 - with mktemp('<% if ADD|int is custom_odd %>odd<% endif %>') as template:
239 - self._testme(['--customize=resources/customize.py', template], 'odd')
240 -
241 - # reset
242 - # otherwise it will load the same module even though its name has changed
243 - del sys.modules['customize-module']
244 -
245 - # Test: no hooks in a file
246 - # Got to restore to the original configuration and use {% %} again
247 - with mktemp('{% if 1 %}1{% endif %}') as template:
248 - self._testme(['--customize=render-test.py', template], '1')
249 --- /dev/null
250 +++ b/tests/test_render.py
251 @@ -0,0 +1,240 @@
252 +# -*- coding: utf-8 -*-
253 +
254 +from __future__ import unicode_literals
255 +
256 +import unittest
257 +import os, sys, io, os.path, tempfile
258 +from copy import copy
259 +from contextlib import contextmanager
260 +from jinja2.exceptions import UndefinedError
261 +
262 +from j2cli.cli import render_command
263 +
264 +@contextmanager
265 +def mktemp(contents):
266 + """ Create a temporary file with the given contents, and yield its path """
267 + _, path = tempfile.mkstemp()
268 + fp = io.open(path, 'wt+', encoding='utf-8')
269 + fp.write(contents)
270 + fp.flush()
271 + try:
272 + yield path
273 + finally:
274 + fp.close()
275 + os.unlink(path)
276 +
277 +
278 +@contextmanager
279 +def mock_environ(new_env):
280 + old_env = copy(os.environ)
281 + os.environ.update(new_env)
282 + yield
283 + os.environ.clear()
284 + os.environ.update(old_env)
285 +
286 +
287 +class RenderTest(unittest.TestCase):
288 + def setUp(self):
289 + os.chdir(
290 + os.path.dirname(__file__)
291 + )
292 +
293 + def _testme(self, argv, expected_output, stdin=None, env=None):
294 + """ Helper test shortcut """
295 + with mock_environ(env or {}):
296 + result = render_command(os.getcwd(), env or {}, stdin, argv)
297 + if isinstance(result, bytes):
298 + result = result.decode('utf-8')
299 + self.assertEqual(result, expected_output)
300 +
301 + #: The expected output
302 + expected_output = """server {
303 + listen 80;
304 + server_name localhost;
305 +
306 + root /var/www/project;
307 + index index.htm;
308 +
309 + access_log /var/log/nginx//http.access.log combined;
310 + error_log /var/log/nginx//http.error.log;
311 +}
312 +"""
313 +
314 + def _testme_std(self, argv, stdin=None, env=None):
315 + self._testme(argv, self.expected_output, stdin, env)
316 +
317 + def test_ini(self):
318 + # Filename
319 + self._testme_std(['resources/nginx.j2', 'resources/data.ini'])
320 + # Format
321 + self._testme_std(['--format=ini', 'resources/nginx.j2', 'resources/data.ini'])
322 + # Stdin
323 + self._testme_std(['--format=ini', 'resources/nginx.j2'], stdin=open('resources/data.ini'))
324 + self._testme_std(['--format=ini', 'resources/nginx.j2', '-'], stdin=open('resources/data.ini'))
325 +
326 + def test_json(self):
327 + # Filename
328 + self._testme_std(['resources/nginx.j2', 'resources/data.json'])
329 + # Format
330 + self._testme_std(['--format=json', 'resources/nginx.j2', 'resources/data.json'])
331 + # Stdin
332 + self._testme_std(['--format=json', 'resources/nginx.j2'], stdin=open('resources/data.json'))
333 + self._testme_std(['--format=json', 'resources/nginx.j2', '-'], stdin=open('resources/data.json'))
334 +
335 + def test_yaml(self):
336 + try:
337 + import yaml
338 + except ImportError:
339 + raise unittest.SkipTest('Yaml lib not installed')
340 +
341 + # Filename
342 + self._testme_std(['resources/nginx.j2', 'resources/data.yml'])
343 + self._testme_std(['resources/nginx.j2', 'resources/data.yaml'])
344 + # Format
345 + self._testme_std(['--format=yaml', 'resources/nginx.j2', 'resources/data.yml'])
346 + # Stdin
347 + self._testme_std(['--format=yaml', 'resources/nginx.j2'], stdin=open('resources/data.yml'))
348 + self._testme_std(['--format=yaml', 'resources/nginx.j2', '-'], stdin=open('resources/data.yml'))
349 +
350 + def test_env(self):
351 + # Filename
352 + self._testme_std(['--format=env', 'resources/nginx-env.j2', 'resources/data.env'])
353 + self._testme_std([ 'resources/nginx-env.j2', 'resources/data.env'])
354 + # Format
355 + self._testme_std(['--format=env', 'resources/nginx-env.j2', 'resources/data.env'])
356 + self._testme_std([ 'resources/nginx-env.j2', 'resources/data.env'])
357 + # Stdin
358 + self._testme_std(['--format=env', 'resources/nginx-env.j2', '-'], stdin=open('resources/data.env'))
359 + self._testme_std([ 'resources/nginx-env.j2', '-'], stdin=open('resources/data.env'))
360 +
361 + # Environment!
362 + # In this case, it's not explicitly provided, but implicitly gotten from the environment
363 + env = dict(NGINX_HOSTNAME='localhost', NGINX_WEBROOT='/var/www/project', NGINX_LOGS='/var/log/nginx/')
364 + self._testme_std(['--format=env', 'resources/nginx-env.j2'], env=env)
365 + self._testme_std([ 'resources/nginx-env.j2'], env=env)
366 +
367 + def test_import_env(self):
368 + # Import environment into a variable
369 + with mktemp('{{ a }}/{{ env.B }}') as template:
370 + with mktemp('{"a":1}') as context:
371 + self._testme(['--format=json', '--import-env=env', template, context], '1/2', env=dict(B='2'))
372 + # Import environment into global scope
373 + with mktemp('{{ a }}/{{ B }}') as template:
374 + with mktemp('{"a":1,"B":1}') as context:
375 + self._testme(['--format=json', '--import-env=', template, context], '1/2', env=dict(B='2'))
376 +
377 + def test_env_file__equals_sign_in_value(self):
378 + # Test whether environment variables with "=" in the value are parsed correctly
379 + with mktemp('{{ A|default('') }}/{{ B }}/{{ C }}') as template:
380 + with mktemp('A\nB=1\nC=val=1\n') as context:
381 + self._testme(['--format=env', template, context], '/1/val=1')
382 +
383 + def test_unicode(self):
384 + # Test how unicode is handled
385 + # I'm using Russian language for unicode :)
386 + with mktemp('Проверка {{ a }} связи!') as template:
387 + with mktemp('{"a": "широкополосной"}') as context:
388 + self._testme(['--format=json', template, context], 'Проверка широкополосной связи!')
389 +
390 + # Test case from issue #17: unicode environment variables
391 + if sys.version_info[0] == 2:
392 + # Python 2: environment variables are bytes
393 + self._testme(['resources/name.j2'], u'Hello Jürgen!\n', env=dict(name=b'J\xc3\xbcrgen'))
394 + else:
395 + # Python 3: environment variables are unicode strings
396 + self._testme(['resources/name.j2'], u'Hello Jürgen!\n', env=dict(name=u'Jürgen'))
397 +
398 + def test_filters__env(self):
399 + with mktemp('user_login: kolypto') as yml_file:
400 + with mktemp('{{ user_login }}:{{ "USER_PASS"|env }}') as template:
401 + # Test: template with an env variable
402 + self._testme(['--format=yaml', template, yml_file], 'kolypto:qwerty123', env=dict(USER_PASS='qwerty123'))
403 +
404 + # environment cleaned up
405 + assert 'USER_PASS' not in os.environ
406 +
407 + # Test: KeyError
408 + with self.assertRaises(KeyError):
409 + self._testme(['--format=yaml', template, yml_file], 'kolypto:qwerty123', env=dict())
410 +
411 + # Test: default
412 + with mktemp('{{ user_login }}:{{ "USER_PASS"|env("-none-") }}') as template:
413 + self._testme(['--format=yaml', template, yml_file], 'kolypto:-none-', env=dict())
414 +
415 + # Test: using as a function
416 + with mktemp('{{ user_login }}:{{ env("USER_PASS") }}') as template:
417 + self._testme(['--format=yaml', template, yml_file], 'kolypto:qwerty123', env=dict(USER_PASS='qwerty123'))
418 +
419 + with self.assertRaises(KeyError):
420 + # Variable not set
421 + self._testme(['--format=yaml', template, yml_file], '', env=dict())
422 +
423 + # Test: using as a function, with a default
424 + with mktemp('{{ user_login }}:{{ env("USER_PASS", "-none-") }}') as template:
425 + self._testme(['--format=yaml', template, yml_file], 'kolypto:qwerty123', env=dict(USER_PASS='qwerty123'))
426 + self._testme(['--format=yaml', template, yml_file], 'kolypto:-none-', env=dict())
427 +
428 +
429 + def test_custom_filters(self):
430 + with mktemp('{{ a|parentheses }}') as template:
431 + self._testme(['--format=env', '--filters=resources/custom_filters.py', template], '(1)', env=dict(a='1'))
432 +
433 + def test_custom_tests(self):
434 + with mktemp('{% if a|int is custom_odd %}odd{% endif %}') as template:
435 + self._testme(['--format=env', '--tests=resources/custom_tests.py', template], 'odd', env=dict(a='1'))
436 +
437 + def test_output_file(self):
438 + with mktemp('{{ a }}') as template:
439 + try:
440 + self._testme(['-o', '/tmp/j2-out', template], '', env=dict(a='123'))
441 + self.assertEqual('123', io.open('/tmp/j2-out', 'r').read())
442 + finally:
443 + os.unlink('/tmp/j2-out')
444 +
445 + def test_undefined(self):
446 + """ Test --undefined """
447 + # `name` undefined: error
448 + self.assertRaises(UndefinedError, self._testme, ['resources/name.j2'], u'Hello !\n', env=dict())
449 + # `name` undefined: no error
450 + self._testme(['--undefined', 'resources/name.j2'], u'Hello !\n', env=dict())
451 +
452 + def test_jinja2_extensions(self):
453 + """ Test that an extension is enabled """
454 + with mktemp('{% do [] %}') as template:
455 + # `do` tag is an extension
456 + self._testme([template], '')
457 +
458 +
459 + def test_customize(self):
460 + """ Test --customize """
461 + # Test: j2_environment_params()
462 + # Custom tag start/end
463 + with mktemp('<% if 1 %>1<% else %>2<% endif %>') as template:
464 + self._testme(['--customize=resources/customize.py', template], '1')
465 +
466 + # Test: j2_environment()
467 + # custom function: my_function
468 + with mktemp('<< my_function("hey") >>') as template:
469 + self._testme(['--customize=resources/customize.py', template], 'my function says "hey"')
470 +
471 + # Test: alter_context()
472 + # Extra variable: ADD=127
473 + with mktemp('<< ADD >>') as template:
474 + self._testme(['--customize=resources/customize.py', template], '127')
475 +
476 + # Test: extra_filters()
477 + with mktemp('<< ADD|parentheses >>') as template:
478 + self._testme(['--customize=resources/customize.py', template], '(127)')
479 +
480 + # Test: extra_tests()
481 + with mktemp('<% if ADD|int is custom_odd %>odd<% endif %>') as template:
482 + self._testme(['--customize=resources/customize.py', template], 'odd')
483 +
484 + # reset
485 + # otherwise it will load the same module even though its name has changed
486 + del sys.modules['customize-module']
487 +
488 + # Test: no hooks in a file
489 + # Got to restore to the original configuration and use {% %} again
490 + with mktemp('{% if 1 %}1{% endif %}') as template:
491 + self._testme(['--customize=test_render.py', template], '1')