feat: add i3-style session-specific autostart support and documentation

This commit is contained in:
Luxferre committed 2026-11-26 11:55:17 +02:00
1 parent f73040a0be
commit 8d6236a63f
13 files changed
+1156 -8

No files matched your search

+477
View File
@@ -0,0 +1,477 @@
#!/usr/bin/env python3
"""Integration and headless Xvfb tests for ANODE i3-style session autostart."""
import ctypes
import ctypes.util
import os
import shutil
import signal
import subprocess
import sys
import tempfile
import time
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
AUTOSTART = os.path.join(ROOT, "scripts", "anode-autostart")
DWM_BIN = os.path.join(ROOT, "build", "dwm", "dwm")
def wait_until(pred, timeout=5.0, interval=0.05):
end = time.monotonic() + timeout
while time.monotonic() < end:
res = pred()
if res:
return res
time.sleep(interval)
return None
def make_base_env(tmp):
home = os.path.join(tmp, "home")
xdg_home = os.path.join(tmp, "xdg_home")
xdg_sys = os.path.join(tmp, "xdg_sys")
sys_prefix = os.path.join(tmp, "sys_prefix")
bin_dir = os.path.join(tmp, "bin")
for d in (
home,
os.path.join(xdg_home, "anode", "autostart.d"),
os.path.join(xdg_home, "autostart"),
os.path.join(xdg_sys, "autostart"),
os.path.join(sys_prefix, "share", "anode", "autostart.d"),
bin_dir,
):
os.makedirs(d, exist_ok=True)
env = dict(os.environ)
env["HOME"] = home
env["XDG_CONFIG_HOME"] = xdg_home
env["XDG_CONFIG_DIRS"] = xdg_sys
env["ANODE_PREFIX"] = sys_prefix
env["XDG_CURRENT_DESKTOP"] = "ANODE"
env["ANODE_XDG_AUTOSTART"] = "1"
env.pop("ANODE_SESSION_PROFILE", None)
env["PATH"] = bin_dir + ":" + os.path.join(ROOT, "scripts") + ":" + env.get("PATH", "/usr/bin:/bin")
return env, home, xdg_home, xdg_sys, sys_prefix, bin_dir
def run_autostart(args, env):
return subprocess.run([AUTOSTART] + args, env=env, capture_output=True, text=True)
def write_text(path, content, mode=0o644):
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w", encoding="utf-8") as f:
f.write(content)
os.chmod(path, mode)
def compile_sleep_dummy(out_path):
cc = os.environ.get("CC", "cc")
c_src = out_path + ".c"
write_text(
c_src,
"#include <stdio.h>\n"
"#include <unistd.h>\n"
"int main(int argc, char **argv) {\n"
" if (argc > 1) {\n"
" FILE *f = fopen(argv[1], \"a\");\n"
" if (f) { fprintf(f, \"%ld\\n\", (long)getpid()); fclose(f); }\n"
" }\n"
" sleep(30);\n"
" return 0;\n"
"}\n",
)
subprocess.run([cc, "-O2", c_src, "-o", out_path], check=True)
os.remove(c_src)
def kill_pids_from_file(path):
if not os.path.exists(path):
return
with open(path, "r", encoding="utf-8") as f:
for line in f:
s = line.strip()
if s.isdigit():
try:
os.kill(int(s), signal.SIGTERM)
except ProcessLookupError:
pass
def test_directives_includes_once_and_reload():
with tempfile.TemporaryDirectory() as tmp:
env, home, xdg_home, xdg_sys, _, bin_dir = make_base_env(tmp)
conf_dir = os.path.join(xdg_home, "anode")
markers = os.path.join(tmp, "markers")
os.makedirs(markers, exist_ok=True)
once_bin1 = os.path.join(bin_dir, "anode_once1")
once_bin2 = os.path.join(bin_dir, "anode_once2")
compile_sleep_dummy(once_bin1)
shutil.copy2(once_bin1, once_bin2)
pre_pid_file = os.path.join(markers, "pre_once1.pid")
m_exec = os.path.join(markers, "exec.txt")
m_nosid = os.path.join(markers, "nosid.txt")
m_once_skip = os.path.join(markers, "once_skip.pid")
m_once_run = os.path.join(markers, "once_run.pid")
m_always = os.path.join(markers, "always.txt")
m_rel = os.path.join(markers, "rel.txt")
m_home = os.path.join(markers, "home.txt")
m_cyc = os.path.join(markers, "cyc.txt")
m_dropin = os.path.join(markers, "dropin.txt")
m_xdg = os.path.join(markers, "xdg.txt")
pre_proc = subprocess.Popen([once_bin1, pre_pid_file])
try:
assert wait_until(lambda: os.path.exists(pre_pid_file), timeout=3.0), "once_bin1 failed to start"
write_text(os.path.join(conf_dir, "sub", "rel.conf"), f"exec printf 'rel\\n' >> '{m_rel}'\n")
write_text(os.path.join(home, "inc_home.conf"), f"exec printf 'home\\n' >> '{m_home}'\n")
write_text(
os.path.join(conf_dir, "cyc_a.conf"),
f"include cyc_b.conf\nexec printf 'cyc\\n' >> '{m_cyc}'\n",
)
write_text(os.path.join(conf_dir, "cyc_b.conf"), "include cyc_a.conf\n")
write_text(
os.path.join(conf_dir, "autostart"),
"# Leading comment\n\n"
f"exec printf 'e1\\n' >> '{m_exec}'\n"
f"exec --no-startup-id printf 'e2\\n' >> '{m_nosid}'\n"
f"exec_once anode_once1 '{m_once_skip}'\n"
f"exec_once anode_once2 '{m_once_run}'\n"
f"exec_always printf 'al\\n' >> '{m_always}'\n"
"include sub/rel.conf\n"
"include ~/inc_home.conf\n"
"include cyc_a.conf\n",
)
write_text(os.path.join(conf_dir, "autostart.d", "10-mark.sh"), f"#!/bin/sh\nprintf 'd\\n' >> '{m_dropin}'\n", 0o755)
write_text(
os.path.join(xdg_sys, "autostart", "xtest.desktop"),
f"[Desktop Entry]\nType=Application\nOnlyShowIn=ANODE;\nExec=sh -c \"printf 'x\\n' >> '{m_xdg}'\"\n",
)
chk = run_autostart(["check"], env)
assert chk.returncode == 0, f"check failed: {chk.stderr}"
lst = run_autostart(["list"], env)
assert lst.returncode == 0, f"list failed: {lst.stderr}"
for expected in ("[exec]", "[exec_once]", "[exec_always]", "[script]", "[desktop]"):
assert expected in lst.stdout, f"missing {expected} in list output:\n{lst.stdout}"
assert not os.path.exists(m_exec), "list must not execute commands"
dry = run_autostart(["start", "--dry-run"], env)
assert dry.returncode == 0
assert "[exec_always]" in dry.stdout
assert not os.path.exists(m_exec), "--dry-run must not execute commands"
st = run_autostart(["start"], env)
assert st.returncode == 0, f"start failed: {st.stderr}"
for p in (m_exec, m_nosid, m_once_run, m_always, m_rel, m_home, m_cyc, m_dropin, m_xdg):
assert wait_until(lambda path=p: os.path.exists(path), timeout=4.0), f"expected marker {p}"
time.sleep(0.35)
assert not os.path.exists(m_once_skip), "exec_once ran even though anode_once1 was already running"
# Verify reload runs ONLY exec_always
for p in (m_exec, m_nosid, m_always, m_rel, m_home, m_cyc, m_dropin, m_xdg):
os.remove(p)
rl = run_autostart(["reload"], env)
assert rl.returncode == 0, f"reload failed: {rl.stderr}"
assert wait_until(lambda: os.path.exists(m_always), timeout=4.0), "reload did not run exec_always"
time.sleep(0.35)
for p in (m_exec, m_nosid, m_rel, m_home, m_cyc, m_dropin, m_xdg):
assert not os.path.exists(p), f"reload must not execute {p}"
finally:
if pre_proc.poll() is None:
pre_proc.terminate()
pre_proc.wait(timeout=2)
kill_pids_from_file(m_once_run)
kill_pids_from_file(m_once_skip)
def test_profiles():
with tempfile.TemporaryDirectory() as tmp:
env, _, xdg_home, _, _, _ = make_base_env(tmp)
conf_dir = os.path.join(xdg_home, "anode")
m_base = os.path.join(tmp, "base.txt")
m_work = os.path.join(tmp, "work.txt")
write_text(os.path.join(conf_dir, "autostart"), f"exec printf 'b\\n' >> '{m_base}'\n")
write_text(os.path.join(conf_dir, "autostart.work"), f"exec printf 'w\\n' >> '{m_work}'\n")
for args, extra_env in (
(["--profile", "work"], {}),
(["--profile=work"], {}),
([], {"ANODE_SESSION_PROFILE": "work"}),
):
for p in (m_base, m_work):
if os.path.exists(p):
os.remove(p)
cur_env = dict(env)
cur_env.update(extra_env)
res = run_autostart(args, cur_env)
assert res.returncode == 0, f"profile run failed: {res.stderr}"
assert wait_until(lambda: os.path.exists(m_base) and os.path.exists(m_work), timeout=4.0), (
f"profile markers missing for args={args}, extra_env={extra_env}"
)
def test_dropins_desktop_filters_and_xdg_masking():
with tempfile.TemporaryDirectory() as tmp:
env, _, xdg_home, xdg_sys, sys_prefix, bin_dir = make_base_env(tmp)
conf_dir = os.path.join(xdg_home, "anode")
markers = os.path.join(tmp, "markers")
os.makedirs(markers, exist_ok=True)
m_override = os.path.join(markers, "override.txt")
m_nonexec = os.path.join(markers, "nonexec.txt")
m_bad = os.path.join(markers, "bad.txt")
m_term = os.path.join(markers, "term.txt")
m_clean = os.path.join(markers, "clean.txt")
m_xdg_sys = os.path.join(markers, "xdg_sys.txt")
m_masked = os.path.join(markers, "masked.txt")
# Stub st to verify Terminal=true delegation
write_text(os.path.join(bin_dir, "st"), f"#!/bin/sh\nprintf '%s\\n' \"$*\" >> '{m_term}'\n", 0o755)
write_text(os.path.join(conf_dir, "autostart"), "# empty\n")
# User drop-in overrides system drop-in of same basename
write_text(
os.path.join(sys_prefix, "share", "anode", "autostart.d", "10-ovr.sh"),
f"#!/bin/sh\nprintf 'sys\\n' >> '{m_override}'\n",
0o755,
)
write_text(
os.path.join(conf_dir, "autostart.d", "10-ovr.sh"),
f"#!/bin/sh\nprintf 'user\\n' >> '{m_override}'\n",
0o755,
)
# Non-executable file must be ignored
write_text(
os.path.join(conf_dir, "autostart.d", "20-nonexec.sh"),
f"#!/bin/sh\nprintf 'bad\\n' >> '{m_nonexec}'\n",
0o644,
)
# .desktop filter cases in autostart.d/
write_text(
os.path.join(conf_dir, "autostart.d", "hidden.desktop"),
f"[Desktop Entry]\nHidden=true\nExec=sh -c \"printf 'bad\\n' >> '{m_bad}'\"\n",
)
write_text(
os.path.join(conf_dir, "autostart.d", "badtry.desktop"),
f"[Desktop Entry]\nTryExec=definitely_missing_binary_999\nExec=sh -c \"printf 'bad\\n' >> '{m_bad}'\"\n",
)
write_text(
os.path.join(conf_dir, "autostart.d", "onlyother.desktop"),
f"[Desktop Entry]\nOnlyShowIn=GNOME;KDE;\nExec=sh -c \"printf 'bad\\n' >> '{m_bad}'\"\n",
)
write_text(
os.path.join(conf_dir, "autostart.d", "notanode.desktop"),
f"[Desktop Entry]\nNotShowIn=ANODE;XFCE;\nExec=sh -c \"printf 'bad\\n' >> '{m_bad}'\"\n",
)
write_text(
os.path.join(conf_dir, "autostart.d", "term.desktop"),
"[Desktop Entry]\nTryExec=sh\nTerminal=true\nOnlyShowIn=ANODE;\nExec=echo ok 100%%\n",
)
write_text(
os.path.join(conf_dir, "autostart.d", "fieldcodes.desktop"),
f"[Desktop Entry]\nNotShowIn=KDE;\nExec=sh -c \"printf 'pc:%%s\\n' 100%% >> '{m_clean}'\" %f %U\n",
)
# XDG autostart masking
write_text(
os.path.join(xdg_sys, "autostart", "sysok.desktop"),
f"[Desktop Entry]\nExec=sh -c \"printf 'sysok\\n' >> '{m_xdg_sys}'\"\n",
)
write_text(
os.path.join(xdg_sys, "autostart", "masked.desktop"),
f"[Desktop Entry]\nExec=sh -c \"printf 'bad\\n' >> '{m_masked}'\"\n",
)
write_text(
os.path.join(xdg_home, "autostart", "masked.desktop"),
f"[Desktop Entry]\nHidden=true\nExec=sh -c \"printf 'bad\\n' >> '{m_masked}'\"\n",
)
st = run_autostart(["start"], env)
assert st.returncode == 0, f"start failed: {st.stderr}"
assert wait_until(lambda: os.path.exists(m_override), timeout=4.0)
assert wait_until(lambda: os.path.exists(m_term), timeout=4.0)
assert wait_until(lambda: os.path.exists(m_clean), timeout=4.0)
assert wait_until(lambda: os.path.exists(m_xdg_sys), timeout=4.0)
time.sleep(0.35)
with open(m_override, "r", encoding="utf-8") as f:
assert f.read().strip() == "user", "user drop-in must shadow system drop-in"
with open(m_term, "r", encoding="utf-8") as f:
term_out = f.read().strip()
assert term_out == "-e echo ok 100%", f"unexpected Terminal=true args: {term_out!r}"
with open(m_clean, "r", encoding="utf-8") as f:
assert f.read().strip() == "pc:100%", "percent unescaping / %f %U stripping failed"
assert not os.path.exists(m_nonexec), "non-executable drop-in ran"
assert not os.path.exists(m_bad), "filtered .desktop ran"
assert not os.path.exists(m_masked), "XDG Hidden=true override failed to mask system .desktop"
# --no-xdg and ANODE_XDG_AUTOSTART=0
for args, xenv in ((["start", "--no-xdg"], {}), (["start"], {"ANODE_XDG_AUTOSTART": "0"})):
for p in (m_xdg_sys, m_override):
if os.path.exists(p):
os.remove(p)
cur = dict(env)
cur.update(xenv)
res = run_autostart(args, cur)
assert res.returncode == 0
assert wait_until(lambda: os.path.exists(m_override), timeout=4.0)
time.sleep(0.3)
assert not os.path.exists(m_xdg_sys), f"XDG ran despite {args} / {xenv}"
def test_check_errors():
with tempfile.TemporaryDirectory() as tmp:
env, _, xdg_home, _, _, _ = make_base_env(tmp)
cfg = os.path.join(xdg_home, "anode", "autostart")
for bad_name, content in (
("unknown directive", "frobnicate foo\n"),
("missing exec cmd", "exec\n"),
("missing --no-startup-id cmd", "exec_once --no-startup-id\n"),
("missing include arg", "include\n"),
("missing include target", "include does_not_exist.conf\n"),
):
write_text(cfg, content)
res = run_autostart(["check"], env)
assert res.returncode != 0, f"expected check failure for {bad_name}"
# Unreadable --config path
bad_cfg = run_autostart(["--config", os.path.join(tmp, "missing.conf"), "check"], env)
assert bad_cfg.returncode != 0
# Depth > 8 include chain
for idx in range(11):
nxt = f"include chain_{idx + 1}.conf\n" if idx < 10 else "exec true\n"
write_text(os.path.join(xdg_home, "anode", f"chain_{idx}.conf"), nxt)
res = run_autostart(["--config", os.path.join(xdg_home, "anode", "chain_0.conf"), "check"], env)
assert res.returncode != 0, "expected include depth > 8 to fail check"
def pick_display():
for d in range(70, 120):
if not os.path.exists(f"/tmp/.X{d}-lock") and not os.path.exists(f"/tmp/.X11-unix/X{d}"):
return f":{d}"
return ":98"
def terminate(proc):
if proc and proc.poll() is None:
proc.terminate()
try:
proc.wait(timeout=3)
except subprocess.TimeoutExpired:
proc.kill()
proc.wait(timeout=2)
def test_dwm_sighup_reload_xvfb():
assert shutil.which("Xvfb"), "Xvfb is required for test_dwm_sighup_reload_xvfb"
assert os.path.isfile(DWM_BIN), f"missing {DWM_BIN}"
libx11_name = ctypes.util.find_library("X11") or "libX11.so.6"
X11 = ctypes.CDLL(libx11_name)
X11.XOpenDisplay.argtypes = [ctypes.c_char_p]
X11.XOpenDisplay.restype = ctypes.c_void_p
X11.XCloseDisplay.argtypes = [ctypes.c_void_p]
X11.XDefaultRootWindow.argtypes = [ctypes.c_void_p]
X11.XDefaultRootWindow.restype = ctypes.c_ulong
X11.XInternAtom.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_int]
X11.XInternAtom.restype = ctypes.c_ulong
X11.XGetWindowProperty.argtypes = [
ctypes.c_void_p, ctypes.c_ulong, ctypes.c_ulong, ctypes.c_long, ctypes.c_long,
ctypes.c_int, ctypes.c_ulong, ctypes.POINTER(ctypes.c_ulong), ctypes.POINTER(ctypes.c_int),
ctypes.POINTER(ctypes.c_ulong), ctypes.POINTER(ctypes.c_ulong), ctypes.POINTER(ctypes.POINTER(ctypes.c_ubyte)),
]
X11.XGetWindowProperty.restype = ctypes.c_int
X11.XFree.argtypes = [ctypes.c_void_p]
disp = pick_display()
with tempfile.TemporaryDirectory() as tmp:
env, _, xdg_home, _, _, _ = make_base_env(tmp)
env["DISPLAY"] = disp
m_exec = os.path.join(tmp, "dwm_exec.txt")
m_always = os.path.join(tmp, "dwm_always.txt")
write_text(
os.path.join(xdg_home, "anode", "autostart"),
f"exec sh -c \"printf 'e\\n' >> '{m_exec}'\"\n"
f"exec_always sh -c \"printf 'a\\n' >> '{m_always}'\"\n",
)
xvfb = subprocess.Popen(
["Xvfb", disp, "-screen", "0", "1280x720x24", "-nolisten", "tcp"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
dwm = None
dpy = None
try:
dpy = wait_until(lambda: X11.XOpenDisplay(disp.encode()), timeout=5.0)
assert dpy, f"Xvfb failed to open on {disp}"
dwm = subprocess.Popen([DWM_BIN], env=env, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
wm_atom = X11.XInternAtom(dpy, b"_NET_SUPPORTING_WM_CHECK", 0)
root = X11.XDefaultRootWindow(dpy)
def has_wm_check():
da = ctypes.c_ulong()
di = ctypes.c_int()
n = ctypes.c_ulong()
dl = ctypes.c_ulong()
prop = ctypes.POINTER(ctypes.c_ubyte)()
rc = X11.XGetWindowProperty(
dpy, root, wm_atom, 0, 1, 0, 0,
ctypes.byref(da), ctypes.byref(di), ctypes.byref(n), ctypes.byref(dl), ctypes.byref(prop),
)
ok = (rc == 0 and bool(prop) and n.value > 0)
if prop:
X11.XFree(prop)
return ok
assert wait_until(has_wm_check, timeout=5.0), "dwm did not advertise _NET_SUPPORTING_WM_CHECK"
time.sleep(0.3)
assert not os.path.exists(m_exec), "dwm initial start must not run anode-autostart reload"
assert not os.path.exists(m_always), "dwm initial start must not run anode-autostart reload"
# Trigger SIGHUP live restart twice and verify only exec_always runs each time
os.kill(dwm.pid, signal.SIGHUP)
assert wait_until(
lambda: os.path.exists(m_always) and len(open(m_always, encoding="utf-8").read().splitlines()) >= 1,
timeout=5.0,
), "dwm SIGHUP restart did not invoke anode-autostart reload"
assert wait_until(has_wm_check, timeout=3.0) and dwm.poll() is None, "dwm died after SIGHUP restart"
os.kill(dwm.pid, signal.SIGHUP)
assert wait_until(
lambda: os.path.exists(m_always) and len(open(m_always, encoding="utf-8").read().splitlines()) >= 2,
timeout=5.0,
), "second dwm SIGHUP restart did not invoke anode-autostart reload"
time.sleep(0.25)
assert not os.path.exists(m_exec), "dwm SIGHUP reload must not run exec directives"
with open(m_always, "r", encoding="utf-8") as f:
assert len(f.read().splitlines()) == 2
finally:
if dpy:
X11.XCloseDisplay(dpy)
terminate(dwm)
terminate(xvfb)
if __name__ == "__main__":
test_directives_includes_once_and_reload()
test_profiles()
test_dropins_desktop_filters_and_xdg_masking()
test_check_errors()
test_dwm_sighup_reload_xvfb()
print("test_autostart: OK")
+1
View File
@@ -55,6 +55,7 @@ def check_scripts() -> None:
"scripts/anode-sync-gtk-theme",
"scripts/anode-sync-dunst-theme",
"scripts/anode-session",
"scripts/anode-autostart",
"scripts/anode-launcher",
"data/env.sh",
"src/dmenu/dmenu_path",