Skip to content

Commit 8050d11

Browse files
authored
tests(plugins): Use pytest monkeypatch to append patchs (#666)
Fixes #658
2 parents 845c6db + f10676a commit 8050d11

File tree

5 files changed

+50
-26
lines changed

5 files changed

+50
-26
lines changed

pyproject.toml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,6 @@ pytest-mock = [
6969
{version="<3.0.0", python="<3"},
7070
{version="*", python=">=3"}
7171
]
72-
tmuxp-test-plugin-bwb = { path = "tests/fixtures/pluginsystem/plugins/tmuxp_test_plugin_bwb/"}
73-
tmuxp-test-plugin-bs = { path = "tests/fixtures/pluginsystem/plugins/tmuxp_test_plugin_bs/"}
74-
tmuxp-test-plugin-r = { path = "tests/fixtures/pluginsystem/plugins/tmuxp_test_plugin_r/"}
75-
tmuxp-test-plugin-owc = { path = "tests/fixtures/pluginsystem/plugins/tmuxp_test_plugin_owc/"}
76-
tmuxp-test-plugin-awf = { path = "tests/fixtures/pluginsystem/plugins/tmuxp_test_plugin_awf/"}
77-
tmuxp-test-plugin-fail = { path = "tests/fixtures/pluginsystem/plugins/tmuxp_test_plugin_fail/"}
7872

7973
### Coverage ###
8074
codecov = "*"

tests/conftest.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
2-
32
import logging
3+
import os
44

55
import pytest
66

@@ -11,6 +11,20 @@
1111
logger = logging.getLogger(__name__)
1212

1313

14+
@pytest.fixture(scope='function')
15+
def monkeypatch_plugin_test_packages(monkeypatch):
16+
paths = [
17+
"tests/fixtures/pluginsystem/plugins/tmuxp_test_plugin_bwb/",
18+
"tests/fixtures/pluginsystem/plugins/tmuxp_test_plugin_bs/",
19+
"tests/fixtures/pluginsystem/plugins/tmuxp_test_plugin_r/",
20+
"tests/fixtures/pluginsystem/plugins/tmuxp_test_plugin_owc/",
21+
"tests/fixtures/pluginsystem/plugins/tmuxp_test_plugin_awf/",
22+
"tests/fixtures/pluginsystem/plugins/tmuxp_test_plugin_fail/",
23+
]
24+
for path in paths:
25+
monkeypatch.syspath_prepend(os.path.abspath(os.path.relpath(path)))
26+
27+
1428
@pytest.fixture(scope='function')
1529
def socket_name(request):
1630
return 'tmuxp_test%s' % next(namer)

tests/test_cli.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,15 @@
1515
import click
1616
import kaptan
1717
from click.testing import CliRunner
18-
from tmuxp_test_plugin_bwb.plugin import PluginBeforeWorkspaceBuilder
1918

2019
import libtmux
2120
from libtmux.common import has_lt_version
2221
from libtmux.exc import LibTmuxException
2322
from tmuxp import cli, config, exc
2423
from tmuxp.cli import (
25-
_reattach,
26-
_load_attached,
2724
_load_append_windows_to_current_session,
25+
_load_attached,
26+
_reattach,
2827
command_debug_info,
2928
command_ls,
3029
get_config_dir,
@@ -995,7 +994,9 @@ def test_ls_cli(monkeypatch, tmpdir):
995994
assert cli_output == '\n'.join(stems) + '\n'
996995

997996

998-
def test_load_plugins():
997+
def test_load_plugins(monkeypatch_plugin_test_packages):
998+
from tmuxp_test_plugin_bwb.plugin import PluginBeforeWorkspaceBuilder
999+
9991000
plugins_config = loadfixture("workspacebuilder/plugin_bwb.yaml")
10001001

10011002
sconfig = kaptan.Kaptan(handler='yaml')
@@ -1023,7 +1024,9 @@ def test_load_plugins():
10231024
)
10241025
],
10251026
)
1026-
def test_load_plugins_version_fail_skip(cli_args, inputs):
1027+
def test_load_plugins_version_fail_skip(
1028+
monkeypatch_plugin_test_packages, cli_args, inputs
1029+
):
10271030
runner = CliRunner()
10281031

10291032
results = runner.invoke(cli.cli, cli_args, input=''.join(inputs))
@@ -1039,7 +1042,9 @@ def test_load_plugins_version_fail_skip(cli_args, inputs):
10391042
)
10401043
],
10411044
)
1042-
def test_load_plugins_version_fail_no_skip(cli_args, inputs):
1045+
def test_load_plugins_version_fail_no_skip(
1046+
monkeypatch_plugin_test_packages, cli_args, inputs
1047+
):
10431048
runner = CliRunner()
10441049

10451050
results = runner.invoke(cli.cli, cli_args, input=''.join(inputs))
@@ -1049,14 +1054,16 @@ def test_load_plugins_version_fail_no_skip(cli_args, inputs):
10491054
@pytest.mark.parametrize(
10501055
"cli_args", [(['load', 'tests/fixtures/workspacebuilder/plugin_missing_fail.yaml'])]
10511056
)
1052-
def test_load_plugins_plugin_missing(cli_args):
1057+
def test_load_plugins_plugin_missing(monkeypatch_plugin_test_packages, cli_args):
10531058
runner = CliRunner()
10541059

10551060
results = runner.invoke(cli.cli, cli_args)
10561061
assert '[Plugin Error]' in results.output
10571062

10581063

