Test helpers#
Helper methods for libtmux and downstream libtmux libraries.
- libtmux.test.retry_until(fun, seconds=8, *, interval=0.05, raises=True)[source]#
Retry a function until a condition meets or the specified time passes.
- Parameters:
fun (callable) – A function that will be called repeatedly until it returns
True
or the specified time passes.seconds (float) – Seconds to retry. Defaults to
8
, which is configurable viaRETRY_TIMEOUT_SECONDS
environment variables.interval (float) – Time in seconds to wait between calls. Defaults to
0.05
and is configurable viaRETRY_INTERVAL_SECONDS
environment variable.raises (bool) – Wether or not to raise an exception on timeout. Defaults to
True
.
Examples
>>> def fn(): ... p = session.attached_window.attached_pane ... return p.pane_current_path is not None
>>> retry_until(fn) True
In pytest:
>>> assert retry_until(fn, raises=False)
- Return type:
- libtmux.test.get_test_session_name(server, prefix='libtmux_')[source]#
Faker to create a session name that doesn’t exist.
- Parameters:
server (
libtmux.Server
) – libtmux serverprefix (str) – prefix for sessions (e.g.
libtmux_
). Defaults toTEST_SESSION_PREFIX
.
- Returns:
Random session name guaranteed to not collide with current ones.
- Return type:
Examples
>>> get_test_session_name(server=server) 'libtmux_...'
Never the same twice: >>> get_test_session_name(server=server) != get_test_session_name(server=server) True
- Return type:
- libtmux.test.get_test_window_name(session, prefix='libtmux_')[source]#
Faker to create a window name that doesn’t exist.
- Parameters:
session (
libtmux.Session
) – libtmux sessionprefix (str) –
prefix for windows (e.g.
libtmux_
). Defaults toTEST_SESSION_PREFIX
.ATM we reuse the test session prefix here.
- Returns:
Random window name guaranteed to not collide with current ones.
- Return type:
Examples
>>> get_test_window_name(session=session) 'libtmux_...'
Never the same twice: >>> get_test_window_name(session=session) != get_test_window_name(session=session) True
- Return type:
- libtmux.test.temp_session(server, *args, **kwargs)[source]#
Return a context manager with a temporary session.
If no
session_name
is entered,get_test_session_name()
will make an unused session name.The session will destroy itself upon closing with
Session. kill_session()
.- Parameters:
server (
libtmux.Server
) –args (list) – Arguments passed into
Server.new_session()
kwargs (dict) – Keyword arguments passed into
Server.new_session()
- Yields:
libtmux.Session
– Temporary session
Examples
>>> with temp_session(server) as session: ... session.new_window(window_name='my window') Window(@3 2:my window, Session($... ...))
- libtmux.test.temp_window(session, *args, **kwargs)[source]#
Return a context manager with a temporary window.
The window will destroy itself upon closing with
window. kill_window()
.If no
window_name
is entered,get_test_window_name()
will make an unused window name.- Parameters:
session (
libtmux.Session
) –args (list) – Arguments passed into
Session.new_window()
kwargs (dict) – Keyword arguments passed into
Session.new_window()
- Yields:
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_window() Pane(%4 Window(@3 2:libtmux_..., Session($1 libtmux_...)))
- class libtmux.test.EnvironmentVarGuard[source]#
Mock environmental variables safetly.
Helps rotect the environment variable properly. Can be used as context manager.
Notes
Vendorized to fix issue with Anaconda Python 2 not including test module, see #121 [1]_
References