|
12 | 12 |
|
13 | 13 | import libtmux |
14 | 14 | from libtmux.common import has_lt_version |
| 15 | +from libtmux.exc import LibTmuxException |
15 | 16 | from tmuxp import cli, config |
16 | 17 | from tmuxp.cli import ( |
17 | 18 | command_ls, |
@@ -406,6 +407,142 @@ def test_load_zsh_autotitle_warning(cli_args, tmpdir, monkeypatch): |
406 | 407 | assert 'Please set' not in result.output |
407 | 408 |
|
408 | 409 |
|
| 410 | +@pytest.mark.parametrize( |
| 411 | + "cli_args,inputs,env,expected_output", |
| 412 | + [ |
| 413 | + ( |
| 414 | + ['shell', '-L{SOCKET_NAME}', '-c', 'print(str(server.socket_name))'], |
| 415 | + [], |
| 416 | + {}, |
| 417 | + '{SERVER_SOCKET_NAME}', |
| 418 | + ), |
| 419 | + ( |
| 420 | + [ |
| 421 | + 'shell', |
| 422 | + '-L{SOCKET_NAME}', |
| 423 | + '{SESSION_NAME}', |
| 424 | + '-c', |
| 425 | + 'print(session.name)', |
| 426 | + ], |
| 427 | + [], |
| 428 | + {}, |
| 429 | + '{SESSION_NAME}', |
| 430 | + ), |
| 431 | + ( |
| 432 | + [ |
| 433 | + 'shell', |
| 434 | + '-L{SOCKET_NAME}', |
| 435 | + '{SESSION_NAME}', |
| 436 | + '{WINDOW_NAME}', |
| 437 | + '-c', |
| 438 | + 'print(server.has_session(session.name))', |
| 439 | + ], |
| 440 | + [], |
| 441 | + {}, |
| 442 | + 'True', |
| 443 | + ), |
| 444 | + ( |
| 445 | + [ |
| 446 | + 'shell', |
| 447 | + '-L{SOCKET_NAME}', |
| 448 | + '{SESSION_NAME}', |
| 449 | + '{WINDOW_NAME}', |
| 450 | + '-c', |
| 451 | + 'print(window.name)', |
| 452 | + ], |
| 453 | + [], |
| 454 | + {}, |
| 455 | + '{WINDOW_NAME}', |
| 456 | + ), |
| 457 | + ( |
| 458 | + [ |
| 459 | + 'shell', |
| 460 | + '-L{SOCKET_NAME}', |
| 461 | + '{SESSION_NAME}', |
| 462 | + '{WINDOW_NAME}', |
| 463 | + '-c', |
| 464 | + 'print(pane.id)', |
| 465 | + ], |
| 466 | + [], |
| 467 | + {}, |
| 468 | + '{PANE_ID}', |
| 469 | + ), |
| 470 | + ( |
| 471 | + [ |
| 472 | + 'shell', |
| 473 | + '-L{SOCKET_NAME}', |
| 474 | + '-c', |
| 475 | + 'print(pane.id)', |
| 476 | + ], |
| 477 | + [], |
| 478 | + {'TMUX_PANE': '{PANE_ID}'}, |
| 479 | + '{PANE_ID}', |
| 480 | + ), |
| 481 | + ], |
| 482 | +) |
| 483 | +def test_shell( |
| 484 | + cli_args, inputs, expected_output, env, tmpdir, monkeypatch, server, session |
| 485 | +): |
| 486 | + monkeypatch.setenv('HOME', str(tmpdir)) |
| 487 | + window_name = 'my_window' |
| 488 | + window = session.new_window(window_name=window_name) |
| 489 | + window.split_window() |
| 490 | + |
| 491 | + template_ctx = dict( |
| 492 | + SOCKET_NAME=server.socket_name, |
| 493 | + SOCKET_PATH=server.socket_path, |
| 494 | + SESSION_NAME=session.name, |
| 495 | + WINDOW_NAME=window_name, |
| 496 | + PANE_ID=window.attached_pane.id, |
| 497 | + SERVER_SOCKET_NAME=server.socket_name, |
| 498 | + ) |
| 499 | + |
| 500 | + cli_args[:] = [cli_arg.format(**template_ctx) for cli_arg in cli_args] |
| 501 | + for k, v in env.items(): |
| 502 | + monkeypatch.setenv(k, v.format(**template_ctx)) |
| 503 | + |
| 504 | + with tmpdir.as_cwd(): |
| 505 | + runner = CliRunner() |
| 506 | + |
| 507 | + result = runner.invoke( |
| 508 | + cli.cli, cli_args, input=''.join(inputs), catch_exceptions=False |
| 509 | + ) |
| 510 | + assert expected_output.format(**template_ctx) in result.output |
| 511 | + |
| 512 | + |
| 513 | +@pytest.mark.parametrize( |
| 514 | + "cli_args,inputs,env,exception, message", |
| 515 | + [ |
| 516 | + ( |
| 517 | + ['shell', '-L{SOCKET_NAME}', '-c', 'print(str(server.socket_name))'], |
| 518 | + [], |
| 519 | + {}, |
| 520 | + LibTmuxException, |
| 521 | + r'.*{SOCKET_NAME}\s\(No such file or directory\).*', |
| 522 | + ), |
| 523 | + ], |
| 524 | +) |
| 525 | +def test_shell_no_server( |
| 526 | + cli_args, inputs, env, exception, message, tmpdir, monkeypatch, socket_name |
| 527 | +): |
| 528 | + monkeypatch.setenv('HOME', str(tmpdir)) |
| 529 | + template_ctx = dict( |
| 530 | + SOCKET_NAME=socket_name, |
| 531 | + ) |
| 532 | + |
| 533 | + cli_args[:] = [cli_arg.format(**template_ctx) for cli_arg in cli_args] |
| 534 | + for k, v in env.items(): |
| 535 | + monkeypatch.setenv(k, v.format(**template_ctx)) |
| 536 | + |
| 537 | + with tmpdir.as_cwd(): |
| 538 | + runner = CliRunner() |
| 539 | + |
| 540 | + with pytest.raises(exception, match=message.format(**template_ctx)): |
| 541 | + runner.invoke( |
| 542 | + cli.cli, cli_args, input=''.join(inputs), catch_exceptions=False |
| 543 | + ) |
| 544 | + |
| 545 | + |
409 | 546 | @pytest.mark.parametrize( |
410 | 547 | "cli_args", |
411 | 548 | [ |
|
0 commit comments