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

rewrite does not work automatically for plugins in develop mode #1934

Closed
nicoddemus opened this issue Sep 15, 2016 · 3 comments
Closed

rewrite does not work automatically for plugins in develop mode #1934

nicoddemus opened this issue Sep 15, 2016 · 3 comments

Comments

@nicoddemus
Copy link
Member

pytest-mock uses pytest's assert introspection to enrich the error message when one of the standard mock.assert* methods fail. Those asserts are in the plugin module (pytest_mock.py), and while they are rewritten just fine when installed from a package, they don't get rewritten in development mode (python setup.py develop).

I tracked down the problem to our code which tries to rewrite all modules of installed plugins in config.py, function _consider_importhook:

self.pluginmanager.rewrite_hook = hook
for entrypoint in pkg_resources.iter_entry_points('pytest11'):
    for entry in entrypoint.dist._get_metadata('RECORD'):
        fn = entry.split(',')[0]
        ...

When in development mode, entrypoint.dist._get_metadata('RECORD') returns an empty list; digging into that method I noticed it tries to access EGG metadata. Being in development mode, there's no EGG meta-data, so it makes sense.

I think the only solution is to move the function which needs assertion rewriting to its own "util" module and call register_assert_rewrite before importing it (which is no big deal).

Any other ideas on how to fix/workaround it?

cc @flub

@nicoddemus
Copy link
Member Author

Investigating further, I found that in development mode there's the SOURCES.txt metadata, but not RECORD. 😞

Perhaps we should just try both RECORD and SOURCES.txt then?

@RonnyPfannschmidt
Copy link
Member

we might want add a general tool to do ahead of time assert rewriting and mark all plugin as always being rewritten

@RonnyPfannschmidt
Copy link
Member

(so that we can opt out of always marking them)

joseph-roitman added a commit to joseph-roitman/pytest-snapshot that referenced this issue Nov 10, 2023
…tions.

See pytest-dev/pytest#1934.

Error message:
```
../../../../home/runner/work/pytest-snapshot/pytest-snapshot/.tox/pypy3-pytest6/lib/pypy3.10/site-packages/_pytest/config/__init__.py:1114
  /home/runner/work/pytest-snapshot/pytest-snapshot/.tox/pypy3-pytest6/lib/pypy3.10/site-packages/_pytest/config/__init__.py:1114: PytestAssertRewriteWarning: Module already imported so cannot be rewritten: pytest_snapshot
    self._mark_plugins_for_rewrite(hook)
```
joseph-roitman added a commit to joseph-roitman/pytest-snapshot that referenced this issue Nov 10, 2023
Using develop mode may miss issues with the library when installed normally.
Additionally, develop mode caused tests to fail on PyPy on Github Actions
due to a bug in pytest. See pytest-dev/pytest#1934.

Error message:
```
../../../../home/runner/work/pytest-snapshot/pytest-snapshot/.tox/pypy3-pytest6/lib/pypy3.10/site-packages/_pytest/config/__init__.py:1114
  /home/runner/work/pytest-snapshot/pytest-snapshot/.tox/pypy3-pytest6/lib/pypy3.10/site-packages/_pytest/config/__init__.py:1114: PytestAssertRewriteWarning: Module already imported so cannot be rewritten: pytest_snapshot
    self._mark_plugins_for_rewrite(hook)
```
joseph-roitman added a commit to joseph-roitman/pytest-snapshot that referenced this issue Nov 10, 2023
Using develop mode may miss issues with the library when installed normally.
Additionally, develop mode caused tests to fail on PyPy on Github Actions
due to a bug in pytest. See pytest-dev/pytest#1934.

Error message:
```
../../../../home/runner/work/pytest-snapshot/pytest-snapshot/.tox/pypy3-pytest6/lib/pypy3.10/site-packages/_pytest/config/__init__.py:1114
  /home/runner/work/pytest-snapshot/pytest-snapshot/.tox/pypy3-pytest6/lib/pypy3.10/site-packages/_pytest/config/__init__.py:1114: PytestAssertRewriteWarning: Module already imported so cannot be rewritten: pytest_snapshot
    self._mark_plugins_for_rewrite(hook)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants