From b7e83d826949db3e26bae8f869bd303d60392b29 Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Sun, 6 Sep 2020 13:36:10 +0200 Subject: [PATCH] Add sanity check framework Signed-off-by: Knut Ahlers --- .../sanity.d/deprecated-repo-runner-images.sh | 16 +++++++ .config/sanity.d/has-readme.sh | 10 +++++ .config/sanity.d/makefile-autohook.sh | 16 +++++++ .config/sanity.d/placeholder-in-license.sh | 16 +++++++ bin/sanity | 43 +++++++++++++++++++ 5 files changed, 101 insertions(+) create mode 100644 .config/sanity.d/deprecated-repo-runner-images.sh create mode 100644 .config/sanity.d/has-readme.sh create mode 100644 .config/sanity.d/makefile-autohook.sh create mode 100644 .config/sanity.d/placeholder-in-license.sh create mode 100755 bin/sanity diff --git a/.config/sanity.d/deprecated-repo-runner-images.sh b/.config/sanity.d/deprecated-repo-runner-images.sh new file mode 100644 index 0000000..b47c79a --- /dev/null +++ b/.config/sanity.d/deprecated-repo-runner-images.sh @@ -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 +} diff --git a/.config/sanity.d/has-readme.sh b/.config/sanity.d/has-readme.sh new file mode 100644 index 0000000..c5a6d4c --- /dev/null +++ b/.config/sanity.d/has-readme.sh @@ -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" +} diff --git a/.config/sanity.d/makefile-autohook.sh b/.config/sanity.d/makefile-autohook.sh new file mode 100644 index 0000000..e2839b1 --- /dev/null +++ b/.config/sanity.d/makefile-autohook.sh @@ -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 +} diff --git a/.config/sanity.d/placeholder-in-license.sh b/.config/sanity.d/placeholder-in-license.sh new file mode 100644 index 0000000..3fa9de9 --- /dev/null +++ b/.config/sanity.d/placeholder-in-license.sh @@ -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 +} diff --git a/bin/sanity b/bin/sanity new file mode 100755 index 0000000..191ecc1 --- /dev/null +++ b/bin/sanity @@ -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}