|
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,117 @@ 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 | + description: str |
| 349 | + |
| 350 | + |
| 351 | +START_DIRECTORY_TEST_FIXTURES: list[StartDirectoryTestFixture] = [ |
| 352 | + StartDirectoryTestFixture( |
| 353 | + test_id="none_value", |
| 354 | + start_directory=None, |
| 355 | + description="None should not add -c flag", |
| 356 | + ), |
| 357 | + StartDirectoryTestFixture( |
| 358 | + test_id="empty_string", |
| 359 | + start_directory="", |
| 360 | + description="Empty string should not add -c flag", |
| 361 | + ), |
| 362 | + StartDirectoryTestFixture( |
| 363 | + test_id="user_path", |
| 364 | + start_directory="{user_path}", |
| 365 | + description="User path should add -c flag", |
| 366 | + ), |
| 367 | + StartDirectoryTestFixture( |
| 368 | + test_id="relative_path", |
| 369 | + start_directory="./relative/path", |
| 370 | + description="Relative path should add -c flag", |
| 371 | + ), |
| 372 | +] |
| 373 | + |
| 374 | + |
| 375 | +@pytest.mark.parametrize( |
| 376 | + list(StartDirectoryTestFixture._fields), |
| 377 | + START_DIRECTORY_TEST_FIXTURES, |
| 378 | + ids=[test.test_id for test in START_DIRECTORY_TEST_FIXTURES], |
| 379 | +) |
| 380 | +def test_split_start_directory( |
| 381 | + test_id: str, |
| 382 | + start_directory: StrPath | None, |
| 383 | + description: str, |
| 384 | + session: Session, |
| 385 | + monkeypatch: pytest.MonkeyPatch, |
| 386 | + tmp_path: pathlib.Path, |
| 387 | + user_path: pathlib.Path, |
| 388 | +) -> None: |
| 389 | + """Test Pane.split start_directory parameter handling.""" |
| 390 | + monkeypatch.chdir(tmp_path) |
| 391 | + |
| 392 | + window = session.new_window(window_name=f"test_split_{test_id}") |
| 393 | + pane = window.active_pane |
| 394 | + assert pane is not None |
| 395 | + |
| 396 | + # Format path placeholders with actual fixture values |
| 397 | + actual_start_directory = start_directory |
| 398 | + expected_path = None |
| 399 | + |
| 400 | + if start_directory and str(start_directory) not in ["", "None"]: |
| 401 | + if "{user_path}" in str(start_directory): |
| 402 | + # Replace placeholder with actual user_path |
| 403 | + actual_start_directory = str(start_directory).format(user_path=user_path) |
| 404 | + expected_path = str(user_path) |
| 405 | + elif str(start_directory).startswith("./"): |
| 406 | + # For relative paths, use tmp_path as base |
| 407 | + temp_dir = tmp_path / "relative" / "path" |
| 408 | + temp_dir.mkdir(parents=True, exist_ok=True) |
| 409 | + actual_start_directory = str(temp_dir) |
| 410 | + expected_path = str(temp_dir.resolve()) |
| 411 | + |
| 412 | + # Should not raise an error |
| 413 | + new_pane = pane.split(start_directory=actual_start_directory) |
| 414 | + |
| 415 | + assert new_pane in window.panes |
| 416 | + assert len(window.panes) == 2 |
| 417 | + |
| 418 | + # Verify working directory if we have an expected path |
| 419 | + if expected_path: |
| 420 | + new_pane.refresh() |
| 421 | + assert new_pane.pane_current_path is not None |
| 422 | + actual_path = str(pathlib.Path(new_pane.pane_current_path).resolve()) |
| 423 | + assert actual_path == expected_path |
| 424 | + |
| 425 | + |
| 426 | +def test_split_start_directory_pathlib( |
| 427 | + session: Session, user_path: pathlib.Path |
| 428 | +) -> None: |
| 429 | + """Test Pane.split accepts pathlib.Path for start_directory.""" |
| 430 | + window = session.new_window(window_name="test_split_pathlib") |
| 431 | + pane = window.active_pane |
| 432 | + assert pane is not None |
| 433 | + |
| 434 | + # Pass pathlib.Path directly to test pathlib.Path acceptance |
| 435 | + new_pane = pane.split(start_directory=user_path) |
| 436 | + |
| 437 | + assert new_pane in window.panes |
| 438 | + assert len(window.panes) == 2 |
| 439 | + |
| 440 | + # Verify working directory |
| 441 | + new_pane.refresh() |
| 442 | + assert new_pane.pane_current_path is not None |
| 443 | + actual_path = str(pathlib.Path(new_pane.pane_current_path).resolve()) |
| 444 | + expected_path = str(user_path.resolve()) |
| 445 | + assert actual_path == expected_path |
0 commit comments