#!/usr/bin/env python3 import contextlib import fcntl import json import os import shutil import select import signal import socket from pathlib import Path import subprocess import struct import sys import tempfile import termios import threading import time from ninep import Client @contextlib.contextmanager def session(binary, root, name, *options, socket_name=None, file=None, tty=False, network=None, launch=None, inherited=None, terminal=None): # No pager of the caller's (a session's own PAGER, SYSTEMD_PAGER...): # its shells get the session's defaults, the ones under test. env = {key: value for key, value in os.environ.items() if not key.startswith('PARDES_') and 'PAGER' not in key} env.update(HOME=str(root), XDG_RUNTIME_DIR=str(root), XDG_CONFIG_HOME=str(root / 'config'), PARDES_NOTIME='1', PARDES_DUMP=str(root / (name + '.dump.zon'))) env.update(inherited or {}) for language in ['RS', 'C', 'GO', 'TS', 'PY']: env['PARDES_LSP_' + language] = '' fixture = root / (name + '.txt') fixture.write_bytes(b'initial\n') address = root / ('pardes-9p-' + (socket_name or name) + '.sock') child = None master = slave = None reader = None try: args = [binary, '--detach=' + name, *options, str(file or fixture)] preexec = None if tty: master, slave = os.openpty() fcntl.ioctl(slave, termios.TIOCSWINSZ, struct.pack('HHHH', 24, 80, 0, 0)) env['TERM'] = 'xterm-256color' args = [binary, '--tty', '--9p=' + (socket_name or name), *options, str(file or fixture)] preexec = lambda: fcntl.ioctl(0, termios.TIOCSCTTY, 0) if launch is not None: args = [binary, *launch] existing = set(root.glob('pardes-9p-*.sock')) child = subprocess.Popen(args, cwd=root, env=env, stdin=slave, stdout=slave if tty else subprocess.DEVNULL, stderr=subprocess.PIPE, start_new_session=True, preexec_fn=preexec) if terminal is not None: # Borrowed handles for input-driving probes, valid inside this context. terminal.update(pid=child.pid, input_fd=master) if launch is not None and socket_name is None: # Named by the session's pid: a terminal's session is forked # off the process launched, which stays as its stub. address = None if slave is not None: os.close(slave) slave = None def drain(): try: while os.read(master, 65536): pass except OSError: pass reader = threading.Thread(target=drain, daemon=True) reader.start() deadline = time.monotonic() + 10 while address is None or not address.exists(): if address is None: address = next(iter(set(root.glob('pardes-9p-*.sock')) - existing), None) if address is not None: continue if child.poll() is not None: raise AssertionError((args, child.returncode, child.stderr.read().decode())) if time.monotonic() > deadline: raise TimeoutError('default 9P socket did not appear') time.sleep(.005) with Client(address) as client: if network is not None: for dial in client.read('/listeners').decode().splitlines(): parts = dial.split('!') if parts[0] in ['tcp', 'quic']: assert len(parts) == 3 and 0 < int(parts[2]) < 65536, dial network[parts[0].upper()] = int(parts[2]) assert set(network) == {'TCP', 'QUIC'}, network yield client, address finally: if terminal is not None: terminal.clear() if child is not None: if child.poll() is None: child.terminate() try: child.wait(timeout=5) except subprocess.TimeoutExpired: child.kill() child.wait() child.stderr.close() if slave is not None: os.close(slave) if master is not None: os.close(master) if reader is not None: reader.join(timeout=1) assert not reader.is_alive(), 'terminal reader did not stop' def new_pane(client, contents): """An open of /pane/new makes a pane; the read names it.""" serial = int(client.read('/pane/new')) if contents: client.write(f'/pane/{serial}/body', contents) return serial def look(client, path, source=None): """A right click on `path` from a pane (a fresh scratch by default).""" serial = source if source is not None else new_pane(client, b'') client.write(f'/pane/{serial}/look', f'{path}\n'.encode()) results = [int(row) for row in client.read('/look').split()] assert len(results) == 1 and results[0] != serial, (path, results) # The scratch it looked from goes: a column holds only so many panes, # each its tag and two rows. if source is None: client.remove(f'/pane/{serial}') return results[0] def execute(client, serial, command): """A middle click on `command` from a pane.""" client.write(f'/pane/{serial}/exec', f'{command}\n'.encode()) def execute_refused(client, serial, command, words): """A middle click on a builtin that fails: the write fails, saying why.""" try: execute(client, serial, command) except OSError as why: assert words in str(why), (command, why) return raise AssertionError(f'{command!r} was taken') 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('= 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('>>'), (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 test(binary, quic=False): started = time.monotonic() workflow_canary(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('= 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) not in [2, 3] or (len(sys.argv) == 3 and sys.argv[2] != '--quic'): raise SystemExit('usage: fs.py [--quic]') test(str(Path(sys.argv[1]).resolve()), quic=len(sys.argv) == 3)