Skip to content

Commit b62fd79

Browse files
committed
Fix issue 2985.
1 parent a9dd37f commit b62fd79

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Grig Gheorghiu
7474
Grigorii Eremeev (budulianin)
7575
Guido Wesdorp
7676
Harald Armin Massa
77+
Henk-Jaap Wagenaar
7778
Hugo van Kemenade
7879
Hui Wang (coldnight)
7980
Ian Bicking

_pytest/main.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,25 @@ def _tryconvertpyarg(self, x):
729729
730730
"""
731731
import pkgutil
732+
733+
if six.PY2: # python 3.4+ uses importlib instead
734+
def find_module_patched(self, fullname, path=None):
735+
subname = fullname.split(".")[-1]
736+
if subname != fullname and self.path is None:
737+
return None
738+
if self.path is None:
739+
path = None
740+
else:
741+
path = [self.path]
742+
try:
743+
file, filename, etc = pkgutil.imp.find_module(subname,
744+
path)
745+
except ImportError:
746+
return None
747+
return pkgutil.ImpLoader(fullname, file, filename, etc)
748+
749+
pkgutil.ImpImporter.find_module = find_module_patched
750+
732751
try:
733752
loader = pkgutil.find_loader(x)
734753
except ImportError:

changelog/2985.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix conversion of pyargs to filename to not convert symlinks and not use deprecated features on Python 3.

testing/acceptance_test.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import os
44
import sys
55

6+
import six
7+
68
import _pytest._code
79
import py
810
import pytest
@@ -645,6 +647,69 @@ def join_pythonpath(*dirs):
645647
"*1 passed*"
646648
])
647649

650+
@pytest.mark.skipif(not hasattr(os, "symlink"), reason="requires symlinks")
651+
def test_cmdline_python_package_symlink(self, testdir, monkeypatch):
652+
"""
653+
test --pyargs option with packages with path containing symlink can
654+
have conftest.py in their package (#2985)
655+
"""
656+
monkeypatch.delenv('PYTHONDONTWRITEBYTECODE', raising=False)
657+
658+
search_path = ["lib", os.path.join("local", "lib")]
659+
660+
dirname = "lib"
661+
d = testdir.mkdir(dirname)
662+
foo = d.mkdir("foo")
663+
foo.ensure("__init__.py")
664+
lib = foo.mkdir('bar')
665+
lib.ensure("__init__.py")
666+
lib.join("test_bar.py"). \
667+
write("def test_bar(): pass\n"
668+
"def test_other(a_fixture):pass")
669+
lib.join("conftest.py"). \
670+
write("import pytest\n"
671+
"@pytest.fixture\n"
672+
"def a_fixture():pass")
673+
674+
d_local = testdir.mkdir("local")
675+
symlink_location = os.path.join(str(d_local), "lib")
676+
if six.PY2:
677+
os.symlink(str(d), symlink_location)
678+
else:
679+
os.symlink(str(d), symlink_location, target_is_directory=True)
680+
681+
# The structure of the test directory is now:
682+
# .
683+
# ├── local
684+
# │ └── lib -> ../world
685+
# └── lib
686+
# └── foo
687+
# ├── __init__.py
688+
# └── bar
689+
# ├── __init__.py
690+
# ├── conftest.py
691+
# └── test_world.py
692+
693+
def join_pythonpath(*dirs):
694+
cur = py.std.os.environ.get('PYTHONPATH')
695+
if cur:
696+
dirs += (cur,)
697+
return os.pathsep.join(str(p) for p in dirs)
698+
699+
monkeypatch.setenv('PYTHONPATH', join_pythonpath(*search_path))
700+
for p in search_path:
701+
monkeypatch.syspath_prepend(p)
702+
703+
# module picked up in symlink-ed directory:
704+
result = testdir.runpytest("--pyargs", "-v", "foo.bar")
705+
testdir.chdir()
706+
assert result.ret == 0
707+
result.stdout.fnmatch_lines([
708+
"*lib/foo/bar/test_bar.py::test_bar*PASSED*",
709+
"*lib/foo/bar/test_bar.py::test_other*PASSED*",
710+
"*2 passed*"
711+
])
712+
648713
def test_cmdline_python_package_not_exists(self, testdir):
649714
result = testdir.runpytest("--pyargs", "tpkgwhatv")
650715
assert result.ret

0 commit comments

Comments
 (0)