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
|
#!/bin/bash
# run static code checks like flake8, pyvulture.
set -eu
# we consider any function named test_* to be a test case
# each test is considered to succeed if it exits with no output
# exit with status 77 is a skip, with the message in the output
# otherwise, any output is a failure, even if exit status is 0
# note: `set -e` is not active during the tests.
find_scripts() {
# Helper to find all Python files in the tree
(
# Any non-binary file which contains a python3 shebang
git grep --cached -lIz '^#!.*'"$1"
# Any file ending in '.py'
git ls-files -z "$2"
) | sort -z | uniq -z
}
find_python_files() {
find_scripts 'python3' '*.py'
}
test_flake8() {
command -v flake8 >/dev/null || skip 'no flake8'
find_python_files | xargs -r -0 flake8
}
test_pyvulture() {
# vulture to find unused variables/functions
python3 -c 'import vulture' 2>/dev/null || skip 'no python3-vulture'
find_python_files | xargs -r -0 python3 -m vulture \
--min-confidence "${VULTURE_CONFIDENCE:-100}" \
--ignore-names 'do_*,test[A-Z0-9]*,__*__,ignore_errors' \
--ignore-decorators '@*'
}
test_js_translatable_strings() {
# Translatable strings must be marked with _(""), not _('')
! git grep -n -E "(gettext|_)\(['\`]" -- {src,pkg}/'*'.{js,jsx}
}
test_eslint() {
test -x node_modules/.bin/eslint -a -x /usr/bin/node || skip 'no eslint'
find_scripts 'node' '*.js?' | xargs -0 node_modules/.bin/eslint
}
test_no_translatable_attr() {
# Use of translatable attribute in HTML: should be 'translate' instead
! git grep -n 'translatable=["'\'']yes' -- pkg doc
}
test_unsafe_security_policy() {
# It's dangerous to have 'unsafe-inline' or 'unsafe-eval' in our
# content-security-policy entries.
git grep -lIz -E 'content-security-policy.*(\*|unsafe)' 'pkg/*/manifest.json' | while read -d '' filename; do
if test ! -f "$(dirname ${filename})/content-security-policy.override"; then
echo "${filename} contains unsafe content security policy"
fi
done
}
test_json_verify() {
# Check all JSON files for validity
git ls-files -z '*.json' | while read -d '' filename; do
python3 -m json.tool "${filename}" /dev/null 2>&1 | sed "s@^@${filename}: @"
done
}
test_include_config_h() {
# Every C file should #include "config.h" at the top
git ls-files -cz '*.c' | while read -d '' filename; do
if sed -n '/^#include "config.h"$/q1; /^\s*#/q;' "${filename}"; then
printf '%s: #include "config.h" is not the first line\n' "${filename}"
fi
done
}
### end of tests. start of machinery.
skip() {
printf "%s\n" "$*"
exit 77
}
main() {
if [ $# = 0 ]; then
tap=''
elif [ $# = 1 -a "$1" = "--tap" ]; then
tap='1'
else
printf "usage: %s [--tap]\n" "$0" >&2
exit 1
fi
cd "${0%/*}/.."
if [ ! -e .git ]; then
echo '1..0 # SKIP not in a git checkout'
exit 0
fi
exit_status=0
counter=0
tests=($(compgen -A function 'test_'))
[ -n "${tap}" ] && printf "1..%d\n" "${#tests[@]}"
for test_function in ${tests[@]}; do
path="/static-code/$(echo ${test_function} | tr '_' '-')"
counter=$((counter + 1))
fail=''
skip=''
# run the test, capturing its output and exit status
output="$(${test_function} 2>&1)" && test_status=0 || test_status=$?
if [ "${test_status}" = 77 ]; then
if [ -z "${tap}" ]; then
printf >&2 "WARNING: skipping %s: %s\n" "${path}" "${output}"
fi
skip=" # SKIP ${output}"
output=''
elif [ "${test_status}" != 0 -o -n "${output}" ]; then
exit_status=1
fail=1
fi
# Only print output on failures or --tap mode
[ -n "${tap}" -o -n "${fail}" ] || continue
# excluding the plan, this is the only output that we ever generate
printf "%s %d %s%s\n" "${fail:+not }ok" "${counter}" "${path}" "${skip}"
if [ -n "${output}" ]; then
printf "%s\n" "${output}" | sed -e 's/^/# /'
fi
done
exit "${exit_status}"
}
main "$@"
|