-
Notifications
You must be signed in to change notification settings - Fork 0
/
test
executable file
·209 lines (192 loc) · 4.26 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
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
#!/usr/bin/env bash
# SPDX-FileCopyrightText: 2021 - 2023 Robin Vobruba <hoijui.quaero@gmail.com>
#
# SPDX-License-Identifier: Unlicense
# Exit immediately on each error and unset variable;
# see: https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/
set -Eeuo pipefail
#set -Eeu
# initial default values
APP_NAME="Rust project test script"
debug=false
function print_help() {
local script_name
script_name="$(basename "$0")"
echo "$APP_NAME -"
echo "We run all tests, even if some are failing."
echo
echo "Usage:"
echo " $script_name [OPTION...]"
echo "Options:"
echo " -h, --help"
echo " Show this help message and exit"
echo " -d, --debug"
echo " Shows stdout for doc-test (and maybe more in the future)"
echo "Examples:"
echo " $script_name"
echo " $script_name --debug"
}
# read command-line args
POSITIONAL=()
while [[ $# -gt 0 ]]
do
arg="$1"
shift # $2 -> $1, $3 -> $2, ...
case "$arg" in
-h|--help)
print_help
exit 0
;;
-d|--debug)
debug=true
;;
*) # non-/unknown option
POSITIONAL+=("$arg") # save it in an array for later
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
tests=0
failures=0
failed_tests=()
function _report_test_res() {
res="$1"
t_human_id="$2"
echo -n "TEST: $t_human_id: "
if [ "$res" -eq 0 ]
then
echo "succeeded."
else
echo "failed."
fi
}
echo
echo
test_extra_args=()
# Checks if 'nextest' is installed
if cargo nextest help &> /dev/null
then
c_test_type="nextest"
test_extra_args+=("run")
else
c_test_type="test"
test_extra_args+=("--release")
fi
t_human_id="'cargo $c_test_type' - Run unit-tests"
echo "TEST: $t_human_id"
failed=0
cargo "$c_test_type" "${test_extra_args[@]}" || failed=1
failures=$((failures + failed))
tests=$((tests + 1))
_report_test_res "$failed" "$t_human_id"
if [ "$failed" = "1" ]
then
failed_tests+=("$t_human_id")
fi
echo
echo
t_human_id="'cargo fmt' - Check source code formatting"
echo "TEST: $t_human_id"
failed=0
cargo fmt -- --check || failed=1
failures=$((failures + failed))
tests=$((tests + 1))
_report_test_res "$failed" "$t_human_id"
if [ "$failed" = "1" ]
then
failed_tests+=("$t_human_id")
fi
# `cargo sort` is not as readily available as other commands,
# thus we make this test optional
if cargo --list | grep "^\\s\+sort\(\\s\|\$\)" -q
then
echo
echo
t_human_id="'cargo sort' - Check Cargo.toml dependencies order"
echo "TEST: $t_human_id"
failed=0
cargo sort --check || failed=1
failures=$((failures + failed))
tests=$((tests + 1))
_report_test_res "$failed" "$t_human_id"
if [ "$failed" = "1" ]
then
failed_tests+=("$t_human_id")
fi
fi
echo
echo
t_human_id="'cargo clippy' - Static code linting"
echo "TEST: $t_human_id"
failed=0
cargo clippy --release -- \
|| failed=1
failures=$((failures + failed))
tests=$((tests + 1))
_report_test_res "$failed" "$t_human_id"
if [ "$failed" = "1" ]
then
failed_tests+=("$t_human_id")
fi
echo
echo
t_human_id="'cargo doc-code(-tests)' - Building documentaiton and running docu code"
echo "TEST: $t_human_id"
# This flag will run your code examples as tests.
# For more, see the chapter on documentation tests:
# https://doc.rust-lang.org/rustdoc/documentation-tests.html
failed=0
if [ -f "src/lib.rs" ]
then
# This only tests the library, but fails if the project is/has no library
doc_target="--doc"
else
doc_target="--all-targets"
fi
if $debug
then
cargo test --release $doc_target -- --show-output || failed=1
else
cargo test --release $doc_target || failed=1
fi
failures=$((failures + failed))
tests=$((tests + 1))
_report_test_res "$failed" "$t_human_id"
if [ "$failed" = "1" ]
then
failed_tests+=("$t_human_id")
fi
echo
echo
t_human_id="'REUSE' - Licensing meta-data check"
echo "TEST: $t_human_id"
if which reuse > /dev/null
then
failed=0
reuse lint || failed=1
failures=$((failures + failed))
tests=$((tests + 1))
_report_test_res "$failed" "$t_human_id"
if [ "$failed" = "1" ]
then
failed_tests+=("$t_human_id")
fi
else
>&2 echo "WARNING: REUSE tool not found; skipping licensing meta-data check."
fi
echo
echo
echo "Successful tests: $((tests - failures))/$tests"
if [ $failures -eq 0 ]
then
echo "All tests successfull!"
else
{
echo "There were $failures test failures:"
for failed_test_id in "${failed_tests[@]}"
do
echo "- $failed_test_id"
done
} >&2
fi
exit $failures