Files
2026-11-04 10:11:29 -07:00

95 lines
2.9 KiB
Bash

#!/bin/bash
# Shared setup for the bats tests, after CoreDesktop's: each test gets its own
# HOME and a bin dir at the front of PATH for fake commands. On top of that,
# HOME holds a fake CoreDesktop: an upstream repo, the deployed copy cloned
# from it, and a working copy at ~/Projects/coredesktop.
# Used by the .bats files that load this one
# shellcheck disable=SC2034
PROJECT="$(cd "$BATS_TEST_DIRNAME/.." && pwd)"
# UPSTREAM, DEPLOYED and WC are used by the .bats files
# shellcheck disable=SC2034
common_setup() {
export HOME="$BATS_TEST_TMPDIR/home"
export STUB_BIN="$BATS_TEST_TMPDIR/bin"
export STUB_LOG="$BATS_TEST_TMPDIR/stub.log"
export TMPDIR="$BATS_TEST_TMPDIR/tmp"
mkdir -p "$HOME" "$STUB_BIN" "$TMPDIR"
: > "$STUB_LOG"
export PATH="$STUB_BIN:$PATH"
unset XDG_CONFIG_HOME XDG_CACHE_HOME FLATPAK_ID COREDESKTOP_DEV_VIA_FLATPAK
export GIT_AUTHOR_NAME=Test GIT_AUTHOR_EMAIL=test@localhost
export GIT_COMMITTER_NAME=Test GIT_COMMITTER_EMAIL=test@localhost
export GIT_CONFIG_NOSYSTEM=1
UPSTREAM="$BATS_TEST_TMPDIR/upstream.git"
DEPLOYED="$HOME/.local/share/coredesktop"
WC="$HOME/Projects/coredesktop"
make_coredesktop
}
setup() {
common_setup
}
# A CoreDesktop upstream with one commit, and the deployed copy cloned from it.
# The fake utils.sh logs install_packages instead of installing anything.
make_coredesktop() {
local src="$BATS_TEST_TMPDIR/src"
git init -q --initial-branch=main "$src"
mkdir -p "$src/updates.d"
printf '%s\n' '#!/bin/bash' 'echo update' > "$src/update"
chmod +x "$src/update"
touch "$src/updates.d/.keep"
printf '%s\n' '#!/bin/bash' 'install_packages() { echo "install_packages $*" >> "$STUB_LOG"; }' \
> "$src/utils.sh"
printf 'check:\n\ttrue\n' > "$src/Makefile"
git -C "$src" add -A
git -C "$src" commit -qm "CoreDesktop"
git clone -q --bare "$src" "$UPSTREAM"
rm -rf "$src"
git clone -q "$UPSTREAM" "$DEPLOYED"
}
# Clone the working copy and point the settings at it
make_working_copy() {
git clone -q "$UPSTREAM" "$WC"
mkdir -p "$HOME/.config/coredesktop-dev"
printf 'WORKING_COPY=%q\n' "$WC" > "$HOME/.config/coredesktop-dev/config"
}
# Run the coredesktop-dev command
cdd() {
"$PROJECT/bin/coredesktop-dev" "$@"
}
# stub NAME [BODY]: put a fake NAME on PATH that appends "NAME args..." to
# $STUB_LOG and then runs BODY, a bash snippet that sees the args as "$@"
stub() {
local name="$1" body="${2:-}"
cat > "$STUB_BIN/$name" << EOF
#!/bin/bash
echo "$name \$*" >> "\$STUB_LOG"
$body
EOF
chmod +x "$STUB_BIN/$name"
}
# called NAME ARGS...: true if the stub log has exactly the line "NAME ARGS..."
called() {
grep -qxF "$*" "$STUB_LOG"
}
# not_called NAME: true if the stub NAME never ran
not_called() {
! grep -q "^$1 " "$STUB_LOG"
}
# uncalled NAME ARGS...: the opposite of called. (A bare "! called ..." would
# not fail the test: bats, like set -e, ignores the status of a ! command.)
uncalled() {
! called "$@"
}