Skip to content

Commit b4d9f23

Browse files
committed
fixup! feat(Session.new_window): set up environment
1 parent 6c0457f commit b4d9f23

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

src/libtmux/session.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
TmuxRelationalObject,
2020
WindowDict,
2121
handle_option_error,
22+
has_gte_version,
2223
has_version,
2324
session_check_name,
2425
)
@@ -261,8 +262,13 @@ def new_window(
261262
)
262263

263264
if environment:
264-
for k, v in environment.items():
265-
window_args += ("-e%s=%s" % (k, v),)
265+
if has_gte_version("3.0"):
266+
for k, v in environment.items():
267+
window_args += ("-e%s=%s" % (k, v),)
268+
else:
269+
logger.warning(
270+
"Cannot set up environment as tmux 3.0 or newer is required."
271+
)
266272

267273
if window_shell:
268274
window_args += (window_shell,)

tests/test_session.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import pytest
77

88
from libtmux import exc
9-
from libtmux.common import has_gte_version
9+
from libtmux.common import has_gte_version, has_lt_version
1010
from libtmux.pane import Pane
1111
from libtmux.server import Server
1212
from libtmux.session import Session
@@ -260,6 +260,10 @@ def test_cmd_inserts_sesion_id(session: Session) -> None:
260260
assert cmd.cmd[-1] == last_arg
261261

262262

263+
@pytest.mark.skipif(
264+
has_lt_version("3.0"),
265+
reason="needs -e flag for new-window which was introduced in 3.0",
266+
)
263267
def test_new_window_with_environment(session: Session) -> None:
264268
env = shutil.which("env")
265269
assert env is not None, "Cannot find usable `env` in PATH."
@@ -274,3 +278,26 @@ def test_new_window_with_environment(session: Session) -> None:
274278
assert pane is not None
275279
pane.send_keys("echo $ENV_VAR")
276280
assert pane.capture_pane() == ["$ echo $ENV_VAR", "window", "$"]
281+
282+
283+
@pytest.mark.skipif(
284+
has_gte_version("3.0"),
285+
reason="3.0 has the -e flag on new-window",
286+
)
287+
def test_new_window_with_environment_logs_warning_for_old_tmux(
288+
session: Session,
289+
caplog: pytest.LogCaptureFixture,
290+
) -> None:
291+
env = shutil.which("env")
292+
assert env is not None, "Cannot find usable `env` in PATH."
293+
294+
session.new_window(
295+
attach=True,
296+
window_name="window_with_environment",
297+
window_shell=f"{env} PS1='$ ' bash --norc --noprofile",
298+
environment={"ENV_VAR": "window"},
299+
)
300+
301+
assert any(
302+
"Cannot set up environment" in record.msg for record in caplog.records
303+
), "Warning missing"

0 commit comments

Comments
 (0)