|
1 | 1 | """Test for libtmux Session object."""
|
2 | 2 | import logging
|
| 3 | +import shutil |
3 | 4 | import typing as t
|
4 | 5 |
|
5 | 6 | import pytest
|
6 | 7 |
|
7 | 8 | from libtmux import exc
|
8 |
| -from libtmux.common import has_gte_version |
| 9 | +from libtmux.common import has_gte_version, has_lt_version |
9 | 10 | from libtmux.pane import Pane
|
10 | 11 | from libtmux.server import Server
|
11 | 12 | from libtmux.session import Session
|
@@ -257,3 +258,46 @@ def test_cmd_inserts_sesion_id(session: Session) -> None:
|
257 | 258 | assert "-t" in cmd.cmd
|
258 | 259 | assert current_session_id in cmd.cmd
|
259 | 260 | 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 | +def test_new_window_with_environment(session: Session) -> None: |
| 268 | + env = shutil.which("env") |
| 269 | + assert env is not None, "Cannot find usable `env` in PATH." |
| 270 | + |
| 271 | + window = session.new_window( |
| 272 | + attach=True, |
| 273 | + window_name="window_with_environment", |
| 274 | + window_shell=f"{env} PS1='$ ' sh", |
| 275 | + environment={"ENV_VAR": "window"}, |
| 276 | + ) |
| 277 | + pane = window.attached_pane |
| 278 | + assert pane is not None |
| 279 | + pane.send_keys("echo $ENV_VAR") |
| 280 | + 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='$ ' sh", |
| 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