#!/usr/bin/env bash # Smoke test for packaging/linux/AppRun: the AppImage's desktop integration writes # and refreshes the host .desktop entry and icons, is idempotent, honours the opt-out # and an unwritable HOME, and always launches the app; `cli` runs figcraft-cli instead. # # Runs in seconds with no build (CI: .github/workflows/packaging-lint.yml). set -euo pipefail HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" ROOT="$(cd "$HERE/../.." && pwd)" APP_ID=ai.storyteller.figcraft TMP="$(mktemp -d)" trap 'rm -rf "$TMP"' EXIT # Fake AppDir with the real AppRun, .desktop template, icons, and a stub binary. APPDIR="$TMP/FigCraft.AppDir" mkdir -p "$APPDIR/usr/bin" install -Dm755 "$HERE/AppRun" "$APPDIR/AppRun" install -Dm644 "$HERE/$APP_ID.desktop" "$APPDIR/usr/share/applications/$APP_ID.desktop" mkdir -p "$APPDIR/usr/share/icons" cp -R "$ROOT/assets/app-icon/hicolor" "$APPDIR/usr/share/icons/" printf '#!/bin/sh\necho "stub: $*"\n' >"$APPDIR/usr/bin/figcraft" chmod +x "$APPDIR/usr/bin/figcraft" APPIMAGE="$TMP/figcraft test.AppImage" # spaces on purpose: Exec must stay quoted : >"$APPIMAGE" export APPIMAGE export HOME="$TMP/home" export XDG_DATA_HOME="$HOME/.local/share" mkdir -p "$HOME" unset FIGCRAFT_NO_DESKTOP_INTEGRATION # Record icon-cache refreshes without requiring a desktop session or GTK install. export REAL_CACHE_UPDATER REAL_CACHE_UPDATER="$(command -v gtk-update-icon-cache || true)" mkdir -p "$TMP/tools" cat >"$TMP/tools/gtk-update-icon-cache" <<'EOF' #!/bin/sh printf '%s\n' "$*" >>"$CACHE_CALLS" if [ "${CACHE_UPDATER_FAIL:-0}" = 1 ]; then exit 1 fi if [ -n "$REAL_CACHE_UPDATER" ]; then "$REAL_CACHE_UPDATER" "$@" else for dir do :; done : >"$dir/icon-theme.cache" fi EOF chmod +x "$TMP/tools/gtk-update-icon-cache" export PATH="$TMP/tools:$PATH" CACHE_CALLS="$TMP/cache-calls" fail() { echo "FAIL: $*" >&2 exit 1 } # 1. Launch works and passes arguments through. out="$("$APPDIR/AppRun" open file.png)" || fail "AppRun exited non-zero" [[ "$out" = "stub: open file.png" ]] || fail "arguments not forwarded: $out" DESKTOP="$XDG_DATA_HOME/applications/$APP_ID.desktop" [[ -f "$DESKTOP" ]] || fail "desktop entry not installed" # 2. Exec rewritten to the AppImage (quoted, spaces intact), TryExec dropped, # everything else preserved from the packaged entry. expected_exec="Exec=\"$(readlink -f "$APPIMAGE")\" %U" grep -Fxq "$expected_exec" "$DESKTOP" || fail "Exec not rewritten: $(grep '^Exec=' "$DESKTOP")" grep -q '^TryExec=' "$DESKTOP" && fail "TryExec kept, the entry would be hidden" grep -Fxq "Icon=$APP_ID" "$DESKTOP" || fail "Icon line lost" grep -Fxq 'Categories=Graphics;2DGraphics;VectorGraphics;X-Design;' "$DESKTOP" || fail "metadata lines lost" if command -v desktop-file-validate >/dev/null; then desktop-file-validate "$DESKTOP" || fail "generated desktop entry is invalid" fi # 3. Icons landed under the name the .desktop entry references and refreshed the cache. for f in "16x16/apps/$APP_ID.png" "256x256/apps/$APP_ID.png" "scalable/apps/$APP_ID.svg"; do [[ -f "$XDG_DATA_HOME/icons/hicolor/$f" ]] || fail "icon missing: $f" done [[ -f "$CACHE_CALLS" ]] || fail "installed icons did not refresh the icon cache" [[ "$(wc -l <"$CACHE_CALLS")" -eq 1 ]] || fail "icon cache refreshed more than once" grep -Fxq -- "-f -t $XDG_DATA_HOME/icons/hicolor" "$CACHE_CALLS" || fail "wrong icon cache arguments" if [[ -n "$REAL_CACHE_UPDATER" ]]; then [[ -f "$XDG_DATA_HOME/icons/hicolor/icon-theme.cache" ]] || fail "GTK did not build the icon cache" fi # 4. Idempotent: a second launch must not rewrite anything (inode unchanged). before="$(stat -c %i "$DESKTOP")" out="$("$APPDIR/AppRun")" || fail "second launch exited non-zero" [[ "$out" = "stub: " ]] || fail "second launch output: $out" after="$(stat -c %i "$DESKTOP")" [[ "$before" = "$after" ]] || fail "desktop entry rewritten although unchanged" [[ "$(wc -l <"$CACHE_CALLS")" -eq 1 ]] || fail "unchanged icons refreshed the cache" # 5. Moving the AppImage refreshes Exec. mv "$APPIMAGE" "$TMP/moved.AppImage" export APPIMAGE="$TMP/moved.AppImage" "$APPDIR/AppRun" >/dev/null || fail "launch after move exited non-zero" grep -Fxq "Exec=\"$(readlink -f "$APPIMAGE")\" %U" "$DESKTOP" || fail "Exec not refreshed after move" # 6. Opt-out installs nothing. rm -rf "$XDG_DATA_HOME" FIGCRAFT_NO_DESKTOP_INTEGRATION=1 "$APPDIR/AppRun" >/dev/null || fail "opt-out launch exited non-zero" [[ ! -e "$XDG_DATA_HOME" ]] || fail "opt-out still integrated" # 7. An unwritable data dir never blocks the launch. mkdir -p "$TMP/ro" && chmod a-w "$TMP/ro" out="$(XDG_DATA_HOME="$TMP/ro/data" "$APPDIR/AppRun" out.txt)" || fail "launch blocked by unwritable data dir" [[ "$out" = "stub: out.txt" ]] || fail "stub not run against unwritable data dir: $out" chmod u+w "$TMP/ro" # 8. No $APPIMAGE (extracted AppDir): launch only, nothing installed. rm -rf "$XDG_DATA_HOME" env -u APPIMAGE "$APPDIR/AppRun" >/dev/null || fail "extracted-AppDir launch exited non-zero" [[ ! -e "$XDG_DATA_HOME" ]] || fail "integrated without $APPIMAGE" # 9. A path that would need Exec escaping (here a `$`): launch only, nothing installed. rm -rf "$XDG_DATA_HOME" odd="$TMP/odd\$(id).AppImage" : >"$odd" out="$(APPIMAGE="$odd" "$APPDIR/AppRun" x)" || fail "launch with an odd path exited non-zero" [[ "$out" = "stub: x" ]] || fail "odd path: $out" [[ ! -e "$XDG_DATA_HOME" ]] || fail "integrated an AppImage path that needs escaping" # 10. `cli ` runs figcraft-cli with the remaining arguments and integrates nothing. rm -rf "$XDG_DATA_HOME" out="$("$APPDIR/AppRun" cli mcp --attach 7979 2>&1)" && fail "cli ran without figcraft-cli in the AppDir" [[ "$out" = "figcraft: this AppImage was built without figcraft-cli" ]] || fail "missing cli: $out" printf '#!/bin/sh\necho "cli stub: $*"\n' >"$APPDIR/usr/bin/figcraft-cli" chmod +x "$APPDIR/usr/bin/figcraft-cli" out="$("$APPDIR/AppRun" cli mcp --attach 7979)" || fail "cli launch exited non-zero" [[ "$out" = "cli stub: mcp --attach 7979" ]] || fail "cli arguments not forwarded: $out" out="$("$APPDIR/AppRun" cli)" || fail "bare cli launch exited non-zero" [[ "$out" = "cli stub: " ]] || fail "bare cli output: $out" [[ ! -e "$XDG_DATA_HOME" ]] || fail "cli launch integrated the desktop" # Only the first argument selects the CLI. out="$("$APPDIR/AppRun" file.figcraft cli)" || fail "app launch with a later cli argument exited non-zero" [[ "$out" = "stub: file.figcraft cli" ]] || fail "later cli argument hijacked the launch: $out" # 11. Pre-existing stale Hicolor cache is rebuilt even when icon bytes are unchanged, # and a subsequent launch does not rebuild again. rm -rf "$XDG_DATA_HOME" "$CACHE_CALLS" "$APPDIR/AppRun" >/dev/null || fail "setup launch for stale-cache test exited non-zero" touch -t 202001010000 "$XDG_DATA_HOME/icons/hicolor/icon-theme.cache" : >"$CACHE_CALLS" "$APPDIR/AppRun" >/dev/null || fail "stale-cache repair launch exited non-zero" [[ "$(wc -l <"$CACHE_CALLS")" -eq 1 ]] || fail "stale icon-theme.cache was not refreshed" grep -Fxq -- "-f -t $XDG_DATA_HOME/icons/hicolor" "$CACHE_CALLS" || fail "wrong stale-cache refresh arguments" "$APPDIR/AppRun" >/dev/null || fail "post-repair launch exited non-zero" [[ "$(wc -l <"$CACHE_CALLS")" -eq 1 ]] || fail "repaired icon-theme.cache refreshed again" # 12. A failing icon-cache updater never blocks the launch. rm -rf "$XDG_DATA_HOME" "$CACHE_CALLS" out="$(CACHE_UPDATER_FAIL=1 "$APPDIR/AppRun" ok)" || fail "failing icon-cache updater blocked launch" [[ "$out" = "stub: ok" ]] || fail "failing icon-cache updater altered launch output: $out" # 13. GTK 4-only fallback: when gtk-update-icon-cache is unavailable, both AppRun # and postinst.sh invoke gtk4-update-icon-cache with the expected arguments. GTK4_BIN="$TMP/gtk4-only" GTK4_CALLS="$TMP/gtk4-calls" mkdir -p "$GTK4_BIN" for cmd in dirname readlink mkdir sed cmp rm mv find cp; do ln -s "$(command -v "$cmd")" "$GTK4_BIN/$cmd" done cat >"$GTK4_BIN/gtk4-update-icon-cache" <<'EOF' #!/bin/sh printf 'gtk4-update-icon-cache %s\n' "$*" >>"$GTK4_CALLS" EOF chmod +x "$GTK4_BIN/gtk4-update-icon-cache" rm -rf "$XDG_DATA_HOME" "$GTK4_CALLS" out="$(PATH="$GTK4_BIN" GTK4_CALLS="$GTK4_CALLS" "$APPDIR/AppRun" gtk4)" || fail "GTK 4-only AppRun launch failed" [[ "$out" = "stub: gtk4" ]] || fail "GTK 4-only AppRun altered launch output: $out" [[ "$(wc -l <"$GTK4_CALLS")" -eq 1 ]] || fail "GTK 4-only AppRun did not invoke gtk4-update-icon-cache once" grep -Fxq -- "gtk4-update-icon-cache -f -t $XDG_DATA_HOME/icons/hicolor" "$GTK4_CALLS" || fail "wrong GTK 4 AppRun arguments" : >"$GTK4_CALLS" PATH="$GTK4_BIN" GTK4_CALLS="$GTK4_CALLS" "$HERE/postinst.sh" || fail "GTK 4-only postinst.sh failed" [[ "$(wc -l <"$GTK4_CALLS")" -eq 1 ]] || fail "GTK 4-only postinst.sh did not invoke gtk4-update-icon-cache once" grep -Fxq -- "gtk4-update-icon-cache -q -t -f /usr/share/icons/hicolor" "$GTK4_CALLS" || fail "wrong GTK 4 postinst.sh arguments" echo "AppRun desktop integration ok"