Skip to content
Merged
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
8 changes: 6 additions & 2 deletions src/tox/_pytestplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,13 @@ def outlines(self):
return err + out

def __repr__(self):
return "RunResult(ret={}, args={}, out=\n{}\n, err=\n{})".format(
self.ret, " ".join(str(i) for i in self.args), self.out, self.err
res = "RunResult(ret={}, args={!r}, out=\n{}\n, err=\n{})".format(
self.ret, self.args, self.out, self.err
)
if six.PY2:
return res.encode("UTF-8")
else:
return res
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you make it one return?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one less line

        if six.PY2:
            res = res.encode("UTF-8")
        return res

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if else is easier for humans to parse as it's clearer that there are two disparate branches -- for maintenance reasons it's also easier to update if functionality were to be added as it's clearer it needs to happen at two places at a glance.

I've seen too many bugs from using your suggested style 😆

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something must be wrong with me then because for me two returns are a lot harder to understand, reason and parse 🤔 I think it's harder to maintain as now I need to update in two locations while I can clearly reduce the second to one, needing to worry only about a single case...

Copy link
Contributor Author

@asottile asottile Apr 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

your suggested pattern also makes mypy unhappy since it changes the type of the variable

Incompatible types in assignment (expression has type "bytes", variable has type "str")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol it's always something >.<

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it worked a while back now it's missing 💭

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

microsoft/azure-pipelines-tasks#9962 turns out the ability to upload coverage report was removed 😆

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoa that's cool


def output(self):
return "{}\n{}\n{}".format(self.ret, self.err, self.out)
Expand Down
15 changes: 14 additions & 1 deletion tests/unit/test_pytest_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

"""
import os
import sys

import py.path
import pytest

from tox._pytestplugin import _filedefs_contains, _path_parts
from tox._pytestplugin import RunResult, _filedefs_contains, _path_parts


class TestInitProj:
Expand Down Expand Up @@ -111,3 +112,15 @@ def test_on_py_path(self):
)
def test_filedefs_contains(base, filedefs, target, expected):
assert bool(_filedefs_contains(base, filedefs, target)) == expected


def test_run_result_repr(capfd):
with RunResult(["hello", "world"], capfd) as run_result:
# simulate tox writing some unicode output
stdout_buffer = getattr(sys.stdout, "buffer", sys.stdout)
stdout_buffer.write(u"\u2603".encode("UTF-8"))

# must not `UnicodeError` on repr(...)
ret = repr(run_result)
# must be native `str`, (bytes in py2, str in py3)
assert isinstance(ret, str)