Skip to content

Decouple libvt.testing, libvt.pytest_plugin, and conditional syrupy hook #65

Done

Description

@tony

Summary

libvt.pytest_plugin (libvt[pytest]) ships pure libvt fixtures (vt_recorder, vt_terminal, vt_factory, vt_parser, and vt_pty) with zero third-party snapshot-tool dependencies, while callers adopting VtSnapshotExtension from libvt.contrib.syrupy (libvt[syrupy]) want assert vt_screen(term) == vt_snapshot without repeating snapshot.use_extension(...) boilerplate in every conftest.py. Moving tool-agnostic VT serialization (serialize_vt_snapshot(), vt_screen(), format_vt_diff_lines()) into libvt.testing and registering vt_snapshot conditionally from libvt.contrib.syrupy (libvt_syrupy = "libvt.contrib.syrupy:_pytest_plugin") keeps libvt.pytest_plugin completely free of syrupy coupling while making vt_snapshot automatic when syrupy is active and invisible in pytest --fixtures when syrupy is uninstalled or disabled (-p no:syrupy).

Motivation

  • Keep libvt.pytest_plugin tool-agnostic: Every fixture in src/libvt/pytest_plugin.py serves libvt itself (Terminal, FlightRecorder, Parser, open_pty()). Placing a syrupy-specific SnapshotAssertion fixture inside libvt.pytest_plugin leaks syrupy types into the core plugin and advertises vt_snapshot in pytest --fixtures even in projects that use inline-snapshot, pytest-regressions, or plain assert_screen_box().
  • Decouple serialization from .ambr storage: vt_screen(), serialize_vt_snapshot(), and format_vt_diff_lines() use zero syrupy APIs; hosting them in libvt.testing lets any test runner serialize and diff styled terminal screens without installing syrupy.
  • Preserve failure diffs past syrupy post-assert resets: In syrupy v7.0.3, passing extension_class per assertion (assert term == snapshot(extension_class=VtSnapshotExtension)) queues setattr callbacks in SnapshotAssertion._post_assert_actions (src/syrupy/assertion.py) that reset _extension back to AmberSnapshotExtension during _post_assert(), before pytest_assertrepr_compare calls get_assert_diff() and self.extension.diff_snapshots(). Binding snapshot.use_extension(get_vt_snapshot_extension()) inside vt_snapshot preserves custom diff_snapshots() output across libvt, libtmux, and tmuxp without per-repository conftest.py duplication or duplicate pytest11 fixture collisions.

Proposal

  1. Move tool-agnostic serialization, scrubbing, and diffing to src/libvt/testing.py:
    • Export ScrubberSpec, VtTarget, VtScreenView, vt_screen(), serialize_vt_snapshot(), and format_vt_diff_lines() from libvt.testing with zero pytest or syrupy coupling, while limiting libvt.contrib.syrupy.__all__ strictly to VtSnapshotExtension, get_vt_snapshot_extension, and vt_snapshot.
  2. Keep src/libvt/pytest_plugin.py 100% syrupy-free:
    • Document vt_terminal, vt_factory, vt_recorder, vt_parser, and vt_pty with Returns and runnable Examples docstrings and autofixture reference cards in docs/api/pytest_plugin.md, with zero syrupy imports or vt_snapshot registration in libvt.pytest_plugin.
  3. Register vt_snapshot conditionally from src/libvt/contrib/syrupy.py:
    • Add libvt_syrupy = "libvt.contrib.syrupy:_pytest_plugin" under [project.entry-points.pytest11] in pyproject.toml.
    • In _SyrupyPytestPlugin.pytest_configure(config), register vt_snapshot only when importlib.util.find_spec("syrupy") is not None and config.pluginmanager.hasplugin("syrupy") is true, keeping import libvt.contrib.syrupy cold (libvt.terminal, libvt.testing, libvt._ucd, syrupy, and pytest unloaded at import time) and omitting vt_snapshot from pytest --fixtures whenever syrupy is absent or disabled via -p no:syrupy.

Alternatives

Keeping vt_snapshot inside src/libvt/pytest_plugin.py was rejected because it couples libvt[pytest] to a specific third-party snapshot tool and lists vt_snapshot in pytest --fixtures for non-syrupy test suites. Requiring callers to pass snapshot(extension_class=VtSnapshotExtension) inline was rejected because _post_assert_actions resets _extension prior to diff_snapshots(). Registering a second vt_snapshot pytest11 fixture inside libtmux.pytest_plugin was rejected because libtmux[vt] already installs libvt's pytest11 entry points.

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions