Skip to content

Commit

Permalink
refactor: removed _external.storage
Browse files Browse the repository at this point in the history
  • Loading branch information
15r10nk committed Feb 11, 2025
1 parent aaf3ef6 commit 21b62cf
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 20 deletions.
14 changes: 6 additions & 8 deletions src/inline_snapshot/_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import Union

from . import _config
from ._global_state import state


class HashError(Exception):
Expand Down Expand Up @@ -70,9 +71,6 @@ def remove(self, name):
self._lookup_path(name).unlink()


storage: Optional[DiscStorage] = None


class external:
def __init__(self, name: str):
"""External objects are used as a representation for outsourced data.
Expand Down Expand Up @@ -130,8 +128,8 @@ def __eq__(self, other):
return True

def _load_value(self):
assert storage is not None
return storage.read(self._path)
assert state().storage is not None
return state().storage.read(self._path)


def outsource(data: Union[str, bytes], *, suffix: Optional[str] = None) -> external:
Expand Down Expand Up @@ -170,12 +168,12 @@ def outsource(data: Union[str, bytes], *, suffix: Optional[str] = None) -> exter
m.update(data)
hash = m.hexdigest()

assert storage is not None
assert state().storage is not None

name = hash + suffix

if not storage.lookup_all(name):
if not state().storage.lookup_all(name):
path = hash + "-new" + suffix
storage.save(path, data)
state().storage.save(path, data)

return external(name)
3 changes: 1 addition & 2 deletions src/inline_snapshot/_find_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from executing import Source

from . import _external
from ._global_state import state
from ._rewrite_code import ChangeRecorder
from ._rewrite_code import end_of
Expand Down Expand Up @@ -54,7 +53,7 @@ def used_externals() -> Set[str]:


def unused_externals() -> Set[str]:
storage = _external.storage
storage = state().storage
assert storage is not None
unused_externals = storage.list()
for name in used_externals():
Expand Down
10 changes: 5 additions & 5 deletions src/inline_snapshot/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def pytest_configure(config):
_config.config.storage_dir or config.rootpath / ".inline-snapshot"
) / "external"

_external.storage = _external.DiscStorage(external_storage)
state().storage = _external.DiscStorage(external_storage)

if flags - {"short-report", "disable"} and not is_pytest_compatible():

Expand All @@ -146,7 +146,7 @@ def pytest_configure(config):

pydantic_fix()

_external.storage.prune_new_files()
state().storage.prune_new_files()


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -397,16 +397,16 @@ def report(flag, message, message_n):
)

for external_name in used:
_external.storage.persist(external_name)
state().storage.persist(external_name)

cr.fix_all()

unused_externals = _find_external.unused_externals()

if unused_externals and state().update_flags.trim:
for name in unused_externals:
assert _external.storage
_external.storage.remove(name)
assert state().storage
state().storage.remove(name)
terminalreporter.write(
f"removed {len(unused_externals)} unused externals\n"
)
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ def run(self, *flags_arg: Category):
with snapshot_env() as state:
recorder = ChangeRecorder()
state.update_flags = flags
inline_snapshot._external.storage = (
inline_snapshot._external.DiscStorage(tmp_path / ".storage")
state.storage = inline_snapshot._external.DiscStorage(
tmp_path / ".storage"
)

error = False
Expand Down
7 changes: 4 additions & 3 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import inline_snapshot._config as _config
import inline_snapshot._external as external
from inline_snapshot._global_state import state
from inline_snapshot._rewrite_code import ChangeRecorder
from inline_snapshot.testing._example import snapshot_env

Expand All @@ -31,10 +32,10 @@ def apply_changes():

@contextmanager
def useStorage(storage):
old_storage = external.storage
external.storage = storage
old_storage = state().storage
state().storage = storage
yield
external.storage = old_storage
state().storage = old_storage


@pytest.fixture()
Expand Down

0 comments on commit 21b62cf

Please sign in to comment.