BlueGit

ict rusty-teams

Files

rusty-teams / packaging / linux / apprun-test.sh

apprun-test.sh

feat(packaging): Linux AppImage, deb, rpm and Flatpak
metis Samuel David Merlin Kemper · pushed by metis · 2 weeks ago
228f36c5 History
apprun-test.sh 15.0 KiB Download Raw
1#!/usr/bin/env bash
2# Smoke test for packaging/linux/AppRun: the AppImage's desktop integration writes
3# and refreshes the host .desktop entry and icons, is idempotent, honours the opt-out
4# and an unwritable HOME, and always launches the app.
5#
6# Runs in seconds with no build (CI: .github/workflows/packaging-lint.yml).
7set -euo pipefail
8
9HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
10APP_ID=rusty-teams
11
12TMP="$(mktemp -d)"
13trap 'rm -rf "$TMP"' EXIT
14
15# Fake AppDir with the real AppRun, .desktop template, icons, and a stub binary.
16APPDIR="$TMP/RustyTeams.AppDir"
17mkdir -p "$APPDIR/usr/bin"
18install -Dm755 "$HERE/AppRun" "$APPDIR/AppRun"
19install -Dm644 "$HERE/$APP_ID.desktop" "$APPDIR/usr/share/applications/$APP_ID.desktop"
20mkdir -p "$APPDIR/usr/share/icons"
21cp -R "$HERE/hicolor" "$APPDIR/usr/share/icons/"
22printf '#!/bin/sh\necho "stub: $*"\n' >"$APPDIR/usr/bin/teams"
23chmod +x "$APPDIR/usr/bin/teams"
24
25APPIMAGE="$TMP/rusty teams test.AppImage" # spaces on purpose: Exec must stay quoted
26: >"$APPIMAGE"
27export APPIMAGE
28export HOME="$TMP/home"
29export XDG_DATA_HOME="$HOME/.local/share"
30unset DESKTOPINTEGRATION RUSTY_TEAMS_NO_DESKTOP_INTEGRATION
31mkdir -p "$HOME"
32
33fail() {
34 echo "FAIL: $*" >&2
35 exit 1
36}
37
38# 1. Launch works and passes arguments through.
39out="$("$APPDIR/AppRun" open file.png)" || fail "AppRun exited non-zero"
40[[ "$out" = "stub: open file.png" ]] || fail "arguments not forwarded: $out"
41
42DESKTOP="$XDG_DATA_HOME/applications/$APP_ID.desktop"
43[[ -f "$DESKTOP" ]] || fail "desktop entry not installed"
44
45# 2. Exec rewritten to the AppImage (quoted, spaces intact), TryExec dropped,
46# everything else preserved from the packaged entry.
47expected_exec="Exec=\"$(readlink -f "$APPIMAGE")\""
48grep -Fxq "$expected_exec" "$DESKTOP" || fail "Exec not rewritten: $(grep '^Exec=' "$DESKTOP")"
49grep -q '^TryExec=' "$DESKTOP" && fail "TryExec kept, the entry would be hidden"
50grep -Fxq "Icon=$APP_ID" "$DESKTOP" || fail "Icon line lost"
51grep -Fxq "X-AppImage-Integrate=false" "$DESKTOP" || fail "X-AppImage-Integrate=false missing"
52grep -Fxq 'Categories=Network;InstantMessaging;Chat;' "$DESKTOP" || fail "metadata lines lost"
53if command -v desktop-file-validate >/dev/null; then
54 desktop-file-validate "$DESKTOP" || fail "generated desktop entry is invalid"
55fi
56
57# 3. Icons landed under the name the .desktop entry references.
58for f in "16x16/apps/$APP_ID.png" "256x256/apps/$APP_ID.png"; do
59 [[ -f "$XDG_DATA_HOME/icons/hicolor/$f" ]] || fail "icon missing: $f"
60done
61
62# 4. Idempotent: a second launch must not rewrite anything (inode unchanged).
63before="$(stat -c %i "$DESKTOP")"
64out="$("$APPDIR/AppRun")" || fail "second launch exited non-zero"
65[[ "$out" = "stub: " ]] || fail "second launch output: $out"
66after="$(stat -c %i "$DESKTOP")"
67[[ "$before" = "$after" ]] || fail "desktop entry rewritten although unchanged"
68
69# 5. Moving the AppImage refreshes Exec.
70mv "$APPIMAGE" "$TMP/moved.AppImage"
71export APPIMAGE="$TMP/moved.AppImage"
72"$APPDIR/AppRun" >/dev/null || fail "launch after move exited non-zero"
73grep -Fxq "Exec=\"$(readlink -f "$APPIMAGE")\"" "$DESKTOP" || fail "Exec not refreshed after move"
74
75# Customized launch options and metadata survive launches and relocation .
76old_path="$(readlink -f "$APPIMAGE")"
77sed -i "s|^Exec=.*|Exec=env WAYLAND_DISPLAY= \"$old_path\" --custom|; s|^Name=.*|Name=My Teams|" "$DESKTOP"
78printf '\nX-User-Setting=keep me\n' >>"$DESKTOP"
79cp "$DESKTOP" "$TMP/custom.desktop"
80before="$(stat -c %i "$DESKTOP")"
81"$APPDIR/AppRun" >/dev/null
82cmp -s "$DESKTOP" "$TMP/custom.desktop" || fail "customized launcher overwritten"
83[ "$before" = "$(stat -c %i "$DESKTOP")" ] || fail "custom launcher rewritten although unchanged"
84mv "$APPIMAGE" "$TMP/new version.AppImage"
85export APPIMAGE="$TMP/new version.AppImage"
86"$APPDIR/AppRun" >/dev/null
87grep -Fxq "Exec=env WAYLAND_DISPLAY= \"$APPIMAGE\" --custom" "$DESKTOP" || fail "custom Exec not relocated"
88grep -Fxq 'Name=My Teams' "$DESKTOP" || fail "custom Name lost"
89grep -Fxq 'X-User-Setting=keep me' "$DESKTOP" || fail "custom key lost"
90
91# Packaged updates apply only to untouched keys; user deletions stay deleted.
92sed -i '/^Comment=/d' "$DESKTOP"
93sed -i 's/^Name=.*/Name=Packaged update/; s/^Categories=.*/Categories=Graphics;/; s/^Comment=.*/Comment=Updated description/' "$APPDIR/usr/share/applications/$APP_ID.desktop"
94printf '\nX-New-Packaged-Key=added\n' >>"$APPDIR/usr/share/applications/$APP_ID.desktop"
95"$APPDIR/AppRun" >/dev/null
96grep -Fxq 'Name=My Teams' "$DESKTOP" || fail "packaged update erased custom Name"
97grep -Fxq 'Categories=Graphics;' "$DESKTOP" || fail "untouched metadata not updated"
98grep -q '^Comment=' "$DESKTOP" && fail "user deletion undone"
99grep -Fxq 'X-New-Packaged-Key=added' "$DESKTOP" || fail "new packaged key missing"
100if command -v desktop-file-validate >/dev/null; then
101 desktop-file-validate "$DESKTOP" || fail "customized entry is invalid"
102fi
103
104# A replaced executable may mention the old AppImage as an argument: leave it.
105sed -i "s|^Exec=.*|Exec=\"/custom/wrapper\" \"$APPIMAGE\"|" "$DESKTOP"
106expected_wrapper="$(grep '^Exec=' "$DESKTOP")"
107mv "$APPIMAGE" "$TMP/another version.AppImage"
108export APPIMAGE="$TMP/another version.AppImage"
109"$APPDIR/AppRun" >/dev/null
110grep -Fxq "$expected_wrapper" "$DESKTOP" || fail "wrapper argument mistaken for managed executable"
111
112# Existing launchers from before snapshots: bootstrap without losing edits.
113rm -f "$XDG_DATA_HOME/applications/.$APP_ID.generated"
114cp "$DESKTOP" "$TMP/legacy.desktop"
115"$APPDIR/AppRun" >/dev/null
116cmp -s "$DESKTOP" "$TMP/legacy.desktop" || fail "legacy custom launcher overwritten"
117rm -f "$XDG_DATA_HOME/applications/.$APP_ID.generated"
118sed -i 's|^Exec=.*|Exec="/custom/wrapper"|' "$DESKTOP"
119"$APPDIR/AppRun" >/dev/null
120grep -Fxq 'Exec="/custom/wrapper"' "$DESKTOP" || fail "legacy executable customization overwritten"
121
122
123# Empty or malformed snapshots must never resurrect user-deleted keys.
124for invalid in empty garbage missing-exec; do
125 baseline="$XDG_DATA_HOME/applications/.$APP_ID.generated"
126 case "$invalid" in
127 empty) : >"$baseline" ;;
128 garbage) printf 'not a desktop entry\n' >"$baseline" ;;
129 missing-exec) printf '[Desktop Entry]\nType=Application\nName=Rusty Teams\n' >"$baseline" ;;
130 esac
131 sed -i '/^Comment=/d; /^StartupNotify=/d' "$DESKTOP"
132 cp "$DESKTOP" "$TMP/before-invalid.desktop"
133 "$APPDIR/AppRun" >/dev/null || fail "invalid baseline blocked launch"
134 cmp -s "$DESKTOP" "$TMP/before-invalid.desktop" || fail "$invalid baseline changed user launcher"
135done
136
137# External integration must preserve customized and unknown legacy entries.
138for baseline_kind in valid missing empty; do
139 baseline="$XDG_DATA_HOME/applications/.$APP_ID.generated"
140 case "$baseline_kind" in
141 missing) rm -f "$baseline" ;;
142 empty) : >"$baseline" ;;
143 esac
144 cp "$DESKTOP" "$TMP/before-external.desktop"
145 DESKTOPINTEGRATION=AppImageLauncher "$APPDIR/AppRun" >/dev/null
146 cmp -s "$DESKTOP" "$TMP/before-external.desktop" || fail "DESKTOPINTEGRATION changed $baseline_kind custom entry"
147 ext="$XDG_DATA_HOME/applications/external.desktop"
148 printf '[Desktop Entry]\nType=Application\nName=External\nExec="%s"\n' "$APPIMAGE" >"$ext"
149 "$APPDIR/AppRun" >/dev/null
150 cmp -s "$DESKTOP" "$TMP/before-external.desktop" || fail "external desktop removed $baseline_kind custom entry"
151 rm -f "$ext"
152done
153
154# Linked launchers and snapshots are externally owned, even when their bytes
155# match a generated entry. Check both normal refresh and external deduplication.
156for integration in normal external; do
157 for linked in launcher dangling-launcher baseline dangling-baseline; do
158 rm -rf "$XDG_DATA_HOME"
159 "$APPDIR/AppRun" >/dev/null
160 baseline="$XDG_DATA_HOME/applications/.$APP_ID.generated"
161 target="$TMP/linked-target"
162 cp "$DESKTOP" "$target"
163 case "$linked" in
164 launcher|dangling-launcher) link="$DESKTOP" ;;
165 baseline|dangling-baseline) link="$baseline" ;;
166 esac
167 rm -f "$link"
168 case "$linked" in dangling-*) rm -f "$target" ;; esac
169 ln -s "$target" "$link"
170 [ ! -f "$target" ] || cp "$target" "$TMP/linked-target-before"
171 # A packaged metadata change would otherwise cause a symlink to be replaced.
172 template="$APPDIR/usr/share/applications/$APP_ID.desktop"
173 cp "$template" "$TMP/link-template-before"
174 printf '\nX-Link-Refresh=changed\n' >>"$template"
175 if [ "$integration" = external ]; then
176 ext="$XDG_DATA_HOME/applications/external.desktop"
177 printf '[Desktop Entry]\nType=Application\nName=External\nExec="%s"\n' "$APPIMAGE" >"$ext"
178 DESKTOPINTEGRATION=AppImageLauncher "$APPDIR/AppRun" >/dev/null
179 else
180 "$APPDIR/AppRun" >/dev/null
181 fi
182 [ -L "$link" ] || fail "$integration replaced or removed $linked"
183 [ "$(readlink "$link")" = "$target" ] || fail "$integration retargeted $linked"
184 case "$linked" in
185 dangling-*) [ ! -e "$target" ] || fail "$integration created dangling target" ;;
186 *) cmp -s "$target" "$TMP/linked-target-before" || fail "$integration changed linked target" ;;
187 esac
188 [ -f "$XDG_DATA_HOME/icons/hicolor/256x256/apps/$APP_ID.png" ] || fail "link preservation skipped icons"
189 cp "$TMP/link-template-before" "$template"
190 done
191done
192
193# Reset the stub fixture so the duplicate-removal test starts with an untouched entry.
194rm -rf "$XDG_DATA_HOME"
195"$APPDIR/AppRun" >/dev/null
196
197# 6. External AppImage integrator: if another .desktop file in $XDG_DATA_HOME/applications
198# already launches this AppImage (e.g. AppImageLauncher's appimagekit-*.desktop), AppRun removes
199# its own duplicate .desktop entry while keeping the hicolor icons installed.
200ext_desktop="$XDG_DATA_HOME/applications/appimagekit-rusty-teams.desktop"
201printf '[Desktop Entry]\nType=Application\nName=Rusty Teams\nExec="%s"\nIcon=%s\n' "$(readlink -f "$APPIMAGE")" "$APP_ID" >"$ext_desktop"
202"$APPDIR/AppRun" >/dev/null || fail "launch with external integrator desktop entry exited non-zero"
203[[ ! -e "$DESKTOP" ]] || fail "duplicate desktop entry kept alongside external integrator entry"
204[[ -f "$ext_desktop" ]] || fail "external integrator desktop entry removed"
205[[ -f "$XDG_DATA_HOME/icons/hicolor/256x256/apps/$APP_ID.png" ]] || fail "icons missing with external integrator"
206rm -f "$ext_desktop"
207
208# 7. AppImageLauncher exports DESKTOPINTEGRATION even after X-AppImage-Integrate=false
209# made it skip the AppImage. That is not an existing launcher, so AppRun still
210# writes its own entry and a later launch must not delete it.
211rm -rf "$XDG_DATA_HOME"
212DESKTOPINTEGRATION=AppImageLauncher "$APPDIR/AppRun" >/dev/null || fail "DESKTOPINTEGRATION launch exited non-zero"
213[[ -f "$DESKTOP" ]] || fail "DESKTOPINTEGRATION suppressed the desktop entry"
214grep -Fxq "Exec=\"$(readlink -f "$APPIMAGE")\"" "$DESKTOP" || fail "Exec missing under DESKTOPINTEGRATION"
215grep -Fxq "X-AppImage-Integrate=false" "$DESKTOP" || fail "X-AppImage-Integrate=false missing under DESKTOPINTEGRATION"
216[[ -f "$XDG_DATA_HOME/icons/hicolor/256x256/apps/$APP_ID.png" ]] || fail "icons not installed under DESKTOPINTEGRATION"
217before="$(stat -c %i "$DESKTOP")"
218DESKTOPINTEGRATION=AppImageLauncher "$APPDIR/AppRun" >/dev/null || fail "second DESKTOPINTEGRATION launch exited non-zero"
219[[ -f "$DESKTOP" ]] || fail "DESKTOPINTEGRATION removed the desktop entry on a later launch"
220[[ "$before" = "$(stat -c %i "$DESKTOP")" ]] || fail "desktop entry rewritten under DESKTOPINTEGRATION although unchanged"
221# A real integrator entry still wins, including when DESKTOPINTEGRATION is also set.
222ext_desktop="$XDG_DATA_HOME/applications/appimagekit-rusty-teams.desktop"
223printf '[Desktop Entry]\nType=Application\nName=Rusty Teams\nExec="%s"\nIcon=%s\n' "$(readlink -f "$APPIMAGE")" "$APP_ID" >"$ext_desktop"
224DESKTOPINTEGRATION=AppImageLauncher "$APPDIR/AppRun" >/dev/null || fail "launch with integrator entry and DESKTOPINTEGRATION exited non-zero"
225[[ ! -e "$DESKTOP" ]] || fail "duplicate desktop entry kept when an integrator entry exists"
226[[ -f "$ext_desktop" ]] || fail "external integrator desktop entry removed"
227[[ -f "$XDG_DATA_HOME/icons/hicolor/256x256/apps/$APP_ID.png" ]] || fail "icons missing when DESKTOPINTEGRATION and an integrator entry coexist"
228rm -f "$ext_desktop"
229
230# 8. Opt-out installs nothing.
231rm -rf "$XDG_DATA_HOME"
232RUSTY_TEAMS_NO_DESKTOP_INTEGRATION=1 "$APPDIR/AppRun" >/dev/null || fail "opt-out launch exited non-zero"
233[[ ! -e "$XDG_DATA_HOME" ]] || fail "opt-out still integrated"
234
235# 9. An unwritable data dir never blocks the launch.
236mkdir -p "$TMP/ro" && chmod a-w "$TMP/ro"
237out="$(XDG_DATA_HOME="$TMP/ro/data" "$APPDIR/AppRun" out.txt)" || fail "launch blocked by unwritable data dir"
238[[ "$out" = "stub: out.txt" ]] || fail "stub not run against unwritable data dir: $out"
239chmod u+w "$TMP/ro"
240
241# 10. No $APPIMAGE (extracted AppDir): launch only, nothing installed.
242rm -rf "$XDG_DATA_HOME"
243env -u APPIMAGE "$APPDIR/AppRun" >/dev/null || fail "extracted-AppDir launch exited non-zero"
244[[ ! -e "$XDG_DATA_HOME" ]] || fail "integrated without $APPIMAGE"
245
246# 11. A path that would need Exec escaping (here a `$`): launch only, nothing installed.
247rm -rf "$XDG_DATA_HOME"
248odd="$TMP/odd\$(id).AppImage"
249: >"$odd"
250out="$(APPIMAGE="$odd" "$APPDIR/AppRun" x)" || fail "launch with an odd path exited non-zero"
251[[ "$out" = "stub: x" ]] || fail "odd path: $out"
252[[ ! -e "$XDG_DATA_HOME" ]] || fail "integrated an AppImage path that needs escaping"
253
254# 12. AM / AppMan: a rusty-teams-AM.desktop launcher in the user's applications
255# dir owns the menu entry. AppRun removes its own entry left from an earlier run,
256# writes no second one and still installs the icons.
257rm -rf "$XDG_DATA_HOME"
258"$APPDIR/AppRun" >/dev/null || fail "launch before AM install exited non-zero"
259[[ -f "$DESKTOP" ]] || fail "no desktop entry before AM install"
260am_desktop="$XDG_DATA_HOME/applications/rusty-teams-AM.desktop"
261printf '[Desktop Entry]
262Type=Application
263Name=Rusty Teams
264Exec=teams
265Icon=%s
266' "$APP_ID" >"$am_desktop"
267"$APPDIR/AppRun" >/dev/null || fail "launch with AM launcher exited non-zero"
268[[ ! -e "$DESKTOP" ]] || fail "duplicate desktop entry kept alongside AM launcher"
269[[ -f "$am_desktop" ]] || fail "AM launcher was removed"
270[[ -f "$XDG_DATA_HOME/icons/hicolor/256x256/apps/$APP_ID.png" ]] || fail "icons missing with AM launcher"
271
272# 13. Once AM removes its launcher, AppRun writes its own entry again.
273rm -f "$am_desktop"
274"$APPDIR/AppRun" >/dev/null || fail "launch after AM removal exited non-zero"
275[[ -f "$DESKTOP" ]] || fail "no desktop entry after the AM launcher was removed"
276
277# 14. The app's own install (Settings > General, ADR 0014) writes the same entry with
278# Exec pointing at ~/.local/share/rusty-teams/teams. AppRun keeps that Exec.
279rm -rf "$XDG_DATA_HOME"
280mkdir -p "$XDG_DATA_HOME/applications"
281self_installed="$XDG_DATA_HOME/rusty-teams/teams"
282printf '[Desktop Entry]\nType=Application\nName=Rusty Teams\nExec="%s"\nTryExec=%s\nIcon=%s\nStartupWMClass=%s\n' \
283 "$self_installed" "$self_installed" "$APP_ID" "$APP_ID" >"$DESKTOP"
284"$APPDIR/AppRun" >/dev/null || fail "launch beside a self-installed copy exited non-zero"
285grep -Fxq "Exec=\"$self_installed\"" "$DESKTOP" || fail "self-installed copy's Exec replaced: $(grep '^Exec=' "$DESKTOP")"
286grep -Fxq "TryExec=$self_installed" "$DESKTOP" || fail "self-installed copy's TryExec dropped"
287[[ -f "$XDG_DATA_HOME/icons/hicolor/256x256/apps/$APP_ID.png" ]] || fail "icons missing beside a self-installed copy"
288
289echo "AppRun desktop integration ok"