77 lines
2.1 KiB
Bash
Executable File
77 lines
2.1 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
# SPDX-FileCopyrightText: 2025-2026 Christian Galo
|
|
#
|
|
# Fail on a function no program reaches, over every package outside status/.
|
|
# deadcode exits 0 even when it reports, so this script reads its output.
|
|
#
|
|
# Run 1 includes the tests' entry points (-test): it must print nothing.
|
|
# Run 2 leaves them out: what it reports is code only tests reach, and each
|
|
# such function must be listed in scripts/deadcode-testonly.txt, one
|
|
# <package path>.<Func> per line with a reason and an owner (an entry ending
|
|
# in .* covers a package; # comments and blank lines are ignored). An entry
|
|
# that no longer reports fails too, so the list cannot go stale.
|
|
set -euo pipefail
|
|
|
|
cd "$(dirname "$0")/.."
|
|
|
|
module=$(go list -m)
|
|
mapfile -t pkgs < <(go list -e ./... | grep -v '/status/' || true)
|
|
|
|
status=0
|
|
|
|
found=$(go tool deadcode -test "${pkgs[@]}")
|
|
if [[ -n $found ]]; then
|
|
printf '%s\n' "$found"
|
|
echo "deadcode: unreachable functions, even counting the tests" >&2
|
|
status=1
|
|
fi
|
|
|
|
reported=$(go tool deadcode -f '{{range .Funcs}}{{$.Path}}.{{.Name}}{{"\n"}}{{end}}' "${pkgs[@]}" |
|
|
sed "s|^${module}/||" | grep -v '^$' | sort -u || true)
|
|
|
|
allowed=$(sed -e 's/#.*//' -e 's/[[:space:]]*$//' scripts/deadcode-testonly.txt | grep -v '^$' | sort -u || true)
|
|
|
|
covers() {
|
|
local entry=$1 fn=$2
|
|
if [[ $entry == *.'*' ]]; then
|
|
[[ $fn == "${entry%'*'}"* ]]
|
|
else
|
|
[[ $fn == "$entry" ]]
|
|
fi
|
|
}
|
|
|
|
while IFS= read -r fn; do
|
|
[[ -z $fn ]] && continue
|
|
listed=0
|
|
while IFS= read -r entry; do
|
|
[[ -z $entry ]] && continue
|
|
if covers "$entry" "$fn"; then
|
|
listed=1
|
|
break
|
|
fi
|
|
done <<<"$allowed"
|
|
if ((listed == 0)); then
|
|
echo "deadcode: $fn is reached only by tests and scripts/deadcode-testonly.txt does not list it" >&2
|
|
status=1
|
|
fi
|
|
done <<<"$reported"
|
|
|
|
while IFS= read -r entry; do
|
|
[[ -z $entry ]] && continue
|
|
hit=0
|
|
while IFS= read -r fn; do
|
|
[[ -z $fn ]] && continue
|
|
if covers "$entry" "$fn"; then
|
|
hit=1
|
|
break
|
|
fi
|
|
done <<<"$reported"
|
|
if ((hit == 0)); then
|
|
echo "deadcode: scripts/deadcode-testonly.txt lists $entry, which no longer reports" >&2
|
|
status=1
|
|
fi
|
|
done <<<"$allowed"
|
|
|
|
exit "$status"
|