forked from rwf2/Rocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.sh
executable file
·303 lines (257 loc) · 8.21 KB
/
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/env bash
set -e
# Brings in _ROOT, _DIR, _DIRS globals.
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "${SCRIPT_DIR}/config.sh"
# Add Cargo to PATH.
export PATH=${HOME}/.cargo/bin:${PATH}
export CARGO_INCREMENTAL=0
export RUSTC_BOOTSTRAP=1
CARGO="cargo"
# Checks that the versions for Cargo projects $@ all match
function check_versions_match() {
local last_version=""
for dir in "${@}"; do
local cargo_toml="${dir}/Cargo.toml"
if ! [ -f "${cargo_toml}" ]; then
echo "Cargo configuration file '${cargo_toml}' does not exist."
exit 1
fi
local version=$(grep version "${cargo_toml}" | head -n 1 | cut -d' ' -f3)
if [ -z "${last_version}" ]; then
last_version="${version}"
elif ! [ "${version}" = "${last_version}" ]; then
echo "Versions differ in '${cargo_toml}'. ${version} != ${last_version}"
exit 1
fi
done
}
function check_style() {
# Ensure there are no tabs in any file.
local tab=$(printf '\t')
local matches=$(git grep -PIn "${tab}" "${PROJECT_ROOT}" | grep -v 'LICENSE')
if ! [ -z "${matches}" ]; then
echo "Tab characters were found in the following:"
echo "${matches}"
exit 1
fi
# Ensure non-comment lines are under 100 characters.
local n=100
local matches=$(git grep -PIn "(?=^..{$n,}$)(?!^\s*\/\/[\/!].*$).*" '*.rs')
if ! [ -z "${matches}" ]; then
echo "Lines longer than $n characters were found in the following:"
echo "${matches}"
exit 1
fi
# Ensure there's no trailing whitespace.
local matches=$(git grep -PIn "\s+$" "${PROJECT_ROOT}" | grep -v -F '.stderr:')
if ! [ -z "${matches}" ]; then
echo "Trailing whitespace was found in the following:"
echo "${matches}"
exit 1
fi
local pattern='tail -n 1 % | grep -q "^$" && echo %'
local matches=$(git grep -z -Il '' | xargs -0 -P 16 -I % sh -c "${pattern}")
if ! [ -z "${matches}" ]; then
echo "Trailing new line(s) found in the following:"
echo "${matches}"
exit 1
fi
}
function indir() {
local dir="${1}"
shift
pushd "${dir}" > /dev/null 2>&1 ; $@ ; popd > /dev/null 2>&1
}
function test_contrib() {
DB_POOLS_FEATURES=(
deadpool_postgres
deadpool_redis
sqlx_mysql
sqlx_postgres
sqlx_sqlite
mongodb
diesel_mysql
diesel_postgres
)
SYNC_DB_POOLS_FEATURES=(
diesel_postgres_pool
diesel_sqlite_pool
diesel_mysql_pool
postgres_pool
sqlite_pool
memcache_pool
)
DYN_TEMPLATES_FEATURES=(
tera
handlebars
minijinja
)
WS_FEATURES=(
tungstenite
)
for feature in "${DB_POOLS_FEATURES[@]}"; do
echo ":: Building and testing db_pools [$feature]..."
$CARGO test -p rocket_db_pools --no-default-features --features $feature $@
done
for feature in "${SYNC_DB_POOLS_FEATURES[@]}"; do
echo ":: Building and testing sync_db_pools [$feature]..."
$CARGO test -p rocket_sync_db_pools --no-default-features --features $feature $@
done
for feature in "${DYN_TEMPLATES_FEATURES[@]}"; do
echo ":: Building and testing dyn_templates [$feature]..."
$CARGO test -p rocket_dyn_templates --no-default-features --features $feature $@
done
for feature in "${WS_FEATURES[@]}"; do
echo ":: Building and testing ws [$feature]..."
$CARGO test -p rocket_ws --no-default-features --features $feature $@
done
}
function test_core() {
FEATURES=(
tokio-macros
http2
http3-preview
secrets
tls
mtls
json
msgpack
uuid
trace
)
echo ":: Building and checking core [no features]..."
RUSTDOCFLAGS="-Zunstable-options --no-run" \
indir "${CORE_LIB_ROOT}" $CARGO test --no-default-features $@
for feature in "${FEATURES[@]}"; do
echo ":: Building and checking core [${feature}]..."
RUSTDOCFLAGS="-Zunstable-options --no-run" \
indir "${CORE_LIB_ROOT}" $CARGO test --no-default-features --features "${feature}" $@
done
}
function test_examples() {
# Cargo compiles Rocket once with the `secrets` feature enabled, so when run
# in production, we need a secret key or tests will fail needlessly. We test
# in core that secret key failing/not failing works as expected, but here we
# provide a valid secret_key so tests don't fail.
echo ":: Building and testing examples..."
indir "${EXAMPLES_DIR}" $CARGO update
ROCKET_SECRET_KEY="itlYmFR2vYKrOmFhupMIn/hyB6lYCCTXz4yaQX89XVg=" \
indir "${EXAMPLES_DIR}" $CARGO test --all $@
}
function test_default() {
echo ":: Building and testing core libraries..."
indir "${PROJECT_ROOT}" $CARGO test --all --all-features $@
echo ":: Checking benchmarks..."
indir "${BENCHMARKS_ROOT}" $CARGO update
indir "${BENCHMARKS_ROOT}" $CARGO check --benches --all-features $@
case "$OSTYPE" in
darwin* | linux*)
echo ":: Checking testbench..."
indir "${TESTBENCH_ROOT}" $CARGO update
indir "${TESTBENCH_ROOT}" $CARGO check $@
echo ":: Checking fuzzers..."
indir "${FUZZ_ROOT}" $CARGO update
indir "${FUZZ_ROOT}" $CARGO check --all --all-features $@
;;
*) echo ":: Skipping testbench, fuzzers [$OSTYPE]" ;;
esac
}
function test_ui() {
echo ":: Testing compile-time UI output..."
indir "${PROJECT_ROOT}" $CARGO test --test ui-fail --all --all-features -- --ignored $@
}
function run_benchmarks() {
echo ":: Running benchmarks..."
indir "${BENCHMARKS_ROOT}" $CARGO update
indir "${BENCHMARKS_ROOT}" $CARGO bench $@
}
function run_testbench() {
echo ":: Running testbench..."
indir "${TESTBENCH_ROOT}" $CARGO update
indir "${TESTBENCH_ROOT}" $CARGO run $@
}
# The kind of test we'll be running.
TEST_KIND="default"
KINDS=("default" "all" "core" "contrib" "examples" "benchmarks" "testbench" "ui")
function print_help() {
echo "USAGE:"
echo " $0 [+<TOOLCHAIN>] [--help|-h] [--<TEST>]"
echo ""
echo "OPTIONS:"
echo " +<TOOLCHAIN> Forwarded to Cargo to select toolchain."
echo " --help, -h Print this help message and exit."
echo " --<TEST> Run the specified test suite."
echo " (Run without --<TEST> to run default tests.)"
echo ""
echo "AVAILABLE <TEST> OPTIONS:"
for kind in "${KINDS[@]}"; do
echo " ${kind}"
done
echo ""
echo "EXAMPLES:"
echo " $0 # Run default tests on current toolchain."
echo " $0 +stable --all # Run all tests on stable toolchain."
echo " $0 --ui # Run UI tests on current toolchain."
}
if [[ $1 == +* ]]; then
CARGO="$CARGO $1"
shift
fi
if [[ $1 == "--help" ]] || [[ $1 == "-h" ]]; then
print_help
exit 0
fi
if [[ " ${KINDS[@]} " =~ " ${1#"--"} " ]]; then
TEST_KIND=${1#"--"}
shift
fi
echo ":: Preparing. Environment is..."
print_environment
echo " CARGO: $CARGO"
echo " EXTRA FLAGS: $@"
echo ":: Ensuring core crate versions match..."
check_versions_match "${CORE_CRATE_ROOTS[@]}"
echo ":: Ensuring contrib sync_db_pools versions match..."
check_versions_match "${CONTRIB_SYNC_DB_POOLS_CRATE_ROOTS[@]}"
echo ":: Ensuring contrib db_pools versions match..."
check_versions_match "${CONTRIB_DB_POOLS_CRATE_ROOTS[@]}"
echo ":: Ensuring minimum style requirements are met..."
check_style
echo ":: Updating dependencies..."
if ! $CARGO update ; then
echo " WARNING: Update failed! Proceeding with possibly outdated deps..."
fi
case $TEST_KIND in
core) test_core $@ ;;
contrib) test_contrib $@ ;;
examples) test_examples $@ ;;
default) test_default $@ ;;
benchmarks) run_benchmarks $@ ;;
testbench) run_testbench $@ ;;
ui) test_ui $@ ;;
all)
test_default $@ & default=$!
test_examples $@ & examples=$!
test_core $@ & core=$!
test_contrib $@ & contrib=$!
run_testbench $@ & testbench=$!
test_ui $@ & ui=$!
failures=()
if ! wait $default ; then failures+=("DEFAULT"); fi
if ! wait $examples ; then failures+=("EXAMPLES"); fi
if ! wait $core ; then failures+=("CORE"); fi
if ! wait $contrib ; then failures+=("CONTRIB"); fi
if ! wait $testbench ; then failures+=("TESTBENCH"); fi
if ! wait $ui ; then failures+=("UI"); fi
if [ ${#failures[@]} -ne 0 ]; then
tput setaf 1;
echo -e "\n!!! ${#failures[@]} TEST SUITE FAILURE(S) !!!"
for failure in "${failures[@]}"; do
echo " :: ${failure}"
done
tput sgr0
exit ${#failures[@]}
fi
;;
esac