Skip to content

Commit

Permalink
Fix #118: Always return str objects from formatter.render
Browse files Browse the repository at this point in the history
Under both Python 2 and Python 3, the output stream (actually
always `sys.stdout` in practice) is opened in text mode, and so
responsibility for handling the encoding lies with `.write`.

The XUnit and YAML backends were previously explicitly forcing an
encoding, which under Python 3 would mean `bytes` objects were
generated. This makes `sys.stdout` remarkably unhappy.

Solved by just removing the explicit encodings.

Test case included.
  • Loading branch information
prophile committed Apr 16, 2015
1 parent 2839602 commit b9645e3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
3 changes: 1 addition & 2 deletions prospector/formatters/xunit.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,4 @@ def render(self, summary=True, messages=True, profile=False):

testsuite_el.appendChild(testcase_el)

return xml_doc.toprettyxml(encoding='utf-8')

return xml_doc.toprettyxml()
1 change: 0 additions & 1 deletion prospector/formatters/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,5 @@ def render(self, summary=True, messages=True, profile=False):
output,
indent=2,
default_flow_style=False,
encoding='utf-8',
allow_unicode=True,
)
23 changes: 23 additions & 0 deletions tests/test_formatter_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from unittest import TestCase
from prospector.formatters import FORMATTERS
from prospector.profiles.profile import ProspectorProfile
import datetime


class FormatterTypeTest(TestCase):
def test_formatter_types(self):
summary = {'started': datetime.datetime(2014, 1, 1),
'completed': datetime.datetime(2014, 1, 1),
'message_count': 0,
'time_taken': '0',
'libraries': [],
'strictness': 'veryhigh',
'profiles': '',
'tools': []}
profile = ProspectorProfile(name='horse',
profile_dict={},
inherit_order=['horse'])
for formatter_name, formatter in FORMATTERS.items():
with self.subTest(formatter=formatter_name):
formatter_instance = formatter(summary, [], profile)
self.assertIsInstance(formatter_instance.render(True, True, False), str)

0 comments on commit b9645e3

Please sign in to comment.