From 3482cb7091b3089d94b5dd3754d6c18e677472d8 Mon Sep 17 00:00:00 2001 From: Madhu Rajanna Date: Thu, 14 May 2020 15:58:14 +0530 Subject: [PATCH] ci: updated shell script to run individual test Earlier we were running all the linter for non-go files in one short, this wont be helpful for the users who want to run particular tests. now the Makefile as different target to run separate lint test for different type of non-go files. Fixes: #979 Signed-off-by: Madhu Rajanna --- .travis.yml | 2 +- Makefile | 20 +++++++-- scripts/lint-extras.sh | 96 ++++++++++++++++++++++++++++++++++++++++++ scripts/lint-text.sh | 57 ------------------------- 4 files changed, 113 insertions(+), 62 deletions(-) create mode 100755 scripts/lint-extras.sh delete mode 100755 scripts/lint-text.sh diff --git a/.travis.yml b/.travis.yml index 1c0d43356..1255377bc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -81,7 +81,7 @@ jobs: | bash -s -- -v "${HELM_VERSION}" script: - make go-lint - - make go-lint-text + - make lint-extras - make gosec - make go-test - make mod-check diff --git a/Makefile b/Makefile index e52fba9c2..27237afe5 100644 --- a/Makefile +++ b/Makefile @@ -60,9 +60,9 @@ endif all: cephcsi -.PHONY: go-test static-check mod-check go-lint go-lint-text gosec +.PHONY: go-test static-check mod-check go-lint lint-extras gosec test: go-test static-check mod-check -static-check: check-env go-lint go-lint-text gosec +static-check: check-env go-lint lint-extras gosec go-test: check-env ./scripts/test-go.sh @@ -74,8 +74,20 @@ mod-check: check-env go-lint: ./scripts/lint-go.sh -go-lint-text: - ./scripts/lint-text.sh --require-all +lint-extras: + ./scripts/lint-extras.sh lint-all + +lint-shell: + ./scripts/lint-extras.sh lint-shell + +lint-markdown: + ./scripts/lint-extras.sh lint-markdown + +lint-yaml: + ./scripts/lint-extras.sh lint-yaml + +lint-helm: + ./scripts/lint-extras.sh lint-helm gosec: ./scripts/gosec.sh diff --git a/scripts/lint-extras.sh b/scripts/lint-extras.sh new file mode 100755 index 000000000..569f23a86 --- /dev/null +++ b/scripts/lint-extras.sh @@ -0,0 +1,96 @@ +#!/bin/bash +# vim: set ts=4 sw=4 et : + +# This script will be used to lint non-go files +# Usage: ./scripts/lint-extras.sh +# Available commands are [lint-shell lint-yaml lint-markdown lint-helmlint-all ] +set -e + +# Run checks from root of the repo +scriptdir="$(dirname "$(realpath "$0")")" +cd "$scriptdir/.." + +# run_check [optional args to checker...] +# Pass empty regex when no regex is needed +function run_check() { + local regex="$1" + shift + local exe="$1" + shift + + if [ -x "$(command -v "${exe}")" ]; then + if [ -z "${regex}" ]; then + "$exe" "$@" + else + find . -path ./vendor -prune -o -regextype egrep -iregex "$regex" -print0 | + xargs -0rt -n1 "$exe" "$@" + fi + 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 + +function lint_markdown() { + # 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 +} + +function lint_shell() { + # Install via: dnf install shellcheck + run_check '.*\.(ba)?sh' shellcheck --external-sources + run_check '.*\.(ba)?sh' bash -n +} + +function lint_yaml() { + # Install via: pip install yamllint + # disable yamlint check for helm charts + run_check '.*\.ya?ml' yamllint -s -d "{extends: default, rules: {line-length: {allow-non-breakable-inline-mappings: true}},ignore: charts/*/templates/*.yaml}" +} + +function lint_helm() { + # Install via: https://github.com/helm/helm/blob/master/docs/install.md + run_check '' helm lint --namespace=test charts/* +} + +function lint_all() { + # runs all checks + all_required=1 + lint_shell + lint_yaml + lint_markdown + lint_helm +} +case "${1:-}" in +lint-shell) + lint_shell + ;; +lint-yaml) + lint_yaml + ;; +lint-markdown) + lint_markdown + ;; +lint-helm) + lint_helm + ;; +lint-all) + lint_all + ;; +*) + echo " $0 [command] +Available Commands: + lint-shell Lint shell files + lint-yaml Lint yaml files + lint-markdown Lint markdown files + lint-helm Lint helm charts + lint-all Run lint on all non-go files +" >&2 + ;; +esac diff --git a/scripts/lint-text.sh b/scripts/lint-text.sh deleted file mode 100755 index 45aa144c8..000000000 --- a/scripts/lint-text.sh +++ /dev/null @@ -1,57 +0,0 @@ -#! /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...] -# Pass empty regex when no regex is needed -function run_check() { - regex="$1" - shift - exe="$1" - shift - - if [ -x "$(command -v "$exe")" ]; then - if [ -z "$regex" ]; then - "$exe" "$@" - else - find . -path ./vendor -prune -o -regextype egrep -iregex "$regex" -print0 | - xargs -0rt -n1 "$exe" "$@" - fi - 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 --external-sources -run_check '.*\.(ba)?sh' bash -n - -# Install via: pip install yamllint -# disable yamlint check for helm charts -run_check '.*\.ya?ml' yamllint -s -d "{extends: default, rules: {line-length: {allow-non-breakable-inline-mappings: true}},ignore: charts/*/templates/*.yaml}" - -# Install via: https://github.com/helm/helm/blob/master/docs/install.md -run_check '' helm lint --namespace=test charts/* - -echo "ALL OK."