Repository navigation
Expand file tree
/
Copy pathtest-codewhale-menu.py
More file actions
150 lines (132 loc) · 5.8 KB
/
Copy pathtest-codewhale-menu.py
File metadata and controls
150 lines (132 loc) · 5.8 KB
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
"""Real pseudo-terminal checks for menu routing; no packages or accounts are used."""
import errno
import os
from pathlib import Path
import pty
import select
import shutil
import signal
import tempfile
import time
import unittest
ROOT = Path(__file__).resolve().parent
class MenuTests(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory(prefix='lmm-menu-')
self.home = Path(self.tmp.name)
shutil.copyfile(ROOT / 'menu.sh', self.home / 'menu.sh')
self.env = dict(os.environ)
self.env.pop('TERMUX_VERSION', None)
self.env.pop('PREFIX', None)
def tearDown(self):
self.tmp.cleanup()
def run_menu(self, choices):
bash = shutil.which('bash')
pid, fd = pty.fork()
if pid == 0:
os.chdir(self.home)
os.execve(bash, [bash, str(self.home / 'menu.sh')], self.env)
output = b''
pending = list(choices)
deadline = time.monotonic() + 8
status = None
try:
while time.monotonic() < deadline:
ready, _, _ = select.select([fd], [], [], 0.05)
if ready:
try:
chunk = os.read(fd, 65536)
except OSError as error:
if error.errno == errno.EIO:
break
raise
if not chunk:
break
output += chunk
if output.endswith(b'> ') and pending:
os.write(fd, (pending.pop(0) + '\n').encode())
done, value = os.waitpid(pid, os.WNOHANG)
if done:
status = value
break
# macOS may close the PTY before waitpid reports the exited child.
# Reap within the original deadline rather than treating EOF as a hang.
while status is None and time.monotonic() < deadline:
done, value = os.waitpid(pid, os.WNOHANG)
if done:
status = value
break
time.sleep(0.01)
if status is None:
os.kill(pid, signal.SIGKILL)
os.waitpid(pid, 0)
self.fail('Menu did not terminate: ' + output.decode(errors='replace'))
self.assertTrue(os.WIFEXITED(status), output)
self.assertEqual(os.WEXITSTATUS(status), 0, output)
self.assertFalse(pending, output)
return output.decode(errors='replace')
finally:
os.close(fd)
def stub(self, name):
(self.home / (name + '.sh')).write_text("printf 'SELECTED:" + name + " %s\\n' \"$*\"\n")
def test_new_eighth_option_routes_to_codewhale_submenu(self):
self.stub('codewhale')
output = self.run_menu(['8', '0'])
self.assertIn('8 codewhale', output)
self.assertIn('SELECTED:codewhale menu', output)
def test_ninth_option_routes_to_opencode_installer_without_arguments(self):
self.stub('opencode')
output = self.run_menu(['9', '0'])
self.assertIn('9 opencode', output)
self.assertIn('SELECTED:opencode ', output)
self.assertNotIn('SELECTED:opencode menu', output)
def test_old_numbering_and_argument_contract_are_preserved(self):
self.stub('pi')
output = self.run_menu(['1', '0'])
self.assertIn('SELECTED:pi ', output)
self.assertNotIn('SELECTED:pi menu', output)
def test_out_of_range_and_overflow_input_return_to_menu(self):
output = self.run_menu(['24', '999999999999999999999999999', '-1', '0'])
self.assertNotIn('SELECTED:', output)
def test_termux_hides_desktop_apps_and_routes_sixth_option(self):
self.stub('codewhale')
self.env['TERMUX_VERSION'] = 'test'
output = self.run_menu(['6', '0'])
self.assertNotIn('cc-switch', output)
self.assertNotIn('clash-verge-rev', output)
self.assertIn('6 codewhale', output)
self.assertIn('SELECTED:codewhale menu', output)
def test_new_cli_install_and_setup_submenus(self):
for number, name in [(10, 'cursor-cli'), (11, 'grok-build'), (13, 'qwen-code'), (17, 'openclaw')]:
self.stub(name)
output = self.run_menu([str(number), '1', str(number), '2', '0'])
self.assertIn('SELECTED:' + name + ' install', output)
self.assertIn('SELECTED:' + name + ' setup', output)
def test_astrbot_directory_with_spaces_is_one_argument(self):
(self.home / 'astrbot.sh').write_text('printf "ASTRBOT:%s:%s:%s\\n" "$#" "$1" "$2"\n')
output = self.run_menu(['18', '2', 'instance with spaces', '0'])
self.assertIn('ASTRBOT:2:setup:instance with spaces', output)
def test_submenu_back_does_not_install(self):
self.stub('hermes')
output = self.run_menu(['16', '0', '0'])
self.assertNotIn('SELECTED:', output)
def test_remote_menu_preserves_submenu_argument(self):
bin_dir = self.home / 'bin'
bin_dir.mkdir()
stub = bin_dir / 'curl'
stub.write_text("#!/bin/sh\ncat <<'SCRIPT'\nprintf 'REMOTE:%s\\n' \"$*\"\nSCRIPT\n")
stub.chmod(0o755)
self.env['PATH'] = str(bin_dir) + os.pathsep + self.env['PATH']
output = self.run_menu(['8', '0'])
self.assertIn('REMOTE:menu', output)
def test_failed_remote_download_is_not_executed(self):
bin_dir = self.home / 'bin'
bin_dir.mkdir()
stub = bin_dir / 'curl'
stub.write_text('#!/bin/sh\necho "touch should-not-exist"\nexit 18\n')
stub.chmod(0o755)
self.env['PATH'] = str(bin_dir) + os.pathsep + self.env['PATH']
self.run_menu(['8', '0'])
self.assertFalse((self.home / 'should-not-exist').exists())
if __name__ == '__main__':
unittest.main(verbosity=2)