Skip to content

Add tmuxp shell_plus (credits to django-extensions, license MIT) #638

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Here you can find the recent changes to tmuxp
current
-------
- *Insert changes/features/fixes for next release here*
- :issue:`636` New command: ``tmuxp shell``
- :issue:`636` New command: ``tmuxp shell`` and :issue:`638` ``tmuxp shell_plus``

Automatically preloads session, window, and pane via `libtmux`_
api objects and makes them available in a python console.
Expand Down Expand Up @@ -41,6 +41,8 @@ current
$ tmuxp shell my_server my_window -c 'print(window.name.upper())'
MY_WINDOW
``tmuxp shell_plus`` has autocompletion

tmuxp 1.5.8 (2020-10-31)
------------------------
- :issue:`639` Passes start_directory through to new tmux session
Expand Down
154 changes: 137 additions & 17 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,18 +407,18 @@ def test_load_zsh_autotitle_warning(cli_args, tmpdir, monkeypatch):
assert 'Please set' not in result.output


@pytest.mark.parametrize("cli_cmd", ['shell', 'shell_plus'])
@pytest.mark.parametrize(
"cli_args,inputs,env,expected_output",
[
(
['shell', '-L{SOCKET_NAME}', '-c', 'print(str(server.socket_name))'],
['-L{SOCKET_NAME}', '-c', 'print(str(server.socket_name))'],
[],
{},
'{SERVER_SOCKET_NAME}',
),
(
[
'shell',
'-L{SOCKET_NAME}',
'{SESSION_NAME}',
'-c',
Expand All @@ -430,7 +430,6 @@ def test_load_zsh_autotitle_warning(cli_args, tmpdir, monkeypatch):
),
(
[
'shell',
'-L{SOCKET_NAME}',
'{SESSION_NAME}',
'{WINDOW_NAME}',
Expand All @@ -443,7 +442,6 @@ def test_load_zsh_autotitle_warning(cli_args, tmpdir, monkeypatch):
),
(
[
'shell',
'-L{SOCKET_NAME}',
'{SESSION_NAME}',
'{WINDOW_NAME}',
Expand All @@ -456,7 +454,6 @@ def test_load_zsh_autotitle_warning(cli_args, tmpdir, monkeypatch):
),
(
[
'shell',
'-L{SOCKET_NAME}',
'{SESSION_NAME}',
'{WINDOW_NAME}',
Expand All @@ -469,7 +466,6 @@ def test_load_zsh_autotitle_warning(cli_args, tmpdir, monkeypatch):
),
(
[
'shell',
'-L{SOCKET_NAME}',
'-c',
'print(pane.id)',
Expand All @@ -481,7 +477,15 @@ def test_load_zsh_autotitle_warning(cli_args, tmpdir, monkeypatch):
],
)
def test_shell(
cli_args, inputs, expected_output, env, tmpdir, monkeypatch, server, session
cli_cmd,
cli_args,
inputs,
expected_output,
env,
tmpdir,
monkeypatch,
server,
session,
):
monkeypatch.setenv('HOME', str(tmpdir))
window_name = 'my_window'
Expand All @@ -497,7 +501,8 @@ def test_shell(
SERVER_SOCKET_NAME=server.socket_name,
)

cli_args[:] = [cli_arg.format(**template_ctx) for cli_arg in cli_args]
cli_args = [cli_cmd] + [cli_arg.format(**template_ctx) for cli_arg in cli_args]

for k, v in env.items():
monkeypatch.setenv(k, v.format(**template_ctx))

Expand All @@ -510,37 +515,152 @@ def test_shell(
assert expected_output.format(**template_ctx) in result.output


@pytest.mark.parametrize("cli_cmd", ['shell', 'shell_plus'])
@pytest.mark.parametrize(
"cli_args,inputs,env,exception, message",
"cli_args,inputs,env,template_ctx,exception,message",
[
(
['shell', '-L{SOCKET_NAME}', '-c', 'print(str(server.socket_name))'],
['-LDoesNotExist', '-c', 'print(str(server.socket_name))'],
[],
{},
{},
LibTmuxException,
r'.*{SOCKET_NAME}\s\(No such file or directory\).*',
r'.*DoesNotExist\s\(No such file or directory\).*',
),
(
[
'-L{SOCKET_NAME}',
'nonexistant_session',
'-c',
'print(str(server.socket_name))',
],
[],
{},
{'session_name': 'nonexistant_session'},
None,
'Session not found: nonexistant_session',
),
(
[
'-L{SOCKET_NAME}',
'{SESSION_NAME}',
'nonexistant_window',
'-c',
'print(str(server.socket_name))',
],
[],
{},
{'window_name': 'nonexistant_window'},
None,
'Window not found: {WINDOW_NAME}',
),
],
)
def test_shell_no_server(
cli_args, inputs, env, exception, message, tmpdir, monkeypatch, socket_name
def test_shell_target_missing(
cli_cmd,
cli_args,
inputs,
env,
template_ctx,
exception,
message,
tmpdir,
monkeypatch,
socket_name,
server,
session,
):
monkeypatch.setenv('HOME', str(tmpdir))
window_name = 'my_window'
window = session.new_window(window_name=window_name)
window.split_window()

template_ctx = dict(
SOCKET_NAME=socket_name,
SOCKET_NAME=server.socket_name,
SOCKET_PATH=server.socket_path,
SESSION_NAME=session.name,
WINDOW_NAME=template_ctx.get('window_name', window_name),
PANE_ID=template_ctx.get('pane_id'),
SERVER_SOCKET_NAME=server.socket_name,
)
cli_args = [cli_cmd] + [cli_arg.format(**template_ctx) for cli_arg in cli_args]

cli_args[:] = [cli_arg.format(**template_ctx) for cli_arg in cli_args]
for k, v in env.items():
monkeypatch.setenv(k, v.format(**template_ctx))

with tmpdir.as_cwd():
runner = CliRunner()

with pytest.raises(exception, match=message.format(**template_ctx)):
runner.invoke(
if exception is not None:
with pytest.raises(exception, match=message.format(**template_ctx)):
result = runner.invoke(
cli.cli, cli_args, input=''.join(inputs), catch_exceptions=False
)
else:
result = runner.invoke(
cli.cli, cli_args, input=''.join(inputs), catch_exceptions=False
)
assert message.format(**template_ctx) in result.output


@pytest.mark.parametrize(
"cli_args,inputs,env,message",
[
(
[
'shell_plus',
'-L{SOCKET_NAME}',
],
[],
{},
'(InteractiveConsole)',
),
(
[
'shell_plus',
'-L{SOCKET_NAME}',
],
[],
{'PANE_ID': '{PANE_ID}'},
'(InteractiveConsole)',
),
],
)
def test_shell_plus(
cli_args,
inputs,
env,
message,
tmpdir,
monkeypatch,
server,
session,
):
monkeypatch.setenv('HOME', str(tmpdir))
window_name = 'my_window'
window = session.new_window(window_name=window_name)
window.split_window()

template_ctx = dict(
SOCKET_NAME=server.socket_name,
SOCKET_PATH=server.socket_path,
SESSION_NAME=session.name,
WINDOW_NAME=window_name,
PANE_ID=window.attached_pane.id,
SERVER_SOCKET_NAME=server.socket_name,
)

cli_args[:] = [cli_arg.format(**template_ctx) for cli_arg in cli_args]
for k, v in env.items():
monkeypatch.setenv(k, v.format(**template_ctx))

with tmpdir.as_cwd():
runner = CliRunner()

result = runner.invoke(
cli.cli, cli_args, input=''.join(inputs), catch_exceptions=True
)
assert message.format(**template_ctx) in result.output


@pytest.mark.parametrize(
Expand Down
Loading