-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathtest_load_parallel.py
More file actions
322 lines (279 loc) · 9.71 KB
/
Copy pathtest_load_parallel.py
File metadata and controls
322 lines (279 loc) · 9.71 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
"""Tests for ``tmuxp load --parallel`` (engine-ops workspace set)."""
from __future__ import annotations
import contextlib
import typing as t
import pytest
from tmuxp import cli
from tmuxp.cli.load import _parse_expand_specs
if t.TYPE_CHECKING:
import pathlib
from libtmux.server import Server
def _kill(server: Server, *session_names: str) -> None:
"""Kill each named session if present, leaving the server clean."""
for name in session_names:
with contextlib.suppress(Exception):
sess = server.sessions.get(session_name=name, default=None)
if sess is not None:
sess.kill()
def _write_workspace(
path: pathlib.Path,
session_name: str,
panes: list[str],
) -> None:
"""Write a minimal single-window workspace file with *panes*."""
pane_list = ", ".join(panes)
path.write_text(
f"session_name: {session_name}\nwindows:\n - panes: [{pane_list}]\n",
encoding="utf-8",
)
class _ParallelCase(t.NamedTuple):
"""A batch of workspaces and the pane count each should build."""
test_id: str
# (session_name, pane commands) per workspace file, in declaration order.
sessions: tuple[tuple[str, list[str]], ...]
_PARALLEL_CASES: tuple[_ParallelCase, ...] = (
_ParallelCase(
test_id="single_file_set",
sessions=(("ppar-solo", ["echo a", "echo b"]),),
),
_ParallelCase(
test_id="two_file_set",
sessions=(
("ppar-a", ["echo a1", "echo a2"]),
("ppar-b", ["echo b1"]),
),
),
)
@pytest.mark.parametrize(
"case",
_PARALLEL_CASES,
ids=[c.test_id for c in _PARALLEL_CASES],
)
def test_parallel_builds_all_sessions(
case: _ParallelCase,
server: Server,
tmp_path: pathlib.Path,
) -> None:
"""``--parallel`` builds every file's session as one folded set."""
assert server.socket_name is not None
files: list[str] = []
names: list[str] = []
for idx, (session_name, panes) in enumerate(case.sessions):
cfg = tmp_path / f"ws{idx}.yaml"
_write_workspace(cfg, session_name, panes)
files.append(str(cfg))
names.append(session_name)
try:
with contextlib.suppress(SystemExit):
cli.cli(
["load", *files, "--parallel", "-d", "-L", server.socket_name],
)
for session_name, panes in case.sessions:
built = server.sessions.get(session_name=session_name, default=None)
assert built is not None
assert len(built.windows[0].panes) == len(panes)
finally:
_kill(server, *names)
def test_parallel_dry_run_does_not_build(
server: Server,
tmp_path: pathlib.Path,
capsys: pytest.CaptureFixture[str],
) -> None:
"""``--dry-run`` prints the compiled plan and touches no tmux server."""
assert server.socket_name is not None
cfg = tmp_path / "ws.yaml"
_write_workspace(cfg, "ppar-dry", ["echo a", "echo b"])
with contextlib.suppress(SystemExit):
cli.cli(
["load", str(cfg), "--parallel", "--dry-run", "-L", server.socket_name],
)
out = capsys.readouterr().out
assert "[Dry run]" in out
assert "new-session" in out
assert server.sessions.get(session_name="ppar-dry", default=None) is None
def test_parallel_rejects_duplicate_names(
server: Server,
tmp_path: pathlib.Path,
capsys: pytest.CaptureFixture[str],
) -> None:
"""Two workspaces with the same session name are unbuildable."""
assert server.socket_name is not None
cfg = tmp_path / "ws.yaml"
_write_workspace(cfg, "ppar-dup", ["echo a"])
try:
with contextlib.suppress(SystemExit):
cli.cli(
[
"load",
str(cfg),
str(cfg),
"--parallel",
"-d",
"-L",
server.socket_name,
],
)
out = capsys.readouterr().out
assert "duplicate sessions" in out
assert server.sessions.get(session_name="ppar-dup", default=None) is None
finally:
_kill(server, "ppar-dup")
@pytest.mark.parametrize("flag", ["--dry-run", "--no-fold"])
def test_dry_run_and_no_fold_require_parallel(
flag: str,
server: Server,
tmp_path: pathlib.Path,
capsys: pytest.CaptureFixture[str],
) -> None:
"""``--dry-run``/``--no-fold`` without ``--parallel`` fail instead of building."""
assert server.socket_name is not None
cfg = tmp_path / "ws.yaml"
_write_workspace(cfg, "ppar-guard", ["echo a"])
try:
with contextlib.suppress(SystemExit):
cli.cli(["load", str(cfg), flag, "-d", "-L", server.socket_name])
out = capsys.readouterr().out
assert "require --parallel" in out
assert server.sessions.get(session_name="ppar-guard", default=None) is None
finally:
_kill(server, "ppar-guard")
class _RejectCase(t.NamedTuple):
"""An incompatible flag and the phrase its rejection must mention."""
test_id: str
extra_args: list[str]
needle: str
_REJECT_CASES: tuple[_RejectCase, ...] = (
_RejectCase(test_id="append", extra_args=["-a"], needle="--append"),
_RejectCase(test_id="new_session_name", extra_args=["-s", "renamed"], needle="-s"),
)
@pytest.mark.parametrize(
"case",
_REJECT_CASES,
ids=[c.test_id for c in _REJECT_CASES],
)
def test_parallel_rejects_incompatible_flags(
case: _RejectCase,
server: Server,
tmp_path: pathlib.Path,
capsys: pytest.CaptureFixture[str],
) -> None:
"""``--parallel`` refuses single-session flags before touching tmux."""
assert server.socket_name is not None
cfg = tmp_path / "ws.yaml"
_write_workspace(cfg, "ppar-reject", ["echo a"])
try:
with contextlib.suppress(SystemExit):
cli.cli(
[
"load",
str(cfg),
"--parallel",
"-d",
"-L",
server.socket_name,
*case.extra_args,
],
)
out = capsys.readouterr().out
assert case.needle in out
assert server.sessions.get(session_name="ppar-reject", default=None) is None
finally:
_kill(server, "ppar-reject")
def test_parse_expand_specs_matrix() -> None:
"""Multiple --expand axes produce the Cartesian product of variants."""
variants = _parse_expand_specs(["env=dev,prod", "region=us,eu"])
assert variants == [
{"env": "dev", "region": "us"},
{"env": "dev", "region": "eu"},
{"env": "prod", "region": "us"},
{"env": "prod", "region": "eu"},
]
class _SpecErrorCase(t.NamedTuple):
"""A malformed --expand spec and the phrase its error must mention."""
test_id: str
specs: list[str]
needle: str
_SPEC_ERROR_CASES: tuple[_SpecErrorCase, ...] = (
_SpecErrorCase(
test_id="no_equals", specs=["noequals"], needle="expected KEY=V1,V2"
),
_SpecErrorCase(test_id="empty_key", specs=["=v1,v2"], needle="expected KEY=V1,V2"),
_SpecErrorCase(test_id="no_values", specs=["env="], needle="no values"),
_SpecErrorCase(test_id="blank_values", specs=["env=,,"], needle="no values"),
)
@pytest.mark.parametrize(
"case",
_SPEC_ERROR_CASES,
ids=[c.test_id for c in _SPEC_ERROR_CASES],
)
def test_parse_expand_specs_rejects_malformed(case: _SpecErrorCase) -> None:
"""Malformed --expand specs raise ValueError with a helpful message."""
with pytest.raises(ValueError, match=case.needle):
_parse_expand_specs(case.specs)
def test_expand_matrix_builds_sessions(
server: Server,
tmp_path: pathlib.Path,
) -> None:
"""``--expand`` renders one session per matrix point, substituting $vars."""
assert server.socket_name is not None
cfg = tmp_path / "svc.yaml"
cfg.write_text(
"session_name: exp-$env-$region\n"
"windows:\n"
" - window_name: $env\n"
" panes: [echo $env $region]\n",
encoding="utf-8",
)
names = ["exp-dev-us", "exp-dev-eu", "exp-prod-us", "exp-prod-eu"]
try:
with contextlib.suppress(SystemExit):
# No --parallel: --expand implies the folded set build.
cli.cli(
[
"load",
str(cfg),
"--expand",
"env=dev,prod",
"--expand",
"region=us,eu",
"-d",
"-L",
server.socket_name,
],
)
built = sorted(
name
for s in server.sessions
if (name := s.name) is not None and name.startswith("exp-")
)
assert built == sorted(names)
dev_us = server.sessions.get(session_name="exp-dev-us", default=None)
assert dev_us is not None
# Substitution reaches nested fields, not just the session name.
assert dev_us.windows[0].window_name == "dev"
finally:
_kill(server, *names)
def test_expand_rejects_multiple_files(
server: Server,
tmp_path: pathlib.Path,
capsys: pytest.CaptureFixture[str],
) -> None:
"""``--expand`` expands a single base file, so extra files are rejected."""
assert server.socket_name is not None
cfg = tmp_path / "a.yaml"
_write_workspace(cfg, "exp-multi-$env", ["echo a"])
with contextlib.suppress(SystemExit):
cli.cli(
[
"load",
str(cfg),
str(cfg),
"--expand",
"env=dev",
"-d",
"-L",
server.socket_name,
],
)
out = capsys.readouterr().out
assert "exactly one workspace file" in out