-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
polyglot-release-test
executable file
·203 lines (188 loc) · 5.4 KB
/
polyglot-release-test
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
#!/bin/bash
set -e
shellcheck polyglot-release*
set +e
function realpath() {
# See the caveats on https://formulae.brew.sh/formula/coreutils
if command -v "grealpath" >/dev/null; then
command grealpath "$@"
else
command realpath "$@"
fi
}
SRC=$(realpath .)
GNUPGHOME=$(mktemp -d)
export GNUPGHOME
function import_gpg_key() {
gpg --homedir="$GNUPGHOME" --batch -q --fast-import "$SRC/tests/gpg-key.private.pgp"
GPG_KEY_ID=DE503E8A237FCB730B01997E17879BF1F32682BC
}
function normalize_git_sha() {
# shellcheck disable=SC2001
sed -e 's/[0-9a-f]\{40\}/<a-git-sha>/g' </dev/stdin
}
function setup_git_repos() {
fixture=$1
mkdir origin
pushd origin >/dev/null || exit 1
git init --quiet --bare
git symbolic-ref HEAD refs/heads/main
popd >/dev/null || exit 1
git clone origin local --quiet 2>/dev/null
pushd local >/dev/null || exit 1
git checkout --quiet -b main
git config user.email "[email protected]"
git config user.name "Cucumber tests"
git config user.signingkey "$GPG_KEY_ID"
cp -R "$fixture"/. .
git add .
git commit --gpg-sign --message "Initial commit" --quiet
git tag v0.0.1
git push --set-upstream origin main --quiet
git push --tags --quiet
popd >/dev/null || exit 1
}
function setup_polyglot_release_git_repo() {
mkdir "polyglot-release.git"
pushd "polyglot-release.git" >/dev/null || exit 1
git init --quiet -b main
git config user.email "[email protected]"
git config user.name "Polyglot Release Author"
git config user.signingkey "$GPG_KEY_ID"
git commit --allow-empty --message "Initial commit" --quiet
git tag v0.0.1
popd >/dev/null || exit 1
}
function get_fixture() {
test=$1
header="$(head -n 1 "$test")"
fixture_pattern="# fixture: (.+)"
if [[ $header =~ $fixture_pattern ]]; then
fixture="${BASH_REMATCH[1]}"
else
fixture=polyglot
fi
echo "$fixture"
}
function setup_workdir() {
workdir=$1
fixture=$2
pushd "$workdir" >/dev/null || exit 1
setup_git_repos "$fixture"
setup_polyglot_release_git_repo
popd >/dev/null || exit 1
}
function manual_test() {
fixture=$1
workdir=$(mktemp -d)
import_gpg_key
setup_workdir "$workdir" "$fixture"
pushd "$workdir/local" >/dev/null || exit 1
PATH=$SRC:$PATH \
POLYGLOT_RELEASE_GIT_REPO="$workdir/polyglot-release.git" \
RELEASE_DATE=2000-01-01 \
GIT_CONFIG_GLOBAL=/dev/null \
BASH_SILENCE_DEPRECATION_WARNING=1 \
/bin/bash --init-file <(echo echo "You are in a manual test sandbox. You can safely git push and commits will be pushed to the repo in ../origin")
popd >/dev/null || exit 1
}
function run_test() {
test=$1
test_failed=
echo -n "${test##*/} "
workdir=$(mktemp -d)
fixture=$(realpath "./tests/fixtures/$(get_fixture "$test")")
import_gpg_key
setup_workdir "$workdir" "$fixture" "polyglot-release.git"
pushd "$workdir/local" >/dev/null || exit 1
PATH=$SRC:$PATH \
POLYGLOT_RELEASE_GIT_REPO="$workdir/polyglot-release.git" \
RELEASE_DATE=2000-01-01 \
GIT_CONFIG_GLOBAL=/dev/null \
/bin/bash "$test" \
3>"$test.actual.output" \
1> >(tee "$test.actual.stdout" >&3) \
2> >(tee "$test.actual.stderr" >&3)
echo $? >"$test.actual.exit-status"
normalize_git_sha <"$test.actual.output" >"$test.actual.output-normalized"
first_commit=$(git log main --pretty=%H --reverse | head -n1)
git log \
--reverse \
--patch \
--unified=0 \
--pretty=%n%x2A%x2A%n%s%n%n%b \
"$first_commit..HEAD" |
sed -e '/^index/d' \
>"$test.actual.git-commits"
git diff --unified=0 |
sed -e '/^index/d' \
>"$test.actual.git-diff"
git log --format="%s %d" \
>"$test.actual.git-log"
popd >/dev/null || exit 1
pushd "$workdir/origin" >/dev/null || exit 1
git log \
--format="%s %d %GS" \
>"$test.actual.origin-git-log"
popd >/dev/null || exit 1
for type in output output-normalized git-commits git-diff git-log exit-status stderr stdout origin-git-log; do
if [ -f "$test.expected.$type" ]; then
if [[ $(diff "$test.expected.$type" "$test.actual.$type") ]]; then
if [ -z "$test_failed" ]; then
echo "🔴"
fi
echo
echo Actual $type output different to expected!
echo
realpath --relative-to="$SRC" "$test.expected.$type"
realpath --relative-to="$SRC" "$test.actual.$type"
echo
diff "$test.expected.$type" "$test.actual.$type"
test_failed="true"
fi
fi
done
if [ -n "$test_failed" ]; then
echo
echo "Working directory: $workdir"
echo "GNUPGHOME: $GNUPGHOME"
echo
return 1
else
echo "✅"
return 0
fi
}
# test the tests!
if [[ ! $* =~ test-the-tests ]]; then
echo -n "testing the tests "
if $0 tests/.test-the-tests.sh >/dev/null; then
echo "🔴"
echo "Expected that testing tests/.test-the-tests.sh would fail, but it didn't!"
echo "This suggests the test runner script itself ($0) is not working properly."
else
echo "✅"
fi
fi
if [[ $1 =~ fixtures/ ]]; then
# Start a manual test session
manual_test "$(realpath "$1")"
exit 0
fi
# Run all tests
if [[ "$1" ]]; then
tests=$(realpath "$1")
else
tests="$(realpath tests)/*.sh"
fi
total_tests=0
total_failed=0
for test in $tests; do
if [[ ! "$1" || $(realpath "$1") == "$test" ]]; then
((total_tests = total_tests + 1))
run_test "$test" || ((total_failed = total_failed + 1))
fi
done
echo
echo "$total_tests tests, $total_failed failed"
test "$total_failed" -eq "0"