Jelajahi Sumber

Handle formatter case where logrecord message is binary containing
unicode characters.

Signed-off-by: Joffrey F <[email protected]>

Joffrey F 9 tahun lalu
induk
melakukan
17c5b45641
2 mengubah file dengan 27 tambahan dan 6 penghapusan
  1. 4 1
      compose/cli/formatter.py
  2. 23 5
      tests/unit/cli/formatter_test.py

+ 4 - 1
compose/cli/formatter.py

@@ -4,6 +4,7 @@ from __future__ import unicode_literals
 import logging
 import os
 
+import six
 import texttable
 
 from compose.cli import colors
@@ -44,5 +45,7 @@ class ConsoleWarningFormatter(logging.Formatter):
         return ''
 
     def format(self, record):
+        if isinstance(record.msg, six.binary_type):
+            record.msg = record.msg.decode('utf-8')
         message = super(ConsoleWarningFormatter, self).format(record)
-        return self.get_level_message(record) + message
+        return '{0}{1}'.format(self.get_level_message(record), message)

+ 23 - 5
tests/unit/cli/formatter_test.py

@@ -11,8 +11,8 @@ from tests import unittest
 MESSAGE = 'this is the message'
 
 
-def makeLogRecord(level):
-    return logging.LogRecord('name', level, 'pathame', 0, MESSAGE, (), None)
+def make_log_record(level, message=None):
+    return logging.LogRecord('name', level, 'pathame', 0, message or MESSAGE, (), None)
 
 
 class ConsoleWarningFormatterTestCase(unittest.TestCase):
@@ -21,15 +21,33 @@ class ConsoleWarningFormatterTestCase(unittest.TestCase):
         self.formatter = ConsoleWarningFormatter()
 
     def test_format_warn(self):
-        output = self.formatter.format(makeLogRecord(logging.WARN))
+        output = self.formatter.format(make_log_record(logging.WARN))
         expected = colors.yellow('WARNING') + ': '
         assert output == expected + MESSAGE
 
     def test_format_error(self):
-        output = self.formatter.format(makeLogRecord(logging.ERROR))
+        output = self.formatter.format(make_log_record(logging.ERROR))
         expected = colors.red('ERROR') + ': '
         assert output == expected + MESSAGE
 
     def test_format_info(self):
-        output = self.formatter.format(makeLogRecord(logging.INFO))
+        output = self.formatter.format(make_log_record(logging.INFO))
         assert output == MESSAGE
+
+    def test_format_unicode_info(self):
+        message = b'\xec\xa0\x95\xec\x88\x98\xec\xa0\x95'
+        output = self.formatter.format(make_log_record(logging.INFO, message))
+        print(output)
+        assert output == message.decode('utf-8')
+
+    def test_format_unicode_warn(self):
+        message = b'\xec\xa0\x95\xec\x88\x98\xec\xa0\x95'
+        output = self.formatter.format(make_log_record(logging.WARN, message))
+        expected = colors.yellow('WARNING') + ': '
+        assert output == '{0}{1}'.format(expected, message.decode('utf-8'))
+
+    def test_format_unicode_error(self):
+        message = b'\xec\xa0\x95\xec\x88\x98\xec\xa0\x95'
+        output = self.formatter.format(make_log_record(logging.ERROR, message))
+        expected = colors.red('ERROR') + ': '
+        assert output == '{0}{1}'.format(expected, message.decode('utf-8'))