From 3c01651e25eea6fc7727d2d7448b11a365c1a982 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sun, 15 Jan 2023 09:29:39 -0600 Subject: [PATCH 1/3] feat(pytest_plugin): Add session_params --- src/libtmux/pytest_plugin.py | 44 ++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/src/libtmux/pytest_plugin.py b/src/libtmux/pytest_plugin.py index d900011a1..f24bfdbd0 100644 --- a/src/libtmux/pytest_plugin.py +++ b/src/libtmux/pytest_plugin.py @@ -144,7 +144,47 @@ def fin() -> None: @pytest.fixture(scope="function") -def session(request: pytest.FixtureRequest, server: Server) -> "Session": +def session_params() -> t.Dict[str, t.Any]: + """Returns a new, temporary :class:`libtmux.Session` + + >>> import pytest + >>> from libtmux.session import Session + + >>> @pytest.fixture + ... def session_params(session_params): + ... return { + ... 'x': 800, + ... 'y': 600, + ... } + + >>> def test_example(session: "Session") -> None: + ... assert isinstance(session.name, str) + ... assert session.name.startswith('libtmux_') + ... window = session.new_window(window_name='new one') + ... assert window.name == 'new one' + + .. :: + >>> locals().keys() + dict_keys(...) + + >>> source = ''.join([e.source for e in request._pyfuncitem.dtest.examples][:4]) + >>> pytester = request.getfixturevalue('pytester') + + >>> pytester.makepyfile(**{'whatever.py': source}) + PosixPath(...) + + >>> result = pytester.runpytest('whatever.py', '--disable-warnings') + ===... + + >>> result.assert_outcomes(passed=1) + """ + return {} + + +@pytest.fixture(scope="function") +def session( + request: pytest.FixtureRequest, session_params: t.Dict[str, t.Any], server: Server +) -> "Session": """Returns a new, temporary :class:`libtmux.Session` >>> from libtmux.session import Session @@ -185,7 +225,7 @@ def session(request: pytest.FixtureRequest, server: Server) -> "Session": TEST_SESSION_NAME = get_test_session_name(server=server) try: - session = server.new_session(session_name=TEST_SESSION_NAME) + session = server.new_session(session_name=TEST_SESSION_NAME, **session_params) except exc.LibTmuxException as e: raise e From ff06d3f3eb7d04ba3acde67f6b94fb176b63aa07 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sun, 15 Jan 2023 09:36:52 -0600 Subject: [PATCH 2/3] docs(CHANGES): Add pytest-plugin example --- docs/pytest-plugin/index.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/docs/pytest-plugin/index.md b/docs/pytest-plugin/index.md index bceb87f08..cc02ac118 100644 --- a/docs/pytest-plugin/index.md +++ b/docs/pytest-plugin/index.md @@ -69,6 +69,30 @@ options: You could also read the code and override {func}`server fixtures `'s in your own doctest. doctest. +(custom_session_params)= + +### Custom session parameters + +You can override `session_params` to custom the `session` fixture. The +dictionary will directly pass into :meth:`Server.new_sesion` keyword arguments. + +```python +import pytest + +@pytest.fixture +def session_params(): + return { + 'x': 800, + 'y': 600 + } + + +def test_something(session): + assert session +``` + +The above will assure the libtmux session launches with `-x 800 -y 600`. + (set_home)= ### Setting a temporary home directory From 0a509c6f171b15c40c83be677c2a8de9643437a6 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sun, 15 Jan 2023 09:37:29 -0600 Subject: [PATCH 3/3] docs(CHANGES): Note updates --- CHANGES | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES b/CHANGES index 5dde2fac7..3b9327f6d 100644 --- a/CHANGES +++ b/CHANGES @@ -18,6 +18,8 @@ $ pip install --user --upgrade --pre libtmux - Server.new_session: Accept `x` and `y`, thanks @rockandska (#469) +- New test fixture: `session_params`. The dict is used direclty in the `session` + pytest fixture (#470) ## libtmux 0.19.1 (2022-01-07)