|
4 | 4 |
|
5 | 5 | import logging
|
6 | 6 | import os
|
| 7 | +import pathlib |
7 | 8 | import subprocess
|
8 | 9 | import time
|
9 | 10 | import typing as t
|
10 | 11 |
|
11 | 12 | import pytest
|
12 | 13 |
|
| 14 | +from libtmux._internal.types import StrPath |
13 | 15 | from libtmux.common import has_gte_version, has_version
|
14 | 16 | from libtmux.server import Server
|
15 | 17 |
|
@@ -308,3 +310,87 @@ def test_server_context_manager(TestServer: type[Server]) -> None:
|
308 | 310 |
|
309 | 311 | # Server should be killed after exiting context
|
310 | 312 | assert not server.is_alive()
|
| 313 | + |
| 314 | + |
| 315 | +class StartDirectoryTestFixture(t.NamedTuple): |
| 316 | + """Test fixture for start_directory parameter testing.""" |
| 317 | + |
| 318 | + test_id: str |
| 319 | + start_directory: StrPath | None |
| 320 | + expected_in_cmd: list[str] |
| 321 | + expected_not_in_cmd: list[str] |
| 322 | + description: str |
| 323 | + |
| 324 | + |
| 325 | +START_DIRECTORY_TEST_FIXTURES: list[StartDirectoryTestFixture] = [ |
| 326 | + StartDirectoryTestFixture( |
| 327 | + test_id="none_value", |
| 328 | + start_directory=None, |
| 329 | + expected_in_cmd=[], |
| 330 | + expected_not_in_cmd=["-c"], |
| 331 | + description="None should not add -c flag", |
| 332 | + ), |
| 333 | + StartDirectoryTestFixture( |
| 334 | + test_id="empty_string", |
| 335 | + start_directory="", |
| 336 | + expected_in_cmd=[], |
| 337 | + expected_not_in_cmd=["-c"], |
| 338 | + description="Empty string should not add -c flag", |
| 339 | + ), |
| 340 | + StartDirectoryTestFixture( |
| 341 | + test_id="absolute_path_string", |
| 342 | + start_directory="/tmp/test", |
| 343 | + expected_in_cmd=["-c", "/tmp/test"], |
| 344 | + expected_not_in_cmd=[], |
| 345 | + description="Absolute path string should add -c flag", |
| 346 | + ), |
| 347 | + StartDirectoryTestFixture( |
| 348 | + test_id="pathlib_absolute", |
| 349 | + start_directory=pathlib.Path("/tmp/test"), |
| 350 | + expected_in_cmd=["-c", "/tmp/test"], |
| 351 | + expected_not_in_cmd=[], |
| 352 | + description="pathlib.Path absolute should add -c flag", |
| 353 | + ), |
| 354 | +] |
| 355 | + |
| 356 | + |
| 357 | +@pytest.mark.parametrize( |
| 358 | + list(StartDirectoryTestFixture._fields), |
| 359 | + START_DIRECTORY_TEST_FIXTURES, |
| 360 | + ids=[test.test_id for test in START_DIRECTORY_TEST_FIXTURES], |
| 361 | +) |
| 362 | +def test_new_session_start_directory( |
| 363 | + test_id: str, |
| 364 | + start_directory: StrPath | None, |
| 365 | + expected_in_cmd: list[str], |
| 366 | + expected_not_in_cmd: list[str], |
| 367 | + description: str, |
| 368 | + server: Server, |
| 369 | +) -> None: |
| 370 | + """Test Server.new_session start_directory parameter handling.""" |
| 371 | + # Create session with start_directory parameter |
| 372 | + session = server.new_session( |
| 373 | + session_name=f"test_start_dir_{test_id}", |
| 374 | + start_directory=start_directory, |
| 375 | + ) |
| 376 | + |
| 377 | + # Verify session was created successfully |
| 378 | + assert session.session_name == f"test_start_dir_{test_id}" |
| 379 | + assert server.has_session(f"test_start_dir_{test_id}") |
| 380 | + |
| 381 | + |
| 382 | +def test_new_session_start_directory_pathlib(server: Server) -> None: |
| 383 | + """Test Server.new_session accepts pathlib.Path for start_directory.""" |
| 384 | + import tempfile |
| 385 | + |
| 386 | + with tempfile.TemporaryDirectory() as temp_dir: |
| 387 | + path_obj = pathlib.Path(temp_dir) |
| 388 | + |
| 389 | + # Should accept pathlib.Path without error |
| 390 | + session = server.new_session( |
| 391 | + session_name="test_pathlib_start_dir", |
| 392 | + start_directory=path_obj, |
| 393 | + ) |
| 394 | + |
| 395 | + assert session.session_name == "test_pathlib_start_dir" |
| 396 | + assert server.has_session("test_pathlib_start_dir") |
0 commit comments