Fix/improve handling of chdir with no-args and testpaths

Fixes https://github.com/pytest-dev/pytest/issues/4332.
This commit is contained in:
Daniel Hahler 2018-11-08 00:54:51 +01:00
parent 5dd509c963
commit 3137c89cf1
2 changed files with 20 additions and 5 deletions

View File

@ -604,7 +604,6 @@ class Config(object):
self._warn = self.pluginmanager._warn
self.pluginmanager.register(self, "pytestconfig")
self._configured = False
self.cwd = os.getcwd()
def do_setns(dic):
import pytest
@ -847,10 +846,13 @@ class Config(object):
args, self.option, namespace=self.option
)
if not args:
if self.cwd == self.rootdir:
args = self.getini("testpaths")
if self.invocation_dir == self.rootdir:
args = [
str(self.invocation_dir.join(x, abs=True))
for x in self.getini("testpaths")
]
if not args:
args = [self.cwd]
args = [str(self.invocation_dir)]
self.args = args
except PrintHelp:
pass

View File

@ -1072,6 +1072,19 @@ def test_collect_with_chdir_during_import(testdir):
"""
% (str(subdir),)
)
result = testdir.runpytest()
with testdir.tmpdir.as_cwd():
result = testdir.runpytest()
result.stdout.fnmatch_lines(["*1 passed in*"])
assert result.ret == 0
# Handles relative testpaths.
testdir.makeini(
"""
[pytest]
testpaths = .
"""
)
with testdir.tmpdir.as_cwd():
result = testdir.runpytest("--collect-only")
result.stdout.fnmatch_lines(["collected 1 item"])
assert result.ret == 0