1059-
def test_plugin_system_before_script(server, monkeypatch):
1064+
def test_plugin_system_before_script(
1065+
monkeypatch_plugin_test_packages, server, monkeypatch
1066+
):
10601067
# this is an implementation test. Since this testsuite may be ran within
10611068
# a tmux session by the developer himself, delete the TMUX variable
10621069
# temporarily.
@@ -1072,7 +1079,7 @@ def test_plugin_system_before_script(server, monkeypatch):
10721079
assert session.name == 'plugin_test_bs'
10731080

10741081

1075-
def test_reattach_plugins(server):
1082+
def test_reattach_plugins(monkeypatch_plugin_test_packages, server):
10761083
config_plugins = loadfixture("workspacebuilder/plugin_r.yaml")
10771084

10781085
sconfig = kaptan.Kaptan(handler='yaml')
@@ -1174,6 +1181,7 @@ def test_load_attached_within_tmux_detached(server, monkeypatch):
11741181

11751182
assert builder.session.switch_client.call_count == 1
11761183

1184+
11771185
def test_load_append_windows_to_current_session(server, monkeypatch):
11781186
yaml_config = loadfixture("workspacebuilder/two_pane.yaml")
11791187
sconfig = kaptan.Kaptan(handler='yaml')
@@ -1195,7 +1203,6 @@ def test_load_append_windows_to_current_session(server, monkeypatch):
11951203
assert len(server._list_windows()) == 6
11961204

11971205

1198-
11991206
def test_debug_info_cli(monkeypatch, tmpdir):
12001207
monkeypatch.setenv('SHELL', '/bin/bash')
12011208

tests/test_plugin.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
)
2525

2626

27+
@pytest.fixture(autouse=True)
28+
def autopatch_sitedir(monkeypatch_plugin_test_packages):
29+
pass
30+
31+
2732
def test_all_pass():
2833
AllVersionPassPlugin()
2934

tests/test_workspacebuilder.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
from libtmux.test import retry, temp_session
1515
from tmuxp import config, exc
1616
from tmuxp._compat import text_type
17-
from tmuxp.workspacebuilder import WorkspaceBuilder
1817
from tmuxp.cli import load_plugins
18+
from tmuxp.workspacebuilder import WorkspaceBuilder
1919

2020
from . import example_dir, fixtures_dir
2121
from .fixtures._util import loadfixture
@@ -676,7 +676,9 @@ def test_before_load_true_if_test_passes_with_args(server):
676676
builder.build(session=session)
677677

678678

679-
def test_plugin_system_before_workspace_builder(session):
679+
def test_plugin_system_before_workspace_builder(
680+
monkeypatch_plugin_test_packages, session
681+
):
680682
config_plugins = loadfixture("workspacebuilder/plugin_bwb.yaml")
681683

682684
sconfig = kaptan.Kaptan(handler='yaml')
@@ -692,7 +694,7 @@ def test_plugin_system_before_workspace_builder(session):
692694
assert proc.stdout[0] == "'plugin_test_bwb'"
693695

694696

695-
def test_plugin_system_on_window_create(session):
697+
def test_plugin_system_on_window_create(monkeypatch_plugin_test_packages, session):
696698
config_plugins = loadfixture("workspacebuilder/plugin_owc.yaml")
697699

698700
sconfig = kaptan.Kaptan(handler='yaml')
@@ -708,7 +710,7 @@ def test_plugin_system_on_window_create(session):
708710
assert proc.stdout[0] == "'plugin_test_owc'"
709711

710712

711-
def test_plugin_system_after_window_finished(session):
713+
def test_plugin_system_after_window_finished(monkeypatch_plugin_test_packages, session):
712714
config_plugins = loadfixture("workspacebuilder/plugin_awf.yaml")
713715

714716
sconfig = kaptan.Kaptan(handler='yaml')
@@ -741,7 +743,9 @@ def test_plugin_system_on_window_create_multiple_windows(session):
741743
assert "'plugin_test_owc_mw_2'" in proc.stdout
742744

743745

744-
def test_plugin_system_after_window_finished_multiple_windows(session):
746+
def test_plugin_system_after_window_finished_multiple_windows(
747+
monkeypatch_plugin_test_packages, session
748+
):
745749
config_plugins = loadfixture("workspacebuilder/plugin_awf_multiple_windows.yaml")
746750

747751
sconfig = kaptan.Kaptan(handler='yaml')
@@ -758,7 +762,7 @@ def test_plugin_system_after_window_finished_multiple_windows(session):
758762
assert "'plugin_test_awf_mw_2'" in proc.stdout
759763

760764

761-
def test_plugin_system_multiple_plugins(session):
765+
def test_plugin_system_multiple_plugins(monkeypatch_plugin_test_packages, session):
762766
config_plugins = loadfixture("workspacebuilder/plugin_multiple_plugins.yaml")
763767

764768
sconfig = kaptan.Kaptan(handler='yaml')
@@ -858,9 +862,9 @@ def test_find_current_active_pane(server, monkeypatch):
858862

859863
# Assign an active pane to the session
860864
second_session = server.list_sessions()[1]
861-
first_pane_on_second_session_id = (
862-
second_session.list_windows()[0].list_panes()[0]["pane_id"]
863-
)
865+
first_pane_on_second_session_id = second_session.list_windows()[0].list_panes()[0][
866+
"pane_id"
867+
]
864868
monkeypatch.setenv("TMUX_PANE", first_pane_on_second_session_id)
865869

866870
builder = WorkspaceBuilder(sconf=sconfig, server=server)

0 commit comments

Comments
 (0)