Outsourced linting tests into its own file (#1636)
* outsourced linting tests into its own file * trigger rebuild * added SCRIPT variable to setup.sh * trigger rebuild again * major test rewrite * outsourced `hadolint` too * rewrote some parts of the linting logic due to a logic bug * adjusted TravisCI * corrected .bats test line * corrected logging in linting tests * updated `hadolint`
This commit is contained in:
parent
4c76565380
commit
ed6e3ebd30
|
@ -8,7 +8,7 @@ sudo: required
|
||||||
|
|
||||||
env:
|
env:
|
||||||
global:
|
global:
|
||||||
- HADOLINT_VERSION=1.17.1
|
- HADOLINT_VERSION=1.18.0
|
||||||
- SHELLCHECK_VERSION=0.7.1
|
- SHELLCHECK_VERSION=0.7.1
|
||||||
- ECLINT_VERSION=2.1.0
|
- ECLINT_VERSION=2.1.0
|
||||||
|
|
||||||
|
@ -33,12 +33,12 @@ before_install:
|
||||||
- sudo chmod +x /usr/bin/eclint
|
- sudo chmod +x /usr/bin/eclint
|
||||||
|
|
||||||
install:
|
install:
|
||||||
- make lint
|
|
||||||
- travis_retry travis_wait make build
|
- travis_retry travis_wait make build
|
||||||
|
|
||||||
script:
|
script:
|
||||||
- make shellcheck
|
|
||||||
- make eclint
|
- make eclint
|
||||||
|
- make hadolint
|
||||||
|
- make shellcheck
|
||||||
- make generate-accounts run generate-accounts-after-run fixtures tests
|
- make generate-accounts run generate-accounts-after-run fixtures tests
|
||||||
|
|
||||||
after_script:
|
after_script:
|
||||||
|
|
29
Makefile
29
Makefile
|
@ -141,11 +141,6 @@ tests:
|
||||||
test/%.bats: ALWAYS_RUN
|
test/%.bats: ALWAYS_RUN
|
||||||
./test/bats/bin/bats $@
|
./test/bats/bin/bats $@
|
||||||
|
|
||||||
lint:
|
|
||||||
# List files which name starts with 'Dockerfile'
|
|
||||||
# eg. Dockerfile, Dockerfile.build, etc.
|
|
||||||
-@ git ls-files --exclude='Dockerfile*' --ignored | xargs --max-lines=1 hadolint
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
# remove running and stopped test containers
|
# remove running and stopped test containers
|
||||||
-@ docker ps -a | grep -E "docker-mailserver:testing|ldap_for_mail" | cut -f 1-1 -d ' ' | xargs --no-run-if-empty docker rm -f
|
-@ docker ps -a | grep -E "docker-mailserver:testing|ldap_for_mail" | cut -f 1-1 -d ' ' | xargs --no-run-if-empty docker rm -f
|
||||||
|
@ -159,23 +154,13 @@ clean:
|
||||||
fi
|
fi
|
||||||
-@ sudo rm -rf test/onedir test/alias test/quota test/relay test/config/dovecot-lmtp/userdb test/config/key* test/config/opendkim/keys/domain.tld/ test/config/opendkim/keys/example.com/ test/config/opendkim/keys/localdomain2.com/ test/config/postfix-aliases.cf test/config/postfix-receive-access.cf test/config/postfix-receive-access.cfe test/config/dovecot-quotas.cf test/config/postfix-send-access.cf test/config/postfix-send-access.cfe test/config/relay-hosts/chksum test/config/relay-hosts/postfix-aliases.cf test/config/dhparams.pem test/config/dovecot-lmtp/dh.pem test/config/relay-hosts/dovecot-quotas.cf test/config/user-patches.sh test/alias/config/postfix-virtual.cf test/quota/config/dovecot-quotas.cf test/quota/config/postfix-accounts.cf test/relay/config/postfix-relaymap.cf test/relay/config/postfix-sasl-password.cf
|
-@ sudo rm -rf test/onedir test/alias test/quota test/relay test/config/dovecot-lmtp/userdb test/config/key* test/config/opendkim/keys/domain.tld/ test/config/opendkim/keys/example.com/ test/config/opendkim/keys/localdomain2.com/ test/config/postfix-aliases.cf test/config/postfix-receive-access.cf test/config/postfix-receive-access.cfe test/config/dovecot-quotas.cf test/config/postfix-send-access.cf test/config/postfix-send-access.cfe test/config/relay-hosts/chksum test/config/relay-hosts/postfix-aliases.cf test/config/dhparams.pem test/config/dovecot-lmtp/dh.pem test/config/relay-hosts/dovecot-quotas.cf test/config/user-patches.sh test/alias/config/postfix-virtual.cf test/quota/config/dovecot-quotas.cf test/quota/config/postfix-accounts.cf test/relay/config/postfix-relaymap.cf test/relay/config/postfix-sasl-password.cf
|
||||||
|
|
||||||
|
lint: eclint hadolint shellcheck
|
||||||
|
|
||||||
|
hadolint:
|
||||||
|
@ ./test/linting/lint.sh hadolint
|
||||||
|
|
||||||
shellcheck:
|
shellcheck:
|
||||||
@ echo -e "Testing shell / bash scripts with shellcheck\n"
|
@ ./test/linting/lint.sh shellcheck
|
||||||
@ /usr/bin/shellcheck --version
|
|
||||||
@ echo ''
|
|
||||||
@ if find -iname "*.sh" -not -path "./test/*" -not -path "./target/docker-configomat/*" -exec /usr/bin/shellcheck -S style -Cauto -o all -e SC2154 -W 50 {} \; | grep .; then\
|
|
||||||
echo -e "\nError" ;\
|
|
||||||
exit 1 ;\
|
|
||||||
else\
|
|
||||||
echo -e '\nSuccess' ;\
|
|
||||||
fi
|
|
||||||
|
|
||||||
eclint:
|
eclint:
|
||||||
@ echo -e "Testing file formatting according to .editorconfig\n"
|
@ ./test/linting/lint.sh eclint
|
||||||
@ printf "Version %s\n\n" "$$(/usr/bin/eclint --version)"
|
|
||||||
@ if /usr/bin/eclint -exclude "\.bats$$" | grep .; then\
|
|
||||||
echo -e "\nError" ;\
|
|
||||||
exit 1 ;\
|
|
||||||
else\
|
|
||||||
echo -e '\nSuccess' ;\
|
|
||||||
fi
|
|
||||||
|
|
24
setup.sh
24
setup.sh
|
@ -3,12 +3,24 @@
|
||||||
# Wrapper for various setup scripts
|
# Wrapper for various setup scripts
|
||||||
# included in the docker-mailserver
|
# included in the docker-mailserver
|
||||||
|
|
||||||
set -euEo pipefail
|
SCRIPT='SETUP'
|
||||||
trap '_report_err ${_} ${LINENO} ${?}' ERR
|
|
||||||
|
|
||||||
function _report_err
|
set -euEo pipefail
|
||||||
|
trap '__log_err ${FUNCNAME[0]:-"?"} ${_:-"?"} ${LINENO:-"?"} ${?:-"?"}' ERR
|
||||||
|
|
||||||
|
function __log_err
|
||||||
{
|
{
|
||||||
echo "ERROR occured :: source ${1} ; line ${2} ; exit code ${3} ;;" >&2
|
local FUNC_NAME LINE EXIT_CODE
|
||||||
|
FUNC_NAME="${1} / ${2}"
|
||||||
|
LINE="${3}"
|
||||||
|
EXIT_CODE="${4}"
|
||||||
|
|
||||||
|
printf "\n––– \e[1m\e[31mUNCHECKED ERROR\e[0m\n%s\n%s\n%s\n%s\n\n" \
|
||||||
|
" – script = ${SCRIPT,,}.sh" \
|
||||||
|
" – function = ${FUNC_NAME}" \
|
||||||
|
" – line = ${LINE}" \
|
||||||
|
" – exit code = ${EXIT_CODE}"
|
||||||
|
|
||||||
_unset_vars
|
_unset_vars
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,7 +100,9 @@ function _inspect
|
||||||
|
|
||||||
function _usage
|
function _usage
|
||||||
{
|
{
|
||||||
echo "Usage: ${0} [-i IMAGE_NAME] [-c CONTAINER_NAME] <subcommand> <subcommand> [args]
|
echo "${SCRIPT,,}.sh
|
||||||
|
|
||||||
|
Usage: ${0} [-i IMAGE_NAME] [-c CONTAINER_NAME] <subcommand> <subcommand> [args]
|
||||||
|
|
||||||
OPTIONS:
|
OPTIONS:
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,176 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
# version v0.1.0 stable
|
||||||
|
# executed by TravisCI / manually
|
||||||
|
# task checks files agains linting targets
|
||||||
|
|
||||||
|
SCRIPT="LINT TESTS"
|
||||||
|
|
||||||
|
function _get_current_directory
|
||||||
|
{
|
||||||
|
if dirname "$(readlink -f "${0}")" &>/dev/null
|
||||||
|
then
|
||||||
|
CDIR="$(cd "$(dirname "$(readlink -f "${0}")")" && pwd)"
|
||||||
|
elif realpath -e -L "${0}" &>/dev/null
|
||||||
|
then
|
||||||
|
CDIR="$(realpath -e -L "${0}")"
|
||||||
|
CDIR="${CDIR%/setup.sh}"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
CDIR="$(pwd)"
|
||||||
|
_get_current_directory
|
||||||
|
|
||||||
|
# ? ––––––––––––––––––––––––––––––––––––––––––––– ERRORS
|
||||||
|
|
||||||
|
set -eEuo pipefail
|
||||||
|
trap '__log_err ${FUNCNAME[0]:-"?"} ${_:-"?"} ${LINENO:-"?"} ${?:-"?"}' ERR
|
||||||
|
|
||||||
|
function __log_err
|
||||||
|
{
|
||||||
|
local FUNC_NAME LINE EXIT_CODE
|
||||||
|
FUNC_NAME="${1} / ${2}"
|
||||||
|
LINE="${3}"
|
||||||
|
EXIT_CODE="${4}"
|
||||||
|
|
||||||
|
printf "\n––– \e[1m\e[31mUNCHECKED ERROR\e[0m\n%s\n%s\n%s\n%s\n\n" \
|
||||||
|
" – script = ${SCRIPT}" \
|
||||||
|
" – function = ${FUNC_NAME}" \
|
||||||
|
" – line = ${LINE}" \
|
||||||
|
" – exit code = ${EXIT_CODE}"
|
||||||
|
|
||||||
|
unset CDIR SCRIPT OS VERSION
|
||||||
|
}
|
||||||
|
|
||||||
|
# ? ––––––––––––––––––––––––––––––––––––––––––––– LOG
|
||||||
|
|
||||||
|
function __log_info
|
||||||
|
{
|
||||||
|
printf "\n––– \e[34m%s\e[0m\n%s\n%s\n\n" \
|
||||||
|
"${SCRIPT}" \
|
||||||
|
" – type = INFO" \
|
||||||
|
" – message = ${*}"
|
||||||
|
}
|
||||||
|
|
||||||
|
function __log_warning
|
||||||
|
{
|
||||||
|
printf "\n––– \e[93m%s\e[0m\n%s\n%s\n\n" \
|
||||||
|
"${SCRIPT}" \
|
||||||
|
" – type = WARNING" \
|
||||||
|
" – message = ${*}"
|
||||||
|
}
|
||||||
|
|
||||||
|
function __log_abort
|
||||||
|
{
|
||||||
|
printf "\n––– \e[91m%s\e[0m\n%s\n%s\n\n" \
|
||||||
|
"${SCRIPT}" \
|
||||||
|
" – type = ABORT" \
|
||||||
|
" – message = ${*:-"errors encountered"}"
|
||||||
|
}
|
||||||
|
|
||||||
|
function __log_success
|
||||||
|
{
|
||||||
|
printf "\n––– \e[32m%s\e[0m\n%s\n%s\n\n" \
|
||||||
|
"${SCRIPT}" \
|
||||||
|
" – type = SUCCESS" \
|
||||||
|
" – message = ${*}"
|
||||||
|
}
|
||||||
|
|
||||||
|
function __in_path { __which "${@}" && return 0 ; return 1 ; }
|
||||||
|
function __which { command -v "${@}" &>/dev/null ; }
|
||||||
|
|
||||||
|
function _eclint
|
||||||
|
{
|
||||||
|
local LINT=(eclint -exclude "(.*\.git.*|.*\.md$|\.bats$)")
|
||||||
|
|
||||||
|
if ! __in_path "${LINT[0]}"
|
||||||
|
then
|
||||||
|
__log_abort 'linter not in PATH'
|
||||||
|
return 102
|
||||||
|
fi
|
||||||
|
|
||||||
|
__log_info \
|
||||||
|
'type: editorconfig' \
|
||||||
|
'(linter version:' "$(${LINT[0]} --version))"
|
||||||
|
|
||||||
|
local SCRIPT='EDITORCONFIG LINTER'
|
||||||
|
if "${LINT[@]}"
|
||||||
|
then
|
||||||
|
__log_success 'no errors detected'
|
||||||
|
else
|
||||||
|
__log_abort
|
||||||
|
return 101
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
function _hadolint
|
||||||
|
{
|
||||||
|
local LINT=(hadolint -c "${CDIR}/.hadolint.yaml")
|
||||||
|
|
||||||
|
if ! __in_path "${LINT[0]}"
|
||||||
|
then
|
||||||
|
__log_abort 'linter not in PATH'
|
||||||
|
return 102
|
||||||
|
fi
|
||||||
|
|
||||||
|
__log_info \
|
||||||
|
'type: Dockerfile' \
|
||||||
|
'(linter version:' "$(${LINT[0]} --version | grep -E -o "v[0-9\.]*"))"
|
||||||
|
|
||||||
|
local SCRIPT='HADOLINT'
|
||||||
|
if git ls-files --exclude='Dockerfile*' --ignored | \
|
||||||
|
xargs --max-lines=1 "${LINT[@]}"
|
||||||
|
then
|
||||||
|
__log_success 'no errors detected'
|
||||||
|
else
|
||||||
|
__log_abort
|
||||||
|
return 101
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
function _shellcheck
|
||||||
|
{
|
||||||
|
local LINT=(/usr/bin/shellcheck -S style -Cauto -o all -e SC2154 -W 50)
|
||||||
|
|
||||||
|
if ! __in_path "${LINT[0]}"
|
||||||
|
then
|
||||||
|
__log_abort 'linter not in PATH'
|
||||||
|
return 102
|
||||||
|
fi
|
||||||
|
|
||||||
|
__log_info \
|
||||||
|
'type: shellcheck' '(linter version:' \
|
||||||
|
"$(${LINT[0]} --version | grep -m 2 -o "[0-9.]*"))"
|
||||||
|
|
||||||
|
local FIND=(
|
||||||
|
find . -iname "*.sh"
|
||||||
|
-not -path "./test/*"
|
||||||
|
-not -path "./target/docker-configomat/*"
|
||||||
|
-exec "${LINT[@]}" {} \;)
|
||||||
|
|
||||||
|
local SCRIPT='SHELLCHECK'
|
||||||
|
if "${FIND[@]}" | grep -q .
|
||||||
|
then
|
||||||
|
"${FIND[@]}"
|
||||||
|
__log_abort
|
||||||
|
return 101
|
||||||
|
else
|
||||||
|
__log_success 'no errors detected'
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
function _main
|
||||||
|
{
|
||||||
|
case ${1:- } in
|
||||||
|
'eclint' ) _eclint ;;
|
||||||
|
'hadolint' ) _hadolint ;;
|
||||||
|
'shellcheck' ) _shellcheck ;;
|
||||||
|
*)
|
||||||
|
__log_abort \
|
||||||
|
"init.sh: '${1}' is not a command nor an option. See 'make help'."
|
||||||
|
exit 11
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
_main "${@}" || exit ${?}
|
|
@ -1206,13 +1206,13 @@ EOF
|
||||||
@test "checking setup.sh: Without arguments: status 1, show help text" {
|
@test "checking setup.sh: Without arguments: status 1, show help text" {
|
||||||
run ./setup.sh
|
run ./setup.sh
|
||||||
assert_failure
|
assert_failure
|
||||||
[ "${lines[0]}" = "Usage: ./setup.sh [-i IMAGE_NAME] [-c CONTAINER_NAME] <subcommand> <subcommand> [args]" ]
|
[ "${lines[1]}" = "Usage: ./setup.sh [-i IMAGE_NAME] [-c CONTAINER_NAME] <subcommand> <subcommand> [args]" ]
|
||||||
}
|
}
|
||||||
|
|
||||||
@test "checking setup.sh: Wrong arguments" {
|
@test "checking setup.sh: Wrong arguments" {
|
||||||
run ./setup.sh lol troll
|
run ./setup.sh lol troll
|
||||||
assert_failure
|
assert_failure
|
||||||
[ "${lines[0]}" = "Usage: ./setup.sh [-i IMAGE_NAME] [-c CONTAINER_NAME] <subcommand> <subcommand> [args]" ]
|
[ "${lines[1]}" = "Usage: ./setup.sh [-i IMAGE_NAME] [-c CONTAINER_NAME] <subcommand> <subcommand> [args]" ]
|
||||||
}
|
}
|
||||||
|
|
||||||
# email
|
# email
|
||||||
|
|
Loading…
Reference in New Issue