Skip to content

Commit 09e3968

Browse files
committed
refactor!(test[temporary]) Move from test to test.temporary
1 parent 9d6ffae commit 09e3968

File tree

2 files changed

+137
-113
lines changed

2 files changed

+137
-113
lines changed

src/libtmux/test/__init__.py

Lines changed: 0 additions & 113 deletions
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

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

0 commit comments

Comments
 (0)