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

Overwriting dirname causes test suite not to fail if snapshots unused #868

Closed
joostlek opened this issue Aug 10, 2024 · 11 comments · Fixed by #892
Closed

Overwriting dirname causes test suite not to fail if snapshots unused #868

joostlek opened this issue Aug 10, 2024 · 11 comments · Fixed by #892
Labels
bug Something isn't working good first issue Good for newcomers released

Comments

@joostlek
Copy link
Contributor

Describe the bug

Within Home Assistant we have a SnapshotExtension to overwrite the default __snapshots__ dir name with snapshots. But today I actually noticed that this had the side effect that it doesn't fail the test suite if there are unused snapshots in the snapshots folder.

class HomeAssistantSnapshotExtension(AmberSnapshotExtension):
    """Home Assistant extension for Syrupy."""

    VERSION = "1"
    """Current version of serialization format.

    Need to be bumped when we change the HomeAssistantSnapshotSerializer.
    """

    serializer_class: type[AmberDataSerializer] = HomeAssistantSnapshotSerializer

    @classmethod
    def dirname(cls, *, test_location: PyTestLocation) -> str:
        """Return the directory for the snapshot files.

        Syrupy, by default, uses the `__snapshosts__` directory in the same
        folder as the test file. For Home Assistant, this is changed to just
        `snapshots` in the same folder as the test file, to match our `fixtures`
        folder structure.
        """
        test_dir = Path(test_location.filepath).parent
        return str(test_dir.joinpath("snapshots"))

To reproduce
Add the SnapshotExtension. Have it generate a snapshot. Add a fake entry to the snapshot. Run the test suite.

Expected behavior

I'd expect it to fail the test suite because there are entries that are not found. I can imagine that we have quite a basic way of overwriting the directory and can imagine people with way more interesting setups for the snapshots which would probably break with the expected behaviour. So I would not be surprised if the failure is turned off when the dirname is overwritten, but I'd then at least expect to read something in a docstring or in the docs.

Screenshots

Environment (please complete the following information):

  • OS: mac OS
  • Syrupy Version: 4.6.1
  • Python Version: 3.12

Additional context

@noahnu noahnu added the bug Something isn't working label Aug 23, 2024
@noahnu
Copy link
Collaborator

noahnu commented Aug 23, 2024

Ah this is an old issue. I'd definitely like to fix this.

I think this should be doable by updating the walk_snapshot_dir function to accept a dirname, then pass it along in discover_snapshots here:

for filepath in walk_snapshot_dir(self.dirname(test_location=test_location)):

For reference, the walk_snapshot_dir function:

if not in_snapshot_dir(filepath):

@iamlucasvieira
Copy link

I've never contributed here before. Would be willing to pick it up.

@noahnu
Copy link
Collaborator

noahnu commented Sep 7, 2024

That'd be great! Let me know if you have any questions

@iamlucasvieira
Copy link

I wonder if we need the not in_snapshot_dir check.

Because, in discover_snapshots we already pass the location including the SNAPSHOT_DIRNAME through self.dirname:

for filepath in walk_snapshot_dir(self.dirname(test_location=test_location)):

So every file that we find in this walk, is always within the SNAPSHOT_DIRNAME(That is also the case for a custom self.dirname.

I removed this check and all tests passed expect test_walk_dir_skips_non_snapshot_path, which is expected, as we pass to walk_snapshot_dir a path outside the SNAPSHOT_DIRNAME

What do you think?

@iamlucasvieira
Copy link

Add the SnapshotExtension. Have it generate a snapshot. Add a fake entry to the snapshot. Run the test suite.

I also tested this using the default snapshots dir and tests do not fail when I added a fake entry

@noahnu
Copy link
Collaborator

noahnu commented Sep 8, 2024

Hmm, yeah I think it can be removed. Feel free to put up a pull request that removes it and updates the test case. I'd also add a test case that covers what @joostlek posted in the original post

@joostlek
Copy link
Contributor Author

Any progress on this? I also wanted to look into the issue, so if I can pick it up, feel free to let me know :)

@iamlucasvieira
Copy link

You can pick up!

@joostlek
Copy link
Contributor Author

Do you have a branch somewhere with what you have?

@iamlucasvieira
Copy link

The only change I made in the code was removing the check if file is inside SNAPSHOT_DIRNAME (which is a constant with the default dirname). I concluded this check is not needed. I was then not able to replicate the issue you had after changing that.

I removed this check and all tests passed expect test_walk_dir_skips_non_snapshot_path, which is expected, as we pass to walk_snapshot_dir a path outside the SNAPSHOT_DIRNAME

@noahnu
Copy link
Collaborator

noahnu commented Oct 6, 2024

🎉 This issue has been resolved in version 4.7.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

@noahnu noahnu added the released label Oct 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers released
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants