Skip to content

Commit 9878f60

Browse files
committed
feat(Session.new_window): set up environment
This allows to set up a custom environment when creating a new window.
1 parent c425c37 commit 9878f60

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

src/libtmux/session.py

+11
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
)
@@ -202,6 +203,7 @@ def new_window(
202203
attach: bool = True,
203204
window_index: str = "",
204205
window_shell: t.Optional[str] = None,
206+
environment: t.Optional[t.Dict[str, str]] = None,
205207
) -> Window:
206208
"""
207209
Return :class:`Window` from ``$ tmux new-window``.
@@ -259,6 +261,15 @@ def new_window(
259261
% (self.id, window_index),
260262
)
261263

264+
if environment:
265+
if has_gte_version("3.0"):
266+
for k, v in environment.items():
267+
window_args += (f"-e{k}={v}",)
268+
else:
269+
logger.warning(
270+
"Cannot set up environment as tmux 3.0 or newer is required."
271+
)
272+
262273
if window_shell:
263274
window_args += (window_shell,)
264275

tests/test_session.py

+56-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
"""Test for libtmux Session object."""
22
import logging
3+
import shutil
34
import typing as t
45

56
import pytest
67

78
from libtmux import exc
8-
from libtmux.common import has_gte_version
9+
from libtmux.common import has_gte_version, has_lt_version
910
from libtmux.pane import Pane
1011
from libtmux.server import Server
1112
from libtmux.session import Session
@@ -257,3 +258,57 @@ def test_cmd_inserts_sesion_id(session: Session) -> None:
257258
assert "-t" in cmd.cmd
258259
assert current_session_id in cmd.cmd
259260
assert cmd.cmd[-1] == last_arg
261+
262+
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+
)
267+
@pytest.mark.parametrize(
268+
"environment",
269+
[
270+
{"ENV_VAR": "window"},
271+
{"ENV_VAR_1": "window_1", "ENV_VAR_2": "window_2"},
272+
],
273+
)
274+
def test_new_window_with_environment(
275+
session: Session,
276+
environment: t.Dict[str, str],
277+
) -> None:
278+
env = shutil.which("env")
279+
assert env is not None, "Cannot find usable `env` in PATH."
280+
281+
window = session.new_window(
282+
attach=True,
283+
window_name="window_with_environment",
284+
window_shell=f"{env} PS1='$ ' sh",
285+
environment=environment,
286+
)
287+
pane = window.attached_pane
288+
assert pane is not None
289+
for k, v in environment.items():
290+
pane.send_keys(f"echo ${k}")
291+
assert pane.capture_pane()[-2] == v
292+
293+
294+
@pytest.mark.skipif(
295+
has_gte_version("3.0"),
296+
reason="3.0 has the -e flag on new-window",
297+
)
298+
def test_new_window_with_environment_logs_warning_for_old_tmux(
299+
session: Session,
300+
caplog: pytest.LogCaptureFixture,
301+
) -> None:
302+
env = shutil.which("env")
303+
assert env is not None, "Cannot find usable `env` in PATH."
304+
305+
session.new_window(
306+
attach=True,
307+
window_name="window_with_environment",
308+
window_shell=f"{env} PS1='$ ' sh",
309+
environment={"ENV_VAR": "window"},
310+
)
311+
312+
assert any(
313+
"Cannot set up environment" in record.msg for record in caplog.records
314+
), "Warning missing"

0 commit comments

Comments
 (0)