diff options
Diffstat (limited to 'test/fs.py')
| -rw-r--r-- | test/fs.py | 1128 |
1 files changed, 0 insertions, 1128 deletions
@@ -4,9 +4,6 @@ import fcntl import json import os import shutil -import select -import signal -import socket from pathlib import Path import subprocess import struct @@ -777,245 +774,6 @@ def store_shared_between_windows(binary): assert (root / '.local' / 'state' / 'pardes' / 'store').exists() -def workflow_canary(binary): - """One session driven as the dogfood agents drive it, asserting the log - and the read-backs at every step: the canary for the main paths (the - round-18 Grep that never found anything went through every narrower - test). Look files, Grep and Find hits and misses, a plain word looked - at, Edit in two files, Save, get, pty/run failing then passing, a - command pane and Kill, a REPL send, the layout and a column's files, - ask/answer, and Dump/Restore last.""" - started = time.monotonic() - with tempfile.TemporaryDirectory(prefix='pardes-flow-') as directory: - root = Path(directory) - (root / 'a.txt').write_bytes(b'alpha one\nshared word\n') - (root / 'b.txt').write_bytes(b'beta two\nshared word\n') - (root / 's.sh').write_bytes(b'echo from-file\n') - - def wait(ready, what, seconds=10): - deadline = time.monotonic() + seconds - while not ready(): - assert time.monotonic() < deadline, what - time.sleep(.05) - - with session(binary, root, 'flow') as (client, address): - def log(): - return client.read('/log').decode('utf-8', 'replace') - - def made_by(serial, line): - """The pane an exec at `serial` made, as exec reads it back.""" - client.write(f'/pane/{serial}/exec', line) - return int(client.read(f'/pane/{serial}/exec').split()[-1]) - - # Look opens files, and the log says new. - a = look(client, str(root / 'a.txt')) - b = look(client, str(root / 'b.txt')) - assert client.read(f'/pane/{a}/body') == b'alpha one\nshared word\n' - assert f'new {a} {root}/a.txt\n' in log() and f'new {b} {root}/b.txt\n' in log() - - # Grep and Find: a hit fills its query pane, line 1 the query and - # the lines under each file's name; a miss fails ENOENT. - grep = made_by(a, b'Grep shared\n') - rows = client.read(f'/pane/{grep}/body').decode() - assert rows == 'Grep shared\na.txt\na.txt:2:1 shared word\nb.txt\nb.txt:2:1 shared word\n', rows - client.remove(f'/pane/{grep}') - execute_refused(client, a, 'Grep zzqqnomatch', 'Grep: text not found') - found = made_by(a, b'Find b.txt\n') - assert 'b.txt' in client.read(f'/pane/{found}/body').decode() - client.remove(f'/pane/{found}') - execute_refused(client, a, 'Find zzqqnomatch', 'Find: name not found') - - # A plain word looked at is found in the pane, after its dot. - client.write(f'/pane/{a}/dot', b' 0 0 ') - client.write(f'/pane/{a}/look', b'shared\n') - assert client.read(f'/pane/{a}/dot').split() == [b'10', b'16'], client.read(f'/pane/{a}/dot') - - # Edit in two files, then Save each: on disk, and logged. - for pane in (a, b): - client.write(f'/pane/{pane}/ctl', b'Edit ,s/shared/SHARED/g\n') - assert client.read(f'/pane/{b}/body') == b'beta two\nSHARED word\n' - for pane in (a, b): - client.write(f'/pane/{pane}/exec', b'Save\n') - assert (root / 'a.txt').read_bytes() == b'alpha one\nSHARED word\n' - assert f'save {a} {root}/a.txt\n' in log() and f'save {b} {root}/b.txt\n' in log() - - # get reloads what changed on disk. - (root / 'b.txt').write_bytes(b'from disk\n') - client.write(f'/pane/{b}/ctl', b'get\n') - assert client.read(f'/pane/{b}/body') == b'from disk\n' - - # A terminal: pty/run failing then passing. - term = made_by(1, b'Tty\n') - - def run(line): - fid = client.open(f'/pane/{term}/pty/run', 2) - try: - client.rpc(118, struct.pack('<IQI', fid, 0, len(line)) + line) - return client.read_fid(fid) - finally: - client.close(fid) - deadline = time.monotonic() + 10 - while (answer := run(b'true\n')) != b'exit 0\n': - assert time.monotonic() < deadline, answer - time.sleep(.05) - assert run(b'false\n') == b'exit 1\n' - assert run(b'echo passed\n') == b'exit 0\npassed\n' - # (A run's answer is its record: the log has no err for these.) - assert f'err {term} pty/run' not in log(), log() - - # A command pane, and Kill: its exit is logged. - sleeper = made_by(a, b'sleep 30\n') - assert f'run {sleeper} sleep 30\n' in log() - client.write('/ctl', b'Kill sleep\n') - wait(lambda: f'\nexit {sleeper} ' in log(), 'Kill: no exit logged') - - # A REPL send: a program in the terminal (cat, which says back - # what it is sent; a nested shell reads as the shell itself) - # bound as bash's REPL, an exec on a .sh file's body (its event - # written back) goes there. - s = look(client, str(root / 's.sh')) - client.write(f'/pane/{term}/pty/data', b'cat\r') - wait(lambda: client.read(f'/pane/{term}/pty/status').split()[-1] == b'1', ('the REPL program did not start', client.read(f'/pane/{term}/body')[-300:])) - client.write(f'/pane/{term}/ctl', b'Repl bash\n') - fid = client.open(f'/pane/{s}/event', 2) - try: - record = b'MX0 0 0 14 echo from-file\n' - client.rpc(118, struct.pack('<IQI', fid, 0, len(record)) + record) - finally: - client.close(fid) - wait(lambda: f'\nsend {s} {term} ' in log(), ('no REPL send logged', log()[-800:])) - wait(lambda: client.read(f'/pane/{term}/body').count(b'echo from-file') >= 2, 'the REPL did not get it') - client.write(f'/pane/{term}/ctl', b'Repl -\n') - client.write(f'/pane/{term}/pty/data', b'\x04') - wait(lambda: run(b'true\n') == b'exit 0\n', 'the terminal did not come back to its shell') - - # The layout and a column's files: Newcol, its tag, rmdir. - assert len(client.read('/layout').splitlines()) >= 2 # a column, then `active` - client.write('/ctl', b'Newcol\n') - columns = [row for row in client.read('/layout').splitlines() if not row.startswith(b'active')] - assert len(columns) == 2, columns - empty = columns[-1].split()[0].decode() - assert b'New' in client.read(f'/col/{empty}/tag') - client.remove(f'/col/{empty}') - assert len([row for row in client.read('/layout').splitlines() if not row.startswith(b'active')]) == 1 - - # ask/answer: Save on a terminal asks for a path over 9P. - client.write(f'/pane/{term}/exec', b'Save\n') - wait(lambda: f'ask {term} save path\n' in log(), 'no ask logged') - client.write(f'/pane/{term}/ctl', f'answer {root}/scroll.txt\n'.encode()) - wait(lambda: (root / 'scroll.txt').exists(), 'the answered Save wrote nothing') - assert f'answer {term} {root}/scroll.txt\n' in log() - - # Dump, then Restore it: the new session names its panes. - client.write('/ctl', b'Dump\n') - saved = root / 'flow.dump.zon' - wait(saved.exists, 'Dump wrote nothing') - assert f'dump {saved}\n' in log() - try: - client.write('/ctl', f'Restore {saved}\n'.encode()) - except (EOFError, OSError): - pass - time.sleep(.5) - with Client(address) as restored: - wait(lambda: f'restore {saved}' in restored.read('/log').decode(), 'Restore not logged') - names = restored.read('/index').decode() - assert f'{root}/a.txt' in names and f'{root}/b.txt' in names, names - # Recent goes on across the Restore, and is kept after it. - assert f'{root}/a.txt\n' in restored.read('/recent').decode(), restored.read('/recent') - (root / 'after.txt').write_bytes(b'after\n') - look(restored, str(root / 'after.txt')) - assert f'open {root}/after.txt\n' in restored.read('/recent').decode() - state = root / '.local' / 'state' / 'pardes' / 'store' - wait(lambda: f'{root}/after.txt'.encode() in state.read_bytes(), 'Recent not kept after Restore') - assert time.monotonic() - started < 30, 'the canary took too long' - - -def python_repl_runs_one_line(binary): - """A one-line exec sent to Python's own REPL (3.13's pyrepl, which asks - for bracketed paste) runs at once, with no second Enter: the Enter - reaches it after the paste, not inside it. A real pty, a real python3.""" - python = shutil.which('python3') - if python is None: - return - version = subprocess.run([python, '-c', 'import sys; print(sys.version_info >= (3, 13))'], - capture_output=True, text=True).stdout.strip() - if version != 'True': - return - with tempfile.TemporaryDirectory(prefix='pardes-repl-') as directory: - with session(binary, Path(directory), 'repl') as (client, _): - def panes(client): - return {int(row.split()[0]) for row in client.read('/index').splitlines()} - before = panes(client) - client.write('/pane/1/ctl', b'Tty\n') - deadline = time.monotonic() + 10 - while time.monotonic() < deadline and panes(client) == before: - time.sleep(.05) - tty = max(panes(client) - before) - def body(): - return client.read(f'/pane/{tty}/body').decode('utf-8', 'replace') - def wait_for(text, seconds=15): - deadline = time.monotonic() + seconds - while time.monotonic() < deadline: - if text in body(): - return True - time.sleep(.1) - return False - assert wait_for('$') or wait_for('>'), body() - client.write(f'/pane/{tty}/pty/data', python.encode() + b' -q\r') - assert wait_for('>>>'), body() - client.write(f'/pane/{tty}/ctl', b'Repl python\n') - # An exec on the REPL's own body goes to the REPL, as its event - # record written back does: an empty range acts on its text. - fid = client.open(f'/pane/{tty}/event', 2) - try: - record = b'MX0 0 0 10 print(6*7)\n' - client.rpc(118, struct.pack('<IQI', fid, 0, len(record)) + record) - assert wait_for('\n42'), body() - # After the session has sat idle, the Enter still waits for - # the paste: core time is as old as the last frame then. - for n in range(5): - time.sleep(2) - line = f'print({1000 + n})'.encode() - record = b'MX0 0 0 %d %s\n' % (len(line), line) - client.rpc(118, struct.pack('<IQI', fid, 0, len(record)) + record) - assert wait_for(f'\n{1000 + n}'), (n, body()) - # More than one line, with and without a newline at the end, - # and a block with a statement after it: each runs whole, - # and the REPL is back at its prompt, not at `...`. - for code, outputs in [(b'print(11)\nprint(22)', ['11', '22']), - (b'print(33)\nprint(44)\n', ['33', '44']), - (b'for i in range(2):\n print(55 + i)\nprint(77)\n', ['55', '56', '77'])]: - record = b'MX0 0 0 %d %s\n' % (len(code), code) - client.rpc(118, struct.pack('<IQI', fid, 0, len(record)) + record) - for out in outputs: - assert wait_for(f'\n{out}\n'), (code, out, body()) - time.sleep(.5) - assert body().rstrip().endswith('>>>'), (code, body()[-200:]) - finally: - client.close(fid) - - -def signal_cleans_up(binary): - """A detached session told to stop by a signal unlinks its sockets. A - hangup is a terminal's end, not a session's: the daemon keeps it.""" - for sig in [signal.SIGTERM, signal.SIGINT, signal.SIGHUP]: - with tempfile.TemporaryDirectory(prefix='pardes-sig-') as directory: - root = Path(directory) - seen = {} - with session(binary, root, 'sig', terminal=seen) as (client, address): - assert address.exists() - if sig == signal.SIGHUP: - os.kill(seen['pid'], sig) - time.sleep(.3) - assert address.exists() and client.read('/index'), 'a hangup ended a kept session' - sig = signal.SIGTERM - os.kill(seen['pid'], sig) - deadline = time.monotonic() + 5 - while address.exists() and time.monotonic() < deadline: - time.sleep(.01) - assert not address.exists(), (sig, list(root.iterdir())) - - def odd_paths(binary): """Long and odd paths written where a path goes -- name, look, DumpDir, Dump, Save -- are refused or said, never a crash: a part over 255 bytes @@ -1217,897 +975,11 @@ def test(binary, quic=False): new_terminals_named_once(binary) directory_panes(binary) putall_answers_after_its_saves(binary) - workflow_canary(binary) recent_survives_restart(binary) store_shared_between_windows(binary) - python_repl_runs_one_line(binary) - signal_cleans_up(binary) - for options, message in [ - (['--fs'], b'no such option'), - (['--fs9'], b'no such option'), - (['--', 'a', 'b'], b'is the second'), - (['--9p'], b'--9p needs a socket name'), - (['--9p='], b'invalid 9P socket name'), - (['--9p=a/b'], b'invalid 9P socket name'), - (['--attach=work', '--9p=work'], b'--attach has none'), - (['--9p-tcp'], b'--9p-tcp needs'), - (['--9p-tcp='], b'--9p-tcp needs'), - (['--9p-tcp=tcp!localhost!5640'], b'--9p-tcp needs'), - (['--9p-tcp=quic!127.0.0.1!5640'], b'--9p-tcp needs'), - (['--9p-tcp=tcp!127.0.0.1!5640', '--9p-tcp=tcp!127.0.0.1!5641'], b'specified twice'), - (['--attach=work', '--9p-tcp=tcp!127.0.0.1!5640'], b'--attach has none'), - (['--9p-quic'], b'--9p-quic needs'), - (['--9p-quic='], b'--9p-quic needs'), - (['--9p-quic=tcp!127.0.0.1!5640'], b'--9p-quic needs'), - (['--mount=os=work'], b'invalid mount name'), - (['--mount=peer='], b'invalid mount name or dial'), - (['--mount=peer=one', '--mount=peer=two'], b'duplicate mount'), - (['--mount=peer=tcp!127.0.0.1!0'], b'invalid mount dial'), - (['--attach=work', '--mount=peer=one'], b'--attach has none'), - ]: - result = subprocess.run([binary, *options], capture_output=True, timeout=5) - assert result.returncode == 1 and message in result.stderr, (options, result) - with tempfile.TemporaryDirectory(prefix='pardes-fs-') as directory: - root = Path(directory) - with session(binary, root, 'renamed', '--9p=explicit', socket_name='explicit') as (named, named_address): - assert named_address.name == 'pardes-9p-explicit.sock' - assert not (root / 'pardes-9p-renamed.sock').exists() - assert named.read('/pane/1/body') == b'initial\n' - with session(binary, root, 'first') as (client, address): - assert {'pane', 'index', 'status', 'look', 'exec', 'log', 'os'} <= set(client.list('/')) - assert 'self' not in client.list('/') - assert client.read('/pane/1/body') == b'initial\n' - assert client.read('/os' + str(root / 'first.txt')) == b'initial\n' - assert 'first.txt' in client.list('/os' + str(root)) - assert client.read('/pane/1/../1/body') == b'initial\n' - assert client.read('/os' + str(root) + '/../' + root.name + '/first.txt') == b'initial\n' - - listing = root / 'listing' - listing.mkdir() - (listing / 'broken').symlink_to(listing / 'missing') - for number in range(24): - (listing / f'entry-{number:02}').touch() - with Client(address, msize=256) as small: - assert set(small.list('/os' + str(listing))) == { - f'entry-{number:02}' for number in range(24)} - # 64 KiB frames: a ctl line with no newline is held whatever the - # size of its write -- a mount cuts lines anywhere -- and runs at - # the close, its failure then in the log. - with Client(address, msize=65536) as big: - assert big.msize == 65536 - ctl = big.open('/pane/1/ctl', 1) - line = b'bogus' + b'x' * 20000 - big.rpc(118, struct.pack('<IQI', ctl, 0, len(line)) + line) - assert b'ctl: unknown control message' not in client.read('/log') - big.close(ctl) - assert b'ctl: unknown control message' in client.read('/log') - - frozen = client.open('/screen') - before = bytearray(client.read_fid(frozen, count=31)) - client.write('/pane/1/body', b'screen changed\n') - while chunk := client.read_fid(frozen, len(before)): - before.extend(chunk) - client.close(frozen) - old_screen = json.loads(before) - old_text = ''.join(cell[0] for cell in old_screen['cells']) - assert 'initial' in old_text and 'screen changed' not in old_text - assert 'screen changed' in ''.join(cell[0] for cell in client.screen()['cells']) - - payload = ('const value = "λ";\n' * 3000).encode() - serial = new_pane(client, payload) - body = f'/pane/{serial}/body' - assert client.read(body) == payload - client.write(body, b'replaced\n', truncate=True) - assert client.read(body) == b'replaced\n' - - event = client.open(f'/pane/{serial}/event') - client.write(body, b'event\n') - record = client.read_fid(event) - assert record.startswith(b'EI') and b'event\n' in record, record - client.close(event) - - os_path = '/os' + str(root / 'first.txt') - client.write(os_path, b'written through 9P\n', truncate=True) - assert (root / 'first.txt').read_bytes() == b'written through 9P\n' - assert client.read(os_path) == b'written through 9P\n' - - for index in range(12): - with Client(address) as other: - assert other.read(body) == b'replaced\nevent\n', index - - missing = False - try: - client.read('/pane/4294967295/body') - except OSError: - missing = True - assert missing - assert client.read(body) == b'replaced\nevent\n' - - if 'src' in client.list('/'): - source = look(client, '/virtual/src/pardes.zig') - assert b'pub const Pardes' in client.read(f'/pane/{source}/body') - - (root / 'index').write_bytes(b'OS takes precedence\n') - opened = look(client, 'index') - assert client.read(f'/pane/{opened}/body') == b'OS takes precedence\n' - opened = look(client, '/n/self/index') - index = client.read(f'/pane/{opened}/body') - assert index.split()[0] == b'1' and b'first.txt' in index, index - (root / 'explicit.txt').write_bytes(b'explicit OS mount\n') - opened = look(client, '/n/os' + str(root / 'explicit.txt')) - assert client.read(f'/pane/{opened}/body') == b'explicit OS mount\n' - - own = root / 'pardes-9p-second.sock' - with session(binary, root, 'second', '--mount=peer=' + str(address), - '--mount=own=' + str(own)) as (remote, _): - opened = look(remote, '/n/own/pane/1/body') - assert remote.read(f'/pane/{opened}/body') == b'initial\n' - opened = look(remote, '/n/peer/pane/' + str(serial) + '/body') - assert remote.read(f'/pane/{opened}/body') == b'replaced\nevent\n' - remote.write(f'/pane/{opened}/body', b'saved to peer\n', truncate=True) - remote.write(f'/pane/{opened}/exec', b'Save\n') - assert client.read(body) == b'saved to peer\n' - - large = b'mounted file contents\n' * 60000 - large_path = root / 'large.txt' - large_path.write_bytes(large) - opened = look(remote, '/n/peer/os' + str(large_path)) - assert remote.read(f'/pane/{opened}/body') == large - edited = large[:-3] + b'edited\n' - remote.write(f'/pane/{opened}/body', edited, truncate=True) - remote.write(f'/pane/{opened}/exec', b'Save\n') - assert large_path.read_bytes() == edited - assert client.read('/os' + str(large_path)) == edited - # A path not there under a peer's mount is a miss, as any - # path to no file is: the write answered, the err logged. - remote.write('/look', b'/n/peer/os' + str(root / 'no-such-file.txt').encode() + b'\n') - assert b'no such file' in remote.read('/log') - - with session(binary, root, 'runtime') as (remote, own_address): - control = new_pane(remote, b'') - execute(remote, control, 'Mount peer ' + str(address)) - opened = look(remote, '/n/peer/pane/1/body', source=control) - assert remote.read(f'/pane/{opened}/body') == client.read('/pane/1/body') - # A builtin that fails through exec fails the write (one rule). - execute_refused(remote, control, 'Mount peer ' + str(own_address), 'already mounted') - execute_refused(remote, control, 'Unmount peer', 'mount in use') # a pane is open on it - remote.write(f'/pane/{opened}/body', b'runtime mounted Save\n', truncate=True) - remote.write(f'/pane/{opened}/exec', b'Save\n') - assert client.read('/pane/1/body') == b'runtime mounted Save\n' - assert remote.read('/pane/1/body') == b'initial\n' - roots = look(remote, '/n', source=control) - assert b'/n/peer/\n' in remote.read(f'/pane/{roots}/body') - remote.write(f'/pane/{opened}/exec', b'Del\n') - execute(remote, control, 'Unmount peer') - remote.write(f'/pane/{roots}/ctl', b'get\n') - assert remote.read(f'/pane/{roots}/body') == b'/n/os/\n/n/self/\n' - execute(remote, control, 'Mount peer ' + str(own_address)) - opened = look(remote, '/n/peer/pane/1/body', source=control) - assert remote.read(f'/pane/{opened}/body') == b'initial\n' - - with session(binary, root, 'mounted-startup', '--mount=peer=' + str(address), - file='/n/peer/pane/1/body:1') as (remote, _): - assert remote.read('/pane/1/body') == b'runtime mounted Save\n' - - client.write(f'/pane/{serial}/exec', b'Del\n') - assert str(serial) not in client.list('/pane') - - for tty in [False, True]: - name = 'restore tty space' if tty else 'restore detached space' - with session(binary, root, name, tty=tty) as (old, address): - old.write('/pane/1/body', b'dumped state\n', truncate=True) - control = new_pane(old, b'') - execute_refused(old, control, 'Restore ' + str(root / 'missing dump.zon'), 'no such file') - assert old.read('/pane/1/body') == b'dumped state\n' - execute(old, control, 'Mount own ' + str(address)) - execute(old, control, 'Dump') - saved = root / (name + '.dump.zon') - deadline = time.monotonic() + 5 - while not saved.exists(): - assert time.monotonic() < deadline, 'Dump did not write its state' - time.sleep(.005) - held_body = old.open('/pane/1/body') - held_screen = old.open('/screen') - old_ids = {int(row.split()[0]) for row in old.read('/index').splitlines()} - assert old.read_fid(held_body) == b'dumped state\n' - assert old.read_fid(held_screen, count=1) == b'{' - old.write('/pane/1/body', b'changed after dump\n', truncate=True) - # Unsaved text is asked about once, as Exit asks; the second - # Restore goes ahead, and is answered before the hang-up. - try: - execute(old, control, 'Restore ' + str(saved)) - except OSError as refused: - assert 'Modified' in str(refused), refused - assert old.read('/pane/1/body') == b'changed after dump\n' - # Answered before the hang-up: the write's reply comes; the - # clunk after it may meet the connection already cut. - restore = old.open(f'/pane/{control}/exec', 1) - line = ('Restore ' + str(saved) + '\n').encode() - old.rpc(118, struct.pack('<IQI', restore, 0, len(line)) + line) - try: - old.close(restore) - except (EOFError, OSError): - pass - # Refused while the connections are cut, or cut already. - try: - old.read_fid(held_body) - except (EOFError, OSError): - pass - else: - raise AssertionError('Restore left an old body fid usable') - with Client(address) as restored: - # The new log names its panes, then where they came from. - log = restored.read('/log') - assert b'\nrestore ' + str(saved).encode() + b'\n' in log, log - # Its panes are the dump's, their serials too. - restored_ids = [int(row.split()[0]) for row in restored.read('/index').splitlines()] - assert len(restored_ids) == 2 and set(restored_ids) == old_ids, restored_ids - file_id, control_id = restored_ids - contents = restored.read(f'/pane/{file_id}/body') - assert contents == b'dumped state\n', (contents, saved.read_text(), restored.read('/index')) - assert restored.read(f'/pane/{control_id}/body') == b'', 'commands ran through exec, not the body' - assert restored.screen()['cols'] == (80 if tty else 160) # a detached one is 160x50 - opened = look(restored, '/n/own/os' + str(root / (name + '.txt')), source=control_id) - assert restored.read(f'/pane/{opened}/body') == b'initial\n' - - with session(binary, root, 'forwarding') as (client, address): - child_dir = root / 'child-working-directory' - child_dir.mkdir() - spaced = child_dir / 'space name.txt' - spaced.write_bytes(b'first line\nsecond line\n') - env = os.environ.copy() - # PARDES_PID is what says "you are inside a pardes"; the socket - # and the pane only say how to reach it. Without the pid the child - # starts its own session, so this test used to pass only when the - # runner itself happened to be running inside one. - status = dict(line.split(maxsplit=1) for line in client.read('/status').decode().splitlines()) - env.update(PARDES_9P=str(address), PARDES_PANE='1', PARDES_PID=status['pid']) - env.pop('PARDES_FORWARD_LOOK', None) - for word, expected, selected, reuse in [('space name.txt:2:4', spaced.read_bytes(), [14, 14], False), - ('/n/self/pane/1/body', b'initial\n', None, False), - ('/virtual/pane/1/body:1:2-4', b'initial\n', [1, 4], True)]: - before = {int(row.split()[0]) for row in client.read('/index').splitlines()} - result = subprocess.run([binary, '--tty', word], cwd=child_dir, env=env, - capture_output=True, timeout=3) - assert result.returncode == 0, (word, result.stderr) - after = {int(row.split()[0]) for row in client.read('/index').splitlines()} - if reuse: - assert after == before - else: - opened, = after - before - assert client.read(f'/pane/{opened}/body') == expected - assert client.read('/pane/1/body') == b'initial\n' - if selected is not None: - # The selection is dot; reading it takes no command at all. - actual = list(map(int, client.read(f'/pane/{opened}/dot').split())) - assert actual == selected, (word, actual, selected) - addr = client.open('/pane/1/addr', 2) - limit = client.open('/pane/1/limit', 2) - for fid, text in [(addr, b'#0,#3'), (limit, b'#0,#3'), (addr, b'#0')]: - client.rpc(118, struct.pack('<IQI', fid, 0, len(text)) + text) - try: - for _ in range(8): - result = subprocess.run([binary], cwd=child_dir, env=env, - capture_output=True, timeout=3) - assert result.returncode == 1 and b'already inside pardes' in result.stderr - text = b'/tial/' - try: - client.rpc(118, struct.pack('<IQI', addr, 0, len(text)) + text) - except OSError: - pass - else: - raise AssertionError('no-argument child cleared the parent address limit') - finally: - client.close(addr) - client.close(limit) - client.read('/pane/1/ctl') - - control = new_pane(client, b'') - execute(client, control, 'Dump') - saved = root / 'forwarding.dump.zon' - deadline = time.monotonic() + 3 - while not saved.exists(): - assert time.monotonic() < deadline, 'forwarding fixture Dump did not finish' - time.sleep(.005) - before = client.read('/index') - inherited = {key: env[key] for key in ['PARDES_9P', 'PARDES_PANE', 'PARDES_PID']} - cases = [ - ('mount', ['--mount=peer=' + str(address), '/n/peer/pane/1/body'], None), - ('name', ['--9p=forwarded-name'], 'forwarded-name'), - ('tcp', ['--9p-tcp=tcp!127.0.0.1!0'], None), - ('load', ['-l', str(saved)], None), - ('shells', ['-n', '3'], None), - ('toggle', ['--tty-toggle=q'], None), - ('toggle-arg', ['--tty-toggle', 'q'], None), - ] - if quic: - cases.append(('quic', ['--9p-quic=quic!127.0.0.1!0'], None)) - for name, options, socket_name in cases: - with session(binary, root, 'forward-' + name, tty=True, socket_name=socket_name, - launch=[*([] if name == 'shells' else ['--tty']), *options], - inherited=inherited) as (local, local_address): - assert local_address != address - index = local.read('/index').splitlines() - if name == 'mount': - assert len(index) == 1 and b'/n/peer/' in index[0], index - assert local.read('/pane/1/body') == b'initial\n' - elif name == 'load': - assert local.read('/index') == before - assert local.read('/pane/1/body') == b'initial\n' - else: - # A bare tty launch is a shell plus the empty text pane - # the boot puts under it; --shells=3 is the classic - # three-shell layout and has no scratch. - assert len(index) == (3 if name == 'shells' else 2), index - if name in ['tcp', 'quic']: - assert (name + '!127.0.0.1!').encode() in local.read('/listeners') - assert client.read('/index') == before - assert client.read('/pane/1/body') == b'initial\n' - - # A session that answers takes the file or says why, as acme's B - # does: a pane it has not is its refusal and exit 1, never a - # nested editor drawn inside the pane that asked. - gone = subprocess.run([binary, '--tty', str(spaced)], cwd=root, - env=dict(env, PARDES_PANE='4294967295'), stdin=subprocess.DEVNULL, - stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10) - assert gone.returncode == 1 and gone.stderr.startswith(b'pardes: '), gone - assert client.read('/index') == before - for name, identity, word in [ - ('stale-file', dict(inherited, PARDES_9P=str(root / 'absent.sock')), str(spaced)), - ('stale-missing', dict(inherited, PARDES_9P=str(root / 'absent.sock')), 'missing-child-file.txt'), - ('stale-noarg', dict(inherited, PARDES_9P=str(root / 'absent.sock')), None), - # No pid means "not inside a pardes at all", which is what - # --nested withholds and what a plain shell has. - ('not-nested', {k: v for k, v in inherited.items() if k != 'PARDES_PID'}, str(spaced)), - ]: - with session(binary, root, name, tty=True, inherited=identity, - launch=['--tty', *([word] if word else [])]) as (local, local_address): - assert local_address != address - if word == str(spaced): - assert local.read('/pane/1/body') == spaced.read_bytes() - elif word: - assert word.encode() in local.read('/index') or word in ''.join( - cell[0] for cell in local.screen()['cells']) - assert client.read('/index') == before - - # --wait (acme's E): a launch inside a pane lasts until the pane it - # opened is deleted, as an $EDITOR must; one on a file already open - # waits on that pane; a session that goes away ends it with 1. - with session(binary, root, 'wait') as (client, address): - # A command run from a pane's tag gets acme's $winid, that pane; - # one from the workspace's tag, none. - def command_body(node, line): - client.write(node, line) - serial = int(client.read(node).split()[-1]) - deadline = time.monotonic() + 10 - while True: - log = client.read('/log').decode() - ran = log.rfind(f'\nrun {serial} ') - if ran >= 0 and f'\nexit {serial} ' in log[ran:]: - body = client.read(f'/pane/{serial}/body').decode().split('% echo winid')[-1] - client.remove(f'/pane/{serial}') - return body - assert time.monotonic() < deadline, log[-400:] - time.sleep(.05) - first_pane = min(int(row.split()[0]) for row in client.read('/index').splitlines()) - assert f'winid={first_pane}\n' in command_body(f'/pane/{first_pane}/tagexec', b'echo winid=$winid\n') - assert 'winid=\n' in command_body('/tagexec', b'echo winid=$winid\n') - # A second --detach of a name a live session holds: said, exit 1, - # no Zig error trace. - second_env = {k: v for k, v in os.environ.items() if not k.startswith('PARDES_')} - second_env.update(HOME=str(root), XDG_RUNTIME_DIR=str(root)) - second = subprocess.run([binary, '--detach=wait'], cwd=root, env=second_env, - stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, timeout=20) - assert second.returncode == 1, second - assert second.stderr == b'pardes: a session named wait is already running\n', second.stderr - status = dict(line.split(maxsplit=1) for line in client.read('/status').decode().splitlines()) - env = {k: v for k, v in os.environ.items() if k != 'PARDES_FORWARD_LOOK'} - env.update(PARDES_9P=str(address), PARDES_PANE='1', PARDES_PID=status['pid']) - edited = root / 'wait edit.txt' - edited.write_bytes(b'edit me\n') - - def serials(): - return {int(row.split()[0]) for row in client.read('/index').splitlines()} - - def launch(flag): - before = serials() - child = subprocess.Popen([binary, flag, str(edited)], cwd=root, env=env, - stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) - deadline = time.monotonic() + 3 - while child.poll() is None and not serials() - before and time.monotonic() < deadline: - time.sleep(.01) - return child - - first = launch('--wait') - opened, = serials() - {1} - second = launch('-w') - time.sleep(.5) - assert first.poll() is None and second.poll() is None, (first.stderr.read(), second.stderr.read()) - assert serials() == {1, opened} - client.write(f'/pane/{opened}/body', b'edited\n', truncate=True) - execute(client, opened, 'Save') - client.remove(f'/pane/{opened}') - deleted = time.monotonic() - assert first.wait(timeout=5) == 0 - # Woken by the `del` record, not a poll. - assert time.monotonic() - deleted < .05, time.monotonic() - deleted - assert second.wait(timeout=5) == 0 - assert edited.read_bytes() == b'edited\n' - # --wait from outside the session's panes, $PARDES_9P alone (an - # agent's EDITOR): asked of the session, not a screen of its own. - outside_env = {k: v for k, v in env.items() if k not in ('PARDES_PANE', 'PARDES_PID')} - far = root / 'wait far.txt' - far.write_bytes(b'far\n') - before = serials() - waiting = subprocess.Popen([binary, '--wait', str(far)], cwd=root, env=outside_env, - stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) - deadline = time.monotonic() + 5 - while not serials() - before and time.monotonic() < deadline: - time.sleep(.01) - far_pane, = serials() - before - time.sleep(.3) - assert waiting.poll() is None, waiting.stderr.read() - client.remove(f'/pane/{far_pane}') - assert waiting.wait(timeout=5) == 0 - # --wait with a $PARDES_9P that answers no more: said, exit 1, - # never a screen of its own drawn in the asker's terminal. - dead_env = dict(outside_env, PARDES_9P=str(root / 'pardes-9p-gone.sock')) - gone = subprocess.run([binary, '--wait', str(far)], cwd=root, env=dead_env, capture_output=True, timeout=10) - assert gone.returncode == 1 and b'does not answer' in gone.stderr, gone - # --wait with no FILE says it needs one, and waits on nothing. - bare = subprocess.run([binary, '--wait'], cwd=root, env=env, capture_output=True, timeout=10) - assert bare.returncode == 1 and b'--wait needs a FILE' in bare.stderr, bare - # `--` ends the options: a name starting with a dash is a name. - dash = root / '-dash.txt' - dash.write_bytes(b'dash\n') - before = serials() - assert subprocess.run([binary, '--', dash.name], cwd=root, env=env, capture_output=True, timeout=10).returncode == 0 - dashed, = serials() - before - assert client.read(f'/pane/{dashed}/name').rstrip(b'\n') == str(dash).encode() - client.remove(f'/pane/{dashed}') - # A name under a directory that may not be searched is refused, - # permission denied, exit 1, no pane: its Save could never be. - # (Root searches any directory.) - if os.geteuid() != 0: - (root / 'noperm').mkdir() - (root / 'noperm').chmod(0) - before = serials() - try: - denied = subprocess.run([binary, 'noperm/a/b/c'], cwd=root, env=env, capture_output=True, timeout=10) - finally: - (root / 'noperm').chmod(0o755) - assert denied.returncode == 1 and b'permission denied' in denied.stderr, denied - assert serials() == before - # --wait waits on the pane its look answered, on the look's own - # open: a name /index shows escaped (a backslash is `\\` there) - # is still the pane waited on, not one already gone. - odd = root / 'wait\\odd.txt' - odd.write_bytes(b'odd\n') - before = serials() - waiting = subprocess.Popen([binary, '--wait', str(odd)], cwd=root, env=env, - stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) - deadline = time.monotonic() + 3 - while not serials() - before and time.monotonic() < deadline: - time.sleep(.01) - odd_pane, = serials() - before - time.sleep(.5) - assert waiting.poll() is None, waiting.stderr.read() - client.remove(f'/pane/{odd_pane}') - assert waiting.wait(timeout=5) == 0 - # A name not there yet opens a pane named it, as acme's B does, - # and Save creates it: git's and fish's temporary files are. - fresh = root / 'wait new.txt' - before = serials() - made = subprocess.Popen([binary, '--wait', fresh.name], cwd=root, env=env, - stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) - # The launch makes the pane (pane/new), then names it: wait for - # the name, not the pane. - deadline = time.monotonic() + 3 - while str(fresh).encode() not in client.read('/index'): - assert made.poll() is None, made.stderr.read() - assert time.monotonic() < deadline, 'a new name opened no pane named it' - time.sleep(.01) - named, = serials() - before - client.write(f'/pane/{named}/body', b'new\n') - execute(client, named, 'Save') - client.remove(f'/pane/{named}') - code = made.wait(timeout=5) - assert code == 0, (code, made.stderr.read()) - assert fresh.read_bytes() == b'new\n' - # ...in a directory not there either: forwarded all the same, and - # Save makes the directories. - deep = root / 'wait-missing' / 'sub' / 'new.txt' - before = serials() - made = subprocess.Popen([binary, 'wait-missing/sub/new.txt'], cwd=root, env=env, - stdout=subprocess.DEVNULL, stderr=subprocess.PIPE) - assert made.wait(timeout=5) == 0, made.stderr.read() - deadline = time.monotonic() + 3 - while str(deep).encode() not in client.read('/index'): - assert time.monotonic() < deadline, 'a name in a missing directory opened no pane named it' - time.sleep(.01) - named, = serials() - before - client.write(f'/pane/{named}/body', b'deep\n') - execute(client, named, 'Save') - client.remove(f'/pane/{named}') - assert deep.read_bytes() == b'deep\n' - # `pardes '~/x'`, quoted so the shell left the `~`: home all the - # same: the launching shell's HOME. - before = serials() - quoted = subprocess.run([binary, '~/fwd-tilde.txt'], cwd=root, env=dict(env, HOME=str(root)), - stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, timeout=10) - assert quoted.returncode == 0, quoted.stderr - assert f'{root}/fwd-tilde.txt'.encode() in client.read('/index'), client.read('/index') - for serial in serials() - before: - client.remove(f'/pane/{serial}') - # A name the session refuses: its reason and exit 1, as B's, - # never a nested editor, and no pane left for it. - before = serials() - refused = subprocess.run([binary, 'x' * 300 + '.txt'], cwd=root, env=env, - stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10) - assert refused.returncode == 1, refused - assert b'a component over 255 bytes' in refused.stderr, refused.stderr - assert serials() == before, (serials(), before) - # A pane address the session has not is refused in words, exit - # 1, no pane made for a file of that name; one it has is looked - # at. A name with a newline is said in words, not an error name. - # `pardes -` (a terminal's PAGER): stdin, its escapes stripped, in a - # +Pager pane of the session, clean, and back at once. - fed = subprocess.run([binary, '-'], cwd=root, env=env, input=b'\x1b[1mbold\x1b[0m text\n', - stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10) - assert fed.returncode == 0, fed.stderr - made_now, = serials() - before - assert client.read(f'/pane/{made_now}/name') == f'{root}/+Pager\n'.encode() - assert client.read(f'/pane/{made_now}/body') == b'bold text\n' - row = [r for r in client.read('/index').splitlines() if r.split()[0] == str(made_now).encode()][0] - assert row.split()[2] == b'0', row # clean: nothing to ask at Exit - # Its `new` record names it as it is: no +New and a rename. - log_now = client.read('/log').decode() - assert f'\nnew {made_now} {root}/+Pager\n' in '\n' + log_now, log_now[-400:] - assert f'\nrename {made_now} ' not in '\n' + log_now - # Its colours are the pane's, drawn and never read: the body is - # the plain text, and the screen shows `red` in the theme's red, - # the rest in the pane's ink. Mapped through the theme, as a - # terminal's are; Filter shows them as sent, `rgb` in the colour - # given. - # Each run is its own +Pager; one there already keeps its page. - fed = subprocess.run([binary, '-'], cwd=root, env=env, - input=b'\x1b]0;title\x07\x1b[31mred\x1b[m \x1b[38;2;1;2;3mrgb\x1b[m plain\n', - stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10) - assert fed.returncode == 0, fed.stderr - coloured, = serials() - before - {made_now} - assert client.read(f'/pane/{coloured}/name') == f'{root}/+Pager\n'.encode() - assert client.read(f'/pane/{coloured}/body') == b'red rgb plain\n' - assert client.read(f'/pane/{made_now}/body') == b'bold text\n' - def inks(): - screen = client.screen() - for row in range(screen['rows']): - cells = screen['cells'][row * screen['cols']:(row + 1) * screen['cols']] - at = ''.join(cell[0] for cell in cells).find('red rgb plain') - if at >= 0: - return [screen['styles'][cells[at + i][1]]['fg'] for i in (0, 4, 8)] - raise AssertionError(('the paged text is not on the screen', screen)) - ink = inks() - assert ink[0] != ink[2] and ink[1] != {'rgb': [1, 2, 3]}, ink - client.write(f'/pane/{coloured}/exec', b'Filter\n') - ink = inks() - assert ink[1] == {'rgb': [1, 2, 3]}, ink - assert ink[0] != ink[2] and ink[0] != ink[1], ink - # A progress line keeps its last state. - fed = subprocess.run([binary, '-'], cwd=root, env=env, input=b'again\r\nprogress 10%\rprogress 100%\n', - stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10) - assert fed.returncode == 0, fed.stderr - again, = serials() - before - {made_now, coloured} - assert client.read(f'/pane/{again}/body') == b'again\nprogress 100%\n' - for serial in (made_now, coloured, again): - client.remove(f'/pane/{serial}') - # More than one 8 MiB write's worth arrives whole, byte for byte, - # in its +Pager (each write once replaced the last). - big = b''.join(b'line %08d of the paged text\n' % i for i in range(400000)) # ~11 MiB - assert len(big) > 8 * 1024 * 1024 - fed = subprocess.run([binary, '-'], cwd=root, env=env, input=big, - stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=120) - assert fed.returncode == 0, fed.stderr - big_pane, = serials() - before - assert client.read(f'/pane/{big_pane}/body') == big, 'the paged body is not the text' - # Past the 256 MiB cap: the first 256 MiB, then the cut note. - cap = 256 * 1024 * 1024 - huge = subprocess.Popen(['sh', '-c', f'yes 0123456789abcdef | head -c {cap + 4096}'], stdout=subprocess.PIPE) - fed = subprocess.run([binary, '-'], cwd=root, env=env, stdin=huge.stdout, - stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=600) - huge.wait() - assert fed.returncode == 0, fed.stderr - client.remove(f'/pane/{big_pane}') - big_pane, = serials() - before - got = client.read(f'/pane/{big_pane}/body') - note = b'\n[pardes: the rest of stdin is cut: a pane holds at most a file\'s limit]\n' - assert got.endswith(note) and len(got) == cap + len(note), (len(got), got[-120:]) - assert got[:cap] == (b'0123456789abcdef\n' * (cap // 17 + 1))[:cap] - del got, big - client.remove(f'/pane/{big_pane}') - # From a directory whose name could not be a ctl line: the - # session's directory's +Pager, the text whole, no stray +New. - odd = root / 'two\nlines' - odd.mkdir() - fed = subprocess.run([binary, '-'], cwd=odd, env=env, input=b'odd dir text\n', - stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10) - assert fed.returncode == 0, fed.stderr - odd_pane, = serials() - before - assert client.read(f'/pane/{odd_pane}/name') == f'{root}/+Pager\n'.encode() - assert client.read(f'/pane/{odd_pane}/body') == b'odd dir text\n' - client.remove(f'/pane/{odd_pane}') - # Nothing on stdin: no pane, and done. - fed = subprocess.run([binary, '-'], cwd=root, env=env, input=b'', - stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10) - assert fed.returncode == 0 and serials() == before, (fed, serials()) - # A new name in a directory there but not writable: refused now, - # not a pane that only fails at Save. (Root writes anywhere.) - shut = root / 'shut-dir' - shut.mkdir() - shut.chmod(0o500) - try: - if os.geteuid() != 0: - refused = subprocess.run([binary, 'shut-dir/new.txt'], cwd=root, env=env, - stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10) - assert refused.returncode == 1, refused - assert b'may not be read or written' in refused.stderr, refused.stderr - assert serials() == before, (serials(), before) - finally: - shut.chmod(0o755) - for word, said in (('@p999:1', b'pardes: @p999:1: this session has no such pane'), - ('two\nlines.txt', b'pardes: two\\nlines.txt: a file name is one line')): - refused = subprocess.run([binary, word], cwd=root, env=env, - stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10) - assert refused.returncode == 1, refused - assert said in refused.stderr, refused.stderr - assert b'NotOneLine' not in refused.stderr and b'NoSuchPane' not in refused.stderr - assert serials() == before, (serials(), before) - looked = subprocess.run([binary, '@p1:1'], cwd=root, env=env, - stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=10) - assert looked.returncode == 0, looked.stderr - assert serials() == before, (serials(), before) - orphan = launch('--wait') - assert orphan.poll() is None - assert orphan.wait(timeout=5) == 1 - - # `pardes -` outside a session: an editor whose first pane is a - # +Pager of stdin, as `vim -`; here a detached one, to read it. - outside_env = {k: v for k, v in os.environ.items() if not k.startswith('PARDES_')} - outside_env.update(HOME=str(root), XDG_RUNTIME_DIR=str(root), PARDES_NOTIME='1') - boot = subprocess.Popen([binary, '--detach=stdinboot', '-'], cwd=root, env=outside_env, - stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, - start_new_session=True) - try: - boot.stdin.write(b'\x1b[32mfrom stdin\x1b[m\nsecond\n') - boot.stdin.close() - sock = root / 'pardes-9p-stdinboot.sock' - deadline = time.monotonic() + 10 - while not sock.exists(): - assert boot.poll() is None, boot.stderr.read() - assert time.monotonic() < deadline, 'the stdin boot never listened' - time.sleep(.05) - with Client(str(sock)) as booted: - first = booted.read('/index').split(b'\n')[0].split() - assert first[3] == f'{root}/+Pager'.encode(), first - assert booted.read(f'/pane/{first[0].decode()}/body') == b'from stdin\nsecond\n' - finally: - boot.terminate() - boot.wait(timeout=10) - - # Killed with SIGTERM or SIGHUP, a tty editor puts its terminal - # back (main screen, cooked mode) before it dies: a nested one - # killed in a pane leaves that pane usable. - # ...and its 9P socket goes with it. One a dead editor left behind - # (no process has its pid) is swept at the next start; a live - # pid's, here this test's own, never is. - def unix_socket_file(path): - made = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) - made.bind(str(path)) - made.close() - for sig in (signal.SIGTERM, signal.SIGHUP): - dead_socket = root / 'pardes-9p-2147483647.sock' - live_socket = root / f'pardes-9p-{os.getpid()}.sock' - for path in (dead_socket, live_socket): - if not path.exists(): - unix_socket_file(path) - master, slave = os.openpty() - fcntl.ioctl(slave, termios.TIOCSWINSZ, struct.pack('HHHH', 24, 80, 0, 0)) - cooked = termios.tcgetattr(slave) - env = {k: v for k, v in os.environ.items() if not k.startswith('PARDES_')} - env.update(HOME=str(root), XDG_RUNTIME_DIR=str(root), TERM='xterm-256color', PARDES_NOTIME='1') - existing = set(root.glob('pardes-9p-*.sock')) - killed = subprocess.Popen([binary, '--tty', 'x.txt'], cwd=root, env=env, - stdin=slave, stdout=slave, stderr=subprocess.DEVNULL, - start_new_session=True, - preexec_fn=lambda: fcntl.ioctl(0, termios.TIOCSCTTY, 0)) - seen = b'' - try: - deadline = time.monotonic() + 10 - while b'\x1b[?1049h' not in seen: - assert time.monotonic() < deadline, seen[-200:] - if select.select([master], [], [], .1)[0]: - seen += os.read(master, 65536) - time.sleep(.3) - # The session's own, named by its pid: forked off the - # launched process, which stays as its stub. - own_socket, = set(root.glob('pardes-9p-*.sock')) - existing - assert not dead_socket.exists() - assert live_socket.exists() - killed.send_signal(sig) - assert killed.wait(timeout=5) == -sig - if sig == signal.SIGHUP: - # A hangup is the terminal's end: the session carries on - # detached (the daemon keeps it) until told to end. - assert own_socket.exists(), own_socket - os.kill(int(own_socket.name[len('pardes-9p-'):-len('.sock')]), signal.SIGTERM) - deadline = time.monotonic() + 5 - while own_socket.exists() and time.monotonic() < deadline: - time.sleep(.01) - assert not own_socket.exists(), own_socket - while select.select([master], [], [], .2)[0]: - try: - chunk = os.read(master, 65536) - except OSError: - break - if not chunk: - break - seen += chunk - tail = seen[seen.rindex(b'\x1b[?1049h'):] - assert b'\x1b[?1049l' in tail, tail[-200:] - assert termios.tcgetattr(slave)[3] & termios.ICANON == cooked[3] & termios.ICANON - finally: - if killed.poll() is None: - killed.kill() - killed.wait() - os.close(master) - os.close(slave) - - # A launch with no terminal to draw on (a pty that is no one's - # controlling terminal) says so and exits 1, no error trace. - master, slave = os.openpty() - try: - env = {k: v for k, v in os.environ.items() if not k.startswith('PARDES_')} - env.update(HOME=str(root), XDG_RUNTIME_DIR=str(root), TERM='xterm-256color') - result = subprocess.run([binary, '--tty', 'x.txt'], cwd=root, env=env, stdin=slave, stdout=slave, - stderr=subprocess.PIPE, start_new_session=True, timeout=10) - finally: - os.close(master) - os.close(slave) - assert result.returncode == 1, result - assert b'pardes: no terminal to draw on' in result.stderr and b'.zig:' not in result.stderr, result.stderr - - for tty in [False, True]: - name = 'lsp-tty' if tty else 'lsp-detached' - with session(binary, root, name, tty=tty) as (client, _): - control = new_pane(client, b'') - execute(client, control, 'Lspinfo') - deadline = time.monotonic() + 3 - while True: - matches = [int(row.split()[0]) for row in client.read('/index').splitlines() - if b'+Lsp' in row] - if matches: - break - assert time.monotonic() < deadline, (name, 'Lspinfo did not complete') - time.sleep(.005) - assert len(matches) == 1, matches - result = client.read(f'/pane/{matches[0]}/body') - assert result.startswith(b'backend: lsp-client\n'), result - assert b'protocol servers (lsp-client):' in result, result - assert client.read('/pane/1/body') == b'initial\n' - assert '+Lsp' in ''.join(cell[0] for cell in client.screen()['cells']) - - with session(binary, root, 'highlight') as (client, _): - client.write('/pane/1/name', b'keywords.zig\n') - source = b'// padding\n' * 9999 + b'fn check() void {\n if (true) return;\n}\n' - client.write('/pane/1/body', source, truncate=True) - screen = client.screen() - assert len(screen['cells']) == screen['cols'] * screen['rows'] - found = set() - for row in range(screen['rows']): - cells = screen['cells'][row * screen['cols']:(row + 1) * screen['cols']] - text = ''.join(cell[0] for cell in cells) - for word in ['fn', 'if']: - start = text.find(word + ' ') - if start < 0: - continue - styles = [screen['styles'][cell[1]] for cell in cells[start:start + len(word)]] - # Weight is opt-in (SyntaxBold, off by default); colour is - # not, so the keyword must differ from the text beside it. - plain = screen['styles'][cells[start + len(word)][1]] - assert all(not style['bold'] and style['fg'] == styles[0]['fg'] for style in styles), (word, styles) - assert styles[0]['fg'] != plain['fg'], (word, styles[0], plain) - found.add(word) - assert found == {'fn', 'if'}, screen - - with session(binary, root, 'long-names') as (client, _): - directory = root / ('directory-' + 'd' * 54) - directory.mkdir() - for length in [29, 63, 127, 255]: - name = 'f' * (length - 4) + '.txt' - path = directory / name - path.write_bytes(b'long filename\n') - wire_path = '/os' + str(path) - assert name in client.list('/os' + str(directory)) - assert client.read(wire_path) == b'long filename\n' - client.write(wire_path, b'written through long filename\n', truncate=True) - assert path.read_bytes() == b'written through long filename\n' - opened = look(client, '/n/os' + str(path)) - assert client.read(f'/pane/{opened}/body') == path.read_bytes() - client.remove(f'/pane/{opened}') # a long name's tag takes rows - opened = look(client, '/n/os' + str(directory)) - listing = client.read(f'/pane/{opened}/body') - for path in directory.iterdir(): - assert ('/n/os' + str(path)).encode() in listing - if quic: - quic_test(binary) print(f'9P integration: passed ({time.monotonic() - started:.3f}s)') -def quic_test(binary): - for tty in [False, True]: - for host in ['127.0.0.1', '::1']: - with tempfile.TemporaryDirectory(prefix='pardes-quic-fs-') as directory: - root = Path(directory) - ports = {} - with session(binary, root, 'server', '--9p-tcp=tcp!' + host + '!0', - '--9p-quic=quic!' + host + '!0', tty=tty, network=ports) as (unix, address): - assert set(unix.read('/listeners').decode().splitlines()) == { - 'unix!' + str(address), 'tcp!' + host + '!' + str(ports['TCP']), - 'quic!' + host + '!' + str(ports['QUIC'])} - with Client((host, ports['TCP'])) as tcp: - unix_body = unix.open('/pane/1/body') - tcp_body = tcp.open('/pane/1/body') - with session(binary, root, 'client') as (remote, _): - control = new_pane(remote, b'') - dial = 'quic!' + host + '!' + str(ports['QUIC']) - execute(remote, control, 'Mount peer ' + dial) - opened = look(remote, '/n/peer/pane/1/body', source=control) - body = f'/pane/{opened}/body' - assert remote.read(body) == b'initial\n' - payload = 'saved café λ\n'.encode() + b'x' * (3 * 8192 + 27) + b'\n' - remote.write(body, payload, truncate=True) - remote.write(f'/pane/{opened}/exec', b'Save\n') - assert unix.read_fid(unix_body) == payload[:unix.msize - 11] - assert tcp.read('/pane/1/body') == payload - tcp.write('/pane/1/body', b'TCP to QUIC Get\n', truncate=True) - remote.write(f'/pane/{opened}/ctl', b'get\n') - assert remote.read(body) == b'TCP to QUIC Get\n' - assert unix.read_fid(unix_body) == b'TCP to QUIC Get\n' - assert tcp.read_fid(tcp_body) == b'TCP to QUIC Get\n' - screen_pane = look(remote, '/n/peer/screen', source=control) - screen = json.loads(remote.read(f'/pane/{screen_pane}/body')) - assert len(screen['cells']) == screen['cols'] * screen['rows'] - # The peer's own screen, whole, over QUIC. Its pane's - # tag, not its text: where the view sits after the - # body shrank from a long wrapped line depends on - # whether a frame was drawn meanwhile (sometimes - # on the empty last line, ` 2`, text off-screen). - assert 'server.txt Save' in ''.join(cell[0] for cell in screen['cells']) - large = ('normal λ line\n' * 20000).encode() - fixture = root / 'large.txt' - fixture.write_bytes(large) - large_pane = look(remote, '/n/peer/os' + str(fixture), source=control) - assert remote.read(f'/pane/{large_pane}/body') == large - edited = b'QUIC file saved\n' - remote.write(f'/pane/{large_pane}/body', edited, truncate=True) - remote.write(f'/pane/{large_pane}/exec', b'Save\n') - assert fixture.read_bytes() == edited - for pane in [opened, screen_pane, large_pane]: - remote.write(f'/pane/{pane}/exec', b'Del\n') - execute(remote, control, 'Unmount peer') - roots = look(remote, '/n', source=control) - assert remote.read(f'/pane/{roots}/body') == b'/n/os/\n/n/self/\n' - assert unix.read_fid(unix_body) == b'TCP to QUIC Get\n' - assert tcp.read_fid(tcp_body) == b'TCP to QUIC Get\n' - # Not checked: a session Looking at its OWN tree through - # a QUIC mount ('Mount own quic!...'). QUIC is still - # answered on the editor's thread (cloud9's QUIC adapter - # is not std.Io based), so that Look waits on the loop - # it is running in and opens nothing; features.txt lists - # it under "Left as they are". Unix and TCP self-mounts - # are covered by test/selfmount.py. - unix.close(unix_body) - tcp.close(tcp_body) - if __name__ == '__main__': if len(sys.argv) in [3, 4] and sys.argv[2] == '--discovery': |
