Handle os.chdir() during collection

This commit is contained in:
Daniel Hahler 2018-11-05 23:17:04 +01:00
parent 1752c7e710
commit cb57159e01
2 changed files with 28 additions and 4 deletions

View File

@ -603,6 +603,7 @@ class Config(object):
self._warn = self.pluginmanager._warn self._warn = self.pluginmanager._warn
self.pluginmanager.register(self, "pytestconfig") self.pluginmanager.register(self, "pytestconfig")
self._configured = False self._configured = False
self.cwd = os.getcwd()
def do_setns(dic): def do_setns(dic):
import pytest import pytest
@ -847,11 +848,10 @@ class Config(object):
args, self.option, namespace=self.option args, self.option, namespace=self.option
) )
if not args: if not args:
cwd = os.getcwd() if self.cwd == self.rootdir:
if cwd == self.rootdir:
args = self.getini("testpaths") args = self.getini("testpaths")
if not args: if not args:
args = [cwd] args = [self.cwd]
self.args = args self.args = args
except PrintHelp: except PrintHelp:
pass pass

View File

@ -1017,5 +1017,29 @@ def test_collect_handles_raising_on_dunder_class(testdir):
""" """
) )
result = testdir.runpytest() result = testdir.runpytest()
assert result.ret == 0
result.stdout.fnmatch_lines(["*1 passed in*"]) result.stdout.fnmatch_lines(["*1 passed in*"])
assert result.ret == 0
def test_collect_with_chdir_during_import(testdir):
subdir = testdir.tmpdir.mkdir("sub")
testdir.tmpdir.join("conftest.py").write(
textwrap.dedent(
"""
import os
os.chdir(%r)
"""
% (str(subdir),)
)
)
testdir.makepyfile(
"""
def test_1():
import os
assert os.getcwd() == %r
"""
% (str(subdir),)
)
result = testdir.runpytest()
result.stdout.fnmatch_lines(["*1 passed in*"])
assert result.ret == 0