From 9b955daaf7f87852f03e834bf8f783c7006ada6a Mon Sep 17 00:00:00 2001 From: Peter Pentchev Date: Tue, 24 Dec 2024 18:55:02 +0200 Subject: [PATCH] fix(server[new_session]): Fix passing both window command and environment (#553) If the `-e this=that` options are passed after a window command, tmux (correctly, IMHO) interprets them as options to be passed to the command itself, does not change the environment, and passes them through to the command, which does not usually expect to get a whole lot of `-e this=that -e other=something` options. --- src/libtmux/server.py | 6 +++--- tests/test_server.py | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/libtmux/server.py b/src/libtmux/server.py index 04e07b75c..89ea2b90b 100644 --- a/src/libtmux/server.py +++ b/src/libtmux/server.py @@ -499,9 +499,6 @@ def new_session( if y is not None: tmux_args += ("-y", y) - if window_command: - tmux_args += (window_command,) - if environment: if has_gte_version("3.2"): for k, v in environment.items(): @@ -511,6 +508,9 @@ def new_session( "Environment flag ignored, tmux 3.2 or newer required.", ) + if window_command: + tmux_args += (window_command,) + proc = self.cmd("new-session", *tmux_args) if proc.stderr: diff --git a/tests/test_server.py b/tests/test_server.py index d7020883e..de337aa77 100644 --- a/tests/test_server.py +++ b/tests/test_server.py @@ -1,7 +1,9 @@ """Test for libtmux Server object.""" import logging +import os import subprocess +import time import pytest @@ -131,6 +133,28 @@ def test_new_session_shell(server: Server) -> None: assert pane_start_command == cmd +def test_new_session_shell_env(server: Server) -> None: + """Verify ``Server.new_session`` creates valid session running w/ command.""" + cmd = "sleep 1m" + env = dict(os.environ) + mysession = server.new_session( + "test_new_session_env", window_command=cmd, environment=env + ) + time.sleep(0.1) + window = mysession.windows[0] + pane = window.panes[0] + assert mysession.session_name == "test_new_session_env" + assert server.has_session("test_new_session_env") + + pane_start_command = pane.pane_start_command + assert pane_start_command is not None + + if has_gte_version("3.2"): + assert pane_start_command.replace('"', "") == cmd + else: + assert pane_start_command == cmd + + @pytest.mark.skipif(has_version("3.2"), reason="Wrong width returned with 3.2") def test_new_session_width_height(server: Server) -> None: """Verify ``Server.new_session`` creates valid session running w/ dimensions."""