Debian Codegraph @

test/common/static-code

raw
1 #!/bin/bash
2 # run static code checks like eslint, mypy, ruff, vulture.
3
4 set -eu
5
6 # requires: pyproject.toml
7 # requires: containers/flatpak/test/ruff.toml
8 # requires: pkg/ruff.toml
9 # requires: test/common/ruff.toml
10 # requires: test/example/ruff.toml
11 # requires: test/verify/ruff.toml
12 # requires: tools/vulture_suppressions/ruff.toml
13
14 # we consider any function named test_* to be a test case
15 # each test is considered to succeed if it exits with no output
16 # exit with status 77 is a skip, with the message in the output
17 # otherwise, any output is a failure, even if exit status is 0
18
19 # note: `set -e` is not active during the tests.
20
21 find_scripts() {
22 # Helper to find all scripts in the tree
23 (
24 # Any non-binary file which contains a given shebang
25 git grep --cached -lIz '^#!.*'"$1"
26 shift
27 # Any file matching the provided globs
28 git ls-files -z "$@"
29 ) | sort -z | uniq -z
30 }
31
32 find_python_files() {
33 find_scripts 'python3' '*.py'
34 }
35
36 test_ruff() {
37 command -v ruff >/dev/null || skip 'no ruff'
38 find_python_files | xargs -r -0 ruff check --quiet --no-cache
39 }
40
41 if [ "${WITH_PARTIAL_TREE:-0}" = 0 ]; then
42 test_mypy() {
43 command -v mypy >/dev/null || skip 'no mypy'
44 for file in $(cat test/mypy-deps 2>/dev/null || true); do
45 test -e "${file}" || skip "no ${file}"
46 done
47 find_python_files | xargs -r -0 mypy --no-error-summary
48 }
49
50 test_vulture() {
51 # vulture to find unused variables/functions
52 command -v vulture >/dev/null || skip 'no vulture'
53 find_python_files | xargs -r -0 vulture
54 }
55 fi
56
57
58 test_js_translatable_strings() {
59 # Translatable strings must be marked with _(""), not _('')
60
61 ! git grep -n -E "(gettext|_)\(['\`]" -- {src,pkg}/'*'.{js,ts,jsx,tsx}
62 }
63
64 if [ "${WITH_PARTIAL_TREE:-0}" = 0 ]; then
65 test_eslint() {
66 test -x node_modules/.bin/eslint -a -x /usr/bin/node || skip 'no eslint'
67 find_scripts 'node' '*.[jt]s' '*.[jt]sx' | xargs -0 node_modules/.bin/eslint
68 }
69
70 test_typescript() {
71 test/common/typecheck
72 }
73 fi
74
75 test_patternfly_barrel_file() {
76 ! git grep -n "@patternfly/react-core[^/]" -- {src,pkg}/'*'.{js,ts,jsx,tsx}
77 }
78
79 test_stylelint() {
80 test -x node_modules/.bin/stylelint -a -x /usr/bin/node || skip 'no stylelint'
81 git ls-files -z '*.css' '*.scss' | xargs -r -0 node_modules/.bin/stylelint
82 }
83
84 test_no_translatable_attr() {
85 # Use of translatable attribute in HTML: should be 'translate' instead
86
87 ! git grep -n 'translatable=["'\'']yes' -- pkg doc
88 }
89
90 test_unsafe_security_policy() {
91 # It's dangerous to have 'unsafe-inline' or 'unsafe-eval' in our
92 # content-security-policy entries.
93
94 git grep -lIz -E 'content-security-policy.*(\*|unsafe)' 'pkg/*/manifest.json' | while read -d '' filename; do
95 if test ! -f "$(dirname ${filename})/content-security-policy.override"; then
96 echo "${filename} contains unsafe content security policy"
97 fi
98 done
99 }
100
101 test_json_verify() {
102 # Check JSON files for validity: exclude top-level configs (which are extended json)
103 git ls-files -z '*/*.json' | xargs -r -0 python3 -c "
104 import argparse
105 import json
106
107 parser = argparse.ArgumentParser()
108 parser.add_argument('files', nargs='+', type=argparse.FileType(encoding='utf-8'))
109 args = parser.parse_args()
110
111 for file in args.files:
112 try:
113 json.load(file)
114 except ValueError as exc: # both JSONDecodeError and UnicodeError
115 print(f'{file.name}: {exc}')
116 "
117 }
118
119 test_html_verify() {
120 # Check all HTML files for syntactic validity
121
122 git ls-files -z 'pkg/*.html' | while read -d '' filename; do
123 if ! python3 -c "import xml.etree.ElementTree as ET; ET.parse('${filename}')"; then
124 echo "${filename} contains invalid XML"
125 fi
126 done
127 }
128
129 test_include_config_h() {
130 # Every C file should #include "config.h" at the top
131
132 git ls-files -cz '*.c' | while read -d '' filename; do
133 if sed -n '/^#include "config.h"$/q1; /^\s*#/q;' "${filename}"; then
134 printf '%s: #include "config.h" is not the first line\n' "${filename}"
135 fi
136 done
137 }
138
139 test_spdx_license_identifier() {
140 # Every source file should have an SPDX-License-Identifier in the first 10 lines
141 # Exclude file types that cannot have comments or don't need a license
142 git ls-files pkg src -- \
143 ':!src/systemd/*' ':!src/cockpit/_vendor' \
144 ':!*.json' ':!*.png' ':!*.jpg' ':!*.gif' ':!*.svg' ':!*.ico' \
145 ':!*mock*' ':!*.pem' ':!*.key' ':!*.p12' \
146 ':!*.policy.in' ':!*.xml.in' ':!*.metainfo*' \
147 ':!*.md' ':!*.txt' ':!*.html' '!:*README' ':!*HACKING' \
148 ':!*.service' ':!*.desktop' | \
149 while read -r filename; do
150 # Check first 10 lines for SPDX-License-Identifier
151 head -n 10 "${filename}" | grep -q "SPDX-License-Identifier:" || \
152 printf '%s: missing SPDX-License-Identifier in first 10 lines\n' "${filename}"
153 done
154 }
155
156 ### end of tests. start of machinery.
157
158 skip() {
159 printf "%s\n" "$*"
160 exit 77
161 }
162
163 main() {
164 if [ $# = 0 ]; then
165 tap=''
166 elif [ $# = 1 -a "$1" = "--tap" ]; then
167 tap='1'
168 else
169 printf "usage: %s [--tap]\n" "$0" >&2
170 exit 1
171 fi
172
173 cd "${0%/*}/../.."
174 if [ ! -e .git ]; then
175 echo '1..0 # SKIP not in a git checkout'
176 exit 0
177 fi
178
179 exit_status=0
180 counter=0
181
182 tests=($(compgen -A function 'test_'))
183 [ -n "${tap}" ] && printf "1..%d\n" "${#tests[@]}"
184
185 for test_function in "${tests[@]}"; do
186 path="/static-code/$(echo ${test_function} | tr '_' '-')"
187 counter=$((counter + 1))
188 fail=''
189 skip=''
190
191 # run the test, capturing its output and exit status
192 output="$(${test_function} 2>&1)" && test_status=0 || test_status=$?
193
194 if [ "${test_status}" = 77 ]; then
195 if [ -z "${tap}" ]; then
196 printf >&2 "WARNING: skipping %s: %s\n" "${path}" "${output}"
197 fi
198 skip=" # SKIP ${output}"
199 output=''
200 elif [ "${test_status}" != 0 -o -n "${output}" ]; then
201 exit_status=1
202 fail=1
203 fi
204
205 # Only print output on failures or --tap mode
206 [ -n "${tap}" -o -n "${fail}" ] || continue
207
208 # excluding the plan, this is the only output that we ever generate
209 printf "%s %d %s%s\n" "${fail:+not }ok" "${counter}" "${path}" "${skip}"
210 if [ -n "${output}" ]; then
211 printf "%s\n" "${output}" | sed -e 's/^/# /'
212 fi
213 done
214
215 exit "${exit_status}"
216 }
217
218 main "$@"