diff options
Diffstat (limited to 'test/test-common.sh')
-rw-r--r-- | test/test-common.sh | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/test/test-common.sh b/test/test-common.sh new file mode 100644 index 0000000..2a8fc37 --- /dev/null +++ b/test/test-common.sh @@ -0,0 +1,78 @@ +#!/hint/bash + +if [[ $HOME == "$(eval echo ~$USER)" ]]; then + libremessages error "\$HOME is the default for %s; use testenv: %s" "$USER" "$HOME" + exit 1 +fi + +_before() { + unset PKGDEST SRCDEST SRCPKGDEST LOGDEST + unset BUILDDIR + unset PKGEXT SRCEXT + unset GPGKEY PACKAGER + tmpdir="$(mktemp -d --tmpdir "test-${roundup_desc//\//-}.XXXXXXXXXXXX")" + chmod 755 "$tmpdir" + stat=0 +} + +_after() { + rm -rf -- "$tmpdir" "$XDG_CONFIG_HOME" "$XDG_CACHE_HOME" +} + +_after_sudo() { + if [[ $SUDO ]]; then + sudo rm -rf -- "$tmpdir" "$XDG_CONFIG_HOME" "$XDG_CACHE_HOME" + else + rm -rf -- "$tmpdir" "$XDG_CONFIG_HOME" "$XDG_CACHE_HOME" + fi +} + +_setup_chrootdir() { + if [[ -z "$chrootdir" ]]; then + export chrootdir="$(mktemp -d --tmpdir "test-chrootdir.XXXXXXXXXXXX")" + trap "$(printf '_cleanup_chrootdir %q' "$chrootdir")" EXIT + fi +} + +_cleanup_chrootdir() ( + chrootdir=$1 + shopt -s nullglob + if [[ $SUDO ]]; then + for copydir in "$chrootdir"/*/*/; do + local chroottype=$(stat -f -c %T "$copydir") + if [[ "$chroottype" == btrfs ]] && ! mountpoint -q "$copydir"; then + sudo btrfs subvolume delete "$copydir" >/dev/null + fi + done + sudo rm -rf -- "$chrootdir" + else + rm -rf -- "$chrootdir" + fi +) + +require() ( + set +x + local missing=() + if libremessages in_array "network" "$@" && ! [[ $NETWORK ]]; then + missing+=('networking') + fi + if libremessages in_array "sudo" "$@" && ! [[ $SUDO ]]; then + missing+=('sudo') + fi + if (( ${#missing[@]} )); then + libremessages warning "Next test requires %s; Skipping (passing)..." "$(echo "${missing[*]}"|sed 's/ /, /g')" &>/dev/tty + return 1 + fi + return 0; +) + +empty() ( + set +x + [[ $(stat -c %s "$1") -eq 0 ]] +) + +# Just using '!' doesn't trip `set -e` +not() ( + set +x + ! eval "$@" +) |