-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmux_hooks_test.py
195 lines (158 loc) · 6.09 KB
/
tmux_hooks_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import os
import pathlib
import subprocess
import tempfile
import time
import unittest
import ptyprocess
from typing import Sequence
def get_tty(pid: int) -> str:
"""Returns tty (e.g. "pts/11") for a PID."""
return subprocess.check_output(f'ps -p {pid} -o tty --no-headers'.split()).strip().decode('utf-8')
class TmuxHooksTest(unittest.TestCase):
def setUp(self):
self.temp_dir_obj = tempfile.TemporaryDirectory()
self.temp_dir = pathlib.Path(self.temp_dir_obj.name)
self.server_socket = self.temp_dir / 'tmux-server.socket'
self.config_file = self.temp_dir / 'tmux.conf'
self.log_file = self.temp_dir / 'log'
self.tmux = 'tmux'
# self.tmux = '/home/ksp/src/tmux/tmux'
self.maxDiff = 2000
def tearDown(self):
if self.server_socket.exists():
subprocess.check_call(f'{self.tmux} -f /dev/null -S {self.server_socket} kill-server'.split())
self.temp_dir_obj.cleanup()
def _get_hook_log(self, hook_name: str) -> str:
variables = ['client_name', 'client_session', 'hook_session_name', 'session_name']
log_record = r'++++ #{hook}\\n ' + ' '.join([r'%(var)s: #{%(var)s}\\n' % {'var': var} for var in variables])
hook_log = """
set-hook -g %(hook)s "run-shell 'echo -e ''%(log_record)s'' >> %(log_file)s'"
""" % {'hook': hook_name, 'log_record': log_record, 'log_file': self.log_file}
return hook_log.replace(r'\\', r'\\\\').strip()
def _get_config(self, hook_names: Sequence[str]) -> str:
config = ['set -g assume-paste-time 0']
config += [self._get_hook_log(hook_name) for hook_name in hook_names]
return '\n'.join(config)
def setup_config(self, hook_names):
with open(self.config_file, 'wt') as f:
f.write(self._get_config(hook_names=hook_names))
def test_creating_new_session_emits_client_session_changed(self):
self.setup_config(hook_names=['client-session-changed'])
# Create the first tmux session. That will also start a tmux server.
first = ptyprocess.PtyProcess.spawn(f'{self.tmux} -f {self.config_file} -S {self.server_socket} new -s alpha'.split())
first.read()
time.sleep(.1)
self.assertLogs(f"""
++++ client-session-changed
client_name: /dev/{get_tty(first.pid)}
client_session: alpha
hook_session_name:
session_name: alpha
""")
def test_creating_new_session_form_inside_tmux_emits_client_session_changed(self):
self.setup_config(hook_names=['client-session-changed'])
# Create the first tmux session. That will also start a tmux server.
first = ptyprocess.PtyProcess.spawn(f'{self.tmux} -f {self.config_file} -S {self.server_socket} new -s alpha'.split())
first.read()
time.sleep(.1)
# Create another session from inside tmux.
first.sendcontrol(b'b')
first.write(b':')
time.sleep(.1)
first.write(b'new-session -s beta\n')
time.sleep(.1)
first.read()
self.assertLogs(f"""
++++ client-session-changed
client_name: /dev/{get_tty(first.pid)}
client_session: alpha
hook_session_name:
session_name: alpha
++++ client-session-changed
client_name: /dev/{get_tty(first.pid)}
client_session: beta
hook_session_name:
session_name: beta
""")
def test_session_renamed(self):
self.setup_config(hook_names=['client-session-changed', 'session-renamed'])
# Create the first tmux session. That will also start a tmux server.
first = ptyprocess.PtyProcess.spawn(f'{self.tmux} -f {self.config_file} -S {self.server_socket} new -s alpha'.split())
first.read()
time.sleep(.1)
# Rename the session.
first.sendcontrol(b'b')
first.write(b':')
time.sleep(.1)
first.write(b'rename-session alpha-renamed\n')
time.sleep(.1)
first.read()
time.sleep(.1)
self.assertLogs(f"""
++++ client-session-changed
client_name: /dev/{get_tty(first.pid)}
client_session: alpha
hook_session_name:
session_name: alpha
++++ session-renamed
client_name: /dev/{get_tty(first.pid)}
client_session: alpha-renamed
hook_session_name: alpha-renamed
session_name: alpha-renamed
""")
def test_session_closed(self):
self.setup_config(hook_names=['client-session-changed', 'session-closed'])
# Create the first tmux session. That will also start a tmux server.
first = ptyprocess.PtyProcess.spawn(f'{self.tmux} -f {self.config_file} -S {self.server_socket} new -s alpha'.split())
first.read()
time.sleep(.1)
first_tty = get_tty(first.pid)
# Detach the first tmux session.
first.sendcontrol(b'b')
first.write(b'd')
time.sleep(.1)
first.read()
# Create the second tmux session.
second = ptyprocess.PtyProcess.spawn(f'{self.tmux} -f {self.config_file} -S {self.server_socket} new -s beta'.split())
second.read()
time.sleep(.1)
second_tty = get_tty(second.pid)
# Connect to the second session with another client.
third = ptyprocess.PtyProcess.spawn(f'{self.tmux} -f {self.config_file} -S {self.server_socket} atta -t beta'.split())
third.read()
time.sleep(.1)
third_tty = get_tty(third.pid)
# Try exiting the second session, see what kind of hooks are called.
third.write(b'exit\n')
time.sleep(.1)
third.read()
time.sleep(.1)
self.assertLogs(f"""
++++ client-session-changed
client_name: /dev/{first_tty}
client_session: alpha
hook_session_name:
session_name: alpha
++++ client-session-changed
client_name: /dev/{second_tty}
client_session: beta
hook_session_name:
session_name: beta
++++ client-session-changed
client_name: /dev/{third_tty}
client_session: beta
hook_session_name:
session_name: beta
++++ session-closed
client_name:
client_session:
hook_session_name: beta
session_name: alpha
""")
def assertLogs(self, expected):
with open(self.log_file, 'rt') as f:
actual = f.read()
self.assertEqual(expected.strip(), actual.strip())
if __name__ == '__main__':
unittest.main()