|
| 1 | +"""Tests for libtmux's temporary test utilities.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import typing as t |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from libtmux.test.temporary import temp_session, temp_window |
| 10 | + |
| 11 | +if t.TYPE_CHECKING: |
| 12 | + from libtmux.server import Server |
| 13 | + from libtmux.session import Session |
| 14 | + |
| 15 | + |
| 16 | +def test_temp_session_creates_and_destroys(server: Server) -> None: |
| 17 | + """Test that temp_session creates and destroys a session.""" |
| 18 | + with temp_session(server) as session: |
| 19 | + session_name = session.session_name |
| 20 | + assert session_name is not None |
| 21 | + assert server.has_session(session_name) |
| 22 | + |
| 23 | + assert session_name is not None |
| 24 | + assert not server.has_session(session_name) |
| 25 | + |
| 26 | + |
| 27 | +def test_temp_session_with_name(server: Server) -> None: |
| 28 | + """Test temp_session with a provided session name.""" |
| 29 | + session_name = "test_session" |
| 30 | + with temp_session(server, session_name=session_name) as session: |
| 31 | + assert session.session_name == session_name |
| 32 | + assert server.has_session(session_name) |
| 33 | + |
| 34 | + assert not server.has_session(session_name) |
| 35 | + |
| 36 | + |
| 37 | +def test_temp_session_cleanup_on_exception(server: Server) -> None: |
| 38 | + """Test that temp_session cleans up even when an exception occurs.""" |
| 39 | + test_error = RuntimeError() |
| 40 | + session_name = None |
| 41 | + |
| 42 | + with pytest.raises(RuntimeError), temp_session(server) as session: |
| 43 | + session_name = session.session_name |
| 44 | + assert session_name is not None |
| 45 | + assert server.has_session(session_name) |
| 46 | + raise test_error |
| 47 | + |
| 48 | + assert session_name is not None |
| 49 | + assert not server.has_session(session_name) |
| 50 | + |
| 51 | + |
| 52 | +def test_temp_window_creates_and_destroys(session: Session) -> None: |
| 53 | + """Test that temp_window creates and destroys a window.""" |
| 54 | + initial_windows = len(session.windows) |
| 55 | + |
| 56 | + with temp_window(session) as window: |
| 57 | + window_id = window.window_id |
| 58 | + assert window_id is not None |
| 59 | + assert len(session.windows) == initial_windows + 1 |
| 60 | + assert any(w.window_id == window_id for w in session.windows) |
| 61 | + |
| 62 | + assert len(session.windows) == initial_windows |
| 63 | + assert window_id is not None |
| 64 | + assert not any(w.window_id == window_id for w in session.windows) |
| 65 | + |
| 66 | + |
| 67 | +def test_temp_window_with_name(session: Session) -> None: |
| 68 | + """Test temp_window with a provided window name.""" |
| 69 | + window_name = "test_window" |
| 70 | + initial_windows = len(session.windows) |
| 71 | + |
| 72 | + with temp_window(session, window_name=window_name) as window: |
| 73 | + assert window.window_name == window_name |
| 74 | + assert len(session.windows) == initial_windows + 1 |
| 75 | + assert any(w.window_name == window_name for w in session.windows) |
| 76 | + |
| 77 | + assert len(session.windows) == initial_windows |
| 78 | + assert not any(w.window_name == window_name for w in session.windows) |
| 79 | + |
| 80 | + |
| 81 | +def test_temp_window_cleanup_on_exception(session: Session) -> None: |
| 82 | + """Test that temp_window cleans up even when an exception occurs.""" |
| 83 | + initial_windows = len(session.windows) |
| 84 | + test_error = RuntimeError() |
| 85 | + window_id = None |
| 86 | + |
| 87 | + with pytest.raises(RuntimeError), temp_window(session) as window: |
| 88 | + window_id = window.window_id |
| 89 | + assert window_id is not None |
| 90 | + assert len(session.windows) == initial_windows + 1 |
| 91 | + assert any(w.window_id == window_id for w in session.windows) |
| 92 | + raise test_error |
| 93 | + |
| 94 | + assert len(session.windows) == initial_windows |
| 95 | + assert window_id is not None |
| 96 | + assert not any(w.window_id == window_id for w in session.windows) |
0 commit comments