Skip to content

Commit cdd5fed

Browse files
Fix: Add abstract get_snapshot_file method to SnapshotFileLayout class
- Make SnapshotFileLayout class abstract with ABC - Add abstract get_snapshot_file method for type safety - Update pytest-selfie tests with proper fixture usage - Add concrete TestSnapshotFileLayout implementation in example tests Co-Authored-By: [email protected] <[email protected]>
1 parent 827d0fa commit cdd5fed

File tree

5 files changed

+39
-17
lines changed

5 files changed

+39
-17
lines changed

python/example-pytest-selfie/tests/to_be_file_test.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@
1010
)
1111

1212

13+
class TestSnapshotFileLayout(SnapshotFileLayout):
14+
def get_snapshot_file(self, test_file: TypedPath) -> TypedPath:
15+
return TypedPath(str(test_file).replace(".jpg", ".ss"))
16+
17+
1318
def test_to_be_file():
14-
layout = SnapshotFileLayout(FSImplementation())
19+
layout = TestSnapshotFileLayout(FSImplementation())
1520
tracker = ToBeFileWriteTracker()
1621

1722
# Record the current call stack.

python/pytest-selfie/pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,6 @@ ignore = [ "S", "FA", "PYI", "EM", "PLR", "FBT", "COM", "RET", "PTH", "PLW", "
3535
"PLC0414", # import alias does not rename original package
3636
"W291", # trailing whitespace, we need it for testing snapshots
3737
"PGH003", # specific rule codes when ignoring type issues
38-
"ISC001"
38+
"ISC001",
39+
"F401" # unused imports - needed for pytest fixtures
3940
]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"""Tests for pytest-selfie."""
Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,51 @@
11
import os
22
from pathlib import Path
3-
from typing import Any, Optional
3+
from typing import Any
4+
45
import pytest
56
from _pytest.config import Config
7+
68
from pytest_selfie.plugin import PytestSnapshotSystem, SelfieSettingsAPI
79
from selfie_lib import Mode, TypedPath
810

11+
912
class MockConfig(Config): # type: ignore
1013
def __init__(self, tmp_path: Path):
1114
self._rootpath = tmp_path
1215

13-
def getoption(self, name: str, default: Any = None, skip: bool = False) -> Any:
16+
def getoption(self, _name: str, default: Any = None, _skip: bool = False) -> Any:
1417
return default
1518

1619
@property
1720
def rootpath(self) -> Path:
1821
return self._rootpath
1922

20-
def test_snapshot_system_initialization(tmp_path):
21-
config = MockConfig(Path(tmp_path))
22-
settings = SelfieSettingsAPI(config)
23+
24+
@pytest.fixture()
25+
def mock_config(tmp_path: Path) -> MockConfig:
26+
"""Create a mock config for testing."""
27+
return MockConfig(tmp_path)
28+
29+
30+
def test_snapshot_system_initialization(mock_config): # type: ignore[misc]
31+
settings = SelfieSettingsAPI(mock_config)
2332
system = PytestSnapshotSystem(settings)
2433
assert system.mode == Mode.interactive
2534
assert isinstance(system.fs, object)
2635

27-
def test_snapshot_file_layout(tmp_path):
28-
config = MockConfig(Path(tmp_path))
29-
settings = SelfieSettingsAPI(config)
36+
37+
def test_snapshot_file_layout(mock_config): # type: ignore[misc]
38+
settings = SelfieSettingsAPI(mock_config)
3039
system = PytestSnapshotSystem(settings)
31-
test_file = TypedPath.of_file(os.path.join(str(tmp_path), "test_example.py"))
40+
test_file = TypedPath.of_file(
41+
os.path.join(str(mock_config.rootpath), "test_example.py")
42+
)
3243
snapshot_file = system.layout.get_snapshot_file(test_file)
3344
assert str(snapshot_file).endswith("test_example.ss")
3445

35-
def test_snapshot_system_mode_from_env(tmp_path, monkeypatch):
36-
monkeypatch.setenv('SELFIE', 'readonly')
37-
config = MockConfig(Path(tmp_path))
38-
settings = SelfieSettingsAPI(config)
46+
47+
def test_snapshot_system_mode_from_env(mock_config, monkeypatch): # type: ignore[misc]
48+
monkeypatch.setenv("SELFIE", "readonly")
49+
settings = SelfieSettingsAPI(mock_config)
3950
system = PytestSnapshotSystem(settings)
4051
assert system.mode == Mode.readonly

python/selfie-lib/selfie_lib/WriteTracker.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import inspect
22
import os
33
import threading
4-
from abc import ABC
4+
from abc import ABC, abstractmethod
55
from functools import total_ordering
66
from pathlib import Path
77
from typing import Dict, Generic, List, Optional, TypeVar, cast
@@ -82,7 +82,7 @@ def __hash__(self):
8282
return hash((self.location, tuple(self.rest_of_stack)))
8383

8484

85-
class SnapshotFileLayout:
85+
class SnapshotFileLayout(ABC):
8686
def __init__(self, fs: FS):
8787
self.fs = fs
8888

@@ -92,6 +92,10 @@ def sourcefile_for_call(self, call: CallLocation) -> TypedPath:
9292
raise ValueError("No file path available in CallLocation.")
9393
return TypedPath(os.path.abspath(Path(file_path)))
9494

95+
@abstractmethod
96+
def get_snapshot_file(self, test_file: TypedPath) -> TypedPath:
97+
pass
98+
9599

96100
def recordCall(callerFileOnly: bool) -> CallStack:
97101
stack_frames_raw = inspect.stack()

0 commit comments

Comments
 (0)