|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" |
| 6 | + |
| 7 | +tmp_dir=$(mktemp -d) |
| 8 | +trap 'rm -rf "$tmp_dir"' EXIT |
| 9 | +mkdir -p "$tmp_dir/bin" "$tmp_dir/home" |
| 10 | + |
| 11 | +for stub in gtk-update-icon-cache update-desktop-database omarchy-notification-send; do |
| 12 | + printf '#!/bin/bash\n:\n' >"$tmp_dir/bin/$stub" |
| 13 | + chmod +x "$tmp_dir/bin/$stub" |
| 14 | +done |
| 15 | + |
| 16 | +run_install() { |
| 17 | + HOME="$tmp_dir/home" PATH="$tmp_dir/bin:$PATH" \ |
| 18 | + "$ROOT/bin/omarchy-webapp-install" "$@" |
| 19 | +} |
| 20 | + |
| 21 | +run_remove() { |
| 22 | + HOME="$tmp_dir/home" PATH="$tmp_dir/bin:$PATH" OMARCHY_REMOVE_NOTIFY=false \ |
| 23 | + "$ROOT/bin/omarchy-webapp-remove" "$@" |
| 24 | +} |
| 25 | + |
| 26 | +apps_dir="$tmp_dir/home/.local/share/applications" |
| 27 | +icons_dir="$tmp_dir/home/.local/share/icons/hicolor/256x256/apps" |
| 28 | + |
| 29 | +# A URL typed into the name field is the reported way in. Every slash used to |
| 30 | +# become a directory level, leaving a launcher nothing could address. Assert on |
| 31 | +# the message: creating the launcher directly in the applications directory |
| 32 | +# already makes the redirect fail on its own, so a bare non-zero exit would pass |
| 33 | +# just as well with no validation at all. |
| 34 | +output=$(run_install "http://example.test/oops" "https://example.com" hey 2>&1) && |
| 35 | + fail "webapp install rejects a name containing a slash" |
| 36 | +[[ $output == *"App name cannot contain '/'"* ]] || |
| 37 | + fail "webapp install says why it refused a slashed name" "$output" |
| 38 | +[[ -e "$apps_dir/http:" ]] && |
| 39 | + fail "webapp install does not create a directory from a slashed name" |
| 40 | +pass "webapp install rejects a name that would nest the launcher" |
| 41 | + |
| 42 | +# The name was a path fragment until something said otherwise, so ../ climbed |
| 43 | +# out of the applications directory entirely and wrote wherever it landed. |
| 44 | +if run_install "../../../../escaped" "https://example.com" hey >/dev/null 2>&1; then |
| 45 | + fail "webapp install rejects a name that climbs out of the applications directory" |
| 46 | +fi |
| 47 | +[[ -e "$tmp_dir/escaped.desktop" ]] && |
| 48 | + fail "webapp install writes no launcher outside the applications directory" |
| 49 | +pass "webapp install refuses a name that would escape the applications directory" |
| 50 | + |
| 51 | +# The interactive prompt reads the name long before it is used as a path, and |
| 52 | +# fetches the site icon in between. Rejecting only at the write leaves that icon |
| 53 | +# behind in the user's icon theme, once per attempt. |
| 54 | +mkdir -p "$tmp_dir/ibin" |
| 55 | +cp "$tmp_dir/bin"/* "$tmp_dir/ibin/" |
| 56 | +cat >"$tmp_dir/ibin/gum" <<'STUB' |
| 57 | +#!/bin/bash |
| 58 | +count_file="${GUM_STUB_COUNT:?}" |
| 59 | +count=$(cat "$count_file" 2>/dev/null || echo 0) |
| 60 | +count=$((count + 1)) |
| 61 | +echo "$count" >"$count_file" |
| 62 | +if (( count == 1 )); then |
| 63 | + echo "http://example.test/oops" |
| 64 | +else |
| 65 | + echo "https://example.com" |
| 66 | +fi |
| 67 | +STUB |
| 68 | +cat >"$tmp_dir/ibin/curl" <<'STUB' |
| 69 | +#!/bin/bash |
| 70 | +# Answer any download with a real PNG so the icon fetch reports success. |
| 71 | +out="" |
| 72 | +prev="" |
| 73 | +for arg in "$@"; do |
| 74 | + [[ $prev == "-o" ]] && out="$arg" |
| 75 | + prev="$arg" |
| 76 | +done |
| 77 | +if [[ -n $out ]]; then |
| 78 | + printf '%s' 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==' | base64 -d >"$out" |
| 79 | +fi |
| 80 | +STUB |
| 81 | +chmod +x "$tmp_dir/ibin/gum" "$tmp_dir/ibin/curl" |
| 82 | + |
| 83 | +if HOME="$tmp_dir/home" PATH="$tmp_dir/ibin:$PATH" \ |
| 84 | + GUM_STUB_COUNT="$tmp_dir/gum-count" \ |
| 85 | + "$ROOT/bin/omarchy-webapp-install" >/dev/null 2>&1; then |
| 86 | + fail "interactive webapp install rejects a name containing a slash" |
| 87 | +fi |
| 88 | +if compgen -G "$icons_dir/*.png" >/dev/null; then |
| 89 | + fail "interactive webapp install downloads no icon for a name it refuses" \ |
| 90 | + "$(ls "$icons_dir")" |
| 91 | +fi |
| 92 | +pass "webapp install refuses a slashed name before fetching its icon" |
| 93 | + |
| 94 | +# A normal name still installs and removes. |
| 95 | +run_install "Example App" "https://example.com" hey >/dev/null |
| 96 | +[[ -f "$apps_dir/Example App.desktop" ]] || |
| 97 | + fail "webapp install writes the launcher for an ordinary name" |
| 98 | +run_remove "Example App" >/dev/null |
| 99 | +[[ -f "$apps_dir/Example App.desktop" ]] && |
| 100 | + fail "webapp remove deletes the launcher it installed" |
| 101 | +pass "webapp install and remove round-trip an ordinary name" |
| 102 | + |
| 103 | +# Anything installed by an older version can still be nested. Removal has to |
| 104 | +# reach it, which a path rebuilt from the displayed name never could. |
| 105 | +mkdir -p "$apps_dir/http:/127.0.0.1:4000" |
| 106 | +cat >"$apps_dir/http:/127.0.0.1:4000/.desktop" <<'DESKTOP' |
| 107 | +[Desktop Entry] |
| 108 | +Name=http://127.0.0.1:4000 |
| 109 | +Exec=omarchy-launch-webapp https://127.0.0.1:4000 |
| 110 | +Type=Application |
| 111 | +DESKTOP |
| 112 | + |
| 113 | +# This is the name the picker shows for that file: the script strips .desktop |
| 114 | +# from the path and then takes the basename, which lands on the directory. |
| 115 | +run_remove "127.0.0.1:4000" >/dev/null |
| 116 | +[[ -f "$apps_dir/http:/127.0.0.1:4000/.desktop" ]] && |
| 117 | + fail "webapp remove deletes a launcher left nested by an older install" |
| 118 | +pass "webapp remove reaches a nested legacy launcher" |
| 119 | + |
| 120 | +# Removing by name on a machine with no applications directory yet must stay |
| 121 | +# quiet: omarchy-remove-gaming-xbox-cloud calls it without hiding stderr. |
| 122 | +noise=$(HOME="$tmp_dir/empty" PATH="$tmp_dir/bin:$PATH" OMARCHY_REMOVE_NOTIFY=false \ |
| 123 | + "$ROOT/bin/omarchy-webapp-remove" "Xbox Cloud Gaming" 2>&1 >/dev/null) |
| 124 | +[[ -n $noise ]] && |
| 125 | + fail "webapp remove stays quiet with no applications directory" "$noise" |
| 126 | +pass "webapp remove stays quiet when there is no applications directory" |
0 commit comments