From 1f3b32085cf652faa65d57e3e66648899f05e63a Mon Sep 17 00:00:00 2001 From: Madhu Rajanna Date: Thu, 7 Feb 2019 15:26:39 +0530 Subject: [PATCH] Add static check tools Added md check to check .md style Add yamllint to check yaml style Moved gometalinter to scripts folder Signed-off-by: Madhu Rajanna --- scripts/lint-go.sh | 12 +++++++++++ scripts/lint-text.sh | 49 ++++++++++++++++++++++++++++++++++++++++++++ scripts/mdl-style.rb | 9 ++++++++ scripts/test-go.sh | 42 +++++++++++++++++++++++++++++++++++++ 4 files changed, 112 insertions(+) create mode 100755 scripts/lint-go.sh create mode 100755 scripts/lint-text.sh create mode 100644 scripts/mdl-style.rb create mode 100755 scripts/test-go.sh diff --git a/scripts/lint-go.sh b/scripts/lint-go.sh new file mode 100755 index 000000000..0f9d49d18 --- /dev/null +++ b/scripts/lint-go.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +set -o pipefail + +if [[ -x "$(command -v gometalinter)" ]]; then + gometalinter -j "${GO_METALINTER_THREADS:-1}" \ + --sort path --sort line --sort column --deadline=10m \ + --enable=misspell --enable=staticcheck \ + --vendor "${@-./...}" +else + echo "WARNING: gometalinter not found, skipping lint tests" >&2 +fi diff --git a/scripts/lint-text.sh b/scripts/lint-text.sh new file mode 100755 index 000000000..066c8d382 --- /dev/null +++ b/scripts/lint-text.sh @@ -0,0 +1,49 @@ +#! /bin/bash +# vim: set ts=4 sw=4 et : + +# Usage: pre-commit.sh [--require-all] +# --require-all Fail instead of warn if a checker is not found + +set -e + +# Run checks from root of the repo +scriptdir="$(dirname "$(realpath "$0")")" +cd "$scriptdir/.." + +# run_check [optional args to checker...] +function run_check() { + regex="$1" + shift + exe="$1" + shift + + if [ -x "$(command -v "$exe")" ]; then + find . -path ./vendor -prune -o -regextype egrep -iregex "$regex" -print0 | + xargs -0rt -n1 "$exe" "$@" + elif [ "$all_required" -eq 0 ]; then + echo "Warning: $exe not found... skipping some tests." + else + echo "FAILED: All checks required, but $exe not found!" + exit 1 + fi +} + +all_required=0 +if [ "x$1" == "x--require-all" ]; then + all_required=1 +fi + +# markdownlint: https://github.com/markdownlint/markdownlint +# https://github.com/markdownlint/markdownlint/blob/master/docs/RULES.md +# Install via: gem install mdl +run_check '.*\.md' mdl --style scripts/mdl-style.rb + +# Install via: dnf install shellcheck +run_check '.*\.(ba)?sh' shellcheck +run_check '.*\.(ba)?sh' bash -n + +# Install via: pip install yamllint +# disable yamlint chekck for helm chats +run_check '.*\.ya?ml' yamllint -s -d "{extends: default, rules: {line-length: {allow-non-breakable-inline-mappings: true}},ignore: deploy/rbd/helm/templates/*.yaml}" + +echo "ALL OK." diff --git a/scripts/mdl-style.rb b/scripts/mdl-style.rb new file mode 100644 index 000000000..3f392568c --- /dev/null +++ b/scripts/mdl-style.rb @@ -0,0 +1,9 @@ +all + +#Refer below url for more information about the markdown rules. +#https://github.com/markdownlint/markdownlint/blob/master/docs/RULES.md + +rule 'MD013', :code_blocks => false, :tables => false + +exclude_rule 'MD040' # Fenced code blocks should have a language specified +exclude_rule 'MD041' # First line in file should be a top level header diff --git a/scripts/test-go.sh b/scripts/test-go.sh new file mode 100755 index 000000000..de3ecd2fb --- /dev/null +++ b/scripts/test-go.sh @@ -0,0 +1,42 @@ +#!/bin/bash + +GOPACKAGES="$(go list ./... | grep -v vendor | grep -v e2e)" +COVERFILE="${GO_COVER_DIR}profile.cov" + +# no special options, exec to go test w/ all pkgs +if [[ ${TEST_EXITFIRST} != "yes" && -z ${TEST_COVERAGE} ]]; then + # shellcheck disable=SC2086 + exec go test ${GOPACKAGES} +fi + +# our options are set so we need to handle each go package one +# at at time +if [[ ${TEST_COVERAGE} ]]; then + GOTESTOPTS="-covermode=count -coverprofile=cover.out" + echo "mode: count" > "${COVERFILE}" +fi + +failed=0 +for gopackage in ${GOPACKAGES}; do + echo "--- testing: ${gopackage} ---" + # shellcheck disable=SC2086 + go test ${GOTESTOPTS} "${gopackage}" || ((failed+=1)) + if [[ -f cover.out ]]; then + # Append to coverfile + grep -v "^mode: count" cover.out >> "${COVERFILE}" + fi + if [[ ${TEST_COVERAGE} = "stdout" && -f cover.out ]]; then + go tool cover -func=cover.out + fi + if [[ ${TEST_COVERAGE} = "html" && -f cover.out ]]; then + mkdir -p coverage + fn="coverage/${gopackage////-}.html" + echo " * generating coverage html: ${fn}" + go tool cover -html=cover.out -o "${fn}" + fi + rm -f cover.out + if [[ ${failed} -ne 0 && ${TEST_EXITFIRST} = "yes" ]]; then + exit ${failed} + fi +done +exit ${failed}