| 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). |
| 7 | set -euo pipefail |
| 8 | |
| 9 | HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 10 | APP_ID=rusty-teams |
| 11 | |
| 12 | TMP="$(mktemp -d)" |
| 13 | trap 'rm -rf "$TMP"' EXIT |
| 14 | |
| 15 | # Fake AppDir with the real AppRun, .desktop template, icons, and a stub binary. |
| 16 | APPDIR="$TMP/RustyTeams.AppDir" |
| 17 | mkdir -p "$APPDIR/usr/bin" |
| 18 | install -Dm755 "$HERE/AppRun" "$APPDIR/AppRun" |
| 19 | install -Dm644 "$HERE/$APP_ID.desktop" "$APPDIR/usr/share/applications/$APP_ID.desktop" |
| 20 | mkdir -p "$APPDIR/usr/share/icons" |
| 21 | cp -R "$HERE/hicolor" "$APPDIR/usr/share/icons/" |
| 22 | printf '#!/bin/sh\necho "stub: $*"\n' >"$APPDIR/usr/bin/teams" |
| 23 | chmod +x "$APPDIR/usr/bin/teams" |
| 24 | |
| 25 | APPIMAGE="$TMP/rusty teams test.AppImage" # spaces on purpose: Exec must stay quoted |
| 26 | : >"$APPIMAGE" |
| 27 | export APPIMAGE |
| 28 | export HOME="$TMP/home" |
| 29 | export XDG_DATA_HOME="$HOME/.local/share" |
| 30 | unset DESKTOPINTEGRATION RUSTY_TEAMS_NO_DESKTOP_INTEGRATION |
| 31 | mkdir -p "$HOME" |
| 32 | |
| 33 | fail() { |
| 34 | echo "FAIL: $*" >&2 |
| 35 | exit 1 |
| 36 | } |
| 37 | |
| 38 | # 1. Launch works and passes arguments through. |
| 39 | out="$("$APPDIR/AppRun" open file.png)" || fail "AppRun exited non-zero" |
| 40 | [[ "$out" = "stub: open file.png" ]] || fail "arguments not forwarded: $out" |
| 41 | |
| 42 | DESKTOP="$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. |
| 47 | expected_exec="Exec=\"$(readlink -f "$APPIMAGE")\"" |
| 48 | grep -Fxq "$expected_exec" "$DESKTOP" || fail "Exec not rewritten: $(grep '^Exec=' "$DESKTOP")" |
| 49 | grep -q '^TryExec=' "$DESKTOP" && fail "TryExec kept, the entry would be hidden" |
| 50 | grep -Fxq "Icon=$APP_ID" "$DESKTOP" || fail "Icon line lost" |
| 51 | grep -Fxq "X-AppImage-Integrate=false" "$DESKTOP" || fail "X-AppImage-Integrate=false missing" |
| 52 | grep -Fxq 'Categories=Network;InstantMessaging;Chat;' "$DESKTOP" || fail "metadata lines lost" |
| 53 | if command -v desktop-file-validate >/dev/null; then |
| 54 | desktop-file-validate "$DESKTOP" || fail "generated desktop entry is invalid" |
| 55 | fi |
| 56 | |
| 57 | # 3. Icons landed under the name the .desktop entry references. |
| 58 | for 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" |
| 60 | done |
| 61 | |
| 62 | # 4. Idempotent: a second launch must not rewrite anything (inode unchanged). |
| 63 | before="$(stat -c %i "$DESKTOP")" |
| 64 | out="$("$APPDIR/AppRun")" || fail "second launch exited non-zero" |
| 65 | [[ "$out" = "stub: " ]] || fail "second launch output: $out" |
| 66 | after="$(stat -c %i "$DESKTOP")" |
| 67 | [[ "$before" = "$after" ]] || fail "desktop entry rewritten although unchanged" |
| 68 | |
| 69 | # 5. Moving the AppImage refreshes Exec. |
| 70 | mv "$APPIMAGE" "$TMP/moved.AppImage" |
| 71 | export APPIMAGE="$TMP/moved.AppImage" |
| 72 | "$APPDIR/AppRun" >/dev/null || fail "launch after move exited non-zero" |
| 73 | grep -Fxq "Exec=\"$(readlink -f "$APPIMAGE")\"" "$DESKTOP" || fail "Exec not refreshed after move" |
| 74 | |
| 75 | # Customized launch options and metadata survive launches and relocation . |
| 76 | old_path="$(readlink -f "$APPIMAGE")" |
| 77 | sed -i "s|^Exec=.*|Exec=env WAYLAND_DISPLAY= \"$old_path\" --custom|; s|^Name=.*|Name=My Teams|" "$DESKTOP" |
| 78 | printf '\nX-User-Setting=keep me\n' >>"$DESKTOP" |
| 79 | cp "$DESKTOP" "$TMP/custom.desktop" |
| 80 | before="$(stat -c %i "$DESKTOP")" |
| 81 | "$APPDIR/AppRun" >/dev/null |
| 82 | cmp -s "$DESKTOP" "$TMP/custom.desktop" || fail "customized launcher overwritten" |
| 83 | [ "$before" = "$(stat -c %i "$DESKTOP")" ] || fail "custom launcher rewritten although unchanged" |
| 84 | mv "$APPIMAGE" "$TMP/new version.AppImage" |
| 85 | export APPIMAGE="$TMP/new version.AppImage" |
| 86 | "$APPDIR/AppRun" >/dev/null |
| 87 | grep -Fxq "Exec=env WAYLAND_DISPLAY= \"$APPIMAGE\" --custom" "$DESKTOP" || fail "custom Exec not relocated" |
| 88 | grep -Fxq 'Name=My Teams' "$DESKTOP" || fail "custom Name lost" |
| 89 | grep -Fxq 'X-User-Setting=keep me' "$DESKTOP" || fail "custom key lost" |
| 90 | |
| 91 | # Packaged updates apply only to untouched keys; user deletions stay deleted. |
| 92 | sed -i '/^Comment=/d' "$DESKTOP" |
| 93 | sed -i 's/^Name=.*/Name=Packaged update/; s/^Categories=.*/Categories=Graphics;/; s/^Comment=.*/Comment=Updated description/' "$APPDIR/usr/share/applications/$APP_ID.desktop" |
| 94 | printf '\nX-New-Packaged-Key=added\n' >>"$APPDIR/usr/share/applications/$APP_ID.desktop" |
| 95 | "$APPDIR/AppRun" >/dev/null |
| 96 | grep -Fxq 'Name=My Teams' "$DESKTOP" || fail "packaged update erased custom Name" |
| 97 | grep -Fxq 'Categories=Graphics;' "$DESKTOP" || fail "untouched metadata not updated" |
| 98 | grep -q '^Comment=' "$DESKTOP" && fail "user deletion undone" |
| 99 | grep -Fxq 'X-New-Packaged-Key=added' "$DESKTOP" || fail "new packaged key missing" |
| 100 | if command -v desktop-file-validate >/dev/null; then |
| 101 | desktop-file-validate "$DESKTOP" || fail "customized entry is invalid" |
| 102 | fi |
| 103 | |
| 104 | # A replaced executable may mention the old AppImage as an argument: leave it. |
| 105 | sed -i "s|^Exec=.*|Exec=\"/custom/wrapper\" \"$APPIMAGE\"|" "$DESKTOP" |
| 106 | expected_wrapper="$(grep '^Exec=' "$DESKTOP")" |
| 107 | mv "$APPIMAGE" "$TMP/another version.AppImage" |
| 108 | export APPIMAGE="$TMP/another version.AppImage" |
| 109 | "$APPDIR/AppRun" >/dev/null |
| 110 | grep -Fxq "$expected_wrapper" "$DESKTOP" || fail "wrapper argument mistaken for managed executable" |
| 111 | |
| 112 | # Existing launchers from before snapshots: bootstrap without losing edits. |
| 113 | rm -f "$XDG_DATA_HOME/applications/.$APP_ID.generated" |
| 114 | cp "$DESKTOP" "$TMP/legacy.desktop" |
| 115 | "$APPDIR/AppRun" >/dev/null |
| 116 | cmp -s "$DESKTOP" "$TMP/legacy.desktop" || fail "legacy custom launcher overwritten" |
| 117 | rm -f "$XDG_DATA_HOME/applications/.$APP_ID.generated" |
| 118 | sed -i 's|^Exec=.*|Exec="/custom/wrapper"|' "$DESKTOP" |
| 119 | "$APPDIR/AppRun" >/dev/null |
| 120 | grep -Fxq 'Exec="/custom/wrapper"' "$DESKTOP" || fail "legacy executable customization overwritten" |
| 121 | |
| 122 | |
| 123 | # Empty or malformed snapshots must never resurrect user-deleted keys. |
| 124 | for 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" |
| 135 | done |
| 136 | |
| 137 | # External integration must preserve customized and unknown legacy entries. |
| 138 | for 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" |
| 152 | done |
| 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. |
| 156 | for 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 |
| 191 | done |
| 192 | |
| 193 | # Reset the stub fixture so the duplicate-removal test starts with an untouched entry. |
| 194 | rm -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. |
| 200 | ext_desktop="$XDG_DATA_HOME/applications/appimagekit-rusty-teams.desktop" |
| 201 | printf '[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" |
| 206 | rm -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. |
| 211 | rm -rf "$XDG_DATA_HOME" |
| 212 | DESKTOPINTEGRATION=AppImageLauncher "$APPDIR/AppRun" >/dev/null || fail "DESKTOPINTEGRATION launch exited non-zero" |
| 213 | [[ -f "$DESKTOP" ]] || fail "DESKTOPINTEGRATION suppressed the desktop entry" |
| 214 | grep -Fxq "Exec=\"$(readlink -f "$APPIMAGE")\"" "$DESKTOP" || fail "Exec missing under DESKTOPINTEGRATION" |
| 215 | grep -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" |
| 217 | before="$(stat -c %i "$DESKTOP")" |
| 218 | DESKTOPINTEGRATION=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. |
| 222 | ext_desktop="$XDG_DATA_HOME/applications/appimagekit-rusty-teams.desktop" |
| 223 | printf '[Desktop Entry]\nType=Application\nName=Rusty Teams\nExec="%s"\nIcon=%s\n' "$(readlink -f "$APPIMAGE")" "$APP_ID" >"$ext_desktop" |
| 224 | DESKTOPINTEGRATION=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" |
| 228 | rm -f "$ext_desktop" |
| 229 | |
| 230 | # 8. Opt-out installs nothing. |
| 231 | rm -rf "$XDG_DATA_HOME" |
| 232 | RUSTY_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. |
| 236 | mkdir -p "$TMP/ro" && chmod a-w "$TMP/ro" |
| 237 | out="$(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" |
| 239 | chmod u+w "$TMP/ro" |
| 240 | |
| 241 | # 10. No $APPIMAGE (extracted AppDir): launch only, nothing installed. |
| 242 | rm -rf "$XDG_DATA_HOME" |
| 243 | env -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. |
| 247 | rm -rf "$XDG_DATA_HOME" |
| 248 | odd="$TMP/odd\$(id).AppImage" |
| 249 | : >"$odd" |
| 250 | out="$(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. |
| 257 | rm -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" |
| 260 | am_desktop="$XDG_DATA_HOME/applications/rusty-teams-AM.desktop" |
| 261 | printf '[Desktop Entry] |
| 262 | Type=Application |
| 263 | Name=Rusty Teams |
| 264 | Exec=teams |
| 265 | Icon=%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. |
| 273 | rm -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. |
| 279 | rm -rf "$XDG_DATA_HOME" |
| 280 | mkdir -p "$XDG_DATA_HOME/applications" |
| 281 | self_installed="$XDG_DATA_HOME/rusty-teams/teams" |
| 282 | printf '[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" |
| 285 | grep -Fxq "Exec=\"$self_installed\"" "$DESKTOP" || fail "self-installed copy's Exec replaced: $(grep '^Exec=' "$DESKTOP")" |
| 286 | grep -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 | |
| 289 | echo "AppRun desktop integration ok" |