Skip to content

Commit af07f59

Browse files
committed
refactor!(pytest_plugin): Remove all autouse from default usage
This prevents plugins from being invoked simply by having libtmux installed. We don't want that - it would interrupt systems. Explicit is better than implicit. Move autouse for libtmux itself to the appropriate places.
1 parent 71176d8 commit af07f59

File tree

4 files changed

+82
-31
lines changed

4 files changed

+82
-31
lines changed

docs/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from libtmux.conftest import * # NOQA: F4

libtmux/conftest.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import pathlib
2+
import shutil
3+
import typing as t
4+
5+
import pytest
6+
7+
from _pytest.doctest import DoctestItem
8+
9+
from libtmux.pytest_plugin import USING_ZSH
10+
11+
if t.TYPE_CHECKING:
12+
from libtmux.session import Session
13+
14+
15+
@pytest.fixture(autouse=True)
16+
def add_doctest_fixtures(
17+
request: pytest.FixtureRequest,
18+
doctest_namespace: t.Dict[str, t.Any],
19+
) -> None:
20+
if isinstance(request._pyfuncitem, DoctestItem) and shutil.which("tmux"):
21+
request.getfixturevalue("set_home")
22+
doctest_namespace["server"] = request.getfixturevalue("server")
23+
session: "Session" = request.getfixturevalue("session")
24+
doctest_namespace["session"] = session
25+
doctest_namespace["window"] = session.attached_window
26+
doctest_namespace["pane"] = session.attached_pane
27+
28+
29+
@pytest.fixture(autouse=True, scope="function")
30+
def set_home(
31+
monkeypatch: pytest.MonkeyPatch,
32+
user_path: pathlib.Path,
33+
) -> None:
34+
monkeypatch.setenv("HOME", str(user_path))
35+
36+
37+
@pytest.fixture(autouse=True, scope="session")
38+
@pytest.mark.usefixtures("clear_env")
39+
def setup(
40+
request: pytest.FixtureRequest,
41+
config_file: pathlib.Path,
42+
) -> None:
43+
if USING_ZSH:
44+
request.getfixturevalue("zshrc")

libtmux/pytest_plugin.py

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,10 @@
22
import logging
33
import os
44
import pathlib
5-
import shutil
65
import typing as t
76

87
import pytest
98

10-
from _pytest.doctest import DoctestItem
11-
from _pytest.fixtures import SubRequest
12-
from _pytest.monkeypatch import MonkeyPatch
13-
149
from libtmux import exc
1510
from libtmux.server import Server
1611
from libtmux.test import TEST_SESSION_PREFIX, get_test_session_name, namer
@@ -22,20 +17,35 @@
2217
USING_ZSH = "zsh" in os.getenv("SHELL", "")
2318

2419

25-
@pytest.fixture(autouse=True, scope="session")
20+
@pytest.fixture(scope="session")
2621
def home_path(tmp_path_factory: pytest.TempPathFactory) -> pathlib.Path:
22+
"""Temporary `/home/` path."""
2723
return tmp_path_factory.mktemp("home")
2824

2925

30-
@pytest.fixture(autouse=True, scope="session")
31-
def user_path(home_path: pathlib.Path) -> pathlib.Path:
32-
p = home_path / getpass.getuser()
26+
@pytest.fixture(scope="session")
27+
def home_user_name() -> str:
28+
"""Default username to set for :func:`user_path` fixture."""
29+
return getpass.getuser()
30+
31+
32+
@pytest.fixture(scope="session")
33+
def user_path(home_path: pathlib.Path, home_user_name: str) -> pathlib.Path:
34+
"""Default temporary user directory.
35+
36+
Used by:
37+
38+
- :func:`config_file`
39+
40+
Note: You will need to set the home directory, see :ref:`set_home`.
41+
"""
42+
p = home_path / home_user_name
3343
p.mkdir()
3444
return p
3545

3646

3747
@pytest.mark.skipif(USING_ZSH, reason="Using ZSH")
38-
@pytest.fixture(autouse=USING_ZSH, scope="session")
48+
@pytest.fixture(scope="session")
3949
def zshrc(user_path: pathlib.Path) -> pathlib.Path:
4050
"""This quiets ZSH default message.
4151
@@ -46,11 +56,15 @@ def zshrc(user_path: pathlib.Path) -> pathlib.Path:
4656
return p
4757

4858

49-
@pytest.fixture(scope="function")
59+
@pytest.fixture(scope="session")
5060
def config_file(user_path: pathlib.Path) -> pathlib.Path:
51-
"""Set default tmux configuration (base indexes for windows, panes)
61+
"""Default `.tmux.conf` configuration.
62+
63+
- ``base-index -g 1``
5264
53-
We need this for tests to work across tmux versions in our CI matrix.
65+
These guarantee pane and windows targets can be reliably referenced and asserted.
66+
67+
Note: You will need to set the home directory, see :ref:`set_home`.
5468
"""
5569
c = user_path / ".tmux.conf"
5670
c.write_text(
@@ -62,8 +76,8 @@ def config_file(user_path: pathlib.Path) -> pathlib.Path:
6276
return c
6377

6478

65-
@pytest.fixture(autouse=True)
66-
def clear_env(monkeypatch: MonkeyPatch) -> None:
79+
@pytest.fixture
80+
def clear_env(monkeypatch: pytest.MonkeyPatch) -> None:
6781
"""Clear out any unnecessary environment variables that could interrupt tests.
6882
6983
tmux show-environment tests were being interrupted due to a lot of crazy env vars.
@@ -92,9 +106,12 @@ def clear_env(monkeypatch: MonkeyPatch) -> None:
92106

93107
@pytest.fixture(scope="function")
94108
def server(
95-
request: SubRequest, monkeypatch: MonkeyPatch, config_file: pathlib.Path
109+
request: pytest.FixtureRequest,
110+
monkeypatch: pytest.MonkeyPatch,
111+
config_file: pathlib.Path,
96112
) -> Server:
97-
t = Server(config_file=str(config_file.absolute()))
113+
"""Returns a new, temporary :class:`libtmux.Server`"""
114+
t = Server()
98115
t.socket_name = "libtmux_test%s" % next(namer)
99116

100117
def fin() -> None:
@@ -106,7 +123,8 @@ def fin() -> None:
106123

107124

108125
@pytest.fixture(scope="function")
109-
def session(request: SubRequest, server: Server) -> "Session":
126+
def session(request: pytest.FixtureRequest, server: Server) -> "Session":
127+
"""Returns a new, temporary :class:`libtmux.Session`"""
110128
session_name = "tmuxp"
111129

112130
if not server.has_session(session_name):
@@ -146,16 +164,3 @@ def session(request: SubRequest, server: Server) -> "Session":
146164
assert TEST_SESSION_NAME != "tmuxp"
147165

148166
return session
149-
150-
151-
@pytest.fixture(autouse=True)
152-
def add_doctest_fixtures(
153-
request: SubRequest,
154-
doctest_namespace: t.Dict[str, t.Any],
155-
) -> None:
156-
if isinstance(request._pyfuncitem, DoctestItem) and shutil.which("tmux"):
157-
doctest_namespace["server"] = request.getfixturevalue("server")
158-
session: "Session" = request.getfixturevalue("session")
159-
doctest_namespace["session"] = session
160-
doctest_namespace["window"] = session.attached_window
161-
doctest_namespace["pane"] = session.attached_pane

tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from libtmux.conftest import * # NOQA: F4

0 commit comments

Comments
 (0)