-
Notifications
You must be signed in to change notification settings - Fork 16
/
bash_test.sh
executable file
·313 lines (284 loc) · 8.94 KB
/
bash_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
304
305
306
307
308
309
310
311
312
313
#!/bin/bash
# Copyright (c) the JPEG XL Project Authors.
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd
# Tests implemented in bash. These typically will run checks about the source
# code rather than the compiled one.
SELF=$(realpath "$0")
MYDIR=$(dirname "${SELF}")
set -u
test_includes() {
local ret=0
local f
for f in $(git ls-files | grep -E '(\.cc|\.cpp|\.h)$'); do
if [ ! -e "$f" ]; then
continue
fi
# Check that the full paths to the public headers are not used, since users
# of the library will include the library as: #include "jxl/foobar.h".
if grep -i -H -n -E '#include\s*[<"]lib/include/jxl' "$f" >&2; then
echo "Don't add \"include/\" to the include path of public headers." >&2
ret=1
fi
if [[ "${f#third_party/}" == "$f" ]]; then
# $f is not in third_party/
# Check that local files don't use the full path to third_party/
# directory since the installed versions will not have that path.
# Add an exception for third_party/dirent.h.
if grep -v -F 'third_party/dirent.h' "$f" | \
grep -i -H -n -E '#include\s*[<"]third_party/' >&2 &&
[[ $ret -eq 0 ]]; then
cat >&2 <<EOF
$f: Don't add third_party/ to the include path of third_party projects. This \
makes it harder to use installed system libraries instead of the third_party/ \
ones.
EOF
ret=1
fi
fi
done
return ${ret}
}
test_include_collision() {
local ret=0
local f
for f in $(git ls-files | grep -E '^lib/include/'); do
if [ ! -e "$f" ]; then
continue
fi
local base=${f#lib/include/}
if [[ -e "lib/${base}" ]]; then
echo "$f: Name collision, both $f and lib/${base} exist." >&2
ret=1
fi
done
return ${ret}
}
test_copyright() {
local ret=0
local f
for f in $(
git ls-files | grep -E \
'(Dockerfile.*|\.c|\.cc|\.cpp|\.gni|\.h|\.java|\.sh|\.m|\.py|\.ui|\.yml)$'); do
if [ ! -e "$f" ]; then
continue
fi
if [[ "${f#third_party/}" == "$f" ]]; then
# $f is not in third_party/
if ! head -n 10 "$f" |
grep -F 'Copyright (c) the JPEG XL Project Authors.' >/dev/null ; then
echo "$f: Missing Copyright blob near the top of the file." >&2
ret=1
fi
if ! head -n 10 "$f" |
grep -F 'Use of this source code is governed by a BSD-style' \
>/dev/null ; then
echo "$f: Missing License blob near the top of the file." >&2
ret=1
fi
fi
done
return ${ret}
}
# Check that we don't use "%zu" or "%zd" in format string for size_t.
test_printf_size_t() {
local ret=0
if grep -n -E '%[0-9]*z[udx]' \
$(git ls-files | grep -E '(\.c|\.cc|\.cpp|\.h)$'); then
echo "Don't use '%zu' or '%zd' in a format string, instead use " \
"'%\" PRIuS \"' or '%\" PRIdS \"'." >&2
ret=1
fi
if grep -n -E '[^_]gtest\.h' \
$(git ls-files | grep -E '(\.c|\.cc|\.cpp|\.h)$' | grep -v -F /testing.h); then
echo "Don't include gtest directly, instead include 'testing.h'. " >&2
ret=1
fi
local f
for f in $(git ls-files | grep -E "\.cc$" | xargs grep 'PRI[udx]S' |
cut -f 1 -d : | uniq); do
if [ ! -e "$f" ]; then
continue
fi
if ! grep -F printf_macros.h "$f" >/dev/null; then
echo "$f: Add lib/base/printf_macros.h for PRI.S, or use other " \
"types for code outside lib/jxl library." >&2
ret=1
fi
done
for f in $(git ls-files | grep -E "\.h$" | grep -v -E '(printf_macros\.h|testing\.h)' |
xargs grep -n 'PRI[udx]S'); do
# Having PRIuS / PRIdS in a header file means that printf_macros.h may
# be included before a system header, in particular before gtest headers.
# those may re-define PRIuS unconditionally causing a compile error.
echo "$f: Don't use PRI.S in header files. Sorry."
ret=1
done
return ${ret}
}
# Check that "dec_" code doesn't depend on "enc_" headers.
test_dec_enc_deps() {
local ret=0
local f
for f in $(git ls-files | grep -E '/dec_'); do
if [ ! -e "$f" ]; then
continue
fi
if [[ "${f#third_party/}" == "$f" ]]; then
# $f is not in third_party/
if grep -n -H -E "#include.*/enc_" "$f" >&2; then
echo "$f: Don't include \"enc_*\" files from \"dec_*\" files." >&2
ret=1
fi
fi
done
return ${ret}
}
# Check for git merge conflict markers.
test_merge_conflict() {
local ret=0
TEXT_FILES='(\.cc|\.cpp|\.h|\.sh|\.m|\.py|\.md|\.txt|\.cmake)$'
for f in $(git ls-files | grep -E "${TEXT_FILES}"); do
if [ ! -e "$f" ]; then
continue
fi
if grep -E '^<<<<<<< ' "$f"; then
echo "$f: Found git merge conflict marker. Please resolve." >&2
ret=1
fi
done
return ${ret}
}
# Check that the library and the package have the same version. This prevents
# accidentally having them out of sync.
get_version() {
local varname=$1
local line=$(grep -F "set(${varname} " lib/CMakeLists.txt | head -n 1)
[[ -n "${line}" ]]
line="${line#set(${varname} }"
line="${line%)}"
echo "${line}"
}
test_version() {
local major=$(get_version JPEGXL_MAJOR_VERSION)
local minor=$(get_version JPEGXL_MINOR_VERSION)
local patch=$(get_version JPEGXL_PATCH_VERSION)
# Check that the version is not empty
if [[ -z "${major}${minor}${patch}" ]]; then
echo "Couldn't parse version from CMakeLists.txt" >&2
return 1
fi
local pkg_version=$(head -n 1 debian/changelog)
# Get only the part between the first "jpeg-xl (" and the following ")".
pkg_version="${pkg_version#jpeg-xl (}"
pkg_version="${pkg_version%%)*}"
if [[ -z "${pkg_version}" ]]; then
echo "Couldn't parse version from debian package" >&2
return 1
fi
local lib_version="${major}.${minor}.${patch}"
lib_version="${lib_version%.0}"
if [[ "${pkg_version}" != "${lib_version}"* ]]; then
echo "Debian package version (${pkg_version}) doesn't match library" \
"version (${lib_version})." >&2
return 1
fi
return 0
}
# Check that the SHA versions in deps.sh matches the git submodules.
test_deps_version() {
while IFS= read -r line; do
if [[ "${line:0:10}" != "[submodule" ]]; then
continue
fi
line="${line#[submodule \"}"
line="${line%\"]}"
local varname=$(tr '[:lower:]' '[:upper:]' <<< "${line}")
varname="${varname/\//_}"
if ! grep -F "${varname}=" deps.sh >/dev/null; then
# Ignoring submodule not in deps.sh
continue
fi
local deps_sha=$(grep -F "${varname}=" deps.sh | cut -f 2 -d '"')
[[ -n "${deps_sha}" ]]
local git_sha=$(git ls-tree -r HEAD "${line}" | cut -f 1 | cut -f 3 -d ' ')
if [[ "${deps_sha}" != "${git_sha}" ]]; then
cat >&2 <<EOF
deps.sh: SHA for project ${line} is at ${deps_sha} but the git submodule is at
${git_sha}. Please update deps.sh
If you did not intend to change the submodule's SHA value, it is possible that
you accidentally included this change in your commit after a rebase or checkout
without running "git submodule --init". To revert the submodule change run from
the top checkout directory:
git -C ${line} checkout ${deps_sha}
git commit --amend ${line}
EOF
return 1
fi
done < .gitmodules
}
# Make sure that all the Fields objects are fuzzed directly.
test_fuzz_fields() {
local ret=0
# List all the classes of the form "ClassName : public Fields".
# This doesn't catch class names that are too long to fit.
local field_classes=$( git ls-files |
grep -E '\.(cc|h)' | grep -v 'test\.cc$' |
xargs grep -h -o -E '\b[^ ]+ : public Fields' | cut -f 1 -d ' ')
local classname
for classname in ${field_classes}; do
if [ ! -e "$classname" ]; then
continue
fi
if ! grep -E "\\b${classname}\\b" tools/fields_fuzzer.cc >/dev/null; then
cat >&2 <<EOF
tools/fields_fuzzer.cc: Class ${classname} not found in the fields_fuzzer.
EOF
ret=1
fi
done
return $ret
}
# Test that we don't use %n in C++ code to avoid using it in printf and scanf.
# This test is not very precise but in cases where "module n" is needed we would
# normally have "% n" instead of "%n". Using %n is not allowed in Android 10+.
test_percent_n() {
local ret=0
local f
for f in $(git ls-files | grep -E '(\.cc|\.cpp|\.h)$'); do
if [ ! -e "$f" ]; then
continue
fi
if grep -i -H -n -E '%h*n' "$f" >&2; then
echo "Don't use \"%n\"." >&2
ret=1
fi
done
return ${ret}
}
main() {
local ret=0
cd "${MYDIR}"
if ! git rev-parse >/dev/null 2>/dev/null; then
echo "Not a git checkout, skipping bash_test"
return 0
fi
IFS=$'\n'
for f in $(declare -F); do
local test_name=$(echo "$f" | cut -f 3 -d ' ')
# Runs all the local bash functions that start with "test_".
if [[ "${test_name}" == test_* ]]; then
echo "Test ${test_name}: Start"
if ${test_name}; then
echo "Test ${test_name}: PASS"
else
echo "Test ${test_name}: FAIL"
ret=1
fi
fi
done
return ${ret}
}
main "$@"