-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
[Bug Fix] Pytester.syspathinsert() has no effect when using runpytest_subprocess() . closes #10651 #12812
base: main
Are you sure you want to change the base?
[Bug Fix] Pytester.syspathinsert() has no effect when using runpytest_subprocess() . closes #10651 #12812
Changes from 26 commits
1a772a3
7c775f3
0a569ca
bc0aaa1
6f95f59
c78ed4a
b88d0d3
5bb131f
f630494
4d691ea
a87a190
6b468fe
30f2ba7
7f10717
d31e435
ac930cd
994d24d
03d80d0
3d24eb3
f731aaf
2690455
6679929
080b763
1d74520
5719422
32fe1b9
2924ad0
deee731
8bd1a5b
3c88995
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fixed bug where the `Pytester.syspathinsert` has no effect when using subprocess. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -900,7 +900,14 @@ | |
if path is None: | ||
path = self.path | ||
|
||
self._monkeypatch.syspath_prepend(str(path)) | ||
path_str = str(path) | ||
self._monkeypatch.syspath_prepend(path_str) | ||
self._syspath_prepended = path_str | ||
|
||
# Store the prepended path in an attribute that persists across method calls | ||
if not hasattr(self, "_prepended_syspaths"): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's avoid |
||
self._prepended_syspaths = [] | ||
self._prepended_syspaths.append(path_str) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's just store |
||
|
||
def mkdir(self, name: str | os.PathLike[str]) -> Path: | ||
"""Create a new (sub)directory. | ||
|
@@ -1337,10 +1344,16 @@ | |
|
||
You probably want to use :py:meth:`run` instead. | ||
""" | ||
env = os.environ.copy() | ||
env["PYTHONPATH"] = os.pathsep.join( | ||
filter(None, [os.getcwd(), env.get("PYTHONPATH", "")]) | ||
) | ||
env = kw.pop("env", None) or os.environ.copy() | ||
pythonpath = env.get("PYTHONPATH", "") | ||
|
||
paths_to_add = [os.getcwd()] | ||
if hasattr(self, "_syspath_prepended"): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should add all paths from |
||
paths_to_add.insert(0, self._syspath_prepended) | ||
|
||
pythonpath = os.pathsep.join(filter(None, [*paths_to_add, pythonpath])) | ||
|
||
env["PYTHONPATH"] = pythonpath | ||
kw["env"] = env | ||
|
||
if stdin is self.CLOSE_STDIN: | ||
|
@@ -1365,6 +1378,7 @@ | |
*cmdargs: str | os.PathLike[str], | ||
timeout: float | None = None, | ||
stdin: NotSetType | bytes | IO[Any] | int = CLOSE_STDIN, | ||
env: dict[str, str] | None = None, | ||
) -> RunResult: | ||
"""Run a command with arguments. | ||
|
||
|
@@ -1413,6 +1427,7 @@ | |
stdout=f1, | ||
stderr=f2, | ||
close_fds=(sys.platform != "win32"), | ||
env=env, | ||
) | ||
if popen.stdin is not None: | ||
popen.stdin.close() | ||
|
@@ -1488,8 +1503,33 @@ | |
plugins = [x for x in self.plugins if isinstance(x, str)] | ||
if plugins: | ||
args = ("-p", plugins[0], *args) | ||
args = self._getpytestargs() + args | ||
return self.run(*args, timeout=timeout) | ||
|
||
env = os.environ.copy() | ||
pythonpath = env.get("PYTHONPATH", "") | ||
|
||
if hasattr(self, "_syspath_prepended"): | ||
pythonpath = os.pathsep.join( | ||
filter(None, [self._syspath_prepended, pythonpath]) | ||
) | ||
|
||
env["PYTHONPATH"] = pythonpath | ||
|
||
python_executable = sys.executable | ||
pytest_command = [python_executable, "-m", "pytest"] | ||
|
||
if hasattr(self, "_syspath_prepended"): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we setting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually I don't think we need to modify |
||
prepend_command = ( | ||
f"import sys; sys.path.insert(0, {self._syspath_prepended!r});" | ||
) | ||
pytest_command = [ | ||
python_executable, | ||
"-c", | ||
f"{prepend_command} import pytest; pytest.main({list(args)!r})", | ||
] | ||
else: | ||
pytest_command.extend(str(arg) for arg in args) | ||
|
||
return self.run(*pytest_command, timeout=timeout, env=env) | ||
|
||
def spawn_pytest(self, string: str, expect_timeout: float = 10.0) -> pexpect.spawn: | ||
"""Run pytest using pexpect. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_syspath_prepended
only stores the latest path called bysyspathinsert
, butsyspathinsert
might be called multiple times, and all paths should be considered.