summaryrefslogtreecommitdiff
path: root/test/testenv
blob: a2e1699d661d04832c6938c43d87d467db761b70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env bash
{
	set -e

	# 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

	if [[ $# == 0 ]]; then
		echo 'You need to run testenv with arguments!' >&2
		exit 1
	fi

	# Set up the working directory, and add the hook to clean it up
	export TMPDIR="$(mktemp --tmpdir -d libretools-test.XXXXXXXXXX)"
	cleanup() {
		set +e
		# coordinate this with ./lib/common.bash
		if [[ $SUDO ]] && [[ -d "$TMPDIR/chroots" ]]; then
			if [[ "$(stat -f -c %T "$TMPDIR/chroots")" == btrfs ]]; then
				sudo find "$TMPDIR/chroots" -depth -inum 256 -exec \
				     btrfs subvolume delete {} \; &>/dev/null
			fi
			sudo rm -rf -- "$TMPDIR/chroots"
		fi
		rm -rf -- "$TMPDIR"
	}
	trap cleanup EXIT

	# Set up the install to work with
	destdir=$TMPDIR/destdir

	old_pwd="$(pwd)"
	if [[ -f $0 ]]; then
		cd "${0%/*}"
	fi
	if ! make -C .. install prefix=/usr sysconfdir=/etc DESTDIR="$destdir" &>"$TMPDIR/make-output"; then
		echo 'error creating local install, cannot run tests' >&2
		cat "$TMPDIR/make-output"
		exit 1
	fi
	cd "$old_pwd"

	# Set up the environment
	export PATH="$destdir/usr/bin:$PATH"
	export LIBRETOOLS_LIBDIR="$destdir/usr/lib/libretools"
	export _librelib_conf_sh_sysconfdir="$destdir/etc"
	export _librelib_conf_sh_pkgconfdir="$destdir/etc/libretools.d"
	sed -i 's,/usr/bin/librefetch,$(which librefetch),' \
	    "${_librelib_conf_sh_sysconfdir}/makepkg.d/librefetch.conf"

	# Hack to respect our variables in sudo
	_sudo() {
		local vars=(
			TMPDIR
			GNUPGHOME XDG_CACHE_HOME XDG_CONFIG_HOME
			PATH LIBRETOOLS_LIBDIR _librelib_conf_sh_sysconfdir
			GPGKEY
		)
		local env=()
		local var
		for var in "${vars[@]}"; do
			env+=("$var=${!var}")
		done
		sudo "${env[@]}" "$@"
	}
	printf '%s\n' \
		'#!/bin/bash' \
		"$(declare -f _sudo)" \
		'_sudo "$@"' \
		> "$destdir/usr/bin/testsudo"
	chmod 755 "$destdir/usr/bin/testsudo"
	# Hack to work around GnuPG being stupid with locating gpg-agent's socket
	install -Dm755 /dev/stdin "$destdir/usr/bin/gpg" <<-'eot'
		#!/bin/bash
		export GNUPGHOME="${GNUPGHOME:-$HOME/.gnupg}"
		if [[ $GNUPGHOME = "$HOME/.gnupg" ]]; then
			export HOME=/var/empty
		fi
		exec /usr/bin/gpg "$@"
		eot
	install -Dm755 /dev/stdin "$destdir/usr/bin/gpg-connect-agent" <<-'eot'
		#!/bin/bash
		export GNUPGHOME="${GNUPGHOME:-$HOME/.gnupg}"
		if [[ $GNUPGHOME = "$HOME/.gnupg" ]]; then
			export HOME=/var/empty
		fi
		exec /usr/bin/gpg-connect-agent "$@"
		eot

	# Run the tests
	command -- "$@"
}