Unicode support

This commit is contained in:
Mark Vartanyan 2018-12-26 13:49:18 +03:00
parent b3e40b2f31
commit c3d0d38003
2 changed files with 17 additions and 4 deletions

View File

@ -4,3 +4,4 @@
* New CLI option: `--import-env` that imports environment variables into the template
* Fix: trailing newline is not removed anymore
* Fix: env vars with "=" in values are now parsed correctly
* Fix: now unicode templates & contexts are fully supported

View File

@ -1,5 +1,9 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import unittest
import os, os.path, tempfile
import os, io, os.path, tempfile
from contextlib import contextmanager
from j2cli.cli import render_command
@ -8,7 +12,7 @@ from j2cli.cli import render_command
def mktemp(contents):
""" Create a temporary file with the given contents, and yield its path """
_, path = tempfile.mkstemp()
fp = open(path, 'w+')
fp = io.open(path, 'wt+', encoding='utf-8')
fp.write(contents)
fp.flush()
try:
@ -28,7 +32,7 @@ class RenderTest(unittest.TestCase):
""" Helper test shortcut """
result = render_command(os.getcwd(), env or {}, stdin, argv)
if isinstance(result, bytes):
result = result.decode()
result = result.decode('utf-8')
self.assertEqual(result, expected_output)
#: The expected output
@ -102,4 +106,12 @@ class RenderTest(unittest.TestCase):
# Test whether environment variables with "=" in the value are parsed correctly
with mktemp('{{ A|default('') }}/{{ B }}/{{ C }}') as template:
with mktemp('A\nB=1\nC=val=1\n') as context:
self._testme(['--format=env', template, context], '/1/val=1', env=dict(B='2'))
self._testme(['--format=env', template, context], '/1/val=1')
def test_unicode(self):
# Test how unicode is handled
# I'm using Russian language for unicode :)
with mktemp('Проверка {{ a }} связи!') as template:
with mktemp('{"a": "широкополосной"}') as context:
self._testme(['--format=json', template, context], 'Проверка широкополосной связи!')