33import os
44import pathlib
55import typing as t
6- from unittest .mock import MagicMock
76
87import pytest
98
109import click
1110import kaptan
1211from click .testing import CliRunner
12+ from pytest_mock import MockerFixture
1313
1414import libtmux
1515from libtmux .common import has_lt_version
3838from .constants import FIXTURE_PATH
3939from .fixtures import utils as test_utils
4040
41+ if t .TYPE_CHECKING :
42+ from libtmux .server import Server
43+
4144
4245def test_creates_config_dir_not_exists (tmp_path : pathlib .Path ):
4346 """cli.startup() creates config dir if not exists."""
@@ -1216,15 +1219,15 @@ def test_reattach_plugins(monkeypatch_plugin_test_packages, server):
12161219 assert proc .stdout [0 ] == "'plugin_test_r'"
12171220
12181221
1219- def test_load_attached (server , monkeypatch ):
1222+ def test_load_attached (
1223+ server : "Server" , monkeypatch : pytest .MonkeyPatch , mocker : MockerFixture
1224+ ) -> None :
12201225 # Load a session and attach from outside tmux
12211226 monkeypatch .delenv ("TMUX" , raising = False )
12221227
1223- attach_session_mock = MagicMock ( )
1228+ attach_session_mock = mocker . patch ( "libtmux.session.Session.attach_session" )
12241229 attach_session_mock .return_value .stderr = None
12251230
1226- monkeypatch .setattr ("libtmux.session.Session.attach_session" , attach_session_mock )
1227-
12281231 yaml_config = test_utils .read_config_file ("workspacebuilder/two_pane.yaml" )
12291232 sconfig = kaptan .Kaptan (handler = "yaml" )
12301233 sconfig = sconfig .import_config (yaml_config ).get ()
@@ -1233,18 +1236,18 @@ def test_load_attached(server, monkeypatch):
12331236
12341237 _load_attached (builder , False )
12351238
1236- assert builder . session . attach_session .call_count == 1
1239+ assert attach_session_mock .call_count == 1
12371240
12381241
1239- def test_load_attached_detached (server , monkeypatch ):
1242+ def test_load_attached_detached (
1243+ server : "Server" , monkeypatch : pytest .MonkeyPatch , mocker : MockerFixture
1244+ ) -> None :
12401245 # Load a session but don't attach
12411246 monkeypatch .delenv ("TMUX" , raising = False )
12421247
1243- attach_session_mock = MagicMock ( )
1248+ attach_session_mock = mocker . patch ( "libtmux.session.Session.attach_session" )
12441249 attach_session_mock .return_value .stderr = None
12451250
1246- monkeypatch .setattr ("libtmux.session.Session.attach_session" , attach_session_mock )
1247-
12481251 yaml_config = test_utils .read_config_file ("workspacebuilder/two_pane.yaml" )
12491252 sconfig = kaptan .Kaptan (handler = "yaml" )
12501253 sconfig = sconfig .import_config (yaml_config ).get ()
@@ -1253,18 +1256,18 @@ def test_load_attached_detached(server, monkeypatch):
12531256
12541257 _load_attached (builder , True )
12551258
1256- assert builder . session . attach_session .call_count == 0
1259+ assert attach_session_mock .call_count == 0
12571260
12581261
1259- def test_load_attached_within_tmux (server , monkeypatch ):
1262+ def test_load_attached_within_tmux (
1263+ server : "Server" , monkeypatch : pytest .MonkeyPatch , mocker : MockerFixture
1264+ ) -> None :
12601265 # Load a session and attach from within tmux
12611266 monkeypatch .setenv ("TMUX" , "/tmp/tmux-1234/default,123,0" )
12621267
1263- switch_client_mock = MagicMock ( )
1268+ switch_client_mock = mocker . patch ( "libtmux.session.Session.switch_client" )
12641269 switch_client_mock .return_value .stderr = None
12651270
1266- monkeypatch .setattr ("libtmux.session.Session.switch_client" , switch_client_mock )
1267-
12681271 yaml_config = test_utils .read_config_file ("workspacebuilder/two_pane.yaml" )
12691272 sconfig = kaptan .Kaptan (handler = "yaml" )
12701273 sconfig = sconfig .import_config (yaml_config ).get ()
@@ -1273,18 +1276,18 @@ def test_load_attached_within_tmux(server, monkeypatch):
12731276
12741277 _load_attached (builder , False )
12751278
1276- assert builder . session . switch_client .call_count == 1
1279+ assert switch_client_mock .call_count == 1
12771280
12781281
1279- def test_load_attached_within_tmux_detached (server , monkeypatch ):
1282+ def test_load_attached_within_tmux_detached (
1283+ server : "Server" , monkeypatch : pytest .MonkeyPatch , mocker : MockerFixture
1284+ ) -> None :
12801285 # Load a session and attach from within tmux
12811286 monkeypatch .setenv ("TMUX" , "/tmp/tmux-1234/default,123,0" )
12821287
1283- switch_client_mock = MagicMock ( )
1288+ switch_client_mock = mocker . patch ( "libtmux.session.Session.switch_client" )
12841289 switch_client_mock .return_value .stderr = None
12851290
1286- monkeypatch .setattr ("libtmux.session.Session.switch_client" , switch_client_mock )
1287-
12881291 yaml_config = test_utils .read_config_file ("workspacebuilder/two_pane.yaml" )
12891292 sconfig = kaptan .Kaptan (handler = "yaml" )
12901293 sconfig = sconfig .import_config (yaml_config ).get ()
@@ -1293,7 +1296,7 @@ def test_load_attached_within_tmux_detached(server, monkeypatch):
12931296
12941297 _load_attached (builder , True )
12951298
1296- assert builder . session . switch_client .call_count == 1
1299+ assert switch_client_mock .call_count == 1
12971300
12981301
12991302def test_load_append_windows_to_current_session (server , monkeypatch ):
0 commit comments