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

Custom extension with _dirname outside the pytest session directory results in crash during (detailed) reporting #620

Closed
huonw opened this issue Sep 20, 2022 · 6 comments · Fixed by #621
Labels

Comments

@huonw
Copy link
Contributor

huonw commented Sep 20, 2022

Describe the bug

Currently a custom extension can write snapshots to a new directory by overriding the _dirname property, however, this directory has to be a child of pytest's root directory, or else syrupy's reporting crashes with an error like:

ValueError: '/tmp/example/__snapshots__/test_file.ambr' is not in the subpath of '/.../path/to/tests' OR one path is relative and the other is absolute.

(This is a very niche bug, sorry. Doing this directory hacking is designed to allow building a work-around for pantsbuild/pants#11622.)

To reproduce

import pytest

from syrupy.extensions.amber import AmberSnapshotExtension

class CustomSnapshotExtension(AmberSnapshotExtension):
    @property
    def _dirname(self):
        return '/tmp/example/__snapshots__'

@pytest.fixture
def snapshot(snapshot):
    return snapshot.use_extension(CustomSnapshotExtension)

def test_do_it(snapshot):
    assert "one" == snapshot
    assert "two" == snapshot
  1. run the tests above with --snapshot-update
  2. comment out the "two" line
  3. run the tests again with --snapshot-update or --snapshot-details

Output:

$ pytest test_file.py --snapshot-details
======================================================================================================================================================= test session starts ========================================================================================================================================================
platform darwin -- Python 3.10.4, pytest-7.1.3, pluggy-1.0.0
rootdir: /Users/huon/projects/tophat/syrupy, configfile: pyproject.toml
plugins: syrupy-1.7.3
collected 1 item                                                                                                                                                                                                                                                                                                                   

test_file.py .                                                                                                                                                                                                                                                                                                               [100%]

----------------------------------------------------------------------------------------------------------------------------------------------------- snapshot report summary ------------------------------------------------------------------------------------------------------------------------------------------------------
1 snapshot passed. 1 snapshot unused.

Traceback (most recent call last):
  File "/Users/huon/projects/tophat/syrupy/test/bin/pytest", line 8, in <module>
    sys.exit(console_main())
...
  File "/Users/huon/projects/tophat/syrupy/src/syrupy/report.py", line 304, in lines
    path_to_file = str(Path(filepath).relative_to(self.base_dir))
  File "/Users/huon/.pyenv/versions/3.10.4/lib/python3.10/pathlib.py", line 816, in relative_to
    raise ValueError("{!r} is not in the subpath of {!r}"

Expected behavior

Syrupy should behave as normal even with a 'weird' snapshot directory like this.

Screenshots

Environment (please complete the following information):

  • OS: macOS
  • Syrupy Version: 3.0.0
  • Python Version: 3.10.4

Additional context

Thanks for Syrupy!

@tophat-opensource-bot
Copy link
Contributor

🎉 This issue has been resolved in version 3.0.1 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

@huonw
Copy link
Contributor Author

huonw commented Sep 23, 2022

Hey @noahnu, thanks for merging my fix for this quickly. Just checking: the 3.0.1 release referenced there doesn't seem to be published to PyPI yet. Is that expected? https://pypi.org/project/syrupy/#history

Screenshot of what I see, for reference:
image

@noahnu
Copy link
Collaborator

noahnu commented Sep 23, 2022

Something went wrong with the release. I'll take a look today. https://github.com/tophat/syrupy/actions/runs/3090398312/jobs/4999313264

@tophat-opensource-bot
Copy link
Contributor

🎉 This issue has been resolved in version 3.0.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@noahnu
Copy link
Collaborator

noahnu commented Sep 23, 2022

@huonw the publish workflow is working now. I introduced a regression a while back that was causing the incorrect version to get published (a dev timestamp instead of semantic version). Latest version is v3.0.2 now.

@huonw
Copy link
Contributor Author

huonw commented Sep 23, 2022

Cool, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants