summaryrefslogtreecommitdiff
path: root/test/lib/common.bash
blob: 1a16ae05d0663e44dc4325a70b6c592085bc5227 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/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_setup() {
	tmpdir="$(mktemp -d --tmpdir test.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
		WORKDIR=${tmpdir@Q}/workdir
		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
	install -Dm644 /dev/stdin "$XDG_CONFIG_HOME"/pacman/makepkg.conf <<-eot
		PKGDEST=${tmpdir@Q}/workdir/pkgdest
		SRCDEST=${tmpdir@Q}/workdir/srcdest
		SRCPKGDEST=${tmpdir@Q}/workdir/srcpkgdest
		LOGDEST=${tmpdir@Q}/workdir/logdest
		PACKAGER='Bob Tester <tester@localhost>'
		eot
	mkdir -p -- "$tmpdir"/workdir/{pkg,src,srcpkg,log}dest
}

common_teardown() {
	gpg-connect-agent KILLAGENT /bye || true
	if [[ -n ${tmpdir:-} ]]; then
		if [[ -f "$tmpdir/.used-sudo" ]]; then
			sudo rm -rf -- "$tmpdir"
		else
			rm -rf -- "$tmpdir"
		fi
	fi
	# Clean up non-root chroots.  On btrfs it wouldn't be terrible
	# to wait until the end because CoW means that they don't take
	# up much space, but on other filesystems, we don't want to
	# require such a large disk.
	if [[ -n ${chrootdir:-} ]]; then
		local dir
		for dir in "$chrootdir"/*/*/; do
			if [[ -d $dir && $dir != */root/ ]]; then
				if [[ "$(stat -f -c %T "$dir")" == btrfs ]]; then
					sudo find "$dir" -depth -inum 256 -exec \
					     btrfs subvolume delete {} \; &>/dev/null
				fi
				sudo rm -rf -- "$dir"
			fi
		done
	fi
}

setup() {
	common_setup
}

teardown() {
	common_teardown
}

# 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
		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 ]]
}