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

fixed a bunch of unicode bugs in pytester.py #3848

Merged
merged 5 commits into from
Aug 23, 2018
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
1 change: 1 addition & 0 deletions changelog/3848.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix bugs where unicode arguments could not be passed to ``testdir.runpytest`` on Python 2.
7 changes: 5 additions & 2 deletions src/_pytest/config/argparsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import warnings
import argparse

import py

FILE_OR_DIR = "file_or_dir"


Expand Down Expand Up @@ -70,7 +72,8 @@ def parse(self, args, namespace=None):

self.optparser = self._getparser()
try_argcomplete(self.optparser)
return self.optparser.parse_args([str(x) for x in args], namespace=namespace)
args = [str(x) if isinstance(x, py.path.local) else x for x in args]
return self.optparser.parse_args(args, namespace=namespace)

def _getparser(self):
from _pytest._argcomplete import filescompleter
Expand Down Expand Up @@ -106,7 +109,7 @@ def parse_known_and_unknown_args(self, args, namespace=None):
the remaining arguments unknown at this point.
"""
optparser = self._getparser()
args = [str(x) for x in args]
args = [str(x) if isinstance(x, py.path.local) else x for x in args]
return optparser.parse_known_args(args, namespace=namespace)

def addini(self, name, help, type=None, default=None):
Expand Down
26 changes: 12 additions & 14 deletions src/_pytest/pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from _pytest.main import Session, EXIT_OK
from _pytest.assertion.rewrite import AssertionRewritingHook
from _pytest.compat import Path
from _pytest.compat import safe_str

IGNORE_PAM = [ # filenames added when obtaining details about the current user
u"/var/lib/sss/mc/passwd"
Expand All @@ -34,7 +35,7 @@ def pytest_addoption(parser):
action="store_true",
dest="lsof",
default=False,
help=("run FD checks if lsof is available"),
help="run FD checks if lsof is available",
)

parser.addoption(
Expand Down Expand Up @@ -273,7 +274,7 @@ def popcall(self, name):
del self.calls[i]
return call
lines = ["could not find call %r, in:" % (name,)]
lines.extend([" %s" % str(x) for x in self.calls])
lines.extend([" %s" % x for x in self.calls])
pytest.fail("\n".join(lines))

def getcall(self, name):
Expand Down Expand Up @@ -885,14 +886,12 @@ def runpytest(self, *args, **kwargs):
return self._runpytest_method(*args, **kwargs)

def _ensure_basetemp(self, args):
args = [str(x) for x in args]
args = list(args)
for x in args:
if str(x).startswith("--basetemp"):
# print("basedtemp exists: %s" %(args,))
if safe_str(x).startswith("--basetemp"):
break
else:
args.append("--basetemp=%s" % self.tmpdir.dirpath("basetemp"))
# print("added basetemp: %s" %(args,))
return args

def parseconfig(self, *args):
Expand Down Expand Up @@ -1018,7 +1017,7 @@ def popen(self, cmdargs, stdout, stderr, **kw):
"""
env = os.environ.copy()
env["PYTHONPATH"] = os.pathsep.join(
filter(None, [str(os.getcwd()), env.get("PYTHONPATH", "")])
filter(None, [os.getcwd(), env.get("PYTHONPATH", "")])
)
kw["env"] = env

Expand All @@ -1037,14 +1036,13 @@ def run(self, *cmdargs):
Returns a :py:class:`RunResult`.

"""
return self._run(*cmdargs)

def _run(self, *cmdargs):
cmdargs = [str(x) for x in cmdargs]
cmdargs = [
str(arg) if isinstance(arg, py.path.local) else arg for arg in cmdargs
]
p1 = self.tmpdir.join("stdout")
p2 = self.tmpdir.join("stderr")
print("running:", " ".join(cmdargs))
print(" in:", str(py.path.local()))
print("running:", *cmdargs)
print(" in:", py.path.local())
f1 = codecs.open(str(p1), "w", encoding="utf8")
f2 = codecs.open(str(p2), "w", encoding="utf8")
try:
Expand Down Expand Up @@ -1076,7 +1074,7 @@ def _dump_lines(self, lines, fp):
print("couldn't print to %s because of encoding" % (fp,))

def _getpytestargs(self):
return (sys.executable, "-mpytest")
return sys.executable, "-mpytest"

def runpython(self, script):
"""Run a python script using sys.executable as interpreter.
Expand Down
7 changes: 6 additions & 1 deletion testing/test_pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from _pytest.pytester import HookRecorder
from _pytest.pytester import CwdSnapshot, SysModulesSnapshot, SysPathsSnapshot
from _pytest.config import PytestPluginManager
from _pytest.main import EXIT_OK, EXIT_TESTSFAILED
from _pytest.main import EXIT_OK, EXIT_TESTSFAILED, EXIT_NOTESTSCOLLECTED


def test_make_hook_recorder(testdir):
Expand Down Expand Up @@ -396,3 +396,8 @@ def test_preserve_container(self, monkeypatch, path_type):
def test_testdir_subprocess(testdir):
testfile = testdir.makepyfile("def test_one(): pass")
assert testdir.runpytest_subprocess(testfile).ret == 0


def test_unicode_args(testdir):
result = testdir.runpytest("-k", u"💩")
assert result.ret == EXIT_NOTESTSCOLLECTED