From 3137c89cf1232d5c68802cdc187354b8d9909f31 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Thu, 8 Nov 2018 00:54:51 +0100 Subject: [PATCH] Fix/improve handling of chdir with no-args and testpaths Fixes https://github.com/pytest-dev/pytest/issues/4332. --- src/_pytest/config/__init__.py | 10 ++++++---- testing/test_collection.py | 15 ++++++++++++++- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/_pytest/config/__init__.py b/src/_pytest/config/__init__.py index fdcca5ebd..d3bdef1ec 100644 --- a/src/_pytest/config/__init__.py +++ b/src/_pytest/config/__init__.py @@ -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 diff --git a/testing/test_collection.py b/testing/test_collection.py index 5747e5ed0..3d65b21dc 100644 --- a/testing/test_collection.py +++ b/testing/test_collection.py @@ -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