10000 WIP: Snapshot by tony · Pull Request #587 · tmux-python/libtmux · GitHub
[go: up one dir, main page]

Skip to content

WIP: Snapshot #587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 17 commits into
base: frozen-dataclasses-custom
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test/test_snapshot.py: uv run ruff check --select ALL src/libtmux/sna…
…pshot.py tests/test_snapshot.py --fix --unsafe-fixes --preview --show-fixes; uv run ruff format .
  • Loading branch information
tony committed Apr 6, 2025
commit cec71a5801033c185a7071f54da1f51ca04bc58b
51 changes: 28 additions & 23 deletions tests/test_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@

from __future__ import annotations

from typing import TYPE_CHECKING
from unittest.mock import MagicMock, patch

import pytest

from libtmux._internal.frozen_dataclass_sealable import is_sealable
from libtmux.server import Server
from libtmux.session import Session
from libtmux.snapshot import (
PaneSnapshot,
ServerSnapshot,
Expand All @@ -19,15 +18,19 @@
snapshot_to_dict,
)

if TYPE_CHECKING:
from libtmux.server import Server
from libtmux.session import Session


class TestPaneSnapshot:
"""Test the PaneSnapshot class."""

def test_pane_snapshot_is_sealable(self):
def test_pane_snapshot_is_sealable(self) -> None:
"""Test that PaneSnapshot is sealable."""
assert is_sealable(PaneSnapshot)

def test_pane_snapshot_creation(self, session: Session):
def test_pane_snapshot_creation(self, session: Session) -> None:
"""Test creating a PaneSnapshot."""
# Get a real pane from the session fixture
pane = session.active_window.active_pane
Expand All @@ -52,7 +55,7 @@ def test_pane_snapshot_creation(self, session: Session):
assert len(snapshot.pane_content) > 0
assert any("test content" in line for line in snapshot.pane_content)

def test_pane_snapshot_no_content(self, session: Session):
def test_pane_snapshot_no_content(self, session: Session) -> None:
"""Test creating a PaneSnapshot without capturing content."""
# Get a real pane from the session fixture
pane = session.active_window.active_pane
Expand All @@ -68,7 +71,7 @@ def test_pane_snapshot_no_content(self, session: Session):
# Test that capture_pane method returns empty list
assert snapshot.capture_pane() == []

def test_pane_snapshot_cmd_not_implemented(self, session: Session):
def test_pane_snapshot_cmd_not_implemented(self, session: Session) -> None:
"""Test that cmd method raises NotImplementedError."""
# Get a real pane from the session fixture
pane = session.active_window.active_pane
Expand All @@ -86,11 +89,11 @@ def test_pane_snapshot_cmd_not_implemented(self, session: Session):
class TestWindowSnapshot:
"""Test the WindowSnapshot class."""

def test_window_snapshot_is_sealable(self):
def test_window_snapshot_is_sealable(self) -> None:
"""Test that WindowSnapshot is sealable."""
assert is_sealable(WindowSnapshot)

def test_window_snapshot_creation(self, session: Session):
def test_window_snapshot_creation(self, session: Session) -> None:
"""Test creating a WindowSnapshot."""
# Get a real window from the session fixture
window = session.active_window
Expand All @@ -115,7 +118,7 @@ def test_window_snapshot_creation(self, session: Session):
# Check active_pane property
assert snapshot.active_pane is not None

def test_window_snapshot_no_content(self, session: Session):
def test_window_snapshot_no_content(self, session: Session) -> None:
"""Test creating a WindowSnapshot without capturing content."""
# Get a real window from the session fixture
window = session.active_window
Expand All @@ -137,7 +140,7 @@ def test_window_snapshot_no_content(self, session: Session):
for pane_snap in snapshot.panes_snapshot:
assert pane_snap.pane_content is None

def test_window_snapshot_cmd_not_implemented(self, session: Session):
def test_window_snapshot_cmd_not_implemented(self, session: Session) -> None:
"""Test that cmd method raises NotImplementedError."""
# Get a real window from the session fixture
window = session.active_window
Expand All @@ -157,11 +160,11 @@ def test_window_snapshot_cmd_not_implemented(self, session: Session):
class TestSessionSnapshot:
"""Test the SessionSnapshot class."""

def test_session_snapshot_is_sealable(self):
def test_session_snapshot_is_sealable(self) -> None:
"""Test that SessionSnapshot is sealable."""
assert is_sealable(SessionSnapshot)

