Skip to content

Commit f1b6401

Browse files
committed
tests(server) Improve start_directory tests for new_session
what: - Added StartDirectoryTestFixture for comprehensive testing of start_directory scenarios - Implemented tests for None, empty string, absolute path, and pathlib.Path inputs - Verified correct command generation based on start_directory values - Ensured compatibility with pathlib.Path in new_session method
1 parent 12c35b3 commit f1b6401

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

tests/test_server.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
import logging
66
import os
7+
import pathlib
78
import subprocess
89
import time
910
import typing as t
1011

1112
import pytest
1213

14+
from libtmux._internal.types import StrPath
1315
from libtmux.common import has_gte_version, has_version
1416
from libtmux.server import Server
1517

@@ -308,3 +310,87 @@ def test_server_context_manager(TestServer: type[Server]) -> None:
308310

309311
# Server should be killed after exiting context
310312
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

Comments
 (0)