-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathtest-merge-pull-request.sh
More file actions
executable file
·266 lines (228 loc) · 8.98 KB
/
Copy pathtest-merge-pull-request.sh
File metadata and controls
executable file
·266 lines (228 loc) · 8.98 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env bash
# Deterministic integration tests for the repository-local merge workflow wrapper.
set -euo pipefail
PROJECT_ROOT=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/../../../.." && pwd)
TEST_DIRECTORY=$(mktemp -d "${TMPDIR:-/tmp}/test-merge-pull-request.XXXXXX")
trap 'rm -rf "${TEST_DIRECTORY}"' EXIT
create_fixture() {
local fixture_name=$1
local fixture_root="${TEST_DIRECTORY}/${fixture_name}"
mkdir -p "${fixture_root}/contrib/dev-tools/git"
cp "${PROJECT_ROOT}/contrib/dev-tools/git/merge-pull-request.sh" "${fixture_root}/contrib/dev-tools/git/"
cp "${PROJECT_ROOT}/contrib/dev-tools/git/github-merge.py" "${fixture_root}/contrib/dev-tools/git/"
chmod +x "${fixture_root}/contrib/dev-tools/git/merge-pull-request.sh"
(
cd "${fixture_root}"
git init --quiet --initial-branch=develop
git config user.name "Merge workflow test"
git config user.email "merge-workflow-test@example.com"
printf 'fixture\n' >README.md
git add .
git -c commit.gpgsign=false -c core.hooksPath=/dev/null commit --quiet -m 'Initial fixture'
git config githubmerge.repository torrust/torrust-tracker
git config githubmerge.branch develop
git config user.signingkey 0123456789ABCDEF
)
printf '%s\n' "${fixture_root}"
}
it_should_pass_deterministic_preflight_when_repository_state_is_supported() {
# Arrange
local fixture_root
fixture_root=$(create_fixture "valid-preflight")
local output_file="${TEST_DIRECTORY}/valid-preflight-output.txt"
# Act
(
cd "${fixture_root}"
./contrib/dev-tools/git/merge-pull-request.sh --dry-run 2022 >"${output_file}"
)
# Assert
grep -F -q 'Dry-run preflight passed for torrust/torrust-tracker PR 2022 targeting develop.' "${output_file}"
}
it_should_refuse_a_dirty_working_tree_without_invoking_the_vendored_tool() {
# Arrange
local fixture_root
fixture_root=$(create_fixture "dirty-working-tree")
printf 'unrelated work\n' >"${fixture_root}/unrelated.txt"
local output_file="${TEST_DIRECTORY}/dirty-working-tree-output.txt"
# Act
if (
cd "${fixture_root}"
./contrib/dev-tools/git/merge-pull-request.sh --dry-run 2022 >"${output_file}" 2>&1
); then
printf 'Expected dirty-worktree preflight to fail.\n' >&2
return 1
fi
# Assert
grep -F -q 'ERROR: Working tree is not clean; preserve or stash unrelated work before merging.' "${output_file}"
[[ -f "${fixture_root}/unrelated.txt" ]]
}
it_should_refuse_a_repository_configuration_that_is_not_the_upstream_tracker() {
# Arrange
local fixture_root
fixture_root=$(create_fixture "wrong-repository")
(
cd "${fixture_root}"
git config githubmerge.repository example/other-repository
)
local output_file="${TEST_DIRECTORY}/wrong-repository-output.txt"
# Act
if (
cd "${fixture_root}"
./contrib/dev-tools/git/merge-pull-request.sh --dry-run 2022 >"${output_file}" 2>&1
); then
printf 'Expected repository preflight to fail.\n' >&2
return 1
fi
# Assert
grep -F -q "ERROR: githubmerge.repository is 'example/other-repository'; run 'git config githubmerge.repository torrust/torrust-tracker'." "${output_file}"
}
it_should_explain_how_to_configure_an_unset_repository() {
# Arrange
local fixture_root
fixture_root=$(create_fixture "unset-repository")
(
cd "${fixture_root}"
git config --unset githubmerge.repository
)
local output_file="${TEST_DIRECTORY}/unset-repository-output.txt"
# Act
if (
cd "${fixture_root}"
GIT_CONFIG_NOSYSTEM=1 \
GIT_CONFIG_GLOBAL=/dev/null \
./contrib/dev-tools/git/merge-pull-request.sh --dry-run 2022 >"${output_file}" 2>&1
); then
printf 'Expected unset repository preflight to fail.\n' >&2
return 1
fi
# Assert
grep -F -q "ERROR: githubmerge.repository is not configured; run 'git config githubmerge.repository torrust/torrust-tracker'." "${output_file}"
}
it_should_explain_how_to_configure_an_unset_signing_key() {
# Arrange
local fixture_root
fixture_root=$(create_fixture "unset-signing-key")
(
cd "${fixture_root}"
git config --unset user.signingkey
)
local output_file="${TEST_DIRECTORY}/unset-signing-key-output.txt"
# Act
if (
cd "${fixture_root}"
GIT_CONFIG_NOSYSTEM=1 \
GIT_CONFIG_GLOBAL=/dev/null \
./contrib/dev-tools/git/merge-pull-request.sh --dry-run 2022 >"${output_file}" 2>&1
); then
printf 'Expected unset signing-key preflight to fail.\n' >&2
return 1
fi
# Assert
grep -F -q "ERROR: user.signingkey is not configured; run 'git config --global user.signingkey <gpg-key-id>'." "${output_file}"
}
it_should_refuse_an_empty_signing_key() {
# Arrange
local fixture_root
fixture_root=$(create_fixture "empty-signing-key")
(
cd "${fixture_root}"
git config user.signingkey ""
)
local output_file="${TEST_DIRECTORY}/empty-signing-key-output.txt"
# Act
if (
cd "${fixture_root}"
./contrib/dev-tools/git/merge-pull-request.sh --dry-run 2022 >"${output_file}" 2>&1
); then
printf 'Expected empty signing-key preflight to fail.\n' >&2
return 1
fi
# Assert
grep -F -q "ERROR: user.signingkey is not configured; run 'git config --global user.signingkey <gpg-key-id>'." "${output_file}"
}
it_should_invoke_the_vendored_tool_with_the_fixed_target_branch_after_preflight() {
# Arrange
local fixture_root
fixture_root=$(create_fixture "vendored-tool-invocation")
local stub_directory="${TEST_DIRECTORY}/vendored-tool-bin"
mkdir -p "${stub_directory}"
cat >"${stub_directory}/python3" <<'EOF'
#!/usr/bin/env bash
printf '%s\n' "$*" >"${TEST_PYTHON_ARGUMENTS}"
EOF
chmod +x "${stub_directory}/python3"
# Act
(
cd "${fixture_root}"
PATH="${stub_directory}:${PATH}" \
TEST_PYTHON_ARGUMENTS="${fixture_root}/python-arguments.txt" \
./contrib/dev-tools/git/merge-pull-request.sh 2022
)
# Assert
grep -F -q 'contrib/dev-tools/git/github-merge.py 2022 develop' "${fixture_root}/python-arguments.txt"
}
it_should_refuse_to_invoke_a_missing_vendored_tool() {
# Arrange
local fixture_root
fixture_root=$(create_fixture "missing-vendored-tool")
rm "${fixture_root}/contrib/dev-tools/git/github-merge.py"
local output_file="${TEST_DIRECTORY}/missing-vendored-tool-output.txt"
# Act
if (
cd "${fixture_root}"
./contrib/dev-tools/git/merge-pull-request.sh 2022 >"${output_file}" 2>&1
); then
printf 'Expected missing vendored tool preflight to fail.\n' >&2
return 1
fi
# Assert
grep -F -q "ERROR: Vendored merge tool is unavailable: '${fixture_root}/contrib/dev-tools/git/github-merge.py'." "${output_file}"
}
it_should_refuse_to_invoke_the_vendored_tool_without_python() {
# Arrange
local fixture_root
fixture_root=$(create_fixture "missing-python")
local stub_directory="${TEST_DIRECTORY}/missing-python-bin"
mkdir -p "${stub_directory}"
ln -s "$(command -v dirname)" "${stub_directory}/dirname"
ln -s "$(command -v git)" "${stub_directory}/git"
local output_file="${TEST_DIRECTORY}/missing-python-output.txt"
# Act
if (
cd "${fixture_root}"
PATH="${stub_directory}" \
/bin/bash ./contrib/dev-tools/git/merge-pull-request.sh 2022 >"${output_file}" 2>&1
); then
printf 'Expected missing Python preflight to fail.\n' >&2
return 1
fi
# Assert
grep -F -q 'ERROR: python3 is required to run the vendored merge tool; install Python 3 and retry.' "${output_file}"
}
it_should_reject_a_non_positive_pull_request_number_before_performing_work() {
# Arrange
local fixture_root
fixture_root=$(create_fixture "invalid-pull-request")
local output_file="${TEST_DIRECTORY}/invalid-pull-request-output.txt"
# Act
if (
cd "${fixture_root}"
./contrib/dev-tools/git/merge-pull-request.sh --dry-run 0 >"${output_file}" 2>&1
); then
printf 'Expected invalid pull request input to fail.\n' >&2
return 1
fi
# Assert
grep -F -q 'ERROR: PULL_REQUEST must be a positive integer.' "${output_file}"
}
it_should_pass_deterministic_preflight_when_repository_state_is_supported
it_should_refuse_a_dirty_working_tree_without_invoking_the_vendored_tool
it_should_refuse_a_repository_configuration_that_is_not_the_upstream_tracker
it_should_explain_how_to_configure_an_unset_repository
it_should_explain_how_to_configure_an_unset_signing_key
it_should_refuse_an_empty_signing_key
it_should_invoke_the_vendored_tool_with_the_fixed_target_branch_after_preflight
it_should_refuse_to_invoke_a_missing_vendored_tool
it_should_refuse_to_invoke_the_vendored_tool_without_python
it_should_reject_a_non_positive_pull_request_number_before_performing_work
printf 'All merge workflow wrapper tests passed.\n'