2
2
import logging
3
3
import os
4
4
import pathlib
5
- import shutil
6
5
import typing as t
7
6
8
7
import pytest
9
8
10
- from _pytest .doctest import DoctestItem
11
- from _pytest .fixtures import SubRequest
12
- from _pytest .monkeypatch import MonkeyPatch
13
-
14
9
from libtmux import exc
15
10
from libtmux .server import Server
16
11
from libtmux .test import TEST_SESSION_PREFIX , get_test_session_name , namer
22
17
USING_ZSH = "zsh" in os .getenv ("SHELL" , "" )
23
18
24
19
25
- @pytest .fixture (autouse = True , scope = "session" )
20
+ @pytest .fixture (scope = "session" )
26
21
def home_path (tmp_path_factory : pytest .TempPathFactory ) -> pathlib .Path :
22
+ """Temporary `/home/` path."""
27
23
return tmp_path_factory .mktemp ("home" )
28
24
29
25
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: :func:`config_file`, :func:`zshrc`
37
+
38
+ Note: You will need to set the home directory, see :ref:`set_home`.
39
+ """
40
+ p = home_path / home_user_name
33
41
p .mkdir ()
34
42
return p
35
43
36
44
37
45
@pytest .mark .skipif (USING_ZSH , reason = "Using ZSH" )
38
- @pytest .fixture (autouse = USING_ZSH , scope = "session" )
46
+ @pytest .fixture (scope = "session" )
39
47
def zshrc (user_path : pathlib .Path ) -> pathlib .Path :
40
48
"""This quiets ZSH default message.
41
49
@@ -46,11 +54,15 @@ def zshrc(user_path: pathlib.Path) -> pathlib.Path:
46
54
return p
47
55
48
56
49
- @pytest .fixture (scope = "function " )
57
+ @pytest .fixture (scope = "session " )
50
58
def config_file (user_path : pathlib .Path ) -> pathlib .Path :
51
- """Set default tmux configuration (base indexes for windows, panes)
59
+ """Default `.tmux.conf` configuration.
60
+
61
+ - ``base-index -g 1``
62
+
63
+ These guarantee pane and windows targets can be reliably referenced and asserted.
52
64
53
- We need this for tests to work across tmux versions in our CI matrix .
65
+ Note: You will need to set the home directory, see :ref:`set_home` .
54
66
"""
55
67
c = user_path / ".tmux.conf"
56
68
c .write_text (
@@ -62,8 +74,8 @@ def config_file(user_path: pathlib.Path) -> pathlib.Path:
62
74
return c
63
75
64
76
65
- @pytest .fixture ( autouse = True )
66
- def clear_env (monkeypatch : MonkeyPatch ) -> None :
77
+ @pytest .fixture
78
+ def clear_env (monkeypatch : pytest . MonkeyPatch ) -> None :
67
79
"""Clear out any unnecessary environment variables that could interrupt tests.
68
80
69
81
tmux show-environment tests were being interrupted due to a lot of crazy env vars.
@@ -92,9 +104,12 @@ def clear_env(monkeypatch: MonkeyPatch) -> None:
92
104
93
105
@pytest .fixture (scope = "function" )
94
106
def server (
95
- request : SubRequest , monkeypatch : MonkeyPatch , config_file : pathlib .Path
107
+ request : pytest .FixtureRequest ,
108
+ monkeypatch : pytest .MonkeyPatch ,
109
+ config_file : pathlib .Path ,
96
110
) -> Server :
97
- t = Server (config_file = str (config_file .absolute ()))
111
+ """Returns a new, temporary :class:`libtmux.Server`"""
112
+ t = Server ()
98
113
t .socket_name = "libtmux_test%s" % next (namer )
99
114
100
115
def fin () -> None :
@@ -106,7 +121,8 @@ def fin() -> None:
106
121
107
122
108
123
@pytest .fixture (scope = "function" )
109
- def session (request : SubRequest , server : Server ) -> "Session" :
124
+ def session (request : pytest .FixtureRequest , server : Server ) -> "Session" :
125
+ """Returns a new, temporary :class:`libtmux.Session`"""
110
126
session_name = "tmuxp"
111
127
112
128
if not server .has_session (session_name ):
@@ -146,16 +162,3 @@ def session(request: SubRequest, server: Server) -> "Session":
146
162
assert TEST_SESSION_NAME != "tmuxp"
147
163
148
164
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
0 commit comments