Skip to content

Commit 3a1689c

Browse files
committed
tmuxp shell: Catch when servers not created
1 parent eaf2e30 commit 3a1689c

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

tests/test_cli.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import libtmux
1414
from libtmux.common import has_lt_version
15+
from libtmux.exc import LibTmuxException
1516
from tmuxp import cli, config
1617
from tmuxp.cli import (
1718
command_ls,
@@ -509,6 +510,39 @@ def test_shell(
509510
assert expected_output.format(**template_ctx) in result.output
510511

511512

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+
512546
@pytest.mark.parametrize(
513547
"cli_args",
514548
[

tmuxp/cli.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from click.exceptions import FileError
1717

1818
from libtmux.common import has_gte_version, has_minimum_version, which
19-
from libtmux.exc import TmuxCommandNotFound
19+
from libtmux.exc import LibTmuxException, TmuxCommandNotFound
2020
from libtmux.server import Server
2121

2222
from . import config, exc, log, util
@@ -682,6 +682,17 @@ def command_shell(session_name, window_name, socket_name, socket_path, command):
682682
"""
683683
server = Server(socket_name=socket_name, socket_path=socket_path)
684684

685+
try:
686+
server.sessions
687+
except LibTmuxException as e:
688+
if 'No such file or directory' in str(e):
689+
raise LibTmuxException(
690+
'no tmux session found. Start a tmux session and try again. \n'
691+
'Original error: ' + str(e)
692+
)
693+
else:
694+
raise e
695+
685696
current_pane = None
686697
if os.getenv('TMUX_PANE') is not None:
687698
try:

0 commit comments

Comments
 (0)