aboutsummaryrefslogtreecommitdiff
blob: e3432db82cb4e9d67837d97e19a1959e77f546ef (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/bin/bash
# Git hook test helpers
# Copyright 2018 Michał Górny
# Distributed under the terms of the GNU General Public License v2 or later

[[ -z ${RC_GOT_FUNCTIONS} ]] && . /lib/gentoo/functions.sh

die() {
	echo "died @ ${BASH_SOURCE[1]}:${BASH_LINENO[0]}" >&2
	exit 1
}

TEST_RET=0

# Starts a test.  Creates temporary git repo and enters it.
# $1 - test description (printed)
tbegin() {
	local desc=${1}
	TEST_DIR=$(mktemp -d) || die
	export GIT_DIR=${TEST_DIR}/.git

	pushd "${TEST_DIR}" >/dev/null || die
	git init -q || die
	# create an initial commit to avoid a lot of pain ;-)
	git commit -q --allow-empty -m 'empty initial commit' || die

	ebegin "${desc}"
}

# Finish a test.  Does popd and cleans up the temporary directory.
# $1 - test result (defaults to $?)
# $2 - error message (optional)
tend() {
	local ret=${1:-${?}}
	local msg=${2}

	popd >/dev/null || die
	rm -rf "${TEST_DIR}" || die

	eend "${ret}" "${msg}"
}

run_test() {
	local initial_commit
	initial_commit=$(git rev-list --all | tail -n 1) || die
	(
		set -- refs/heads/master "${initial_commit}" HEAD
		set +e
		. "${HOOK_PATH}"
	)
}

# Run the test for specified ref, presuming it's a new branch/tag.
# $1 - ref path
run_test_ref() {
	local ref=${1}

	(
		set -- "${ref}" 0000000000000000000000000000000000000000 HEAD
		set +e
		. "${HOOK_PATH}"
	)
}

# Run the hook for all commits since the initial commit.
# Expect success.
test_success() {
	run_test
	tend ${?}
	: $(( TEST_RET |= ${?} ))
}

# Run the hook presuming new branch is added.
# Expect success.
# $1 - branch name
test_branch_success() {
	local branch=${1}
	run_test_ref "refs/heads/${branch}"
	tend ${?}
	: $(( TEST_RET |= ${?} ))
}

# Run the hook presuming new tag is added.
# Expect success.
# $1 - tag name
test_tag_success() {
	local tag=${1}
	run_test_ref "refs/tags/${tag}"
	tend ${?}
	: $(( TEST_RET |= ${?} ))
}

# Run the hook for all commits since the initial commit.
# Expect failure with message matching the pattern.
# $1 - bash pattern to match
test_failure() {
	local expected=${1}
	local msg

	if msg=$(run_test); then
		tend 1 "Hook unexpectedly succeeded"
		return 1
	fi

	[[ ${msg} == ${expected} ]]
	tend ${?} "'${msg}' != '${expected}'"
	: $(( TEST_RET |= ${?} ))
}

# Run the hook presuming new branch is added.
# Expect failure with message matching the pattern.
# $1 - branch name
# $2 - bash pattern to match
test_branch_failure() {
	local branch=${1}
	local expected=${2}
	local msg

	if msg=$(run_test_ref "refs/heads/${branch}"); then
		tend 1 "Hook unexpectedly succeeded"
		return 1
	fi

	[[ ${msg} == ${expected} ]]
	tend ${?} "'${msg}' != '${expected}'"
	: $(( TEST_RET |= ${?} ))
}

# Run the hook presuming new tag is added.
# Expect failure with message matching the pattern.
# $1 - tag name
# $2 - bash pattern to match
test_tag_failure() {
	local tag=${1}
	local expected=${2}
	local msg

	if msg=$(run_test_ref "refs/tags/${tag}"); then
		tend 1 "Hook unexpectedly succeeded"
		return 1
	fi

	[[ ${msg} == ${expected} ]]
	tend ${?} "'${msg}' != '${expected}'"
	: $(( TEST_RET |= ${?} ))
}

# Run the hook presuming branch is being removed.
# Expect success (our hooks shouldn't prevent removal).
test_branch_removal() {
	(
		set -- "refs/heads/removed-branch" HEAD 0000000000000000000000000000000000000000
		set +e
		. "${HOOK_PATH}"
	)

	tend ${?}
	: $(( TEST_RET |= ${?} ))
}