Skip to content

Commit ce459bc

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 ce459bc

File tree

4 files changed

+74
-25
lines changed

4 files changed

+74
-25
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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
doctest_namespace["server"] = request.getfixturevalue("server")
22+
session: "Session" = request.getfixturevalue("session")
23+
doctest_namespace["session"] = session
24+
doctest_namespace["window"] = session.attached_window
25+
doctest_namespace["pane"] = session.attached_pane
26+
27+
28+
@pytest.fixture(autouse=True, scope="function")
29+
def set_home(
30+
monkeypatch: pytest.MonkeyPatch,
31+
user_path: pathlib.Path,
32+
) -> None:
33+
monkeypatch.setenv("HOME", str(user_path))
34+
35+
36+
@pytest.fixture(autouse=True, scope="session")
37+
@pytest.mark.usefixtures("clear_env")
38+
def setup(
39+
request: pytest.FixtureRequest,
40+
config_file: pathlib.Path,
41+
) -> None:
42+
if USING_ZSH:
43+
request.getfixturevalue("zshrc")

libtmux/pytest_plugin.py

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +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
119
from _pytest.fixtures import SubRequest
1210
from _pytest.monkeypatch import MonkeyPatch
1311

@@ -22,20 +20,35 @@
2220
USING_ZSH = "zsh" in os.getenv("SHELL", "")
2321

2422

25-
@pytest.fixture(autouse=True, scope="session")
23+
@pytest.fixture(scope="session")
2624
def home_path(tmp_path_factory: pytest.TempPathFactory) -> pathlib.Path:
25+
"""Temporary `/home/` path."""
2726
return tmp_path_factory.mktemp("home")
2827

2928

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

3649

3750
@pytest.mark.skipif(USING_ZSH, reason="Using ZSH")
38-
@pytest.fixture(autouse=USING_ZSH, scope="session")
51+
@pytest.fixture(scope="session")
3952
def zshrc(user_path: pathlib.Path) -> pathlib.Path:
4053
"""This quiets ZSH default message.
4154
@@ -46,11 +59,15 @@ def zshrc(user_path: pathlib.Path) -> pathlib.Path:
4659
return p
4760

4861

49-
@pytest.fixture(scope="function")
62+
@pytest.fixture(scope="session")
5063
def config_file(user_path: pathlib.Path) -> pathlib.Path:
51-
"""Set default tmux configuration (base indexes for windows, panes)
64+
"""Default `.tmux.conf` configuration.
65+
66+
- ``base-index -g 1``
67+
68+
These guarantee pane and windows targets can be reliably referenced and asserted.
5269
53-
We need this for tests to work across tmux versions in our CI matrix.
70+
Note: You will need to set the home directory, see :ref:`set_home`.
5471
"""
5572
c = user_path / ".tmux.conf"
5673
c.write_text(
@@ -62,7 +79,7 @@ def config_file(user_path: pathlib.Path) -> pathlib.Path:
6279
return c
6380

6481

65-
@pytest.fixture(autouse=True)
82+
@pytest.fixture
6683
def clear_env(monkeypatch: MonkeyPatch) -> None:
6784
"""Clear out any unnecessary environment variables that could interrupt tests.
6885
@@ -94,7 +111,7 @@ def clear_env(monkeypatch: MonkeyPatch) -> None:
94111
def server(
95112
request: SubRequest, monkeypatch: MonkeyPatch, config_file: pathlib.Path
96113
) -> Server:
97-
t = Server(config_file=str(config_file.absolute()))
114+
t = Server()
98115
t.socket_name = "libtmux_test%s" % next(namer)
99116

100117
def fin() -> None:
@@ -146,16 +163,3 @@ def session(request: SubRequest, server: Server) -> "Session":
146163
assert TEST_SESSION_NAME != "tmuxp"
147164

148165
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)