Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

test.py: Split into test/ #578

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

Merged
merged 12 commits into from
Feb 23, 2025
Merged
Show file tree
Hide file tree
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
refactor!(test[temporary]) Move from test to test.temporary
  • Loading branch information
tony committed Feb 23, 2025
commit 8751eb283df321c78c20446751fe99a8a07ee3ea
113 changes: 0 additions & 113 deletions 113 src/libtmux/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,116 +88,3 @@ def retry_until(
return False
time.sleep(interval)
return True


@contextlib.contextmanager
def temp_session(
server: Server,
*args: t.Any,
**kwargs: t.Any,
) -> Generator[Session, t.Any, t.Any]:
"""
Return a context manager with a temporary session.

If no ``session_name`` is entered, :func:`get_test_session_name` will make
an unused session name.

The session will destroy itself upon closing with :meth:`Session.session()`.

Parameters
----------
server : :class:`libtmux.Server`

Other Parameters
----------------
args : list
Arguments passed into :meth:`Server.new_session`
kwargs : dict
Keyword arguments passed into :meth:`Server.new_session`

Yields
------
:class:`libtmux.Session`
Temporary session

Examples
--------
>>> with temp_session(server) as session:
... session.new_window(window_name='my window')
Window(@3 2:my window, Session($... ...))
"""
if "session_name" in kwargs:
session_name = kwargs.pop("session_name")
else:
session_name = get_test_session_name(server)

session = server.new_session(session_name, *args, **kwargs)

try:
yield session
finally:
if server.has_session(session_name):
session.kill()
return


@contextlib.contextmanager
def temp_window(
session: Session,
*args: t.Any,
**kwargs: t.Any,
) -> Generator[Window, t.Any, t.Any]:
"""
Return a context manager with a temporary window.

The window will destroy itself upon closing with :meth:`window.
kill()`.

If no ``window_name`` is entered, :func:`get_test_window_name` will make
an unused window name.

Parameters
----------
session : :class:`libtmux.Session`

Other Parameters
----------------
args : list
Arguments passed into :meth:`Session.new_window`
kwargs : dict
Keyword arguments passed into :meth:`Session.new_window`

Yields
------
:class:`libtmux.Window`
temporary window

Examples
--------
>>> with temp_window(session) as window:
... window
Window(@2 2:... Session($1 libtmux_...))


>>> with temp_window(session) as window:
... window.split()
Pane(%4 Window(@3 2:libtmux_..., Session($1 libtmux_...)))
"""
if "window_name" not in kwargs:
window_name = get_test_window_name(session)
else:
window_name = kwargs.pop("window_name")

window = session.new_window(window_name, *args, **kwargs)

# Get ``window_id`` before returning it, it may be killed within context.
window_id = window.window_id
assert window_id is not None
assert isinstance(window_id, str)

try:
yield window
finally:
if len(session.windows.filter(window_id=window_id)) > 0:
window.kill()
return
135 changes: 135 additions & 0 deletions 135 src/libtmux/test/temporary.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
"""Temporary object helpers for libtmux and downstream libtmux libraries."""

from __future__ import annotations

import contextlib
import logging
import typing as t

from libtmux.test.random import get_test_session_name, get_test_window_name

logger = logging.getLogger(__name__)

if t.TYPE_CHECKING:
import sys
from collections.abc import Generator

from libtmux.server import Server
from libtmux.session import Session
from libtmux.window import Window

if sys.version_info >= (3, 11):
pass


@contextlib.contextmanager
def temp_session(
server: Server,
*args: t.Any,
**kwargs: t.Any,
) -> Generator[Session, t.Any, t.Any]:
"""
Return a context manager with a temporary session.

If no ``session_name`` is entered, :func:`get_test_session_name` will make
an unused session name.

The session will destroy itself upon closing with :meth:`Session.session()`.

Parameters
----------
server : :class:`libtmux.Server`

Other Parameters
----------------
args : list
Arguments passed into :meth:`Server.new_session`
kwargs : dict
Keyword arguments passed into :meth:`Server.new_session`

Yields
------
:class:`libtmux.Session`
Temporary session

Examples
--------
>>> with temp_session(server) as session:
... session.new_window(window_name='my window')
Window(@3 2:my window, Session($... ...))
"""
if "session_name" in kwargs:
session_name = kwargs.pop("session_name")
else:
session_name = get_test_session_name(server)

session = server.new_session(session_name, *args, **kwargs)

try:
yield session
finally:
if server.has_session(session_name):
session.kill()
return


@contextlib.contextmanager
def temp_window(
session: Session,
*args: t.Any,
**kwargs: t.Any,
) -> Generator[Window, t.Any, t.Any]:
"""
Return a context manager with a temporary window.

The window will destroy itself upon closing with :meth:`window.
kill()`.

If no ``window_name`` is entered, :func:`get_test_window_name` will make
an unused window name.

Parameters
----------
session : :class:`libtmux.Session`

Other Parameters
----------------
args : list
Arguments passed into :meth:`Session.new_window`
kwargs : dict
Keyword arguments passed into :meth:`Session.new_window`

Yields
------
:class:`libtmux.Window`
temporary window

Examples
--------
>>> with temp_window(session) as window:
... window
Window(@2 2:... Session($1 libtmux_...))


>>> with temp_window(session) as window:
... window.split()
Pane(%4 Window(@3 2:libtmux_..., Session($1 libtmux_...)))
"""
if "window_name" not in kwargs:
window_name = get_test_window_name(session)
else:
window_name = kwargs.pop("window_name")

window = session.new_window(window_name, *args, **kwargs)

# Get ``window_id`` before returning it, it may be killed within context.
window_id = window.window_id
assert window_id is not None
assert isinstance(window_id, str)

try:
yield window
finally:
if len(session.windows.filter(window_id=window_id)) > 0:
window.kill()
return
Morty Proxy This is a proxified and sanitized view of the page, visit original site.