summaryrefslogtreecommitdiff
path: root/test/testenv
diff options
context:
space:
mode:
Diffstat (limited to 'test/testenv')
-rwxr-xr-xtest/testenv60
1 files changed, 60 insertions, 0 deletions
diff --git a/test/testenv b/test/testenv
new file mode 100755
index 0000000..e6c882f
--- /dev/null
+++ b/test/testenv
@@ -0,0 +1,60 @@
+#!/bin/bash
+
+# Parse the arguments
+NETWORK=true
+SUDO=true
+while [[ $# -gt 0 ]]; do
+ case "$1" in
+ --no-network) shift; unset NETWORK;;
+ --network) shift; NETWORK=true;;
+ --no-sudo) shift; unset SUDO;;
+ --sudo) shift; SUDO=true;;
+ --) shift; break;;
+ *) break;;
+ esac
+done
+export NETWORK SUDO
+
+# Set up the working directory, and add the hook to clean it up
+export TMPDIR="$(mktemp --tmpdir -d libretools-test.XXXXXXXXXX)"
+trap "rm -rf '$TMPDIR'" EXIT
+
+# Set up the install to work with
+destdir=$TMPDIR/destdir
+
+old_pwd="$(pwd)"
+if [[ -f $0 ]]; then
+ cd "${0%/*}"
+fi
+make -C .. install DESTDIR=$destdir &>/dev/null || {
+ echo 'error creating local install, cannot run tests'
+ exit 1
+}
+cd "$old_pwd"
+
+# Set up the environment
+export PATH="$destdir/usr/bin:$PATH"
+export LIBRETOOLS_LIBDIR="$destdir/usr/lib/libretools"
+export HOME=$TMPDIR/home
+export XDG_CACHE_HOME="$HOME/.cache"
+export XDG_CONFIG_HOME="$HOME/.config"
+
+# Hack to respect our variables in sudo
+_sudo() {
+ local vars=(TMPDIR PATH LIBRETOOLS_LIBDIR XDG_CACHE_HOME XDG_CONFIG_HOME)
+ local args=()
+ local var
+ for var in "${vars[@]}"; do
+ args+=("$var=${!var}")
+ done
+ sudo env "${args[@]}" "$@"
+}
+printf '%s\n' \
+ '#!/bin/bash' \
+ "$(declare -f _sudo)" \
+ '_sudo "$@"' \
+ > "$destdir/usr/bin/testsudo"
+chmod 755 "$destdir/usr/bin/testsudo"
+
+# Run the tests
+eval "$@"