|
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 |
|
11 | 12 | from libtmux import exc
|
| 13 | +from libtmux._internal.types import StrPath |
12 | 14 | from libtmux.common import has_gte_version, has_lt_version
|
13 | 15 | from libtmux.constants import WindowDirection
|
14 | 16 | from libtmux.pane import Pane
|
@@ -424,9 +426,94 @@ def test_session_context_manager(server: Server) -> None:
|
424 | 426 | """Test Session context manager functionality."""
|
425 | 427 | with server.new_session() as session:
|
426 | 428 | window = session.new_window()
|
427 |
| - assert session in server.sessions |
| 429 | + assert len(session.windows) >= 2 # Initial window + new window |
428 | 430 | assert window in session.windows
|
429 |
| - assert len(session.windows) == 2 # Initial window + new window |
430 | 431 |
|
431 | 432 | # Session should be killed after exiting context
|
432 |
| - assert session not in server.sessions |
| 433 | + session_name = session.session_name |
| 434 | + assert session_name is not None |
| 435 | + assert not server.has_session(session_name) |
| 436 | + |
| 437 | + |
| 438 | +class StartDirectoryTestFixture(t.NamedTuple): |
| 439 | + """Test fixture for start_directory parameter testing.""" |
| 440 | + |
| 441 | + test_id: str |
| 442 | + start_directory: StrPath | None |
| 443 | + expected_in_cmd: list[str] |
| 444 | + expected_not_in_cmd: list[str] |
| 445 | + description: str |
| 446 | + |
| 447 | + |
| 448 | +START_DIRECTORY_TEST_FIXTURES: list[StartDirectoryTestFixture] = [ |
| 449 | + StartDirectoryTestFixture( |
| 450 | + test_id="none_value", |
| 451 | + start_directory=None, |
| 452 | + expected_in_cmd=[], |
| 453 | + expected_not_in_cmd=["-c"], |
| 454 | + description="None should not add -c flag", |
| 455 | + ), |
| 456 | + StartDirectoryTestFixture( |
| 457 | + test_id="empty_string", |
| 458 | + start_directory="", |
| 459 | + expected_in_cmd=[], |
| 460 | + expected_not_in_cmd=["-c"], |
| 461 | + description="Empty string should not add -c flag", |
| 462 | + ), |
| 463 | + StartDirectoryTestFixture( |
| 464 | + test_id="absolute_path_string", |
| 465 | + start_directory="/tmp/test", |
| 466 | + expected_in_cmd=["-c"], |
| 467 | + expected_not_in_cmd=[], |
| 468 | + description="Absolute path string should add -c flag", |
| 469 | + ), |
| 470 | + StartDirectoryTestFixture( |
| 471 | + test_id="pathlib_absolute", |
| 472 | + start_directory=pathlib.Path("/tmp/test"), |
| 473 | + expected_in_cmd=["-c"], |
| 474 | + expected_not_in_cmd=[], |
| 475 | + description="pathlib.Path absolute should add -c flag", |
| 476 | + ), |
| 477 | +] |
| 478 | + |
| 479 | + |
| 480 | +@pytest.mark.parametrize( |
| 481 | + list(StartDirectoryTestFixture._fields), |
| 482 | + START_DIRECTORY_TEST_FIXTURES, |
| 483 | + ids=[test.test_id for test in START_DIRECTORY_TEST_FIXTURES], |
| 484 | +) |
| 485 | +def test_new_window_start_directory( |
| 486 | + test_id: str, |
| 487 | + start_directory: StrPath | None, |
| 488 | + expected_in_cmd: list[str], |
| 489 | + expected_not_in_cmd: list[str], |
| 490 | + description: str, |
| 491 | + session: Session, |
| 492 | +) -> None: |
| 493 | + """Test Session.new_window start_directory parameter handling.""" |
| 494 | + # Create window with start_directory parameter |
| 495 | + window = session.new_window( |
| 496 | + window_name=f"test_start_dir_{test_id}", |
| 497 | + start_directory=start_directory, |
| 498 | + ) |
| 499 | + |
| 500 | + # Verify window was created successfully |
| 501 | + assert window.window_name == f"test_start_dir_{test_id}" |
| 502 | + assert window in session.windows |
| 503 | + |
| 504 | + |
| 505 | +def test_new_window_start_directory_pathlib(session: Session) -> None: |
| 506 | + """Test Session.new_window accepts pathlib.Path for start_directory.""" |
| 507 | + import tempfile |
| 508 | + |
| 509 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 510 | + path_obj = pathlib.Path(temp_dir) |
| 511 | + |
| 512 | + # Should accept pathlib.Path without error |
| 513 | + window = session.new_window( |
| 514 | + window_name="test_pathlib_start_dir", |
| 515 | + start_directory=path_obj, |
| 516 | + ) |
| 517 | + |
| 518 | + assert window.window_name == "test_pathlib_start_dir" |
| 519 | + assert window in session.windows |
0 commit comments