summaryrefslogtreecommitdiff
path: root/test/test-common.sh
blob: c1d050a09f17c24815a15ea2d3b29034ab839d83 (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
#!/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
		local chroottype=$(stat -f -c %T "$chrootdir")
		if [[ "$chroottype" == btrfs ]]; then
			sudo find "$chrootdir" -type d -print0 |
				LC_ALL=C sort -rz |
				xargs -r0 sudo -n \
				      btrfs subvolume delete &>/dev/null || true
		fi
		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 libremessages in_array "btrfs" "$@" && ! [[ "$(stat -f -c %T "$chrootdir" 2>/dev/null || true)" == 'btrfs' ]]; then
		missing+=('btrfs')
	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 "$@"
)