1- import getpass
21import logging
32import os
43import pathlib
4+ import shutil
55import typing as t
66
77import pytest
88
99from _pytest .doctest import DoctestItem
10- from _pytest .fixtures import SubRequest
1110
12- from libtmux import exc
13- from libtmux .common import which
14- from libtmux .server import Server
15- from libtmux .test import TEST_SESSION_PREFIX , get_test_session_name , namer
11+ from libtmux .test import namer
1612from tests .fixtures import utils as test_utils
1713
1814if t .TYPE_CHECKING :
2218USING_ZSH = "zsh" in os .getenv ("SHELL" , "" )
2319
2420
25- @pytest .fixture (autouse = True , scope = "session" )
26- def home_path (tmp_path_factory : pytest .TempPathFactory ):
27- return tmp_path_factory .mktemp ("home" )
28-
29-
30- @pytest .fixture (autouse = True , scope = "session" )
31- def user_path (home_path : pathlib .Path ):
32- p = home_path / getpass .getuser ()
33- p .mkdir ()
34- return p
35-
36-
3721@pytest .mark .skipif (USING_ZSH , reason = "Using ZSH" )
3822@pytest .fixture (autouse = USING_ZSH , scope = "session" )
3923def zshrc (user_path : pathlib .Path ):
@@ -47,8 +31,8 @@ def zshrc(user_path: pathlib.Path):
4731
4832
4933@pytest .fixture (autouse = True )
50- def home_path_default (user_path : pathlib .Path ):
51- os . environ [ "HOME" ] = str (user_path )
34+ def home_path_default (monkeypatch : pytest . MonkeyPatch , user_path : pathlib .Path ) -> None :
35+ monkeypatch . setenv ( "HOME" , str (user_path ) )
5236
5337
5438@pytest .fixture (scope = "function" )
@@ -70,77 +54,12 @@ def socket_name(request) -> str:
7054 return "tmuxp_test%s" % next (namer )
7155
7256
73- @pytest .fixture (scope = "function" )
74- def server (
75- request : SubRequest , monkeypatch : pytest .MonkeyPatch , socket_name : str
76- ) -> Server :
77- tmux = Server (socket_name = socket_name )
78-
79- def fin () -> None :
80- tmux .kill_server ()
81-
82- request .addfinalizer (fin )
83-
84- return tmux
85-
86-
87- @pytest .fixture (scope = "function" )
88- def session (server ):
89- session_name = "tmuxp"
90-
91- if not server .has_session (session_name ):
92- server .cmd (
93- "-f" ,
94- "/dev/null" , # use a blank config to reduce side effects
95- "new-session" ,
96- "-d" , # detached
97- "-s" ,
98- session_name ,
99- "/bin/sh" , # use /bin/sh as a shell to reduce side effects
100- # normally, it'd be -c, but new-session is special
101- )
102-
103- # find current sessions prefixed with tmuxp
104- old_test_sessions = [
105- s .get ("session_name" )
106- for s in server ._sessions
107- if s .get ("session_name" ).startswith (TEST_SESSION_PREFIX )
108- ]
109-
110- TEST_SESSION_NAME = get_test_session_name (server = server )
111-
112- try :
113- session = server .new_session (session_name = TEST_SESSION_NAME )
114- except exc .LibTmuxException as e :
115- raise e
116-
117- """
118- Make sure that tmuxp can :ref:`test_builder_visually` and switches to
119- the newly created session for that testcase.
120- """
121- session_id = session .get ("session_id" )
122- assert session_id is not None
123- try :
124- server .switch_client (target_session = session_id )
125- except exc .LibTmuxException :
126- # server.attach_session(session.get('session_id'))
127- pass
128-
129- for old_test_session in old_test_sessions :
130- logger .debug ("Old test test session %s found. Killing it." % old_test_session )
131- server .kill_session (old_test_session )
132- assert TEST_SESSION_NAME == session .get ("session_name" )
133- assert TEST_SESSION_NAME != "tmuxp"
134-
135- return session
136-
137-
13857@pytest .fixture (autouse = True )
13958def add_doctest_fixtures (
140- request : SubRequest ,
59+ request : pytest . FixtureRequest ,
14160 doctest_namespace : t .Dict [str , t .Any ],
14261) -> None :
143- if isinstance (request ._pyfuncitem , DoctestItem ) and which ("tmux" ):
62+ if isinstance (request ._pyfuncitem , DoctestItem ) and shutil . which ("tmux" ):
14463 doctest_namespace ["server" ] = request .getfixturevalue ("server" )
14564 session : "Session" = request .getfixturevalue ("session" )
14665 doctest_namespace ["session" ] = session
0 commit comments