blob: 12160a7a9e2d63586de5e46814af68fcb461f004 (
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
|
#!/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 hook for all commits since the initial commit.
# Expect success.
test_success() {
run_test
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 |= ${?} ))
}
|