summaryrefslogtreecommitdiff
path: root/test/test-common.sh
blob: 81c8557e2add63ff4c799863dacdc6f5cc334075 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/hint/bash

if [[ -z $LIBRETOOLS_LIBDIR || -z $_librelib_conf_sh_sysconfdir || -z $_librelib_conf_sh_pkgconfdir ]]; then
	libremessages error 'Must be run with ./testenv'
	exit 1
fi

# per-test setup/teardown ############################################

common_before() {
	tmpdir="$(mktemp -d --tmpdir "test-${roundup_desc//\//-}.${roundup_test_name}.XXXXXXXXXXXX")"
	chmod 755 "$tmpdir"

	status=0

	# Clear the list of makepkg variables
	unset PKGDEST SRCDEST SRCPKGDEST LOGDEST BUILDDIR PKGEXT SRCEXT GPGKEY PACKAGER CARCH

	# Set up a test HOME
	export HOME="$tmpdir/home"
	export GNUPGHOME="$HOME/.gnupg"
	export XDG_CACHE_HOME="$HOME/.cache"
	export XDG_CONFIG_HOME="$HOME/.config"

	# Create a GPGKEY
	mkdir -p -- "$GNUPGHOME"
	chmod 700 -- "$GNUPGHOME"
	gpg --quiet --no-tty --batch --gen-key <<-eot
		Key-Type: default
		Key-Usage: sign
		Name-Real: Bob Tester
		Name-Email: tester@localhost
		Expire-Date: 0
		%no-protection
		%commit
		eot
	export GPGKEY="$(gpg --quiet --list-secret-keys --with-colons | awk -F: '/^sec:/{print substr($5,9)}')"

	# Configure libretools
	export chrootdir="${chrootdir:-$TMPDIR/chroots}"

	install -Dm644 /dev/stdin "$XDG_CONFIG_HOME"/libretools/libretools.conf <<-eot
		BLACKLIST=https://git.parabola.nu/blacklist.git/plain/blacklist.txt
		eot
	install -Dm644 /dev/stdin "$XDG_CONFIG_HOME"/libretools/chroot.conf <<-eot
		CHROOTDIR=${chrootdir@Q}
		CHROOT=default
		CHROOTEXTRAPKG=()
		eot
}

common_after() {
	gpg-connect-agent KILLAGENT /bye || true
	if [[ -f "$tmpdir/.used-sudo" ]]; then
		sudo rm -rf -- "$tmpdir"
	else
		rm -rf -- "$tmpdir"
	fi
}

before() {
	common_before
}

after() {
	common_after
}

# Utility functions for use in test definitions ######################

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
	if libremessages in_array "sudo" "$@"; then
		touch "$tmpdir/.used-sudo"
	fi
	return 0
)

empty() {
	diff -u /dev/null "$1"
}

# Just using '!' doesn't trip `set -e`
not() (
	set +x
	# we don't care about what is in the file on 'not empty'
	# checks, so redefine 'empty' to be a bit quieter.
	empty() {
		[[ $(stat -c %s "$1") -eq 0 ]]
	}
	! eval "$@"
)

# Plain command substitution would remove trailing whitespace, despite
# being significant when testing for newline-terminated lines.
equals() {
	local stdin
	IFS= read -rd '' stdin || :
	[[ $1 == "$stdin" ]]
}

globfile() {
	[[ -f $1 ]]
}