Skip to content

Commit 74a2b82

Browse files
committed
Server(feat[source_file]): add source_file() wrapping tmux source-file
why: source-file is needed for loading configuration files programmatically, useful for applying settings or initializing environments. what: - Add Server.source_file() with path and quiet (-q) parameters - Quiet mode suppresses errors for missing files - Add tests for sourcing a config and verifying option applied, and quiet mode
1 parent 53f92a1 commit 74a2b82

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

src/libtmux/server.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,40 @@ def list_buffers(self) -> list[str]:
718718

719719
return proc.stdout
720720

721+
def source_file(
722+
self,
723+
path: StrPath,
724+
*,
725+
quiet: bool | None = None,
726+
) -> None:
727+
"""Source a tmux configuration file via ``$ tmux source-file``.
728+
729+
Parameters
730+
----------
731+
path : str or PathLike
732+
Path to the configuration file.
733+
quiet : bool, optional
734+
Suppress errors for missing files (``-q`` flag).
735+
736+
Examples
737+
--------
738+
>>> import pathlib
739+
>>> conf = pathlib.Path(request.config.rootdir) / '..' / 'tmp_src.conf'
740+
>>> _ = conf.write_text('set -g @test_source yes')
741+
>>> server.source_file(str(conf))
742+
"""
743+
tmux_args: tuple[str, ...] = ()
744+
745+
if quiet:
746+
tmux_args += ("-q",)
747+
748+
tmux_args += (str(pathlib.Path(path).expanduser()),)
749+
750+
proc = self.cmd("source-file", *tmux_args)
751+
752+
if proc.stderr:
753+
raise exc.LibTmuxException(proc.stderr)
754+
721755
def list_clients(self) -> list[str]:
722756
"""List connected clients via ``$ tmux list-clients``.
723757

tests/test_server.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,28 @@ def test_list_buffers(server: Server) -> None:
605605
assert len(result) >= 2
606606

607607

608+
def test_source_file(server: Server, tmp_path: pathlib.Path) -> None:
609+
"""Test Server.source_file() sources a config file."""
610+
server.new_session(session_name="source_test")
611+
612+
conf = tmp_path / "source_test.conf"
613+
conf.write_text("set -g @source_test_opt yes\n")
614+
615+
server.source_file(conf)
616+
617+
# Verify the option was set
618+
result = server.cmd("show-options", "-gv", "@source_test_opt")
619+
assert result.stdout[0] == "yes"
620+
621+
622+
def test_source_file_quiet(server: Server) -> None:
623+
"""Test Server.source_file() with quiet flag ignores missing files."""
624+
server.new_session(session_name="source_quiet")
625+
626+
# Non-existent file with quiet should not raise
627+
server.source_file("/nonexistent/path.conf", quiet=True)
628+
629+
608630
def test_list_clients(server: Server) -> None:
609631
"""Test Server.list_clients() returns list without error."""
610632
server.new_session(session_name="list_clients_test")

0 commit comments

Comments
 (0)