Files

57 lines
1.6 KiB
Python

"""The reference tracker plugin (``worktrees.json`` — the shape the
``worktrees`` hub plugin ships) and a fake plugin store to run the parser and
the cards on it, as ``fixtures/resource_plugins/kit.py`` does for resources."""
from __future__ import annotations
import contextlib
import copy
import json
import pathlib
import types
from plugins import trackers as plugin_trackers
HERE = pathlib.Path(__file__).resolve().parent
KEY = "worktrees:worktrees"
def manifest(name: str = "worktrees") -> dict:
return json.loads((HERE / f"{name}.json").read_text())
class FakePluginStore:
def __init__(self, manifests=None):
self.version = 1
self.rows = {m["id"]: m for m in (manifests if manifests is not None else [manifest()])}
def active_manifests(self, uid):
return [(p, copy.deepcopy(m), {}) for p, m in sorted(self.rows.items())]
def put(self, mf: dict) -> None:
self.rows[mf["id"]] = mf
self.version += 1
def remove(self, pid: str) -> None:
self.rows.pop(pid, None)
self.version += 1
def deps_with(store) -> types.SimpleNamespace:
return types.SimpleNamespace(plugin_store=store)
def registry(manifests=None):
return plugin_trackers.registry(deps_with(FakePluginStore(manifests)))
@contextlib.contextmanager
def bound(store=None):
"""The parser and the deps-less accessors read ``store``'s trackers."""
prev = plugin_trackers._bound
plugin_trackers.bind(deps_with(store if store is not None else FakePluginStore()))
try:
yield
finally:
plugin_trackers._bound = prev