Commit 899ebf1e authored by Michal Čihař's avatar Michal Čihař

Fix file format tests for Python 3

Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent b99dd264
...@@ -727,7 +727,7 @@ class FileFormat(object): ...@@ -727,7 +727,7 @@ class FileFormat(object):
if cls.new_translation is None: if cls.new_translation is None:
raise ValueError('Not supported') raise ValueError('Not supported')
with open(filename, 'wb') as output: with open(filename, 'w') as output:
output.write(cls.new_translation) output.write(cls.new_translation)
def iterate_merge(self, fuzzy): def iterate_merge(self, fuzzy):
...@@ -1175,7 +1175,7 @@ class JSONFormat(FileFormat): ...@@ -1175,7 +1175,7 @@ class JSONFormat(FileFormat):
@classmethod @classmethod
def create_new_file(cls, filename, code, base): def create_new_file(cls, filename, code, base):
"""Handles creation of new translation file.""" """Handles creation of new translation file."""
content = '{}\n' content = b'{}\n'
if base: if base:
with open(base, 'rb') as handle: with open(base, 'rb') as handle:
content = handle.read() content = handle.read()
......
...@@ -22,13 +22,15 @@ File format specific behavior. ...@@ -22,13 +22,15 @@ File format specific behavior.
''' '''
from __future__ import unicode_literals from __future__ import unicode_literals
from io import BytesIO
import io import io
import tempfile import tempfile
from unittest import TestCase, SkipTest from unittest import TestCase, SkipTest
from six import StringIO
from django.test import SimpleTestCase from django.test import SimpleTestCase
from django.utils.encoding import force_text
import six
from translate.storage.po import pofile from translate.storage.po import pofile
...@@ -54,7 +56,7 @@ TEST_TS = get_test_file('cs.ts') ...@@ -54,7 +56,7 @@ TEST_TS = get_test_file('cs.ts')
class AutoLoadTest(TestCase): class AutoLoadTest(TestCase):
def single_test(self, filename, fileclass): def single_test(self, filename, fileclass):
with open(filename, 'r') as handle: with open(filename, 'rb') as handle:
store = AutoFormat.parse(handle) store = AutoFormat.parse(handle)
self.assertIsInstance(store, fileclass) self.assertIsInstance(store, fileclass)
...@@ -83,10 +85,10 @@ class AutoLoadTest(TestCase): ...@@ -83,10 +85,10 @@ class AutoLoadTest(TestCase):
def test_content(self): def test_content(self):
"""Test content based guess from ttkit""" """Test content based guess from ttkit"""
with open(TEST_PO, 'r') as handle: with open(TEST_PO, 'rb') as handle:
data = handle.read() data = handle.read()
handle = StringIO(data) handle = BytesIO(data)
store = AutoFormat.parse(handle) store = AutoFormat.parse(handle)
self.assertIsInstance(store, AutoFormat) self.assertIsInstance(store, AutoFormat)
self.assertIsInstance(store.store, pofile) self.assertIsInstance(store.store, pofile)
...@@ -113,13 +115,13 @@ class AutoFormatTest(SimpleTestCase): ...@@ -113,13 +115,13 @@ class AutoFormatTest(SimpleTestCase):
def test_save(self): def test_save(self):
# Read test content # Read test content
with open(self.FILE, 'r') as handle: with open(self.FILE, 'rb') as handle:
testdata = handle.read() testdata = handle.read()
# Create test file # Create test file
testfile = tempfile.NamedTemporaryFile( testfile = tempfile.NamedTemporaryFile(
suffix='.{0}'.format(self.EXT), suffix='.{0}'.format(self.EXT),
mode='w+' mode='wb+'
) )
try: try:
# Write test data to file # Write test data to file
...@@ -133,11 +135,14 @@ class AutoFormatTest(SimpleTestCase): ...@@ -133,11 +135,14 @@ class AutoFormatTest(SimpleTestCase):
storage.save() storage.save()
# Read new content # Read new content
with open(testfile.name, 'r') as handle: with open(testfile.name, 'rb') as handle:
newdata = handle.read() newdata = handle.read()
# Check if content matches # Check if content matches
self.assert_same(newdata, testdata) self.assert_same(
force_text(newdata),
force_text(testdata)
)
finally: finally:
testfile.close() testfile.close()
...@@ -297,10 +302,13 @@ class TSFormatTest(XMLMixin, AutoFormatTest): ...@@ -297,10 +302,13 @@ class TSFormatTest(XMLMixin, AutoFormatTest):
def assert_same(self, newdata, testdata): def assert_same(self, newdata, testdata):
# Comparing of XML with doctype fails... # Comparing of XML with doctype fails...
self.assertXMLEqual( newdata = newdata.replace('<!DOCTYPE TS>', '')
newdata.replace(b'<!DOCTYPE TS>', b''), testdata = testdata.replace('<!DOCTYPE TS>', '')
testdata.replace(b'<!DOCTYPE TS>', b'') # Magic for Python 2.x
) if six.PY2:
testdata = testdata.encode('utf-8')
newdata = newdata.encode('utf-8')
super(TSFormatTest, self).assert_same(newdata, testdata)
class OutputTest(TestCase): class OutputTest(TestCase):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment