Files

174 lines
7.0 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/env python3
"""Verify XDG Desktop Entry, Flatpak symlink, localization, and action handling in anode-launcher."""
import os
import subprocess
import tempfile
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
LAUNCHER = os.path.join(ROOT, "scripts", "anode-launcher")
def write_file(path, text, mode=0o644):
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w", encoding="utf-8") as f:
f.write(text)
os.chmod(path, mode)
def run_launcher_list(env):
res = subprocess.run([LAUNCHER, "--list"], env=env, capture_output=True, text=True)
assert res.returncode == 0, f"anode-launcher --list failed: {res.stderr}"
entries = {}
ordered_labels = []
for line in res.stdout.splitlines():
if not line:
continue
parts = line.split("\t", 3)
assert len(parts) == 4, f"Malformed output line: {line!r}"
label, workdir, term, cmd = parts
entries[label] = (workdir, term, cmd)
ordered_labels.append(label)
return entries, ordered_labels
def test_xdg_and_flatpak_integration():
with tempfile.TemporaryDirectory() as tmp:
home = os.path.join(tmp, "home")
xdg_data = os.path.join(tmp, "xdg_data")
flatpak_root = os.path.join(tmp, "flatpak_exports", "share")
flatpak_real = os.path.join(tmp, "flatpak_app")
usr_share = os.path.join(tmp, "usr_share")
fbin = os.path.join(tmp, "bin")
for d in (home, xdg_data, flatpak_root, flatpak_real, usr_share, fbin):
os.makedirs(d, exist_ok=True)
tool_path = os.path.join(fbin, "my_existing_tool")
write_file(tool_path, "#!/bin/sh\nexit 0\n", 0o755)
# 1. Symlinked Flatpak .desktop with @@u %U @@ file-forwarding placeholders
discord_real = os.path.join(flatpak_real, "com.discordapp.Discord.desktop")
write_file(
discord_real,
"[Desktop Entry]\n"
"Name=Discord\n"
"GenericName=Internet Messenger\n"
"Exec=/bin/sh -c flatpak run --file-forwarding com.discordapp.Discord @@u %U @@\n"
"Type=Application\n",
)
os.makedirs(os.path.join(flatpak_root, "applications"), exist_ok=True)
os.symlink(
discord_real,
os.path.join(flatpak_root, "applications", "com.discordapp.Discord.desktop"),
)
# 2. Recursive subdir desktop masked by Desktop File ID in XDG_DATA_HOME
write_file(
os.path.join(usr_share, "applications", "sub", "nested.desktop"),
"[Desktop Entry]\nName=NestedApp\nExec=sh -c true\nType=Application\n",
)
write_file(
os.path.join(xdg_data, "applications", "sub-nested.desktop"),
"[Desktop Entry]\nName=NestedApp\nHidden=true\nExec=sh -c true\nType=Application\n",
)
write_file(
os.path.join(usr_share, "applications", "nodisp.desktop"),
"[Desktop Entry]\nName=ShouldHide\nNoDisplay=true\nExec=sh -c true\nType=Application\n",
)
# 3. OnlyShowIn / NotShowIn / TryExec filters
write_file(
os.path.join(usr_share, "applications", "gnomeonly.desktop"),
"[Desktop Entry]\nName=GnomeOnly\nOnlyShowIn=GNOME;KDE;\nExec=sh -c true\nType=Application\n",
)
write_file(
os.path.join(usr_share, "applications", "notanode.desktop"),
"[Desktop Entry]\nName=NotAnode\nNotShowIn=ANODE;XFCE;\nExec=sh -c true\nType=Application\n",
)
write_file(
os.path.join(usr_share, "applications", "anodeonly.desktop"),
"[Desktop Entry]\nName=AnodeOnly\nOnlyShowIn=ANODE;\nExec=sh -c anode\nType=Application\n",
)
write_file(
os.path.join(usr_share, "applications", "badtry.desktop"),
"[Desktop Entry]\nName=BadTry\nTryExec=definitely_missing_bin_999\nExec=sh -c true\nType=Application\n",
)
# 4. Terminal=true, Path=, field codes (%i %c %k %%), and Actions=
write_file(
os.path.join(usr_share, "applications", "termtool.desktop"),
"[Desktop Entry]\n"
"Name=TermTool\n"
"Name[uk]=УкрТерм\n"
"Icon=term-icon\n"
"Path=/tmp\n"
"TryExec=my_existing_tool\n"
"Terminal=true\n"
"Exec=my_existing_tool %i %c %k %% %f\n"
"Actions=AltRun;\n"
"Type=Application\n\n"
"[Desktop Action AltRun]\n"
"Name=Secondary\n"
"Name[uk]=Другий\n"
"Exec=my_existing_tool --alt\n",
)
# 5. DBusActivatable=true without Exec=
write_file(
os.path.join(usr_share, "applications", "org.example.DbusApp.desktop"),
"[Desktop Entry]\n"
"Name=DbusApp\n"
"DBusActivatable=true\n"
"Type=Application\n",
)
env = dict(os.environ)
env["HOME"] = home
env["XDG_DATA_HOME"] = xdg_data
env["XDG_DATA_DIRS"] = f"{flatpak_root}:{usr_share}"
env["XDG_CURRENT_DESKTOP"] = "ANODE"
env["XDG_CACHE_HOME"] = os.path.join(tmp, "cache")
env["PATH"] = fbin + ":" + env.get("PATH", "/usr/bin:/bin")
env["LC_ALL"] = "C"
env["LC_MESSAGES"] = "C"
entries, labels = run_launcher_list(env)
assert "Discord (Internet Messenger)" in entries, labels
d_wd, d_tm, d_cmd = entries["Discord (Internet Messenger)"]
assert d_tm == "0" and "@@u" not in d_cmd and "%U" not in d_cmd, d_cmd
assert "--file-forwarding com.discordapp.Discord" in d_cmd, d_cmd
assert "NestedApp" not in entries, "Desktop File ID Hidden=true masking failed"
assert "ShouldHide" not in entries, "NoDisplay=true leaked"
assert "GnomeOnly" not in entries, "OnlyShowIn failed"
assert "NotAnode" not in entries, "NotShowIn failed"
assert "BadTry" not in entries, "TryExec filter failed"
assert "AnodeOnly" in entries
t_lbl = "TermTool [term]"
a_lbl = "TermTool: Secondary [term]"
assert t_lbl in entries and a_lbl in entries, labels
assert labels.index(a_lbl) == labels.index(t_lbl) + 1
wd, tm, cmd = entries[t_lbl]
assert wd == "/tmp" and tm == "1"
assert "--icon 'term-icon' 'TermTool'" in cmd, cmd
assert "termtool.desktop" in cmd and cmd.endswith(" %"), cmd
assert "%f" not in cmd and "%i" not in cmd and "%c" not in cmd and "%k" not in cmd
assert "DbusApp" in entries
_, _, dbus_cmd = entries["DbusApp"]
assert "org.example.DbusApp" in dbus_cmd and "/org/example/DbusApp" in dbus_cmd, dbus_cmd
# 6. Localized Name[uk] check
loc_env = dict(env)
loc_env.pop("LC_ALL", None)
loc_env["LC_MESSAGES"] = "uk_UA.UTF-8"
loc_entries, loc_labels = run_launcher_list(loc_env)
assert "УкрТерм [term]" in loc_entries, loc_labels
assert "УкрТерм: Другий [term]" in loc_entries, loc_labels
if __name__ == "__main__":
test_xdg_and_flatpak_integration()
print("test_launcher: ALL PASSED")