diff options
Diffstat (limited to 'test/real_zls.py')
| -rw-r--r-- | test/real_zls.py | 168 |
1 files changed, 168 insertions, 0 deletions
diff --git a/test/real_zls.py b/test/real_zls.py new file mode 100644 index 00000000..74f7cd04 --- /dev/null +++ b/test/real_zls.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python3 +"""Real ZLS through Pardes: desktop PATH, imports, hover, references and editing. + +python3 -B test/real_zls.py GUI_BINARY ZLS_BINARY OUTPUT_DIRECTORY +Uses a private HOME and a project with spaces in its path; needs Zig on PATH. +""" +import argparse +import json +import os +from pathlib import Path +import shutil +import struct +import subprocess +from ninep import Client +from gpu_snake import wait +from daemon import processes + +parser = argparse.ArgumentParser(description=__doc__) +for arg in ['binary', 'zls', 'output']: + parser.add_argument(arg, type=Path) +args = parser.parse_args() +root = args.output.resolve() +root.mkdir(parents=True, mode=0o700) +(root / '.local/bin').mkdir(parents=True) +(root / '.local/bin/zls').symlink_to(args.zls.resolve()) +(root / 'desktop-bin').mkdir() +zig = shutil.which('zig') +assert zig, 'Zig must be on PATH' +(root / 'desktop-bin/zig').symlink_to(Path(zig).resolve()) +(root / 'config/pardes').mkdir(parents=True) +(root / 'config/pardes/init').write_text('Shell /bin/bash\nMessageLinger 0\n') +project = root / 'project with spaces' +project.mkdir() +(project / 'build.zig').write_text('''const std = @import("std"); +pub fn build(b: *std.Build) void { + const target = b.standardTargetOptions(.{}); + const helper = b.addModule("helper", .{ .root_source_file = b.path("helper.zig"), .target = target }); + const exe = b.addExecutable(.{ .name = "fixture", .root_module = b.createModule(.{ + .root_source_file = b.path("main.zig"), .target = target, + .imports = &.{.{ .name = "helper", .module = helper }}, + }) }); + b.installArtifact(exe); +} +''') +(project / 'helper.zig').write_text('''/// Double a number. +pub fn double(x: u32) u32 { return x * 2; } +pub fn triple(x: u32) u32 { return x * 3; } +''') +(project / 'local file.zig').write_text('pub const answer: u32 = 42;\n') +source = '''const std = @import("std"); +const local = @import("local file.zig"); +const helper = @import("helper"); +pub fn main() void { + _ = std.mem.len("hello"); + _ = local.answer; + _ = helper.double(21); +} +pub fn broken() void { const unused_local: u32 = 7; } +pub fn broken_too() void { const unused_other: u32 = 8; } +''' +(project / 'main.zig').write_text(source) +env = {k: v for k, v in os.environ.items() if not k.startswith('PARDES_')} +env.pop('XDG_DATA_HOME', None) +env.update(HOME=str(root), PATH=str(root / 'desktop-bin') + ':/usr/bin:/bin', XDG_RUNTIME_DIR=str(root), + XDG_CONFIG_HOME=str(root / 'config'), XDG_CACHE_HOME=str(root / 'cache'), + PARDES_TEST='1', PARDES_TEST_ROWS='60', PARDES_TEST_CAPTURE_DIR=str(root), SDL_VIDEO_DRIVER='offscreen', PARDES_DAEMON='on') +log = (root / 'stderr.log').open('w') +p = subprocess.Popen([str(args.binary.resolve()), '--9p=zls', str(project / 'main.zig')], + env=env, cwd=project, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, + stderr=log, start_new_session=True) +c = None +passed = [] +try: + sock = root / '9p/pardes/zls' + wait(lambda: sock.exists() or p.poll() is not None) + assert p.poll() is None, (root / 'stderr.log').read_text() + c = Client(sock, timeout=40) + main = int(next(line.split()[0] for line in c.read('/index').splitlines() if b'/main.zig ' in line)) + + def execute(word): + fid = c.open(f'/pane/{main}/exec', 2) + try: + data = (word + '\n').encode() + c.rpc(118, struct.pack('<IQI', fid, 0, len(data)) + data) + answer = c.read_fid(fid).decode().strip() + return c.read('/pane/' + answer + '/body').decode() if answer.isdigit() else '' + finally: + c.close(fid) + + def at(needle, offset=0, text=source): + pos = text.index(needle) + offset + c.write('/focus', str(main).encode()) + c.write(f'/pane/{main}/addr', f'#{pos},#{pos}'.encode()) + c.write(f'/pane/{main}/ctl', b'dot=addr\n') + + def result(suffix, contains=''): + for line in reversed(c.read('/index').splitlines()): + if ('/' + suffix + ' ').encode() in line: + body = c.read('/pane/' + line.split()[0].decode() + '/body').decode() + if contains in body: + return body + return '' + + def definition(needle, offset, ending): + at(needle, offset) + execute('Def') + focus = c.read('/focus').decode().strip() + target = c.read('/pane/' + focus + '/name').decode().strip() + assert target.endswith(ending), (needle, target) + passed.append('definition: ' + needle) + + definition('@import("helper")', 11, '/helper.zig') + definition('@import("std")', 10, '/std/std.zig') + definition('@import("local file.zig")', 12, '/local file.zig') + definition('helper.double', 9, '/helper.zig') + definition('std.mem.len', 9, '/std/mem.zig') + at('helper.double', 9) + hover = execute('Hover') + assert 'Double a number' in hover, hover + passed.append('hover: imported function documentation') + refs = execute('Refs') + assert 'main.zig:' in refs and 'helper.zig:' in refs, refs + passed.append('references: across imported files') + at('local.answer', 8) + rename = execute('Rename renamed_answer') + assert 'renamed_answer' in rename and 'local file.zig:' in rename and 'main.zig:' in rename, rename + passed.append('rename: cross-file preview without changing disk files') + + def diagnostics(): + execute('Diagnostics') + rows = result('+Search', 'Diagnostics ') + return 'main.zig:9:' in rows and 'main.zig:10:' in rows + wait(diagnostics, 20) + passed.append('diagnostics: unused local constant') + partial = source.replace('helper.double(21)', 'helper.') + c.write(f'/pane/{main}/body', partial.encode(), truncate=True) + assert c.read(f'/pane/{main}/body').decode() == partial + at('helper.;', len('helper.'), partial) + c.write('/kbdin', b'i\t') + wait(lambda: 'triple' in result('+Search', 'double')) + passed.append('completion: imported module members in unsaved text') + c.write('/kbdin', b'\x1b') + c.write(f'/pane/{main}/body', source.encode(), truncate=True) + info = execute('Lspinfo') + assert 'zls ready' in info and str(project) in info, info + assert (project / 'main.zig').read_text() == source + assert (project / 'local file.zig').read_text() == 'pub const answer: u32 = 42;\n' + (root / 'lspinfo.txt').write_text(info) + (root / 'result.json').write_text(json.dumps(passed, indent=2) + '\n') + print('PASS:', '; '.join(passed), flush=True) +except Exception: + if c: + print(c.read('/log').decode()[-4000:], flush=True) + print('checks passed:', passed, '\nlast results:', result('+Search'), flush=True) + raise +finally: + if c: + c.socket.close() + if p.poll() is None: + p.kill() + p.wait(timeout=10) + for pid in processes(root): + try: + os.kill(pid, 9) + except ProcessLookupError: + pass + p.stdin.close() + log.close() |
