Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to manually set messages encoding for pytest plugin #100

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions teamcity/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def pytest_addoption(parser):
dest="teamcity", default=0, help="force output of JetBrains TeamCity service messages")
group._addoption('--no-teamcity', action="count",
dest="no_teamcity", default=0, help="disable output of JetBrains TeamCity service messages")
group._addoption('--teamcity-encoding', action="store",
dest="teamcity_encoding", default='auto', help="manually set encoding to JetBrains TeamCity service messages")


def pytest_configure(config):
Expand All @@ -43,7 +45,9 @@ def pytest_configure(config):
output_capture_enabled = getattr(config.option, 'capture', 'fd') != 'no'
coverage_controller = _get_coverage_controller(config)

config._teamcityReporting = EchoTeamCityMessages(output_capture_enabled, coverage_controller)
config._teamcityReporting = EchoTeamCityMessages(output_capture_enabled,
coverage_controller,
config.option.teamcity_encoding)
config.pluginmanager.register(config._teamcityReporting)


Expand All @@ -66,11 +70,11 @@ def _get_coverage_controller(config):


class EchoTeamCityMessages(object):
def __init__(self, output_capture_enabled, coverage_controller):
def __init__(self, output_capture_enabled, coverage_controller, encoding):
self.coverage_controller = coverage_controller
self.output_capture_enabled = output_capture_enabled

self.teamcity = TeamcityServiceMessages()
self.teamcity = TeamcityServiceMessages(encoding=encoding)
self.test_start_reported_mark = set()

self.max_reported_output_size = 1 * 1024 * 1024
Expand Down
4 changes: 4 additions & 0 deletions tests/guinea-pigs/pytest/encoding_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import pytest

def test_cp():
assert 'Ф'.encode('utf8') != b'\xd0\xa4'
9 changes: 8 additions & 1 deletion tests/integration-tests/pytest_integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ def test_reporting_disabled(venv):
output = run(venv, 'namespace', set_tc_version=True, options="--no-teamcity")
assert not has_service_messages(output)

def test_custom_teamcity_encoding(venv):
output = run(venv, 'encoding_test.py', set_tc_version=True, options="--teamcity-encoding cp1252")
assert "UnicodeEncodeError: 'charmap' codec can't encode character" in output

def test_default_teamcity_encoding(venv):
output = run(venv, 'encoding_test.py', set_tc_version=True, options="")
assert "UnicodeEncodeError: 'charmap' codec can't encode character" not in output

def test_custom_test_items(venv):
output = run(venv, 'custom')
Expand Down Expand Up @@ -323,7 +330,7 @@ def run(venv, file_name, test=None, options='', set_tc_version=True):
os.path.join('tests', 'guinea-pigs', 'pytest', file_name) + test_suffix
print("RUN: " + command)
proc = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env, shell=True)
output = "".join([x.decode() for x in proc.stdout.readlines()])
output = "".join([x.decode(errors="ignore") for x in proc.stdout.readlines()])
print("OUTPUT: " + output.replace("#", "*"))
proc.wait()

Expand Down