Skip to content

Commit bfc0e07

Browse files
committed
get_session should return attached local session by default
1 parent 199c32e commit bfc0e07

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

tests/test_util.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from tmuxp import exc
77
from tmuxp.exc import BeforeLoadScriptError, BeforeLoadScriptNotExists
8-
from tmuxp.util import run_before_script
8+
from tmuxp.util import run_before_script, get_session
99

1010
from . import fixtures_dir
1111

@@ -49,3 +49,23 @@ def test_beforeload_returns_stderr_messages():
4949
with pytest.raises(exc.BeforeLoadScriptError) as excinfo:
5050
run_before_script(script_file)
5151
assert excinfo.match(r'failed with returncode')
52+
53+
54+
def test_get_session_should_default_to_local_attached_session(server, monkeypatch):
55+
server.new_session(session_name='myfirstsession')
56+
second_session = server.new_session(session_name='mysecondsession')
57+
58+
# Assign an active pane to the session
59+
first_pane_on_second_session_id = second_session.list_windows()[0].list_panes()[0][
60+
'pane_id'
61+
]
62+
monkeypatch.setenv('TMUX_PANE', first_pane_on_second_session_id)
63+
64+
assert get_session(server) == second_session
65+
66+
67+
def test_get_session_should_raise_exception_if_no_session(server):
68+
first_session = server.new_session(session_name='myfirstsession')
69+
server.new_session(session_name='mysecondsession')
70+
71+
assert get_session(server) == first_session

tmuxp/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ def command_freeze(session_name, socket_name, socket_path, force):
924924
if session_name:
925925
session = t.find_where({'session_name': session_name})
926926
else:
927-
session = t.list_sessions()[0]
927+
session = util.get_session(t)
928928

929929
if not session:
930930
raise exc.TmuxpException('Session not found.')

tmuxp/util.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,17 @@ def get_session(server, session_name=None, current_pane=None):
111111
elif current_pane is not None:
112112
session = server.find_where({'session_id': current_pane['session_id']})
113113
else:
114-
session = server.list_sessions()[0]
114+
current_pane = get_current_pane(server)
115+
if current_pane:
116+
session = server.find_where({'session_id': current_pane['session_id']})
117+
else:
118+
session = server.list_sessions()[0]
115119

116120
if not session:
117-
raise exc.TmuxpException('Session not found: %s' % session_name)
121+
if session_name:
122+
raise exc.TmuxpException('Session not found: %s' % session_name)
123+
else:
124+
raise exc.TmuxpException('Session not found')
118125

119126
return session
120127

0 commit comments

Comments
 (0)