Skip to content

Commit 4b4871a

Browse files
committed
Fix/improve handling of chdir with no-args and testpaths
Fixes #4332.
1 parent 4c00552 commit 4b4871a

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/_pytest/config/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,6 @@ def __init__(self, pluginmanager):
603603
self._warn = self.pluginmanager._warn
604604
self.pluginmanager.register(self, "pytestconfig")
605605
self._configured = False
606-
self.cwd = os.getcwd()
607606

608607
def do_setns(dic):
609608
import pytest
@@ -848,10 +847,13 @@ def parse(self, args, addopts=True):
848847
args, self.option, namespace=self.option
849848
)
850849
if not args:
851-
if self.cwd == self.rootdir:
852-
args = self.getini("testpaths")
850+
if self.invocation_dir == self.rootdir:
851+
args = [
852+
self.invocation_dir.join(x) if not os.path.isabs(x) else x
853+
for x in self.getini("testpaths")
854+
]
853855
if not args:
854-
args = [self.cwd]
856+
args = [self.invocation_dir]
855857
self.args = args
856858
except PrintHelp:
857859
pass

testing/test_collection.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,19 @@ def test_1():
10401040
"""
10411041
% (str(subdir),)
10421042
)
1043-
result = testdir.runpytest()
1043+
with testdir.tmpdir.as_cwd():
1044+
result = testdir.runpytest()
10441045
result.stdout.fnmatch_lines(["*1 passed in*"])
10451046
assert result.ret == 0
1047+
1048+
# Handles relative testpaths.
1049+
testdir.makeini(
1050+
"""
1051+
[pytest]
1052+
testpaths = .
1053+
"""
1054+
)
1055+
with testdir.tmpdir.as_cwd():
1056+
result = testdir.runpytest("--collect-only")
1057+
result.stdout.fnmatch_lines(["collected 1 item"])
1058+
assert result.ret == 0

0 commit comments

Comments
 (0)