Files
Ilija Chrchev 1d28fc6439 Refresh user icon cache after AppImage desktop integration (#1877) (#2115)
On Fedora (and other GTK desktops), the hicolor icon cache is not
rebuilt after the AppImage copies its icons into $XDG_DATA_HOME.
The desktop keeps showing a generic icon until the user logs out
or manually runs gtk-update-icon-cache.

Add a conditional cache refresh to the AppRun integration step:
- Track whether any icon file was actually copied (changed).
- Only refresh if the cache file (icon-theme.cache) already exists,
  so we never create one where the desktop hasn't built one yet.
- Skip silently when gtk-update-icon-cache is not installed.

The fix lives in the AppImage's own AppRun rather than in the
launcher because the launcher (appimagelauncher) is a separate
project with its own release cycle, and many users run AppImages
directly without it. Both fixes are complementary.

Tested on Fedora 41 (GNOME 47, Wayland) by launching from the app
grid after clearing the icon cache. The correct icon appeared on
the first launch without a logout.

Closes #1877
2026-11-19 20:02:02 -07:00

134 lines
6.2 KiB
Bash
Executable File

#!/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.
#
# 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.craft-anything
TMP="$(mktemp -d)"
trap 'rm -rf "$TMP"' EXIT
# Fake AppDir with the real AppRun, .desktop template, icons, and a stub binary.
APPDIR="$TMP/Craft Anything.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/craft-anything"
chmod +x "$APPDIR/usr/bin/craft-anything"
printf '#!/bin/sh\necho "cli: $*"\n' >"$APPDIR/usr/bin/craft-anything-cli"
chmod +x "$APPDIR/usr/bin/craft-anything-cli"
APPIMAGE="$TMP/craft-anything 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"
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")\" %F"
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=Development;Debugger;' "$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.
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
# 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"
# 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")\" %F" "$DESKTOP" || fail "Exec not refreshed after move"
# 6. Opt-out installs nothing.
rm -rf "$XDG_DATA_HOME"
CRAFT_ANYTHING_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 the bundled CLI with the rest of the arguments, and integrates nothing (#723).
rm -rf "$XDG_DATA_HOME"
out="$("$APPDIR/AppRun" --cli mcp --project "a b.cany")" || fail "--cli exited non-zero"
[[ "$out" = "cli: mcp --project a b.cany" ]] || fail "--cli not routed to the CLI: $out"
[[ ! -e "$XDG_DATA_HOME" ]] || fail "a CLI run integrated the desktop entry"
# 11. Started through a link named craft-anything-cli ($ARGV0), every argument goes to the CLI.
out="$(ARGV0="/home/u/.local/bin/craft-anything-cli" "$APPDIR/AppRun" --version)" || fail "ARGV0 route exited non-zero"
[[ "$out" = "cli: --version" ]] || fail "ARGV0 not routed to the CLI: $out"
[[ ! -e "$XDG_DATA_HOME" ]] || fail "an ARGV0 CLI run integrated the desktop entry"
# 12. Any other name, or --cli later on the line, still starts the app.
out="$(ARGV0="craft-anything" "$APPDIR/AppRun" open --cli)" || fail "app launch exited non-zero"
[[ "$out" = "stub: open --cli" ]] || fail "app launch routed to the CLI: $out"
# 13. No icon cache is created where there was none (#1877).
[[ ! -f "$XDG_DATA_HOME/icons/hicolor/icon-theme.cache" ]] || fail "icon cache created where there was none"
# 14. When no icon changed, the cache file is not touched (#1877).
icons_dir="$XDG_DATA_HOME/icons/hicolor"
mkdir -p "$icons_dir"
echo "sentinel" >"$icons_dir/icon-theme.cache"
"$APPDIR/AppRun" >/dev/null || fail "launch with unchanged icons exited non-zero"
[[ "$(cat "$icons_dir/icon-theme.cache")" = "sentinel" ]] || fail "icon cache refreshed although nothing changed"
# 15. When an icon changed and a cache exists, the cache may be refreshed (#1877).
src_icon="$APPDIR/usr/share/icons/hicolor/16x16/apps/$APP_ID.png"
cp "$src_icon" "$src_icon.bak"
printf 'x' >>"$src_icon"
echo "old" >"$icons_dir/icon-theme.cache"
"$APPDIR/AppRun" >/dev/null || fail "launch with changed icon exited non-zero"
mv "$src_icon.bak" "$src_icon"
echo "AppRun desktop integration and CLI routing ok"