diff --git a/tests/kernel-pr-check b/tests/kernel-pr-check index bfff310ff..47457d38c 100755 --- a/tests/kernel-pr-check +++ b/tests/kernel-pr-check @@ -1,8 +1,9 @@ #!/bin/bash # SPDX-License-Identifier: MIT # tools/kernel-pr-check's commit checks against a stand-in kernel tree (a -# git repository with a fake scripts/checkpatch.pl): sign-offs, prefixes and -# checkpatch errors. Its device-tree comparison runs against a second +# git repository with a fake scripts/checkpatch.pl): sign-offs, prefixes, +# checkpatch errors and warnings, and a checker that is missing or doesn't +# run to the end. Its device-tree comparison runs against a second # stand-in whose make prints the messages each board file asks for; the # real builds (clang, dtc, dt-validate) need a real tree and are run by hand. set -euo pipefail @@ -13,10 +14,17 @@ fail() { echo "not ok - $*" >&2; exit 1; } k=$t/linux mkdir -p "$k/scripts" "$k/drivers" -# checkpatch's stand-in: an ERROR for any added line with "BADSTYLE". +# checkpatch's stand-in: an ERROR for any added line with "BADSTYLE", a +# WARNING for "WARNSTYLE", and checkpatch's exit status: 1 when it reported +# something. cat > "$k/scripts/checkpatch.pl" <<'EOF' #!/usr/bin/perl -while () { print "-:1: ERROR:CODE_STYLE: bad style\n" if /^\+.*BADSTYLE/; } +my $st = 0; +while () { + if (/^\+.*BADSTYLE/) { print "-:1: ERROR:CODE_STYLE: bad style\n"; $st = 1; } + if (/^\+.*WARNSTYLE/) { print "-:1: WARNING:CODE_STYLE: odd style\n"; $st = 1; } +} +exit $st; EOF chmod +x "$k/scripts/checkpatch.pl" cd "$k" @@ -26,11 +34,12 @@ git config user.email human@example.org echo base > drivers/a.c git add -A && git commit -q -m "base" -run() { # name expect pattern +run() { # name expect [pattern] [absent pattern]; $rbase and $rhead stand in for omamobile and HEAD local out status=0 - out=$("$here/tools/kernel-pr-check" -C "$k" omamobile HEAD 2>&1) || status=$? + out=$("$here/tools/kernel-pr-check" -C "$k" "${rbase:-omamobile}" "${rhead:-HEAD}" 2>&1) || status=$? [[ $status == "$2" ]] || { echo "$out"; fail "$1: exit $status, expected $2"; } [[ -z ${3:-} ]] || grep -c -- "$3" <<<"$out" >/dev/null || { echo "$out"; fail "$1: says '$3'"; } + [[ -z ${4:-} ]] || ! grep -c -- "$4" <<<"$out" >/dev/null || { echo "$out"; fail "$1: says '$4'"; } } branch() { git checkout -q omamobile && git checkout -q -b "$1"; } @@ -67,7 +76,7 @@ echo "ok - a subject without a subsystem prefix fails" branch style echo "BADSTYLE" >> drivers/a.c git commit -q -am "drivers: a: style" -m "Signed-off-by: A Human " -run "a checkpatch error" 1 "checkpatch --strict errors" +run "a checkpatch error" 1 "checkpatch --strict errors" "didn't run to the end" echo "ok - a checkpatch error fails" nc_status=0 @@ -85,6 +94,37 @@ st_out=$("$here/tools/kernel-pr-check" --strict-trailers -C "$k" omamobile HEAD [[ $st_status == 0 ]] && grep -c "^ok - Co-Authored-By trailers.*# WARN" <<<"$st_out" >/dev/null || { echo "$st_out"; fail "--strict-trailers: a Co-Authored-By trailer still only warns"; } echo "ok - a Co-Authored-By trailer warns, with the old --strict-trailers too" +branch warn +echo "WARNSTYLE" >> drivers/a.c +git commit -q -am "drivers: a: warn" -m "Signed-off-by: A Human " +run "a checkpatch warning" 0 "checkpatch --strict: no errors (1 warnings" +echo "ok - a checkpatch warning (exit 1) is listed, not failed" + +# A checker that doesn't run to the end fails, and what it said on stderr +# is shown. checkpatch comes from the merge base, so each one is committed +# on its own base, with a clean change on top. +cpbase() { # name [checker] (no checker: the base deletes it) + branch "cp-$1" + if [[ -n ${2:-} ]]; then printf '%s\n' "$2" > scripts/checkpatch.pl; else git rm -q scripts/checkpatch.pl; fi + git commit -q -am "base: $1 checker" + git checkout -q -b "cp-$1-change" + echo x >> drivers/a.c + git commit -q -am "drivers: a: a clean change" -m "Signed-off-by: A Human " + git checkout -q omamobile # the tree the tool reads always has a checker +} +cpbase crash 'while () {} print STDERR "checkpatch: crashed here\n"; exit 255;' +rbase=cp-crash rhead=cp-crash-change run "a crashing checker" 1 "^ checkpatch: crashed here" "no errors" +cpbase syntax 'sub {' +rbase=cp-syntax rhead=cp-syntax-change run "a checker that doesn't compile" 1 "didn't run to the end" "no errors" +cpbase stdout 'while () {} print "checkpatch: needs a newer perl\n"; exit 1;' +rbase=cp-stdout rhead=cp-stdout-change run "a checker that exits 1 reporting nothing" 1 "^ checkpatch: needs a newer perl" "no errors" +cpbase signal 'kill "TERM", $$;' +rbase=cp-signal rhead=cp-signal-change run "a checker killed by a signal" 1 ": exit 143$" "no errors" +echo "ok - a checker that crashes, doesn't compile, exits 1 with no report or dies on a signal fails, with what it said" +cpbase missing +rbase=cp-missing rhead=cp-missing-change run "a merge base without a checker" 1 "Can't open perl script" "no errors" +echo "ok - a merge base without scripts/checkpatch.pl fails" + # CI runs `kernel-pr-check -C linux` from the directory that contains the # kernel checkout. The path has to still work after the tool cds in. rel_status=0 diff --git a/tools/kernel-pr-check b/tools/kernel-pr-check index 6fef60186..ae0613c35 100755 --- a/tools/kernel-pr-check +++ b/tools/kernel-pr-check @@ -14,7 +14,8 @@ # - every subject has a subsystem prefix ("clk: samsung: ..."); # - scripts/checkpatch.pl --strict on each commit, against that commit's # bindings and MAINTAINERS: errors fail, warnings -# are listed; +# are listed, and a checker that is missing or doesn't run to the end +# fails with what it printed; # - tools/pr-check's policy checks: identifiers, blobs; # - with --build OUT: each touched C file builds with W=1 (arm64, LLVM, # defconfig plus kernel/configs/omamobile.config), every touched @@ -68,7 +69,7 @@ while (($#)); do --merge) merge=1; shift ;; --no-checkpatch) checkpatch=0; shift ;; --strict-trailers) shift ;; # no longer stricter; see above - -h|--help) sed -n '3,48p' "$self"; exit 0 ;; + -h|--help) sed -n '3,49p' "$self"; exit 0 ;; -*) echo "kernel-pr-check: unknown option $1" >&2; exit 2 ;; *) break ;; esac @@ -201,12 +202,15 @@ done for e in "$kernel"/Documentation/*; do [[ ${e##*/} == devicetree ]] || ln -s "$e" "$cpdir/tree/Documentation/${e##*/}" done -cp_errors="" cp_warnings="" cp_count=0 +# A checker that is missing, can't run or crashes fails the check, with what +# it said: its empty report is not a clean one. +cp_errors="" cp_warnings="" cp_count=0 cp_broken="" for c in $commits; do rm -rf "$cpdir/tree/Documentation/devicetree" "$cpdir/tree/MAINTAINERS" docs=$(git ls-tree --name-only "$c" Documentation/devicetree MAINTAINERS) [[ -n $docs ]] && git archive "$c" $docs | tar -x -C "$cpdir/tree" - report=$(git format-patch -1 --stdout "$c" | perl "$cpdir/checker/scripts/checkpatch.pl" --root="$cpdir/tree" --strict --no-signoff --terse --show-types - 2>/dev/null) + report=$({ git format-patch -1 --stdout "$c" | perl "$cpdir/checker/scripts/checkpatch.pl" --root="$cpdir/tree" --strict --no-signoff --terse --show-types -; } 2>"$cpdir/stderr") + cp_status=$? short=$(git log -1 --format='%h %s' "$c") errors=$(grep -E ': ERROR:' <<<"$report" || true) [[ -n $errors ]] && cp_errors+=" $short"$'\n'"$(sed 's/^/ /' <<<"$errors")"$'\n' @@ -215,9 +219,20 @@ for c in $commits; do cp_count=$((cp_count + $(wc -l <<<"$warnings"))) cp_warnings+="# $short"$'\n'"$(sed -E -e 's/^-:[0-9]+: /# /' -e "s,$cpdir/tree/,," <<<"$warnings")"$'\n' fi + # checkpatch exits 1 when it reports something, 0 when it doesn't. + # Anything else, or a 1 with nothing reported, means it never finished: + # show what it said, on stderr and in any stdout line that isn't a + # diagnostic (some of its own failures print there). + if ((cp_status > 1)) || { ((cp_status == 1)) && [[ -z $errors$warnings ]]; }; then + cp_said=$({ grep -vE ': (ERROR|WARNING|CHECK):' <<<"$report"; cat "$cpdir/stderr"; } | sed -e '/^$/d' -e "s,$cpdir/,,g" -e 's/^/ /') + cp_broken+=" $short: exit $cp_status"$'\n'"${cp_said:+$cp_said$'\n'}" + fi done -[[ -z $cp_errors ]] && pass "checkpatch --strict: no errors ($cp_count warnings and checks to read)" || fail "checkpatch --strict errors: +[[ -n $cp_broken ]] && fail "checkpatch --strict didn't run to the end: +${cp_broken%$'\n'}" +[[ -n $cp_errors ]] && fail "checkpatch --strict errors: ${cp_errors%$'\n'}" +[[ -z $cp_broken$cp_errors ]] && pass "checkpatch --strict: no errors ($cp_count warnings and checks to read)" # The warnings themselves, so a reviewer reads them without rerunning it. [[ -n $cp_warnings ]] && printf '%s' "$cp_warnings" else