Skip to content

Commit 9202412

Browse files
committed
tests(session) Add new_window start_directory tests
what: - Added comprehensive tests for `start_directory` parameter handling in `Session.new_window` - Included scenarios for None, empty string, absolute path, and pathlib.Path - Verified correct command generation and window creation based on `start_directory` values - Ensured compatibility with pathlib.Path for start_directory input
1 parent f1b6401 commit 9202412

File tree

1 file changed

+90
-3
lines changed

1 file changed

+90
-3
lines changed

tests/test_session.py

Lines changed: 90 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
from __future__ import annotations
44

55
import logging
6+
import pathlib
67
import shutil
78
import typing as t
89

910
import pytest
1011

1112
from libtmux import exc
13+
from libtmux._internal.types import StrPath
1214
from libtmux.common import has_gte_version, has_lt_version
1315
from libtmux.constants import WindowDirection
1416
from libtmux.pane import Pane
@@ -424,9 +426,94 @@ def test_session_context_manager(server: Server) -> None:
424426
"""Test Session context manager functionality."""
425427
with server.new_session() as session:
426428
window = session.new_window()
427-
assert session in server.sessions
429+
assert len(session.windows) >= 2 # Initial window + new window
428430
assert window in session.windows
429-
assert len(session.windows) == 2 # Initial window + new window
430431

431432
# 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

Comments
 (0)