Test helpers#

Helper methods for libtmux and downstream libtmux libraries.

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

set(self, envvar, value)[source]#
unset(self, envvar)[source]#
libtmux.test.RETRY_INTERVAL_SECONDS#
libtmux.test.RETRY_TIMEOUT_SECONDS#
libtmux.test.TEST_SESSION_PREFIX = libtmux_#
exception libtmux.test.WaitTimeout[source]#

Function timed out without meeting condition

libtmux.test.current_dir#
libtmux.test.example_dir#
libtmux.test.fixtures_dir#
libtmux.test.get_test_session_name(server, prefix=TEST_SESSION_PREFIX)[source]#

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

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

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

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

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

libtmux.test.logger#
libtmux.test.namer#
libtmux.test.retry(seconds: Optional[float] = RETRY_TIMEOUT_SECONDS)[source]#

Retry a block of code until a time limit or break.

Deprecated since version 0.12.0: retry doesn’t work, it will be removed in libtmux 0.13.0, it is replaced by retry_until, more info: https://github.com/tmux-python/libtmux/issues/368.

Parameters:

seconds (float) – Seconds to retry, defaults to RETRY_TIMEOUT_SECONDS, which is configurable via environmental variables.

Returns:

True if time passed since retry() invoked less than seconds param.

Return type:

bool

Examples

>>> while retry():
...     p = w.attached_pane
...     p.server._update_panes()
...     if p.current_path == pane_path:
...         break
libtmux.test.retry_until(fun: Callable, seconds: float = RETRY_TIMEOUT_SECONDS, *, interval: Optional[float] = RETRY_INTERVAL_SECONDS, raises: Optional[bool] = 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 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) – Wether or not to raise an exception on timeout. Defaults to True.

Examples

>>> def f():
...     p = w.attached_pane
...     p.server._update_panes()
...     return p.current_path == pane_path
...
... retry(f)

In pytest:

>>> assert retry(f, raises=False)
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')
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:
...     my_pane = window.split_window()