blob: f4fcaaa8a81724eb001a560b15253ceb876c6b71 (
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
|
#!/bin/bash
# gentoo-infra: infra/githooks.git:local/tests/lib.sh
# 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 branch, presuming it's a new branch.
# $1 - branch name
run_test_branch() {
local branch=${1}
(
set -- "refs/heads/${branch}" 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_branch "${branch}"
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_branch "${branch}"); 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 |= ${?} ))
}
|