diff options
| author | user <user@clank> | 2026-08-15 16:22:27 +0200 |
|---|---|---|
| committer | user <user@clank> | 2026-08-15 16:22:27 +0200 |
| commit | 95b9b0414a2a8a3b1ec8e91d530c8b7119901257 (patch) | |
| tree | 8c8929534ab94f1a3c53de41ea108216d69b66bc /tests | |
| parent | sl0ppty: C-a e opens the config, in a pane, in your editor (diff) | |
| download | slosh-95b9b0414a2a8a3b1ec8e91d530c8b7119901257.tar.gz slosh-95b9b0414a2a8a3b1ec8e91d530c8b7119901257.tar.xz slosh-95b9b0414a2a8a3b1ec8e91d530c8b7119901257.zip | |
sl0ppty: render the config instead of shipping a copy of it
`C-a e` on a machine with no config opened an empty buffer, which tells you
nothing about what you can set. The obvious fix -- embed config/config.kdl and
write it out -- trades one problem for a worse one: a checked-in copy of the
defaults is a second source of truth, and it drifts. Ours already had. Four
theme colours existed in the code and not in that file, and nothing said so.
So the file is generated. `sl0ppty --dump-config` renders every setting with
the value it currently holds -- geometry, behaviour, all 48 theme colours, the
state shader chains, and every binding including the leader-less ones -- and
`C-a e` seeds a missing config with exactly that, creating the directories on
the way (a fresh container has no ~/.config either).
config/config.kdl stays, and is now explicitly the *prose*: what a setting is
for, what it cost to get right, which ones are opinions. Values are generated,
essays are written, and the file says so at the top.
Three things keep the two honest:
- the theme colours became one file-scope table that the parser and the
renderer both walk. 48 names that used to exist in one place and be typed
again in another now exist once.
- the dump must parse back to itself byte-for-byte. That is what caught
`bind \ split-cols` rendering unquoted -- valid-looking output that would
not have survived being read back.
- a test scans config.c for `kdl_child(root, "...")`, which is the parser's
own vocabulary, and fails if a key is missing from the dump. Add a knob,
forget to render it, and the suite tells you rather than a user.
It never overwrites a config that exists: fopen(..., "wx") loses the race
rather than an edit, and there is a test for that too, because it is the one
thing here that must never go wrong.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test_dump_config.py | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/tests/test_dump_config.py b/tests/test_dump_config.py new file mode 100644 index 0000000..8d9d580 --- /dev/null +++ b/tests/test_dump_config.py @@ -0,0 +1,138 @@ +#!/usr/bin/env python3 +"""`sl0ppty --dump-config`: every setting, with the value it currently has. + +Generated from the code rather than kept as a file on disk, because a +checked-in copy of the defaults is a second source of truth and drifts — ours +had already lost four theme colours before anyone noticed. + +Two properties make it worth trusting, and both are cheap to check: what it +writes must parse back to exactly what it wrote, and every key the parser +knows must appear in it. The second is the one that catches "added a knob, +forgot to render it", which is the failure this whole approach exists to +prevent. +""" +import os +import re +import subprocess +import sys +import tempfile + +from harness import BIN, Session, check, report + +HERE = os.path.dirname(os.path.abspath(__file__)) +SRC = os.path.join(HERE, "..", "src", "config.c") + + +def dump(config=None): + env = dict(os.environ) + env["SL0PPTY_CONFIG"] = config or "/nonexistent/sl0ppty.kdl" + return subprocess.run([BIN, "--dump-config"], capture_output=True, + text=True, env=env).stdout + + +def test_it_dumps_something_that_looks_like_a_config(): + text = dump() + check("it has content", len(text.splitlines()) > 100, str(len(text))) + for expect in ("gap ", "theme {", "states {", "keys {", "prefix "): + check(f"it has {expect!r}", expect in text, text[:200]) + + +def test_it_parses_back_to_exactly_itself(): + """The round trip: dump, load that, dump again. If the two differ then one + of them is losing something, and this is the only way to find out which + without reading 180 lines every time.""" + one = dump() + f = tempfile.NamedTemporaryFile("w", suffix=".kdl", delete=False) + f.write(one) + f.close() + two = dump(f.name) + check("a dump of a dump is the same dump", one == two, + "\n".join(l for l in __import__("difflib").unified_diff( + one.splitlines(), two.splitlines(), "first", "second", lineterm=""))[:1500]) + os.unlink(f.name) + + +def test_every_key_the_parser_knows_is_in_it(): + """The drift check. `kdl_child(root, "x")` is how config.c reads a + top-level setting, so that list is the parser's own vocabulary.""" + src = open(SRC).read() + keys = set(re.findall(r'kdl_child\(root, "([a-z_]+)"\)', src)) + # These are containers whose contents are rendered, not scalars. + keys -= {"keys"} + text = dump() + missing = sorted(k for k in keys + if not re.search(r'^(// )?%s[ {"]' % re.escape(k), text, re.M)) + check(f"all {len(keys)} settings are rendered", not missing, + "missing: " + ", ".join(missing)) + + +def test_every_theme_colour_is_in_it(): + src = open(SRC).read() + colours = set(re.findall(r'\{"([a-z_]+)", offsetof\(config_t', src)) + text = dump() + missing = sorted(c for c in colours if f" {c} " not in text) + check(f"all {len(colours)} colours are rendered", not missing, + "missing: " + ", ".join(missing)) + check("and there are plenty of them", len(colours) > 40, str(len(colours))) + + +def test_the_dump_is_a_working_config(): + """Not just parseable: a session started with it behaves like one started + with nothing, because that is what "these are the defaults" means.""" + f = tempfile.NamedTemporaryFile("w", suffix=".kdl", delete=False) + f.write(dump()) + f.close() + sh = ["/bin/sh", "-c", 'printf "\\033]2;p\\007"; read x'] + with Session(sh, cols=70, rows=14, config=f.name) as s: + s.settle(20) + with_dump = s.snapshot().screen() + with Session(sh, cols=70, rows=14) as s: + s.settle(20) + with_none = s.snapshot().screen() + check("a session run from the dump looks like one run from nothing", + with_dump == with_none, + "\n--- from the dump\n%s\n--- from nothing\n%s" % (with_dump, with_none)) + os.unlink(f.name) + + +def test_edit_config_writes_it_when_there_is_none(): + """The reason it exists: C-a e on a fresh install opened an empty buffer, + which tells you nothing about what you can set.""" + home = tempfile.mkdtemp(prefix="sl0ppty-home-") + sh = ["/bin/sh", "-c", 'printf "\\033]2;p\\007"; read x'] + # No config, and no ~/.config either: a fresh container has neither. + with Session(sh, cols=74, rows=24, env={"HOME": home, "EDITOR": "tail -f"}, + config=os.path.join(home, ".config", "sl0ppty", "config.kdl")) as s: + s.settle() + s.key("e") + # `tail -f` shows the *end* of the file, so wait for something that is + # actually on screen rather than for the first thing in the config. + snap = s.until_text("select-tab-9") + check("the editor pane has the config in it", + "select-tab-9" in snap.screen(), snap.screen()[-400:]) + + path = os.path.join(home, ".config", "sl0ppty", "config.kdl") + check("and it was written, directories and all", os.path.exists(path), path) + if os.path.exists(path): + check("with the real defaults in it", "theme {" in open(path).read(), + open(path).read()[:200]) + + +def test_it_never_overwrites_a_config_you_already_have(): + home = tempfile.mkdtemp(prefix="sl0ppty-home-") + path = os.path.join(home, "config.kdl") + open(path, "w").write("// mine\ngap 3\n") + sh = ["/bin/sh", "-c", 'printf "\\033]2;p\\007"; read x'] + with Session(sh, cols=74, rows=24, config=path, + env={"HOME": home, "EDITOR": "tail -f"}) as s: + s.settle() + s.key("e") + s.settle(60) + check("the file is untouched", open(path).read() == "// mine\ngap 3\n", + open(path).read()) + + +for name, fn in sorted(list(globals().items())): + if name.startswith("test_"): + fn() +sys.exit(report()) |
