Skip to content
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

Draft
wants to merge 30 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
1a772a3
progress
Oreldm Sep 13, 2024
7c775f3
Update Authors
Oreldm Sep 13, 2024
0a569ca
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 13, 2024
bc0aaa1
Merge branch 'main' into bug-fix-10651-syspathinsert-not-passing-to-p…
Oreldm Sep 13, 2024
6f95f59
Fix DOCString
Oreldm Sep 13, 2024
c78ed4a
Fix DOCString
Oreldm Sep 13, 2024
b88d0d3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 13, 2024
5bb131f
Fix DOCString
Oreldm Sep 13, 2024
f630494
Merge branch 'bug-fix-10651-syspathinsert-not-passing-to-pytester-sub…
Oreldm Sep 13, 2024
4d691ea
progress
Oreldm Sep 13, 2024
a87a190
progress
Oreldm Sep 13, 2024
6b468fe
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 13, 2024
30f2ba7
added bugfix.rst
Oreldm Sep 13, 2024
7f10717
Merge branch 'bug-fix-10651-syspathinsert-not-passing-to-pytester-sub…
Oreldm Sep 13, 2024
d31e435
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 13, 2024
ac930cd
progress
Oreldm Sep 13, 2024
994d24d
Merge branch 'bug-fix-10651-syspathinsert-not-passing-to-pytester-sub…
Oreldm Sep 13, 2024
03d80d0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 13, 2024
3d24eb3
progress
Oreldm Sep 13, 2024
f731aaf
Merge branch 'bug-fix-10651-syspathinsert-not-passing-to-pytester-sub…
Oreldm Sep 13, 2024
2690455
progress
Oreldm Sep 13, 2024
6679929
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 13, 2024
080b763
Fix SyntaxError in Pytester.runpytest_subprocess due to unescaped paths
Oreldm Sep 13, 2024
1d74520
Merge branch 'bug-fix-10651-syspathinsert-not-passing-to-pytester-sub…
Oreldm Sep 13, 2024
5719422
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 13, 2024
32fe1b9
Merge branch 'main' into bug-fix-10651-syspathinsert-not-passing-to-p…
Oreldm Sep 17, 2024
2924ad0
[PR Fix] Update testing/test_pytester.py remove timeout
Oreldm Sep 20, 2024
deee731
PR Fix - multiple env variable fix
Oreldm Sep 20, 2024
8bd1a5b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 20, 2024
3c88995
Merge branch 'main' into bug-fix-10651-syspathinsert-not-passing-to-p…
Oreldm Sep 20, 2024
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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ Oliver Bestwalter
Omar Kohl
Omer Hadari
Ondřej Súkup
Orel Damari
Oscar Benjamin
Parth Patel
Patrick Hayes
Expand Down
1 change: 1 addition & 0 deletions changelog/10651.bugfix.rst
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.
34 changes: 28 additions & 6 deletions src/_pytest/pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,7 @@
self._request.addfinalizer(self._finalize)
self._method = self._request.config.getoption("--runpytest")
self._test_tmproot = tmp_path_factory.mktemp(f"tmp-{name}", numbered=True)
self._prepended_syspaths: list[Path] = []

self._monkeypatch = mp = monkeypatch
self.chdir()
Expand Down Expand Up @@ -900,7 +901,9 @@
if path is None:
path = self.path

self._monkeypatch.syspath_prepend(str(path))
path_obj = Path(path)
self._monkeypatch.syspath_prepend(str(path_obj))
self._prepended_syspaths.append(path_obj)

def mkdir(self, name: str | os.PathLike[str]) -> Path:
"""Create a new (sub)directory.
Expand Down Expand Up @@ -1337,10 +1340,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", "")

Check warning on line 1344 in src/_pytest/pytester.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/pytester.py#L1343-L1344

Added lines #L1343 - L1344 were not covered by tests

paths_to_add = [os.getcwd(), *self._prepended_syspaths]

Check warning on line 1346 in src/_pytest/pytester.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/pytester.py#L1346

Added line #L1346 was not covered by tests

pythonpath = os.pathsep.join(

Check warning on line 1348 in src/_pytest/pytester.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/pytester.py#L1348

Added line #L1348 was not covered by tests
filter(None, [*map(str, paths_to_add), pythonpath])
)

env["PYTHONPATH"] = pythonpath

Check warning on line 1352 in src/_pytest/pytester.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/pytester.py#L1352

Added line #L1352 was not covered by tests
kw["env"] = env

if stdin is self.CLOSE_STDIN:
Expand All @@ -1365,6 +1374,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.

Expand Down Expand Up @@ -1413,6 +1423,7 @@
stdout=f1,
stderr=f2,
close_fds=(sys.platform != "win32"),
env=env,
)
if popen.stdin is not None:
popen.stdin.close()
Expand Down Expand Up @@ -1488,8 +1499,19 @@
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", "")

Check warning on line 1504 in src/_pytest/pytester.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/pytester.py#L1503-L1504

Added lines #L1503 - L1504 were not covered by tests

# Add all prepended syspaths to PYTHONPATH
prepended_paths = [str(path) for path in self._prepended_syspaths]
pythonpath = os.pathsep.join(filter(None, [*prepended_paths, pythonpath]))

Check warning on line 1508 in src/_pytest/pytester.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/pytester.py#L1508

Added line #L1508 was not covered by tests

env["PYTHONPATH"] = pythonpath

Check warning on line 1510 in src/_pytest/pytester.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/pytester.py#L1510

Added line #L1510 was not covered by tests

return self.run(

Check warning on line 1512 in src/_pytest/pytester.py

View check run for this annotation

Codecov / codecov/patch

src/_pytest/pytester.py#L1512

Added line #L1512 was not covered by tests
*[sys.executable, "-m", "pytest", *args], timeout=timeout, env=env
)

def spawn_pytest(self, string: str, expect_timeout: float = 10.0) -> pexpect.spawn:
"""Run pytest using pexpect.
Expand Down
34 changes: 34 additions & 0 deletions testing/test_pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,3 +835,37 @@ def test_two():
result.assert_outcomes(passed=1, deselected=1)
# If deselected is not passed, it is not checked at all.
result.assert_outcomes(passed=1)


def test_syspathinsert__in_process__path_exists(pytester: Pytester):
some_dir = "abcd"
pytester.syspathinsert(some_dir)
pytester.makepyfile(
f"""
import sys

def test_foo():
assert "{some_dir}" in sys.path
"""
)

result = pytester.runpytest_inprocess()

result.assert_outcomes(passed=1)


def test_syspathinsert__sub_process__path_exists(pytester: Pytester):
some_dir = "abcd"
pytester.syspathinsert(some_dir)
pytester.makepyfile(
f"""
import sys

def test_foo():
assert "{some_dir}" in sys.path
"""
)

result = pytester.runpytest_subprocess()

result.assert_outcomes(passed=1)
Loading