#!/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('