-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test
executable file
·110 lines (97 loc) · 2.49 KB
/
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
#!/bin/bash
set -e
shellcheck ./publish-to-subrepo
set +e
src=$(realpath .)
tests="$(realpath "$src"/tests)/*.sh"
function run_test() {
test=$1
echo -n "${test##*/} "
# create main working directory
workdir=$(mktemp -d)
# create monorepo
monorepo_dir="$workdir/monorepo"
mkdir "$monorepo_dir"
pushd "$monorepo_dir" >/dev/null || exit
git init --quiet -b main
git config user.email "publish-subrepo-tests@example.com"
git config user.name "Publish Subrepo Tests"
popd >/dev/null
# create empty target subrepo
subrepo_dir="$workdir/subrepo"
mkdir "$subrepo_dir"
pushd "$subrepo_dir" >/dev/null || exit
git init --quiet -b main
git config receive.denyCurrentBranch ignore # allow us to push to a non-bare repo
popd >/dev/null
# run the test
echo -n "..."
pushd "$workdir" >/dev/null || exit
PATH=$src:$PATH \
SUBREPO="$subrepo_dir" \
GIT_CONFIG_GLOBAL=/dev/null \
bash "$test" 3>"$test.output.actual" \
1> >(tee "$test.stdout.actual" >&3) \
2> >(tee "$test.stderr.actual" >&3)
echo $? >"$test.exit-status.actual"
popd >/dev/null
echo -ne "\b\b\b \b\b\b"
# check that the subrepo contains what we expect
pushd "$subrepo_dir" >/dev/null || exit
git reset --hard --quiet # sync local index with what's been pushed
if [[ ! $(git log 2>&1) =~ "fatal" ]]; then
git log --format="%s %d" \
>"$test.subrepo-git-log.actual"
git log \
--reverse \
--patch \
--unified=0 \
--pretty=%n%x2A%x2A%n%s%n%n%b |
sed -e '/^index/d' \
>"$test.subrepo-git-commits.actual"
fi
popd >/dev/null
for type in subrepo-git-commits subrepo-git-log exit-status; do
expected="$test.$type.expected"
actual="$test.$type.actual"
if [ -f "$expected" ]; then
if [[ $(diff "$expected" "$actual") ]]; then
if [ -z "$test_failed" ]; then
echo "🔴"
fi
echo
echo Actual $type output different to expected!
echo
realpath --relative-to="$src" "$expected"
realpath --relative-to="$src" "$actual"
echo
diff "$expected" "$actual"
test_failed="true"
fi
fi
done
if [ -n "$test_failed" ]; then
echo
echo "Working directory: $workdir"
echo
return 1
else
echo "✅"
return 0
fi
}
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 "$total_tests tests, $total_failed failed"
test "$total_failed" -eq "0"