Skip to content

Issue with overlapping filenames in different directories #1079

@wyardley

Description

@wyardley

Describe the bug
Came across this when working on moving a large codebase over from pytest-snapshot to syrupy. This mostly comes up when randomized subsets of files are chosen (using test splitting), and a file from one directory is also a file in another directory that's not called in the test invocation.

To reproduce

Given a structure like this:

|-- module_a
|   |-- __init__.py
|   `-- tests
|       |-- __init__.py
|       |-- __snapshots__
|       |   |-- test_foo.ambr
|       |   `-- test_views.ambr
|       |-- test_foo.py
|       `-- test_views.py
|-- module_b
|   |-- __init__.py
|   `-- tests
|       |-- __init__.py
|       |-- __snapshots__
|       |   `-- test_views.ambr
|       `-- test_views.py

Here's a minimal reproduction case: https://github.com/wyardley/syrupy_repro

  1. Clone repo above
  2. uv sync --dev
  3. uv run pytest module_a/tests/test_foo.py module_b/tests/test_views.py

Expected behavior
syrupy doesn't try to create / remove snapshots for similar file name patterns, unless it's actually the exact corresponding snapshot.

Screenshots

Image

Environment (please complete the following information):

  • OS: Mac OSX 26.3.1
  • Syrupy Version: 5.1.0
  • Python Version: 3.12.11

Additional context
In our case, we have certain file patterns that are common, and use CircleCI test splitting with high parallelism, so this came up a lot.

Possibly related areas of the code:

syrupy/src/syrupy/report.py

Lines 224 to 226 in 2034d2b

if self._provided_test_paths and not self._ran_items_match_location(
snapshot_location
):

def matches_snapshot_location(self, snapshot_location: str) -> bool:
loc = Path(snapshot_location)
if self.is_item_parametrized:
return self.basename == loc.stem or (
self.basename == loc.parent.name
and (
loc.stem == self.snapshot_name_parametrized
or loc.stem.startswith(f"{self.snapshot_name_parametrized}.")
or loc.stem.startswith(f"{self.snapshot_name_parametrized}[")
)
)
# "test_file" should match "test_file.ext" or "test_file/whatever.ext", but not
# "test_file_suffix.ext"
return self.basename == loc.stem or self.basename == loc.parent.name

(possibly because of only the stem being compared).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions