Skip to content

Commit 8751eb2

Browse files
committed
refactor!(test[temporary]) Move from test to test.temporary
1 parent 5e87bc0 commit 8751eb2

File tree

2 files changed

+135
-113
lines changed

2 files changed

+135
-113
lines changed

src/libtmux/test/__init__.py

-113
Original file line numberDiff line numberDiff line change
@@ -88,116 +88,3 @@ def retry_until(
8888
return False
8989
time.sleep(interval)
9090
return True
91-
92-
93-
@contextlib.contextmanager
94-
def temp_session(
95-
server: Server,
96-
*args: t.Any,
97-
**kwargs: t.Any,
98-
) -> Generator[Session, t.Any, t.Any]:
99-
"""
100-
Return a context manager with a temporary session.
101-
102-
If no ``session_name`` is entered, :func:`get_test_session_name` will make
103-
an unused session name.
104-
105-
The session will destroy itself upon closing with :meth:`Session.session()`.
106-
107-
Parameters
108-
----------
109-
server : :class:`libtmux.Server`
110-
111-
Other Parameters
112-
----------------
113-
args : list
114-
Arguments passed into :meth:`Server.new_session`
115-
kwargs : dict
116-
Keyword arguments passed into :meth:`Server.new_session`
117-
118-
Yields
119-
------
120-
:class:`libtmux.Session`
121-
Temporary session
122-
123-
Examples
124-
--------
125-
>>> with temp_session(server) as session:
126-
... session.new_window(window_name='my window')
127-
Window(@3 2:my window, Session($... ...))
128-
"""
129-
if "session_name" in kwargs:
130-
session_name = kwargs.pop("session_name")
131-
else:
132-
session_name = get_test_session_name(server)
133-
134-
session = server.new_session(session_name, *args, **kwargs)
135-
136-
try:
137-
yield session
138-
finally:
139-
if server.has_session(session_name):
140-
session.kill()
141-
return
142-
143-
144-
@contextlib.contextmanager
145-
def temp_window(
146-
session: Session,
147-
*args: t.Any,
148-
**kwargs: t.Any,
149-
) -> Generator[Window, t.Any, t.Any]:
150-
"""
151-
Return a context manager with a temporary window.
152-
153-
The window will destroy itself upon closing with :meth:`window.
154-
kill()`.
155-
156-
If no ``window_name`` is entered, :func:`get_test_window_name` will make
157-
an unused window name.
158-
159-
Parameters
160-
----------
161-
session : :class:`libtmux.Session`
162-
163-
Other Parameters
164-
----------------
165-
args : list
166-
Arguments passed into :meth:`Session.new_window`
167-
kwargs : dict
168-
Keyword arguments passed into :meth:`Session.new_window`
169-
170-
Yields
171-
------
172-
:class:`libtmux.Window`
173-
temporary window
174-
175-
Examples
176-
--------
177-
>>> with temp_window(session) as window:
178-
... window
179-
Window(@2 2:... Session($1 libtmux_...))
180-
181-
182-
>>> with temp_window(session) as window:
183-
... window.split()
184-
Pane(%4 Window(@3 2:libtmux_..., Session($1 libtmux_...)))
185-
"""
186-
if "window_name" not in kwargs:
187-
window_name = get_test_window_name(session)
188-
else:
189-
window_name = kwargs.pop("window_name")
190-
191-
window = session.new_window(window_name, *args, **kwargs)
192-
193-
# Get ``window_id`` before returning it, it may be killed within context.
194-
window_id = window.window_id
195-
assert window_id is not None
196-
assert isinstance(window_id, str)
197-
198-
try:
199-
yield window
200-
finally:
201-
if len(session.windows.filter(window_id=window_id)) > 0:
202-
window.kill()
203-
return

src/libtmux/test/temporary.py

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
"""Temporary object helpers for libtmux and downstream libtmux libraries."""
2+
3+
from __future__ import annotations
4+
5+
import contextlib
6+
import logging
7+
import typing as t
8+
9+
from libtmux.test.random import get_test_session_name, get_test_window_name
10+
11+
logger = logging.getLogger(__name__)
12+
13+
if t.TYPE_CHECKING:
14+
import sys
15+
from collections.abc import Generator
16+
17+
from libtmux.server import Server
18+
from libtmux.session import Session
19+
from libtmux.window import Window
20+
21+
if sys.version_info >= (3, 11):
22+
pass
23+
24+
25+
@contextlib.contextmanager
26+
def temp_session(
27+
server: Server,
28+
*args: t.Any,
29+
**kwargs: t.Any,
30+
) -> Generator[Session, t.Any, t.Any]:
31+
"""
32+
Return a context manager with a temporary session.
33+
34+
If no ``session_name`` is entered, :func:`get_test_session_name` will make
35+
an unused session name.
36+
37+
The session will destroy itself upon closing with :meth:`Session.session()`.
38+
39+
Parameters
40+
----------
41+
server : :class:`libtmux.Server`
42+
43+
Other Parameters
44+
----------------
45+
args : list
46+
Arguments passed into :meth:`Server.new_session`
47+
kwargs : dict
48+
Keyword arguments passed into :meth:`Server.new_session`
49+
50+
Yields
51+
------
52+
:class:`libtmux.Session`
53+
Temporary session
54+
55+
Examples
56+
--------
57+
>>> with temp_session(server) as session:
58+
... session.new_window(window_name='my window')
59+
Window(@3 2:my window, Session($... ...))
60+
"""
61+
if "session_name" in kwargs:
62+
session_name = kwargs.pop("session_name")
63+
else:
64+
session_name = get_test_session_name(server)
65+
66+
session = server.new_session(session_name, *args, **kwargs)
67+
68+
try:
69+
yield session
70+
finally:
71+
if server.has_session(session_name):
72+
session.kill()
73+
return
74+
75+
76+
@contextlib.contextmanager
77+
def temp_window(
78+
session: Session,
79+
*args: t.Any,
80+
**kwargs: t.Any,
81+
) -> Generator[Window, t.Any, t.Any]:
82+
"""
83+
Return a context manager with a temporary window.
84+
85+
The window will destroy itself upon closing with :meth:`window.
86+
kill()`.
87+
88+
If no ``window_name`` is entered, :func:`get_test_window_name` will make
89+
an unused window name.
90+
91+
Parameters
92+
----------
93+
session : :class:`libtmux.Session`
94+
95+
Other Parameters
96+
----------------
97+
args : list
98+
Arguments passed into :meth:`Session.new_window`
99+
kwargs : dict
100+
Keyword arguments passed into :meth:`Session.new_window`
101+
102+
Yields
103+
------
104+
:class:`libtmux.Window`
105+
temporary window
106+
107+
Examples
108+
--------
109+
>>> with temp_window(session) as window:
110+
... window
111+
Window(@2 2:... Session($1 libtmux_...))
112+
113+
114+
>>> with temp_window(session) as window:
115+
... window.split()
116+
Pane(%4 Window(@3 2:libtmux_..., Session($1 libtmux_...)))
117+
"""
118+
if "window_name" not in kwargs:
119+
window_name = get_test_window_name(session)
120+
else:
121+
window_name = kwargs.pop("window_name")
122+
123+
window = session.new_window(window_name, *args, **kwargs)
124+
125+
# Get ``window_id`` before returning it, it may be killed within context.
126+
window_id = window.window_id
127+
assert window_id is not None
128+
assert isinstance(window_id, str)
129+
130+
try:
131+
yield window
132+
finally:
133+
if len(session.windows.filter(window_id=window_id)) > 0:
134+
window.kill()
135+
return

0 commit comments

Comments
 (0)