summaryrefslogtreecommitdiff
path: root/test/hxcases/hx_indent.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/hxcases/hx_indent.py')
-rw-r--r--test/hxcases/hx_indent.py89
1 files changed, 89 insertions, 0 deletions
diff --git a/test/hxcases/hx_indent.py b/test/hxcases/hx_indent.py
new file mode 100644
index 00000000..c85c9417
--- /dev/null
+++ b/test/hxcases/hx_indent.py
@@ -0,0 +1,89 @@
+#!/usr/bin/env python3
+"""The goldens of indent.jsonl from helix itself, in a pty: $HX (else `hx`
+on PATH) with its grammars and queries ($HELIX_RUNTIME) and its defaults
+(auto-pairs on), LSP and auto-format off. They were made by the reference
+helix (docs/helix-keys.md), 278b24389's `hx` built from the genizah checkout
+with HELIX_RUNTIME its runtime/. Each case's file is named by its "path" (its
+extension picks the language) and holds its "text"; the keys are typed, then
+a marker where the cursor is, then `:wq!`. The marker's place is the cursor,
+and the anchor: every case ends in insert mode with one cursor no wider than
+helix's block (no `a` or `c` over a selection, whose anchor stays behind).
+
+ HX=... HELIX_RUNTIME=... python3 -B test/hxcases/hx_indent.py \
+ test/hxcases/indent.jsonl > test/hxcases/indent-goldens.jsonl
+"""
+import json, os, pty, select, struct, sys, tempfile, time, fcntl, termios
+
+MARK = "ยง"
+KEYS = {"ret": "\r", "esc": "\x1b", "tab": "\t", "backspace": "\x7f", "lt": "<", "gt": ">"}
+
+
+def keys(spec):
+ out, i = [], 0
+ while i < len(spec):
+ if spec[i] == "<":
+ j = spec.index(">", i)
+ out.append(KEYS[spec[i + 1 : j]])
+ i = j + 1
+ else:
+ out.append(spec[i])
+ i += 1
+ return out
+
+
+def drain(fd, secs):
+ end = time.time() + secs
+ while time.time() < end:
+ r, _, _ = select.select([fd], [], [], 0.02)
+ if r:
+ try:
+ os.read(fd, 65536)
+ except OSError:
+ return
+
+
+def run(case, home):
+ path = os.path.join(home, os.path.basename(case["path"]))
+ with open(path, "w") as f:
+ f.write(case["text"])
+ cfg = os.path.join(home, "config.toml")
+ with open(cfg, "w") as f:
+ f.write("[editor]\nauto-format = false\n[editor.lsp]\nenable = false\n")
+ pid, fd = pty.fork()
+ if pid == 0:
+ for v in ("CONFIG", "CACHE", "DATA", "STATE"):
+ os.environ["XDG_%s_HOME" % v] = home
+ os.environ["TERM"] = "xterm-256color"
+ hx = os.environ.get("HX", "hx")
+ os.execvp(hx, [hx, "-c", cfg, path])
+ fcntl.ioctl(fd, termios.TIOCSWINSZ, struct.pack("HHHH", 24, 80, 0, 0))
+ drain(fd, 1.0)
+ for k in keys(case["keys"]) + [MARK, "\x1b"]:
+ os.write(fd, k.encode())
+ drain(fd, 0.25 if k == "\x1b" else 0.05)
+ for k in ":wq!\r":
+ os.write(fd, k.encode())
+ drain(fd, 0.05)
+ for _ in range(100):
+ done, _ = os.waitpid(pid, os.WNOHANG)
+ if done:
+ break
+ drain(fd, 0.05)
+ else:
+ os.kill(pid, 9)
+ os.waitpid(pid, 0)
+ sys.exit("hx did not quit on case " + case["name"])
+ os.close(fd)
+ with open(path) as f:
+ text = f.read()
+ at = text.index(MARK)
+ row = text.count("\n", 0, at)
+ col = len(text[text.rfind("\n", 0, at) + 1 : at].encode())
+ cursor = {"row": row, "col": col}
+ return {"name": case["name"], "text": text.replace(MARK, "", 1), "mode": "insert", "cursor": cursor, "anchor": cursor}
+
+
+with tempfile.TemporaryDirectory() as home:
+ for line in open(sys.argv[1]):
+ if line.strip():
+ print(json.dumps(run(json.loads(line), home), separators=(",", ":"), ensure_ascii=False))