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

Conversation

Oreldm
Copy link

@Oreldm Oreldm commented Sep 13, 2024

  • Include documentation when adding new features.
  • Include new tests or update existing tests when applicable.
  • Allow maintainers to push and squash when merging my commits. Please uncheck this if you prefer to squash the commits yourself.

If this change fixes an issue, please:

  • Add text like closes #XYZW to the PR description and/or commits (where XYZW is the issue number). See the github docs for more information.

Unless your change is trivial or a small documentation fix (e.g., a typo or reword of a small section) please:

  • Create a new changelog file in the changelog folder, with a name like <ISSUE NUMBER>.<TYPE>.rst. See changelog/README.rst for details.

    Write sentences in the past or present tense, examples:

    • Improved verbose diff output with sequences.
    • Terminal summary statistics now use multiple colors.

    Also make sure to end the sentence with a ..

  • Add yourself to AUTHORS in alphabetical order.

Closes #10651

@Oreldm Oreldm marked this pull request as draft September 13, 2024 07:11
@psf-chronographer psf-chronographer bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Sep 13, 2024
self._syspath_prepended = path_str

# Store the prepended path in an attribute that persists across method calls
if not hasattr(self, "_prepended_syspaths"):
Copy link
Member

Choose a reason for hiding this comment

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

Let's avoid hasattr hacks, just declare the required attributes on __init__. 👍

self._monkeypatch.syspath_prepend(str(path))
path_str = str(path)
self._monkeypatch.syspath_prepend(path_str)
self._syspath_prepended = path_str
Copy link
Member

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 by syspathinsert, but syspathinsert might be called multiple times, and all paths should be considered.

# Store the prepended path in an attribute that persists across method calls
if not hasattr(self, "_prepended_syspaths"):
self._prepended_syspaths = []
self._prepended_syspaths.append(path_str)
Copy link
Member

Choose a reason for hiding this comment

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

Let's just store Path objects here, no need to store strings -- we can convert to strings later.

pythonpath = env.get("PYTHONPATH", "")

paths_to_add = [os.getcwd()]
if hasattr(self, "_syspath_prepended"):
Copy link
Member

Choose a reason for hiding this comment

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

We should add all paths from _prepended_syspaths at this point.

python_executable = sys.executable
pytest_command = [python_executable, "-m", "pytest"]

if hasattr(self, "_syspath_prepended"):
Copy link
Member

Choose a reason for hiding this comment

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

Why are we setting sys.path and configuring PYTHONPATH? Isn't sufficient to only configure PYTHONPATH?

Copy link
Member

Choose a reason for hiding this comment

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

Actually I don't think we need to modify runpytest_subprocess at all, given we are already configuring PYTHONPATH in popen?

testing/test_pytester.py Outdated Show resolved Hide resolved
@nicoddemus
Copy link
Member

Thanks @Oreldm for the contribution, please take a look at my comments.

@Oreldm Oreldm marked this pull request as draft September 19, 2024 13:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot:chronographer:provided (automation) changelog entry is part of PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pytester.syspathinsert() has no effect when using runpytest_subprocess()
2 participants