def test_session_snapshot_creation(self, session: Session):
def test_session_snapshot_creation(self, session: Session) -> None:
"""Test creating a SessionSnapshot."""
# Create a mock return value instead of trying to modify a real SessionSnapshot
mock_snapshot = MagicMock(spec=SessionSnapshot)
Expand All @@ -170,15 +173,16 @@ def test_session_snapshot_creation(self, session: Session):

# Patch the from_session method to return our mock
with patch(
"libtmux.snapshot.SessionSnapshot.from_session", return_value=mock_snapshot
"libtmux.snapshot.SessionSnapshot.from_session",
return_value=mock_snapshot,
):
snapshot = SessionSnapshot.from_session(session)

# Check that the snapshot has the correct attributes
assert snapshot.id == session.id
assert snapshot.name == session.name

def test_session_snapshot_cmd_not_implemented(self):
def test_session_snapshot_cmd_not_implemented(self) -> None:
"""Test that cmd method raises NotImplementedError."""
# Create a minimal SessionSnapshot instance without using from_session
snapshot = SessionSnapshot.__new__(SessionSnapshot)
Expand All @@ -191,11 +195,11 @@ def test_session_snapshot_cmd_not_implemented(self):
class TestServerSnapshot:
"""Test the ServerSnapshot class."""

def test_server_snapshot_is_sealable(self):
def test_server_snapshot_is_sealable(self) -> None:
"""Test that ServerSnapshot is sealable."""
assert is_sealable(ServerSnapshot)

def test_server_snapshot_creation(self, server: Server, session: Session):
def test_server_snapshot_creation(self, server: Server, session: Session) -> None:
"""Test creating a ServerSnapshot."""
# Create a mock with the properties we want to test
mock_session_snapshot = MagicMock(spec=SessionSnapshot)
Expand All @@ -208,7 +212,8 @@ def test_server_snapshot_creation(self, server: Server, session: Session):

# Patch the from_server method to return our mock
with patch(
"libtmux.snapshot.ServerSnapshot.from_server", return_value=mock_snapshot
"libtmux.snapshot.ServerSnapshot.from_server",
return_value=mock_snapshot,
):
snapshot = ServerSnapshot.from_server(server)

Expand All @@ -218,7 +223,7 @@ def test_server_snapshot_creation(self, server: Server, session: Session):
# Check that sessions were added
assert len(snapshot.sessions) == 1

def test_server_snapshot_cmd_not_implemented(self):
def test_server_snapshot_cmd_not_implemented(self) -> None:
"""Test that cmd method raises NotImplementedError."""
# Create a minimal ServerSnapshot instance
snapshot = ServerSnapshot.__new__(ServerSnapshot)
Expand All @@ -227,15 +232,15 @@ def test_server_snapshot_cmd_not_implemented(self):
with pytest.raises(NotImplementedError):
snapshot.cmd("test-command")

def test_server_snapshot_is_alive(self):
def test_server_snapshot_is_alive(self) -> None:
"""Test that is_alive method returns False."""
# Create a minimal ServerSnapshot instance
snapshot = ServerSnapshot.__new__(ServerSnapshot)

# Test that is_alive method returns False
assert snapshot.is_alive() is False

def test_server_snapshot_raise_if_dead(self):
def test_server_snapshot_raise_if_dead(self) -> None:
"""Test that raise_if_dead method raises ConnectionError."""
# Create a minimal ServerSnapshot instance
snapshot = ServerSnapshot.__new__(ServerSnapshot)
Expand All @@ -245,7 +250,7 @@ def test_server_snapshot_raise_if_dead(self):
snapshot.raise_if_dead()


def test_snapshot_to_dict(session: Session):
def test_snapshot_to_dict(session: Session) -> None:
"""Test the snapshot_to_dict function."""
# Create a mock pane snapshot with the attributes we need
mock_snapshot = MagicMock(spec=PaneSnapshot)
Expand All @@ -263,7 +268,7 @@ def test_snapshot_to_dict(session: Session):
assert mock_snapshot.pane_index in str(snapshot_dict.values())


def test_snapshot_active_only():
def test_snapshot_active_only() -> None:
"""Test the snapshot_active_only function."""
# Create a minimal server snapshot with a session, window and pane
mock_server_snap = MagicMock(spec=ServerSnapshot)
Expand All @@ -282,7 +287,7 @@ def test_snapshot_active_only():
mock_server_snap.sessions_snapshot = [mock_session_snap]

# Create mock filter function that passes everything through
def mock_filter(snapshot):
def mock_filter(snapshot) -> bool:
return True

# Apply the filter with a patch to avoid actual implementation
Expand Down
0