Test helpers#

Helper methods for libtmux and downstream libtmux libraries.

class libtmux.test.RandomStrSequence[source]#

Factory to generate random string.

Create a random letter / number generator. 8 chars in length.

>>> rng = RandomStrSequence()
>>> next(rng)
'...'
>>> len(next(rng))
8
>>> type(next(rng))
<class 'str'>
__init__(characters='abcdefghijklmnopqrstuvwxyz0123456789_')[source]#

Create a random letter / number generator. 8 chars in length.

>>> rng = RandomStrSequence()
>>> next(rng)
'...'
>>> len(next(rng))
8
>>> type(next(rng))
<class 'str'>
Parameters:

characters (str) –

Return type:

None

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.

Return type:

bool

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 via RETRY_TIMEOUT_SECONDS environment variables.

  • interval (float) – Time in seconds to wait between calls. Defaults to 0.05 and is configurable via RETRY_INTERVAL_SECONDS environment variable.

  • raises (bool) – Whether or not to raise an exception on timeout. Defaults to True.

Examples

>>> def fn():
...     p = session.active_window.active_pane
...     return p.pane_current_path is not None
>>> retry_until(fn)
True

In pytest:

>>> assert retry_until(fn, raises=False)
libtmux.test.get_test_session_name(server, prefix='libtmux_')[source]#

Faker to create a session name that doesn’t exist.

Return type:

str

Parameters:
  • server (libtmux.Server) – libtmux server

  • prefix (str) – prefix for sessions (e.g. libtmux_). Defaults to TEST_SESSION_PREFIX.

Returns:

Random session name guaranteed to not collide with current ones.

Return type:

str

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

libtmux.test.get_test_window_name(session, prefix='libtmux_')[source]#

Faker to create a window name that doesn’t exist.

Return type:

str

Parameters:
  • session (libtmux.Session) – libtmux session

  • prefix (str) –

    prefix for windows (e.g. libtmux_). Defaults to TEST_SESSION_PREFIX.

    ATM we reuse the test session prefix here.

Returns:

Random window name guaranteed to not collide with current ones.

Return type:

str

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

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.session().

Return type:

Generator[Session, Any, Any]

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().

If no window_name is entered, get_test_window_name() will make an unused window name.

Return type:

Generator[Window, Any, Any]

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()
Pane(%4 Window(@3 2:libtmux_..., Session($1 libtmux_...)))
class libtmux.test.EnvironmentVarGuard[source]#

Mock environmental variables safely.

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

__init__()[source]#
Return type:

None

set(envvar, value)[source]#

Set environment variable.

Return type:

None

Parameters:
  • envvar (str) –

  • value (str) –

unset(envvar)[source]#

Unset environment variable.

Return type:

None

Parameters:

envvar (str) –