|
3 | 3 | from __future__ import annotations
|
4 | 4 |
|
5 | 5 | import logging
|
| 6 | +import pathlib |
6 | 7 | import shutil
|
7 | 8 | import typing as t
|
8 | 9 |
|
9 | 10 | import pytest
|
10 | 11 |
|
| 12 | +from libtmux._internal.types import StrPath |
11 | 13 | from libtmux.common import has_gte_version, has_lt_version, has_lte_version
|
12 | 14 | from libtmux.constants import PaneDirection, ResizeAdjustmentDirection
|
13 | 15 | from libtmux.test.retry import retry_until
|
@@ -327,11 +329,98 @@ def test_split_pane_size(session: Session) -> None:
|
327 | 329 | def test_pane_context_manager(session: Session) -> None:
|
328 | 330 | """Test Pane context manager functionality."""
|
329 | 331 | window = session.new_window()
|
| 332 | + initial_pane_count = len(window.panes) |
| 333 | + |
330 | 334 | with window.split() as pane:
|
331 |
| - pane.send_keys('echo "Hello"') |
| 335 | + assert len(window.panes) == initial_pane_count + 1 |
332 | 336 | assert pane in window.panes
|
333 |
| - assert len(window.panes) == 2 # Initial pane + new pane |
334 | 337 |
|
335 | 338 | # Pane should be killed after exiting context
|
336 |
| - assert pane not in window.panes |
337 |
| - assert len(window.panes) == 1 # Only initial pane remains |
| 339 | + window.refresh() |
| 340 | + assert len(window.panes) == initial_pane_count |
| 341 | + |
| 342 | + |
| 343 | +class StartDirectoryTestFixture(t.NamedTuple): |
| 344 | + """Test fixture for start_directory parameter testing.""" |
| 345 | + |
| 346 | + test_id: str |
| 347 | + start_directory: StrPath | None |
| 348 | + expected_in_cmd: list[str] |
| 349 | + expected_not_in_cmd: list[str] |
| 350 | + description: str |
| 351 | + |
| 352 | + |
| 353 | +START_DIRECTORY_TEST_FIXTURES: list[StartDirectoryTestFixture] = [ |
| 354 | + StartDirectoryTestFixture( |
| 355 | + test_id="none_value", |
| 356 | + start_directory=None, |
| 357 | + expected_in_cmd=[], |
| 358 | + expected_not_in_cmd=["-c"], |
| 359 | + description="None should not add -c flag", |
| 360 | + ), |
| 361 | + StartDirectoryTestFixture( |
| 362 | + test_id="empty_string", |
| 363 | + start_directory="", |
| 364 | + expected_in_cmd=[], |
| 365 | + expected_not_in_cmd=["-c"], |
| 366 | + description="Empty string should not add -c flag", |
| 367 | + ), |
| 368 | + StartDirectoryTestFixture( |
| 369 | + test_id="absolute_path_string", |
| 370 | + start_directory="/tmp/test", |
| 371 | + expected_in_cmd=["-c"], |
| 372 | + expected_not_in_cmd=[], |
| 373 | + description="Absolute path string should add -c flag", |
| 374 | + ), |
| 375 | + StartDirectoryTestFixture( |
| 376 | + test_id="pathlib_absolute", |
| 377 | + start_directory=pathlib.Path("/tmp/test"), |
| 378 | + expected_in_cmd=["-c"], |
| 379 | + expected_not_in_cmd=[], |
| 380 | + description="pathlib.Path absolute should add -c flag", |
| 381 | + ), |
| 382 | +] |
| 383 | + |
| 384 | + |
| 385 | +@pytest.mark.parametrize( |
| 386 | + list(StartDirectoryTestFixture._fields), |
| 387 | + START_DIRECTORY_TEST_FIXTURES, |
| 388 | + ids=[test.test_id for test in START_DIRECTORY_TEST_FIXTURES], |
| 389 | +) |
| 390 | +def test_split_start_directory( |
| 391 | + test_id: str, |
| 392 | + start_directory: StrPath | None, |
| 393 | + expected_in_cmd: list[str], |
| 394 | + expected_not_in_cmd: list[str], |
| 395 | + description: str, |
| 396 | + session: Session, |
| 397 | +) -> None: |
| 398 | + """Test Pane.split start_directory parameter handling.""" |
| 399 | + window = session.new_window(window_name=f"test_split_{test_id}") |
| 400 | + pane = window.active_pane |
| 401 | + assert pane is not None |
| 402 | + |
| 403 | + # Split pane with start_directory parameter |
| 404 | + new_pane = pane.split(start_directory=start_directory) |
| 405 | + |
| 406 | + # Verify pane was created successfully |
| 407 | + assert new_pane in window.panes |
| 408 | + assert len(window.panes) == 2 |
| 409 | + |
| 410 | + |
| 411 | +def test_split_start_directory_pathlib(session: Session) -> None: |
| 412 | + """Test Pane.split accepts pathlib.Path for start_directory.""" |
| 413 | + import tempfile |
| 414 | + |
| 415 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 416 | + path_obj = pathlib.Path(temp_dir) |
| 417 | + |
| 418 | + window = session.new_window(window_name="test_split_pathlib") |
| 419 | + pane = window.active_pane |
| 420 | + assert pane is not None |
| 421 | + |
| 422 | + # Should accept pathlib.Path without error |
| 423 | + new_pane = pane.split(start_directory=path_obj) |
| 424 | + |
| 425 | + assert new_pane in window.panes |
| 426 | + assert len(window.panes) == 2 |
0 commit comments