2 # Copyright (c) the JPEG XL Project Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style
5 # license that can be found in the LICENSE file.
7 # Tests implemented in bash. These typically will run checks about the source
8 # code rather than the compiled one.
10 MYDIR
=$
(dirname $
(realpath
"$0"))
17 for f
in $
(git ls-files |
grep -E '(\.cc|\.cpp|\.h)$'); do
18 if [ ! -e "$f" ]; then
21 # Check that the full paths to the public headers are not used, since users
22 # of the library will include the library as: #include "jxl/foobar.h".
23 if grep -i -H -n -E '#include\s*[<"]lib/include/jxl' "$f" >&2; then
24 echo "Don't add \"include/\" to the include path of public headers." >&2
28 if [[ "${f#third_party/}" == "$f" ]]; then
29 # $f is not in third_party/
31 # Check that local files don't use the full path to third_party/
32 # directory since the installed versions will not have that path.
33 # Add an exception for third_party/dirent.h.
34 if grep -v -F 'third_party/dirent.h' "$f" | \
35 grep -i -H -n -E '#include\s*[<"]third_party/' >&2 &&
36 [[ $ret -eq 0 ]]; then
38 $f: Don't add third_party/ to the include path of third_party projects. This \
39 makes it harder to use installed system libraries instead of the third_party/ \
50 test_include_collision
() {
53 for f
in $
(git ls-files |
grep -E '^lib/include/'); do
54 if [ ! -e "$f" ]; then
57 local base
=${f#lib/include/}
58 if [[ -e "lib/${base}" ]]; then
59 echo "$f: Name collision, both $f and lib/${base} exist." >&2
70 git ls-files |
grep -E \
71 '(Dockerfile.*|\.c|\.cc|\.cpp|\.gni|\.h|\.java|\.sh|\.m|\.py|\.ui|\.yml)$'); do
72 if [ ! -e "$f" ]; then
75 if [[ "${f#third_party/}" == "$f" ]]; then
76 # $f is not in third_party/
77 if ! head -n 10 "$f" |
78 grep -F 'Copyright (c) the JPEG XL Project Authors.' >/dev
/null
; then
79 echo "$f: Missing Copyright blob near the top of the file." >&2
82 if ! head -n 10 "$f" |
83 grep -F 'Use of this source code is governed by a BSD-style' \
85 echo "$f: Missing License blob near the top of the file." >&2
93 # Check that we don't use "%zu" or "%zd" in format string for size_t.
94 test_printf_size_t
() {
96 if grep -n -E '%[0-9]*z[udx]' \
97 $
(git ls-files |
grep -E '(\.c|\.cc|\.cpp|\.h)$'); then
98 echo "Don't use '%zu' or '%zd' in a format string, instead use " \
99 "'%\" PRIuS \"' or '%\" PRIdS \"'." >&2
103 if grep -n -E '[^_]gtest\.h' \
104 $
(git ls-files |
grep -E '(\.c|\.cc|\.cpp|\.h)$' |
grep -v -F /testing.h
); then
105 echo "Don't include gtest directly, instead include 'testing.h'. " >&2
109 if grep -n -E 'gmock\.h' \
110 $
(git ls-files |
grep -E '(\.c|\.cc|\.cpp|\.h)$' |
grep -v -F /testing.h
); then
111 echo "Don't include gmock directly, instead include 'testing.h'. " >&2
116 for f
in $
(git ls-files |
grep -E "\.cc$" |
xargs grep 'PRI[udx]S' |
117 cut
-f 1 -d : |
uniq); do
118 if [ ! -e "$f" ]; then
121 if ! grep -F printf_macros.h
"$f" >/dev
/null
; then
122 echo "$f: Add lib/jxl/base/printf_macros.h for PRI.S, or use other " \
123 "types for code outside lib/jxl library." >&2
128 for f
in $
(git ls-files |
grep -E "\.h$" |
grep -v -E '(printf_macros\.h|testing\.h)' |
129 xargs grep -n 'PRI[udx]S'); do
130 # Having PRIuS / PRIdS in a header file means that printf_macros.h may
131 # be included before a system header, in particular before gtest headers.
132 # those may re-define PRIuS unconditionally causing a compile error.
133 echo "$f: Don't use PRI.S in header files. Sorry."
140 # Check that "dec_" code doesn't depend on "enc_" headers.
141 test_dec_enc_deps
() {
144 for f
in $
(git ls-files |
grep -E '/dec_'); do
145 if [ ! -e "$f" ]; then
148 if [[ "${f#third_party/}" == "$f" ]]; then
149 # $f is not in third_party/
150 if grep -n -H -E "#include.*/enc_" "$f" >&2; then
151 echo "$f: Don't include \"enc_*\" files from \"dec_*\" files." >&2
159 # Check for git merge conflict markers.
160 test_merge_conflict
() {
162 TEXT_FILES
='(\.cc|\.cpp|\.h|\.sh|\.m|\.py|\.md|\.txt|\.cmake)$'
163 for f
in $
(git ls-files |
grep -E "${TEXT_FILES}"); do
164 if [ ! -e "$f" ]; then
167 if grep -E '^<<<<<<< ' "$f"; then
168 echo "$f: Found git merge conflict marker. Please resolve." >&2
175 # Check that the library and the package have the same version. This prevents
176 # accidentally having them out of sync.
179 local line
=$
(grep -F "set(${varname} " lib
/CMakeLists.txt |
head -n 1)
181 line
="${line#set(${varname} }"
187 local major
=$
(get_version JPEGXL_MAJOR_VERSION
)
188 local minor
=$
(get_version JPEGXL_MINOR_VERSION
)
189 local patch=$
(get_version JPEGXL_PATCH_VERSION
)
190 # Check that the version is not empty
191 if [[ -z "${major}${minor}${patch}" ]]; then
192 echo "Couldn't parse version from CMakeLists.txt" >&2
195 local pkg_version
=$
(head -n 1 debian
/changelog
)
196 # Get only the part between the first "jpeg-xl (" and the following ")".
197 pkg_version
="${pkg_version#jpeg-xl (}"
198 pkg_version
="${pkg_version%%)*}"
199 if [[ -z "${pkg_version}" ]]; then
200 echo "Couldn't parse version from debian package" >&2
204 local lib_version
="${major}.${minor}.${patch}"
205 lib_version
="${lib_version%.0}"
206 if [[ "${pkg_version}" != "${lib_version}"* ]]; then
207 echo "Debian package version (${pkg_version}) doesn't match library" \
208 "version (${lib_version})." >&2
214 # Check that the SHA versions in deps.sh matches the git submodules.
215 test_deps_version
() {
216 while IFS
= read -r line
; do
217 if [[ "${line:0:10}" != "[submodule" ]]; then
220 line
="${line#[submodule \"}"
222 local varname
=$
(tr '[:lower:]' '[:upper:]' <<< "${line}")
223 varname
="${varname/\//_}"
224 if ! grep -F "${varname}=" deps.sh
>/dev
/null
; then
225 # Ignoring submodule not in deps.sh
228 local deps_sha
=$
(grep -F "${varname}=" deps.sh | cut
-f 2 -d '"')
229 [[ -n "${deps_sha}" ]]
230 local git_sha
=$
(git ls-tree
-r HEAD
"${line}" | cut
-f 1 | cut
-f 3 -d ' ')
231 if [[ "${deps_sha}" != "${git_sha}" ]]; then
233 deps.sh: SHA for project ${line} is at ${deps_sha} but the git submodule is at
234 ${git_sha}. Please update deps.sh
236 If you did not intend to change the submodule's SHA value, it is possible that
237 you accidentally included this change in your commit after a rebase or checkout
238 without running "git submodule --init". To revert the submodule change run from
239 the top checkout directory:
241 git -C ${line} checkout ${deps_sha}
242 git commit --amend ${line}
250 # Make sure that all the Fields objects are fuzzed directly.
253 # List all the classes of the form "ClassName : public Fields".
254 # This doesn't catch class names that are too long to fit.
255 local field_classes
=$
( git ls-files |
256 grep -E '\.(cc|h)' |
grep -v 'test\.cc$' |
257 xargs grep -h -o -E '\b[^ ]+ : public Fields' | cut
-f 1 -d ' ')
259 for classname
in ${field_classes}; do
260 if [ ! -e "$classname" ]; then
263 if ! grep -E "\\b${classname}\\b" tools
/fields_fuzzer.cc
>/dev
/null
; then
265 tools/fields_fuzzer.cc: Class ${classname} not found in the fields_fuzzer.
273 # Test that we don't use %n in C++ code to avoid using it in printf and scanf.
274 # This test is not very precise but in cases where "module n" is needed we would
275 # normally have "% n" instead of "%n". Using %n is not allowed in Android 10+.
279 for f
in $
(git ls-files |
grep -E '(\.cc|\.cpp|\.h)$'); do
280 if [ ! -e "$f" ]; then
283 if grep -i -H -n -E '%h*n' "$f" >&2; then
284 echo "Don't use \"%n\"." >&2
295 if ! git rev-parse
>/dev
/null
2>/dev
/null
; then
296 echo "Not a git checkout, skipping bash_test"
301 for f
in $
(declare -F); do
302 local test_name
=$
(echo "$f" | cut
-f 3 -d ' ')
303 # Runs all the local bash functions that start with "test_".
304 if [[ "${test_name}" == test_
* ]]; then
305 echo "Test ${test_name}: Start"
306 if ${test_name}; then
307 echo "Test ${test_name}: PASS"
309 echo "Test ${test_name}: FAIL"