3
3
import os
4
4
import pathlib
5
5
import typing as t
6
- from unittest .mock import MagicMock
7
6
8
7
import pytest
9
8
10
9
import click
11
10
import kaptan
12
11
from click .testing import CliRunner
12
+ from pytest_mock import MockerFixture
13
13
14
14
import libtmux
15
15
from libtmux .common import has_lt_version
38
38
from .constants import FIXTURE_PATH
39
39
from .fixtures import utils as test_utils
40
40
41
+ if t .TYPE_CHECKING :
42
+ from libtmux .server import Server
43
+
41
44
42
45
def test_creates_config_dir_not_exists (tmp_path : pathlib .Path ):
43
46
"""cli.startup() creates config dir if not exists."""
@@ -1216,15 +1219,15 @@ def test_reattach_plugins(monkeypatch_plugin_test_packages, server):
1216
1219
assert proc .stdout [0 ] == "'plugin_test_r'"
1217
1220
1218
1221
1219
- def test_load_attached (server , monkeypatch ):
1222
+ def test_load_attached (
1223
+ server : "Server" , monkeypatch : pytest .MonkeyPatch , mocker : MockerFixture
1224
+ ) -> None :
1220
1225
# Load a session and attach from outside tmux
1221
1226
monkeypatch .delenv ("TMUX" , raising = False )
1222
1227
1223
- attach_session_mock = MagicMock ( )
1228
+ attach_session_mock = mocker . patch ( "libtmux.session.Session.attach_session" )
1224
1229
attach_session_mock .return_value .stderr = None
1225
1230
1226
- monkeypatch .setattr ("libtmux.session.Session.attach_session" , attach_session_mock )
1227
-
1228
1231
yaml_config = test_utils .read_config_file ("workspacebuilder/two_pane.yaml" )
1229
1232
sconfig = kaptan .Kaptan (handler = "yaml" )
1230
1233
sconfig = sconfig .import_config (yaml_config ).get ()
@@ -1233,18 +1236,18 @@ def test_load_attached(server, monkeypatch):
1233
1236
1234
1237
_load_attached (builder , False )
1235
1238
1236
- assert builder . session . attach_session .call_count == 1
1239
+ assert attach_session_mock .call_count == 1
1237
1240
1238
1241
1239
- def test_load_attached_detached (server , monkeypatch ):
1242
+ def test_load_attached_detached (
1243
+ server : "Server" , monkeypatch : pytest .MonkeyPatch , mocker : MockerFixture
1244
+ ) -> None :
1240
1245
# Load a session but don't attach
1241
1246
monkeypatch .delenv ("TMUX" , raising = False )
1242
1247
1243
- attach_session_mock = MagicMock ( )
1248
+ attach_session_mock = mocker . patch ( "libtmux.session.Session.attach_session" )
1244
1249
attach_session_mock .return_value .stderr = None
1245
1250
1246
- monkeypatch .setattr ("libtmux.session.Session.attach_session" , attach_session_mock )
1247
-
1248
1251
yaml_config = test_utils .read_config_file ("workspacebuilder/two_pane.yaml" )
1249
1252
sconfig = kaptan .Kaptan (handler = "yaml" )
1250
1253
sconfig = sconfig .import_config (yaml_config ).get ()
@@ -1253,18 +1256,18 @@ def test_load_attached_detached(server, monkeypatch):
1253
1256
1254
1257
_load_attached (builder , True )
1255
1258
1256
- assert builder . session . attach_session .call_count == 0
1259
+ assert attach_session_mock .call_count == 0
1257
1260
1258
1261
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 :
1260
1265
# Load a session and attach from within tmux
1261
1266
monkeypatch .setenv ("TMUX" , "/tmp/tmux-1234/default,123,0" )
1262
1267
1263
- switch_client_mock = MagicMock ( )
1268
+ switch_client_mock = mocker . patch ( "libtmux.session.Session.switch_client" )
1264
1269
switch_client_mock .return_value .stderr = None
1265
1270
1266
- monkeypatch .setattr ("libtmux.session.Session.switch_client" , switch_client_mock )
1267
-
1268
1271
yaml_config = test_utils .read_config_file ("workspacebuilder/two_pane.yaml" )
1269
1272
sconfig = kaptan .Kaptan (handler = "yaml" )
1270
1273
sconfig = sconfig .import_config (yaml_config ).get ()
@@ -1273,18 +1276,18 @@ def test_load_attached_within_tmux(server, monkeypatch):
1273
1276
1274
1277
_load_attached (builder , False )
1275
1278
1276
- assert builder . session . switch_client .call_count == 1
1279
+ assert switch_client_mock .call_count == 1
1277
1280
1278
1281
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 :
1280
1285
# Load a session and attach from within tmux
1281
1286
monkeypatch .setenv ("TMUX" , "/tmp/tmux-1234/default,123,0" )
1282
1287
1283
- switch_client_mock = MagicMock ( )
1288
+ switch_client_mock = mocker . patch ( "libtmux.session.Session.switch_client" )
1284
1289
switch_client_mock .return_value .stderr = None
1285
1290
1286
- monkeypatch .setattr ("libtmux.session.Session.switch_client" , switch_client_mock )
1287
-
1288
1291
yaml_config = test_utils .read_config_file ("workspacebuilder/two_pane.yaml" )
1289
1292
sconfig = kaptan .Kaptan (handler = "yaml" )
1290
1293
sconfig = sconfig .import_config (yaml_config ).get ()
@@ -1293,7 +1296,7 @@ def test_load_attached_within_tmux_detached(server, monkeypatch):
1293
1296
1294
1297
_load_attached (builder , True )
1295
1298
1296
- assert builder . session . switch_client .call_count == 1
1299
+ assert switch_client_mock .call_count == 1
1297
1300
1298
1301
1299
1302
def test_load_append_windows_to_current_session (server , monkeypatch ):
0 commit comments