Skip to content

Handle os.chdir() during collection #4317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/_pytest/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,7 @@ def __init__(self, pluginmanager):
self._warn = self.pluginmanager._warn
self.pluginmanager.register(self, "pytestconfig")
self._configured = False
self.cwd = os.getcwd()

def do_setns(dic):
import pytest
Expand Down Expand Up @@ -847,11 +848,10 @@ def parse(self, args, addopts=True):
args, self.option, namespace=self.option
)
if not args:
cwd = os.getcwd()
if cwd == self.rootdir:
if self.cwd == self.rootdir:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i just noticed this one - its "incorrect"

if test-paths is relative to the original cwd, then the result is entirely incorrect

its absolutely necessary to trigger a warning there - fundamentally a testsuite that changes the cwd at import time for bits of the suite already is broken and users should rectify that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RonnyPfannschmidt
Ah.. so this should make testpaths absolute then probably.

I agree that os.chdir during import etc is bad, but it seemed easy enough to work around / fix this.
Will create a follow-up.

args = self.getini("testpaths")
if not args:
args = [cwd]
args = [self.cwd]
self.args = args
except PrintHelp:
pass
Expand Down
24 changes: 24 additions & 0 deletions testing/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1017,5 +1017,29 @@ def test_1():
"""
)
result = testdir.runpytest()
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