-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathtest_freeze.py
149 lines (118 loc) · 4.25 KB
/
test_freeze.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"""Test workspace freezing functionality for tmuxp."""
from __future__ import annotations
import contextlib
import io
import typing as t
import pytest
from tmuxp import cli
from tmuxp._internal.config_reader import ConfigReader
if t.TYPE_CHECKING:
import pathlib
from libtmux.server import Server
class FreezeTestFixture(t.NamedTuple):
"""Test fixture for tmuxp freeze command tests."""
test_id: str
cli_args: list[str]
inputs: list[str]
class FreezeOverwriteTestFixture(t.NamedTuple):
"""Test fixture for tmuxp freeze overwrite command tests."""
test_id: str
cli_args: list[str]
inputs: list[str]
FREEZE_TEST_FIXTURES: list[FreezeTestFixture] = [
FreezeTestFixture(
test_id="freeze_named_session",
cli_args=["freeze", "myfrozensession"],
inputs=["y\n", "./la.yaml\n", "y\n"],
),
FreezeTestFixture(
test_id="freeze_named_session_exists",
cli_args=["freeze", "myfrozensession"],
inputs=["y\n", "./exists.yaml\n", "./la.yaml\n", "y\n"],
),
FreezeTestFixture(
test_id="freeze_current_session",
cli_args=["freeze"],
inputs=["y\n", "./la.yaml\n", "y\n"],
),
FreezeTestFixture(
test_id="freeze_current_session_exists",
cli_args=["freeze"],
inputs=["y\n", "./exists.yaml\n", "./la.yaml\n", "y\n"],
),
]
FREEZE_OVERWRITE_TEST_FIXTURES: list[FreezeOverwriteTestFixture] = [
FreezeOverwriteTestFixture(
test_id="force_overwrite_named_session",
cli_args=["freeze", "mysession", "--force"],
inputs=["\n", "\n", "y\n", "./exists.yaml\n", "y\n"],
),
FreezeOverwriteTestFixture(
test_id="force_overwrite_current_session",
cli_args=["freeze", "--force"],
inputs=["\n", "\n", "y\n", "./exists.yaml\n", "y\n"],
),
]
@pytest.mark.parametrize(
list(FreezeTestFixture._fields),
FREEZE_TEST_FIXTURES,
ids=[test.test_id for test in FREEZE_TEST_FIXTURES],
)
def test_freeze(
server: Server,
test_id: str,
cli_args: list[str],
inputs: list[str],
tmp_path: pathlib.Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Parametrized test for freezing a tmux session to a tmuxp config file."""
monkeypatch.setenv("HOME", str(tmp_path))
exists_yaml = tmp_path / "exists.yaml"
exists_yaml.touch()
server.new_session(session_name="myfirstsession")
server.new_session(session_name="myfrozensession")
# Assign an active pane to the session
second_session = server.sessions[1]
first_pane_on_second_session_id = second_session.windows[0].panes[0].pane_id
assert first_pane_on_second_session_id
monkeypatch.setenv("TMUX_PANE", first_pane_on_second_session_id)
monkeypatch.chdir(tmp_path)
# Use tmux server (socket name) used in the test
assert server.socket_name is not None
cli_args = [*cli_args, "-L", server.socket_name]
monkeypatch.setattr("sys.stdin", io.StringIO("".join(inputs)))
with contextlib.suppress(SystemExit):
cli.cli(cli_args)
yaml_config_path = tmp_path / "la.yaml"
assert yaml_config_path.exists()
yaml_config = yaml_config_path.open().read()
frozen_config = ConfigReader._load(fmt="yaml", content=yaml_config)
assert frozen_config["session_name"] == "myfrozensession"
@pytest.mark.parametrize(
list(FreezeOverwriteTestFixture._fields),
FREEZE_OVERWRITE_TEST_FIXTURES,
ids=[test.test_id for test in FREEZE_OVERWRITE_TEST_FIXTURES],
)
def test_freeze_overwrite(
server: Server,
test_id: str,
cli_args: list[str],
inputs: list[str],
tmp_path: pathlib.Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Test overwrite prompt when freezing a tmuxp configuration file."""
monkeypatch.setenv("HOME", str(tmp_path))
exists_yaml = tmp_path / "exists.yaml"
exists_yaml.touch()
server.new_session(session_name="mysession")
monkeypatch.chdir(tmp_path)
# Use tmux server (socket name) used in the test
assert server.socket_name is not None
cli_args = [*cli_args, "-L", server.socket_name]
monkeypatch.setattr("sys.stdin", io.StringIO("".join(inputs)))
with contextlib.suppress(SystemExit):
cli.cli(cli_args)
yaml_config_path = tmp_path / "exists.yaml"
assert yaml_config_path.exists()