diff --git a/bin/omarchy-pkg-add b/bin/omarchy-pkg-add index 1f5e4091..887499ee 100755 --- a/bin/omarchy-pkg-add +++ b/bin/omarchy-pkg-add @@ -5,11 +5,17 @@ # omarchy:examples=omarchy pkg add jq ripgrep # omarchy:requires-sudo=true -if omarchy-pkg-missing "$@"; then +# Only the missing ones: an installed package from no repository is a "target not found" to pacman -S. +missing=() +for pkg in "$@"; do + omarchy-pkg-missing "$pkg" && missing+=("$pkg") +done + +if (( ${#missing[@]} > 0 )); then if (( EUID == 0 )); then - pacman -S --noconfirm --needed -- "$@" || exit 1 + pacman -S --noconfirm --needed -- "${missing[@]}" || exit 1 else - sudo pacman -S --noconfirm --needed -- "$@" || exit 1 + sudo pacman -S --noconfirm --needed -- "${missing[@]}" || exit 1 fi fi diff --git a/test/shell.d/pkg-add-test.sh b/test/shell.d/pkg-add-test.sh new file mode 100644 index 00000000..660c4ef7 --- /dev/null +++ b/test/shell.d/pkg-add-test.sh @@ -0,0 +1,39 @@ +#!/bin/bash + +set -euo pipefail + +source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" + +test_tmp=$(mktemp -d) +trap 'rm -rf "$test_tmp"' EXIT + +mock_path="$test_tmp/bin" +mkdir -p "$mock_path" +cat >"$mock_path/pacman" <<'MOCK' +#!/bin/bash +if [[ $1 == "-Q" ]]; then + [[ " $TEST_INSTALLED " == *" $3 "* ]] + exit +fi +printf '%s\n' "$*" >>"$TEST_TMP/pacman-sync" +for arg in "$@"; do + [[ $arg == -* ]] || TEST_INSTALLED+=" $arg" +done +printf '%s\n' "$TEST_INSTALLED" >"$TEST_TMP/installed" +MOCK +cat >"$mock_path/sudo" <<'MOCK' +#!/bin/bash +exec "$@" +MOCK +chmod +x "$mock_path"/* +export PATH="$mock_path:$ROOT/bin:$PATH" TEST_TMP="$test_tmp" + +TEST_INSTALLED="local-only fcitx5" "$ROOT/bin/omarchy-pkg-add" fcitx5-rime local-only opencc >/dev/null 2>&1 || true +[[ $(<"$test_tmp/pacman-sync") == "-S --noconfirm --needed -- fcitx5-rime opencc" ]] || + fail "pkg-add asks pacman only for the missing packages" "$(<"$test_tmp/pacman-sync")" +pass "an installed package from no repository is left out of the pacman -S call" + +rm -f "$test_tmp/pacman-sync" +TEST_INSTALLED="local-only fcitx5" "$ROOT/bin/omarchy-pkg-add" local-only fcitx5 +[[ ! -e $test_tmp/pacman-sync ]] || fail "pkg-add runs no transaction when nothing is missing" "$(<"$test_tmp/pacman-sync")" +pass "pkg-add runs no transaction when everything is installed"