Skip to content

Commit 026201e

Browse files
committed
refactor(tests): convert test_shell.py parametrize to NamedTuple fixtures
1 parent 1c35e36 commit 026201e

File tree

1 file changed

+169
-50
lines changed

1 file changed

+169
-50
lines changed

tests/cli/test_shell.py

+169-50
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,26 @@ class CLIShellFixture(t.NamedTuple):
2525
test_id: str
2626

2727
# test params
28+
cli_cmd: list[str]
2829
cli_args: list[str]
2930
inputs: list[t.Any]
3031
env: dict[str, str]
3132
expected_output: str
3233

3334

3435
TEST_SHELL_FIXTURES: list[CLIShellFixture] = [
36+
# Regular shell command
3537
CLIShellFixture(
3638
test_id="print-socket-name",
39+
cli_cmd=["shell"],
3740
cli_args=["-L{SOCKET_NAME}", "-c", "print(str(server.socket_name))"],
3841
inputs=[],
3942
env={},
4043
expected_output="{SERVER_SOCKET_NAME}",
4144
),
4245
CLIShellFixture(
4346
test_id="print-session-name",
47+
cli_cmd=["shell"],
4448
cli_args=[
4549
"-L{SOCKET_NAME}",
4650
"{SESSION_NAME}",
@@ -53,6 +57,7 @@ class CLIShellFixture(t.NamedTuple):
5357
),
5458
CLIShellFixture(
5559
test_id="print-has-session",
60+
cli_cmd=["shell"],
5661
cli_args=[
5762
"-L{SOCKET_NAME}",
5863
"{SESSION_NAME}",
@@ -66,6 +71,7 @@ class CLIShellFixture(t.NamedTuple):
6671
),
6772
CLIShellFixture(
6873
test_id="print-window-name",
74+
cli_cmd=["shell"],
6975
cli_args=[
7076
"-L{SOCKET_NAME}",
7177
"{SESSION_NAME}",
@@ -79,6 +85,7 @@ class CLIShellFixture(t.NamedTuple):
7985
),
8086
CLIShellFixture(
8187
test_id="print-pane-id",
88+
cli_cmd=["shell"],
8289
cli_args=[
8390
"-L{SOCKET_NAME}",
8491
"{SESSION_NAME}",
@@ -92,6 +99,83 @@ class CLIShellFixture(t.NamedTuple):
9299
),
93100
CLIShellFixture(
94101
test_id="print-pane-id-obeys-tmux-pane-env-var",
102+
cli_cmd=["shell"],
103+
cli_args=[
104+
"-L{SOCKET_NAME}",
105+
"-c",
106+
"print(pane.id)",
107+
],
108+
inputs=[],
109+
env={"TMUX_PANE": "{PANE_ID}"},
110+
expected_output="{PANE_ID}",
111+
),
112+
# Shell with --pdb
113+
CLIShellFixture(
114+
test_id="print-socket-name-pdb",
115+
cli_cmd=["shell", "--pdb"],
116+
cli_args=["-L{SOCKET_NAME}", "-c", "print(str(server.socket_name))"],
117+
inputs=[],
118+
env={},
119+
expected_output="{SERVER_SOCKET_NAME}",
120+
),
121+
CLIShellFixture(
122+
test_id="print-session-name-pdb",
123+
cli_cmd=["shell", "--pdb"],
124+
cli_args=[
125+
"-L{SOCKET_NAME}",
126+
"{SESSION_NAME}",
127+
"-c",
128+
"print(session.name)",
129+
],
130+
inputs=[],
131+
env={},
132+
expected_output="{SESSION_NAME}",
133+
),
134+
CLIShellFixture(
135+
test_id="print-has-session-pdb",
136+
cli_cmd=["shell", "--pdb"],
137+
cli_args=[
138+
"-L{SOCKET_NAME}",
139+
"{SESSION_NAME}",
140+
"{WINDOW_NAME}",
141+
"-c",
142+
"print(server.has_session(session.name))",
143+
],
144+
inputs=[],
145+
env={},
146+
expected_output="True",
147+
),
148+
CLIShellFixture(
149+
test_id="print-window-name-pdb",
150+
cli_cmd=["shell", "--pdb"],
151+
cli_args=[
152+
"-L{SOCKET_NAME}",
153+
"{SESSION_NAME}",
154+
"{WINDOW_NAME}",
155+
"-c",
156+
"print(window.name)",
157+
],
158+
inputs=[],
159+
env={},
160+
expected_output="{WINDOW_NAME}",
161+
),
162+
CLIShellFixture(
163+
test_id="print-pane-id-pdb",
164+
cli_cmd=["shell", "--pdb"],
165+
cli_args=[
166+
"-L{SOCKET_NAME}",
167+
"{SESSION_NAME}",
168+
"{WINDOW_NAME}",
169+
"-c",
170+
"print(pane.id)",
171+
],
172+
inputs=[],
173+
env={},
174+
expected_output="{PANE_ID}",
175+
),
176+
CLIShellFixture(
177+
test_id="print-pane-id-obeys-tmux-pane-env-var-pdb",
178+
cli_cmd=["shell", "--pdb"],
95179
cli_args=[
96180
"-L{SOCKET_NAME}",
97181
"-c",
@@ -108,6 +192,7 @@ class CLIShellTargetMissingFixture(t.NamedTuple):
108192
"""Test fixture for tmuxp shell target missing tests."""
109193

110194
test_id: str
195+
cli_cmd: list[str]
111196
cli_args: list[str]
112197
inputs: list[t.Any]
113198
env: dict[t.Any, t.Any]
@@ -117,8 +202,10 @@ class CLIShellTargetMissingFixture(t.NamedTuple):
117202

118203

119204
TEST_SHELL_TARGET_MISSING_FIXTURES: list[CLIShellTargetMissingFixture] = [
205+
# Regular shell command
120206
CLIShellTargetMissingFixture(
121207
test_id="nonexistent_socket",
208+
cli_cmd=["shell"],
122209
cli_args=["-LDoesNotExist", "-c", "print(str(server.socket_name))"],
123210
inputs=[],
124211
env={},
@@ -128,6 +215,7 @@ class CLIShellTargetMissingFixture(t.NamedTuple):
128215
),
129216
CLIShellTargetMissingFixture(
130217
test_id="nonexistent_session",
218+
cli_cmd=["shell"],
131219
cli_args=[
132220
"-L{SOCKET_NAME}",
133221
"nonexistent_session",
@@ -142,6 +230,49 @@ class CLIShellTargetMissingFixture(t.NamedTuple):
142230
),
143231
CLIShellTargetMissingFixture(
144232
test_id="nonexistent_window",
233+
cli_cmd=["shell"],
234+
cli_args=[
235+
"-L{SOCKET_NAME}",
236+
"{SESSION_NAME}",
237+
"nonexistent_window",
238+
"-c",
239+
"print(str(server.socket_name))",
240+
],
241+
inputs=[],
242+
env={},
243+
template_ctx={"window_name": "nonexistent_window"},
244+
exception=exc.TmuxpException,
245+
message="Window not found: {WINDOW_NAME}",
246+
),
247+
# Shell with --pdb
248+
CLIShellTargetMissingFixture(
249+
test_id="nonexistent_socket_pdb",
250+
cli_cmd=["shell", "--pdb"],
251+
cli_args=["-LDoesNotExist", "-c", "print(str(server.socket_name))"],
252+
inputs=[],
253+
env={},
254+
template_ctx={},
255+
exception=subprocess.CalledProcessError,
256+
message=r".*DoesNotExist.*",
257+
),
258+
CLIShellTargetMissingFixture(
259+
test_id="nonexistent_session_pdb",
260+
cli_cmd=["shell", "--pdb"],
261+
cli_args=[
262+
"-L{SOCKET_NAME}",
263+
"nonexistent_session",
264+
"-c",
265+
"print(str(server.socket_name))",
266+
],
267+
inputs=[],
268+
env={},
269+
template_ctx={"session_name": "nonexistent_session"},
270+
exception=exc.TmuxpException,
271+
message="Session not found: nonexistent_session",
272+
),
273+
CLIShellTargetMissingFixture(
274+
test_id="nonexistent_window_pdb",
275+
cli_cmd=["shell", "--pdb"],
145276
cli_args=[
146277
"-L{SOCKET_NAME}",
147278
"{SESSION_NAME}",
@@ -158,15 +289,49 @@ class CLIShellTargetMissingFixture(t.NamedTuple):
158289
]
159290

160291

161-
@pytest.mark.parametrize("cli_cmd", [["shell"], ["shell", "--pdb"]])
292+
class CLIShellInteractiveFixture(t.NamedTuple):
293+
"""Test fixture for tmuxp shell interactive tests."""
294+
295+
test_id: str
296+
cli_cmd: list[str]
297+
cli_args: list[str]
298+
inputs: list[t.Any]
299+
env: dict[str, str]
300+
message: str
301+
302+
303+
TEST_SHELL_INTERACTIVE_FIXTURES: list[CLIShellInteractiveFixture] = [
304+
CLIShellInteractiveFixture(
305+
test_id="basic_interactive",
306+
cli_cmd=["shell", "--code"],
307+
cli_args=[
308+
"-L{SOCKET_NAME}",
309+
],
310+
inputs=[],
311+
env={},
312+
message="(InteractiveConsole)",
313+
),
314+
CLIShellInteractiveFixture(
315+
test_id="interactive_with_pane_id",
316+
cli_cmd=["shell", "--code"],
317+
cli_args=[
318+
"-L{SOCKET_NAME}",
319+
],
320+
inputs=[],
321+
env={"PANE_ID": "{PANE_ID}"},
322+
message="(InteractiveConsole)",
323+
),
324+
]
325+
326+
162327
@pytest.mark.parametrize(
163328
list(CLIShellFixture._fields),
164329
TEST_SHELL_FIXTURES,
165330
ids=[test.test_id for test in TEST_SHELL_FIXTURES],
166331
)
167332
def test_shell(
168-
cli_cmd: list[str],
169333
test_id: str,
334+
cli_cmd: list[str],
170335
cli_args: list[str],
171336
inputs: list[t.Any],
172337
env: dict[str, str],
@@ -205,28 +370,20 @@ def test_shell(
205370
assert expected_output.format(**template_ctx) in result.out
206371

207372

208-
@pytest.mark.parametrize(
209-
"cli_cmd",
210-
[
211-
["shell"],
212-
["shell", "--pdb"],
213-
],
214-
)
215373
@pytest.mark.parametrize(
216374
list(CLIShellTargetMissingFixture._fields),
217375
TEST_SHELL_TARGET_MISSING_FIXTURES,
218376
ids=[test.test_id for test in TEST_SHELL_TARGET_MISSING_FIXTURES],
219377
)
220378
def test_shell_target_missing(
221-
cli_cmd: list[str],
222379
test_id: str,
380+
cli_cmd: list[str],
223381
cli_args: list[str],
224382
inputs: list[t.Any],
225383
env: dict[t.Any, t.Any],
226384
template_ctx: dict[str, str],
227385
exception: type[exc.TmuxpException | subprocess.CalledProcessError],
228386
message: str,
229-
socket_name: str,
230387
server: Server,
231388
session: Session,
232389
tmp_path: pathlib.Path,
@@ -265,52 +422,14 @@ def test_shell_target_missing(
265422
assert message.format(**template_ctx) in result.out
266423

267424

268-
class CLIShellInteractiveFixture(t.NamedTuple):
269-
"""Test fixture for tmuxp shell interactive tests."""
270-
271-
test_id: str
272-
cli_args: list[str]
273-
inputs: list[t.Any]
274-
env: dict[str, str]
275-
message: str
276-
277-
278-
TEST_SHELL_INTERACTIVE_FIXTURES: list[CLIShellInteractiveFixture] = [
279-
CLIShellInteractiveFixture(
280-
test_id="basic_interactive",
281-
cli_args=[
282-
"-L{SOCKET_NAME}",
283-
],
284-
inputs=[],
285-
env={},
286-
message="(InteractiveConsole)",
287-
),
288-
CLIShellInteractiveFixture(
289-
test_id="interactive_with_pane_id",
290-
cli_args=[
291-
"-L{SOCKET_NAME}",
292-
],
293-
inputs=[],
294-
env={"PANE_ID": "{PANE_ID}"},
295-
message="(InteractiveConsole)",
296-
),
297-
]
298-
299-
300-
@pytest.mark.parametrize(
301-
"cli_cmd",
302-
[
303-
["shell", "--code"],
304-
],
305-
)
306425
@pytest.mark.parametrize(
307426
list(CLIShellInteractiveFixture._fields),
308427
TEST_SHELL_INTERACTIVE_FIXTURES,
309428
ids=[test.test_id for test in TEST_SHELL_INTERACTIVE_FIXTURES],
310429
)
311430
def test_shell_interactive(
312-
cli_cmd: list[str],
313431
test_id: str,
432+
cli_cmd: list[str],
314433
cli_args: list[str],
315434
inputs: list[t.Any],
316435
env: dict[str, str],

0 commit comments

Comments
 (0)