-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
run-test.sh
executable file
·271 lines (246 loc) · 8.98 KB
/
run-test.sh
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
267
268
269
270
271
#!/usr/bin/env bash
# This is the entrypoint to the testing system, written for Baystation12 and
# inspired by Rust's configure system
#
# The tests are split up into groups by the `case` at the bottom, and the
# group(s) to run are selected by the TEST environment variable. Right now,
# there are 4 test groups:
# - ALL: Run all tests
# - CODE: Run code quality checks
# - WEB: Run tgui tests
# - MAP: Run map tests (notably, only this one compiles!)
#
# Additionally, the MAP group requires an additional environent variable,
# MAP_PATH, to be set. This variable is passed to the compiler to indicate which
# map to test. You will want to configure CI to run each of these passes, and
# run MAP several times with MAP_PATH set to each map that should be tested.
#
# The general structure of the test execution is as follows:
# - find_code: Look for the project root directory and fail fast if
# it can't be found. Assumes that it is in . or ..;
# custom locations can be specified in CODEPATH
# environment variable.
# - run_configured_tests: Evaluate TEST variable, call into appropriate test
# runner function.
# - run_all_tests: Run every test group in sequence.
# - run_xxx_tests: Run the tests for $xxx, doing any necessary setup
# first, including calling find_xxx_deps.
# - find_xxx_deps: Using need_cmd, ensure that programs needed to run
# tests that are part of $xxx are available.
# - need_cmd: Checks availability of command passed as the single
# argument. Fails fast if it's not available.
# - err: Prints arguments as text, exits indicating failure.
# - warn: Prints arguments as text, indicating a warning
# condition.
# - msg: Used by all printing, formats text nicely.
# - run_test: Runs a test. The first argument is the friendly name
# of the test. The remaining arguments are the shell
# command(s) to run. If a test fails, a global counter
# is incremented and a warning is emitted.
# - run_test_fail: Exactly as run_test, but considers failure of the
# command to be a successful test.
# - run_test_ci: Gates run_test to only run test when being run on a
# CI platform. This is used to gate tests that are
# destructive in some manner.
# - exec_test: Called by run_test{,fail}, actually executes the
# test and returns its resulti.
# - check_fail: Called at the end of the run, prints final report
# and sets exit status appropriately.
# !!!!!!!! Instructions for adding tests:
# In general, if you want to add a test, it will probably belong to one of the
# groups that already exists. Add it to the relevant run_xxx_tests function, and
# if it introduces any new dependencies, add them to the check_xxx_deps
# function. Some dependencies are guaranteed to be on CI platforms by outside
# means (like .github/workflows/test.yml), others will need to be installed by this script.
# You'll see plenty of examples of checking for CI and gating tests on that,
# installing instead of checking when running on CI.
#
# If you are *SURE* you need to add a new test group, you'll want to add it
# first to the case at the end of the file, and then add the run_xxx_tests and
# find_xxx_deps for it, adding things to them as appropriate. Importantly, make
# sure to also call run_xxx_tests from run_all_tests. Make sure also to call
# your find_xxx_deps from run_xxx_tests.
#
# Good luck!
# - xales
# Global counter of failed tests
FAILED=0
# List of names of failed tests
FAILED_BYNAME=()
# Global counter of passed tests
PASSED=0
# Version of Node to install for tgui
NODE_VERSION=4
function msg {
echo -e "\t\e[34mtest\e[0m: $*"
}
function msg_bad {
echo -e "\e[31m$*\e[0m"
}
function msg_good {
echo -e "\e[32m$*\e[0m"
}
function msg_meh {
echo -e "\e[33m$*\e[0m"
}
function warn {
msg_meh "WARNING: $*"
}
function err {
msg_bad "error: $*"
exit 1
}
function fail {
warn "test \"$1\" failed: $2"
((FAILED ))
FAILED_BYNAME =("$1")
}
function need_cmd {
if command -v $1 >/dev/null 2>&1
then msg "found '$1'"
else err "program '$1' is missing, please install it"
fi
}
function run_test {
msg "running \"$1\""
name=$1
shift
exec_test "$*"
ret=$?
if [[ ret -ne 0 ]]
then fail "$name" $ret
else ((PASSED ))
fi
}
function run_test_fail {
msg "running(fail) \"$1\""
name=$1
shift
exec_test "$*"
ret=$?
if [[ ret -eq 0 ]]
then fail "$name" $ret
else ((PASSED ))
fi
}
function run_test_ci {
if [[ "$CI" == "true" ]]
then run_test "$@"
else msg_meh "skipping \"$1\""
fi
}
function check_fail {
if [[ $FAILED -ne 0 ]]; then
for t in "${FAILED_BYNAME[@]}"; do
msg_bad "TEST FAILED: \"$t\""
done
err "$FAILED tests failed"
else msg_good "$PASSED tests passed"
fi
}
function exec_test {
eval "$*"
ret=$?
return $ret
}
function find_code_deps {
need_cmd grep
need_cmd awk
need_cmd md5sum
need_cmd python3
need_cmd pip
}
function find_web_deps {
need_cmd npm
[[ "$CI" != "true" ]] && need_cmd gulp
}
function find_byond_deps {
[[ "$CI" != "true" ]] && need_cmd DreamDaemon
}
function find_code {
if [[ -z ${CODEPATH x} ]]; then
if [[ -d ./code ]]
then CODEPATH=.
else if [[ -d ../code ]]
then CODEPATH=..
fi
fi
fi
cd $CODEPATH
if [[ ! -d ./code ]]
then err "invalid CODEPATH: $PWD"
else msg "found code at $PWD"
fi
}
function run_code_tests {
msg "*** running code tests ***"
find_code_deps
pip install --user PyYaml -q
pip install --user beautifulsoup4 -q
pip install --user Pillow -q
shopt -s globstar
run_test_fail "maps contain no step_[xy]" "grep 'step_[xy]' maps/**/*.dmm"
run_test_fail "maps contain no layer adjustments" "grep 'layer = ' maps/**/*.dmm"
run_test_fail "maps contain no plane adjustments" "grep 'plane = ' maps/**/*.dmm"
run_test_fail "ensure nanoui templates unique" "find nano/templates/ -type f -exec md5sum {} | sort | uniq -D -w 32 | grep nano"
run_test_fail "no invalid spans" "grep -En \"<\s*span\s class\s*=\s*('[^'>] |[^'>] ')\s*>\" **/*.dm"
run_test "code quality checks" "test/check-paths.sh"
run_test "indentation check" "awk -f tools/indentation.awk **/*.dm"
run_test "check changelog example unchanged" "md5sum -c - <<< '683a3e0d21b90581ae6e4c95052d461e *html/changelogs/example.yml'"
run_test "check tags" "python3 tools/TagMatcher/tag-matcher.py ."
run_test "check color hex" "python3 tools/ColorHexChecker/color-hex-checker.py ."
run_test "check punctuation" "python3 tools/PunctuationChecker/punctuation-checker.py ."
run_test "check icon state limit" "python3 test/check_icon_state_limit.py ."
run_test_ci "check changelog builds" "python3 tools/changelog/ss13_genchangelog.py html/changelog.html html/changelogs"
}
function run_byond_tests {
msg "*** running map tests ***"
find_byond_deps
if [[ -z "${MAP_PATH x}" ]]
then exit 1
else msg "configured map is '$MAP_PATH'"
fi
cp config/example/* config/
if [[ "$CI" == "true" ]]; then
msg "installing BYOND"
./install-byond.sh || exit 1
source ~/BYOND-${BYOND_MAJOR}.${BYOND_MINOR}/byond/bin/byondsetup
fi
run_test "build map unit tests" "scripts/dm.sh -DUNIT_TEST -M$MAP_PATH baystation12.dme"
run_test "check no warnings in build" "grep ', 0 warnings' build_log.txt"
run_test "run unit tests" "DreamDaemon baystation12.dmb -invisible -trusted -core 2>&1 | tee log.txt"
run_test "check tests passed" "grep 'All Unit Tests Passed' log.txt"
run_test "check no runtimes" "grep 'Caught 0 Runtimes' log.txt"
run_test_fail "check no runtimes 2" "grep 'runtime error:' log.txt"
run_test_fail "check no scheduler failures" "grep 'Process scheduler caught exception processing' log.txt"
run_test_fail "check no warnings" "grep 'WARNING:' log.txt"
run_test_fail "check no errors" "grep 'ERROR:' log.txt"
}
function run_all_tests {
run_code_tests
run_byond_tests
}
function run_configured_tests {
if [[ -z ${TEST z} ]]; then
msg_bad "You must provide TEST in environment; valid options ALL,MAP,WEB,CODE"
msg_meh "Note: map tests require MAP_PATH set"
exit 1
fi
case $TEST in
"ALL")
run_all_tests
;;
"MAP")
run_byond_tests
;;
"CODE")
run_code_tests
;;
*)
fail "invalid option for \$TEST: '$TEST'"
;;
esac
}
find_code
run_configured_tests
check_fail