Skip to content

Commit f336a59

Browse files
committed
ControlMode(fix[client_name]): bind client name to spawned client
why: recording the first client returned by list-clients can capture an existing attached client instead of the control-mode client spawned for the test. what: - wait for the spawned client pid to appear in list-clients - record client_name from the matching pid row - add a nested control-mode regression that verifies pid-to-name binding
1 parent 3654a36 commit f336a59

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

src/libtmux/_internal/control_mode.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,24 @@ def __enter__(self) -> ControlMode:
9191
self._write_fd = os.open(self._fifo_path, os.O_WRONLY)
9292

9393
self.stdout = self._proc.stdout # type: ignore[assignment]
94+
client_pid = str(self._proc.pid)
9495

9596
# Wait for client to register
9697
def client_registered() -> bool:
97-
clients = self.server.list_clients()
98-
return len(clients) > 0
98+
result = self.server.cmd(
99+
"list-clients",
100+
"-F",
101+
"#{client_pid}\t#{client_name}",
102+
)
103+
for line in result.stdout:
104+
pid, _, client_name = line.partition("\t")
105+
if pid == client_pid and client_name:
106+
self.client_name = client_name.strip()
107+
return True
108+
return False
99109

100110
retry_until(client_registered, 3, raises=True)
101111

102-
# Capture client name
103-
result = self.server.cmd(
104-
"list-clients",
105-
"-F",
106-
"#{client_name}",
107-
)
108-
self.client_name = result.stdout[0].strip() if result.stdout else ""
109-
110112
return self
111113

112114
def __exit__(

tests/test_control_mode.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,23 @@ def test_control_mode_client_name(
4040
"""ControlMode.client_name contains the tmux client identifier."""
4141
with control_mode() as ctl:
4242
assert "client-" in ctl.client_name
43+
44+
45+
def test_control_mode_client_name_matches_spawned_client(
46+
control_mode: t.Callable[[], ControlMode],
47+
server: Server,
48+
) -> None:
49+
"""ControlMode records the client name for its own subprocess."""
50+
with control_mode() as first, control_mode() as second:
51+
clients = {
52+
tuple(line.split("\t", 1))
53+
for line in server.cmd(
54+
"list-clients",
55+
"-F",
56+
"#{client_pid}\t#{client_name}",
57+
).stdout
58+
}
59+
60+
assert first.client_name != second.client_name
61+
assert (str(first._proc.pid), first.client_name) in clients
62+
assert (str(second._proc.pid), second.client_name) in clients

0 commit comments

Comments
 (0)