-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_dotfiles.py
315 lines (226 loc) · 9.43 KB
/
test_dotfiles.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/env python3
import shutil
import concurrent.futures
import logging
import os
from random import sample
from typing import List
import time
import pytest
import libtmux
import random
import typing as t
from libtmux.server import Server
from libtmux import exc
from libtmux.test import TEST_SESSION_PREFIX, get_test_session_name, namer
from libtmux.session import Session
if t.TYPE_CHECKING:
from libtmux.session import Session
log_level = os.environ.get("LOG_LEVEL", "INFO")
logging.basicConfig(format="%(asctime)s:%(levelname)s:%(name)s: %(message)s", level=getattr(logging, log_level))
logger = logging.getLogger(__name__)
class RandomStrSequence:
def __init__(
self, characters: str = "abcdefghijklmnopqrstuvwxyz0123456789_"
) -> None:
"""Create a random letter / number generator. 8 chars in length.
>>> rng = RandomStrSequence()
>>> next(rng)
'...'
>>> len(next(rng))
8
>>> type(next(rng))
<class 'str'>
"""
self.characters: str = characters
def __iter__(self) -> "RandomStrSequence":
return self
def __next__(self) -> str:
return "".join(random.sample(self.characters, k=8))
# namer = RandomStrSequence()
@pytest.fixture(scope="module")
def tmux_client() -> libtmux.server.Server:
return libtmux.Server()
@pytest.fixture(scope="function")
def tmux_fake_server(
request: pytest.FixtureRequest,
monkeypatch: pytest.MonkeyPatch,
# config_file: pathlib.Path,
) -> libtmux.server.Server:
"""Returns a new, temporary :class:`libtmux.Server`
>>> from libtmux.server import Server
>>> def test_example(server: Server) -> None:
... assert isinstance(server, Server)
... session = server.new_session('my session')
... assert len(server.sessions) == 1
... assert [session.name.startswith('my') for session in server.sessions]
.. ::
>>> locals().keys()
dict_keys(...)
>>> source = ''.join([e.source for e in request._pyfuncitem.dtest.examples][:3])
>>> pytester = request.getfixturevalue('pytester')
>>> pytester.makepyfile(**{'whatever.py': source})
PosixPath(...)
>>> result = pytester.runpytest('whatever.py', '--disable-warnings')
===...
>>> result.assert_outcomes(passed=1)
"""
t = libtmux.Server(socket_name="zsh_dotfiles_test%s" % next(namer))
def fin() -> None:
t.kill_server()
request.addfinalizer(fin)
return t
@pytest.fixture(scope="function")
def tmux_fake_session(
request: pytest.FixtureRequest, session_params: t.Dict[str, t.Any], tmux_fake_server: Server
) -> "libtmux.Session":
"""Returns a new, temporary :class:`libtmux.Session`
>>> from libtmux.session import Session
>>> def test_example(session: "Session") -> None:
... assert isinstance(session.name, str)
... assert session.name.startswith('libtmux_')
... window = session.new_window(window_name='new one')
... assert window.name == 'new one'
.. ::
>>> locals().keys()
dict_keys(...)
>>> source = ''.join([e.source for e in request._pyfuncitem.dtest.examples][:3])
>>> pytester = request.getfixturevalue('pytester')
>>> pytester.makepyfile(**{'whatever.py': source})
PosixPath(...)
>>> result = pytester.runpytest('whatever.py', '--disable-warnings')
===...
>>> result.assert_outcomes(passed=1)
"""
session_name = "bosstest"
if not tmux_fake_server.has_session(session_name):
tmux_fake_server.cmd("new-session", "-d", "-s", session_name)
# find current sessions prefixed with bosstest
old_test_sessions = []
for s in tmux_fake_server.sessions:
old_name = s.session_name
if old_name is not None and old_name.startswith(TEST_SESSION_PREFIX):
old_test_sessions.append(old_name)
TEST_SESSION_NAME = get_test_session_name(server=tmux_fake_server)
try:
session = tmux_fake_server.new_session(session_name=TEST_SESSION_NAME, **session_params)
except exc.LibTmuxException as e:
raise e
"""
Make sure that bosstest can :ref:`test_builder_visually` and switches to
the newly created session for that testcase.
"""
session_id = session.session_id
assert session_id is not None
try:
tmux_fake_server.switch_client(target_session=session_id)
except exc.LibTmuxException:
# server.attach_session(session.get('session_id'))
pass
for old_test_session in old_test_sessions:
logger.debug(f"Old test test session {old_test_session} found. Killing it.")
tmux_fake_server.kill_session(old_test_session)
assert TEST_SESSION_NAME == session.session_name
assert TEST_SESSION_NAME != "bosstest"
return session
class TestDotfiles:
@pytest.mark.skipif(
os.getenv("GITHUB_ACTOR"),
reason="These tests are meant to only run locally on laptop prior to porting it over to new system",
)
def test_pure_prompt(self, tmux_fake_session: Session) -> None:
"""Verify pure prompt is initialized
"""
env = shutil.which("env")
assert env is not None, "Cannot find usable `env` in PATH."
tmux_fake_session.new_window(attach=True, window_name="test_pure_prompt", window_shell=f"{env} PURE_PROMPT_SYMBOL='>' zsh")
# takes a couple seconds to start up
time.sleep(5)
# get current window
attached_window = tmux_fake_session.attached_window
pane = attached_window.attached_pane
assert pane is not None
pane.enter()
pane_contents = "\n".join(pane.capture_pane())
assert ">" in pane_contents
# pane.send_keys(r'printf "%s"', literal=True, suppress_history=False)
# pane_contents = "\n".join(pane.capture_pane())
# assert pane_contents == '> printf "%s"\n>'
pane.send_keys("clear -x", literal=True, suppress_history=False)
pane_contents = "\n".join(pane.capture_pane())
assert '>' in pane_contents
def test_aliases(self, tmux_fake_session: Session) -> None:
"""Verify aliases are set correctly
> typeset -f dl-hls
dl-hls () {
pyenv activate yt-dlp3 || true
yt-dlp -S 'res:500' --downloader ffmpeg -o $(uuidgen).mp4 --cookies=~/Downloads/yt-cookies.txt ${1}
}
"""
env = shutil.which("env")
assert env is not None, "Cannot find usable `env` in PATH."
# breakpoint()
tmux_fake_session.new_window(attach=True, window_name="test_pure_prompt", window_shell=f"{env} PURE_PROMPT_SYMBOL='>' zsh")
# takes a couple seconds to start up
time.sleep(5)
attached_window: libtmux.window.Window = tmux_fake_session.attached_window
attached_window.select_layout("main-vertical")
attached_window.set_window_option("main-pane-height", 80)
assert attached_window.show_window_option("main-pane-height") == 80
# get current window
pane: libtmux.pane.Pane = attached_window.attached_pane
assert pane is not None
pane.clear()
pane.resize_pane(height=60)
pane.set_height(60)
pane.set_width(60)
pane.enter()
time.sleep(3)
pane.send_keys("clear -x", literal=True, suppress_history=False)
time.sleep(3)
pane_contents = "\n".join(pane.capture_pane())
# assert '>' in pane_contents
pane.send_keys('typeset -f dl-hls\n', literal=True, suppress_history=False)
pane_contents = "\n".join(pane.capture_pane())
# TODO: Figure out how to expand width of pane to fit output
expected_contents = """> typeset -f dl-hls
dl-hls () {
pyenv activate yt-dlp3 || true
yt-dlp -S 'res:500' --downloader ffmpeg -o $(uuidgen).mp4 --cookies=~/Do
wnloads/yt-cookies.txt ${1}
}"""
assert expected_contents in pane_contents
def test_golang(self, tmux_fake_session: Session) -> None:
"""Verify golang installed correctly
~/go/src/git.corp.adobe.com
❯ whence gopls
/Users/malcolm/go/bin/gopls
"""
env = shutil.which("env")
assert env is not None, "Cannot find usable `env` in PATH."
# breakpoint()
tmux_fake_session.new_window(attach=True, window_name="test_pure_prompt", window_shell=f"{env} PURE_PROMPT_SYMBOL='>' zsh")
# takes a couple seconds to start up
time.sleep(5)
attached_window: libtmux.window.Window = tmux_fake_session.attached_window
attached_window.select_layout("main-vertical")
attached_window.set_window_option("main-pane-height", 80)
assert attached_window.show_window_option("main-pane-height") == 80
# get current window
pane: libtmux.pane.Pane = attached_window.attached_pane
assert pane is not None
pane.clear()
pane.resize_pane(height=60)
pane.set_height(60)
pane.set_width(60)
pane.enter()
time.sleep(3)
pane.send_keys("clear -x", literal=True, suppress_history=False)
time.sleep(3)
pane_contents = "\n".join(pane.capture_pane())
pane.send_keys('whence gopls\n', literal=True, suppress_history=False)
pane_contents = "\n".join(pane.capture_pane())
# TODO: Figure out how to expand width of pane to fit output
expected_contents = """> whence gopls
/Users/malcolm/go/bin/gopls"""
assert expected_contents in pane_contents