Add sanity check framework

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2020-09-06 13:36:10 +02:00
parent ee77e6edcc
commit b7e83d8269
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
5 changed files with 101 additions and 0 deletions

View file

@ -0,0 +1,16 @@
function sanity_deprecated_reporunner_images() {
report debug "Check for deprecated repo-runner images"
[[ -f .repo-runner.yaml ]] || {
report debug "No Repo-Runner config found"
return 0
}
grep 'quay.io/luzifer/' .repo-runner.yaml || {
report success "No Quay image found in Repo-Runner config"
return 0
}
report error "Repo-Runner config contains old Quay image"
return 1
}

View file

@ -0,0 +1,10 @@
function sanity_has_readme() {
report debug "Check for README.md existence"
[[ -f README.md ]] || {
report error "No README.md found"
return 1
}
report success "README.md found"
}

View file

@ -0,0 +1,16 @@
function sanity_makefile_contains_autohook() {
report debug "Check for auto-hook in Makefile"
[[ -f Makefile ]] || {
report debug "No Makefile found"
return 0
}
grep '^auto-hook-' Makefile || {
report success "No auto-hooks found"
return 0
}
report error "Makefile contains auto-hooks"
return 1
}

View file

@ -0,0 +1,16 @@
function sanity_year_placeholder_in_license() {
report debug "Check for [year] placeholder in LICENSE file"
[[ -f LICENSE ]] || {
report warn "No LICENSE file found"
return 0
}
grep '\[year\]' LICENSE || {
report success "No placeholder found"
return 0
}
report error "LICENSE file contains [year] placeholder"
return 1
}

43
bin/sanity Executable file
View file

@ -0,0 +1,43 @@
#!/bin/bash
set -euo pipefail
source ${HOME}/bin/script_framework.sh
reportpad=0
retcode=0
# report is a wrapper to display messages in a certain level with
# added information about the function yielding the result. It
# must be called from within a "sanity_*" function
function report() {
local levelfunc="$1"
shift
local message="$@"
local func="${FUNCNAME[1]#sanity_}"
"$levelfunc" "$(printf "%-${reportpad}s" ${func}) ${message}"
}
# Load all part-files from the ~/.config/sanity.d directory
for partFile in "${HOME}/.config/sanity.d"/*.sh; do
source "${partFile}"
done
# Collect all functions present within the part-files matching the
# "sanity_*" naming schema
parts=($(declare -F | awk '/sanity_[0-9a-zA-Z_-]+$/{ print $3 }'))
# For display reasons store the longest function name to be used in
# report to format the output in columns
for func in "${parts[@]}"; do
lfunc=$((${#func} - 7)) # 7 = length of stripped "sanity_" prefix
[ ${lfunc} -lt ${reportpad} ] || reportpad=${lfunc}
done
# Execute all "sanity_*" functions and store exit-code 1 if one of
# them fails to exit gracefully
for func in "${parts[@]}"; do
"${func}" || retcode=1
done
exit ${retcode}