Merge branch 'master' of github.com:kvz/bash3boilerplate

This commit is contained in:
Kevin van Zonneveld 2016-12-15 15:12:10 +01:00
commit d0bfc96166
3 changed files with 83 additions and 33 deletions

View File

@ -12,6 +12,12 @@ env:
- secure: BeM6mpPEATdeFcAr/222QBZ9vRkZtU2WOi9QQy3mxsuDbWfM8RxYESIEJLipyhW9kXGoe6HGMqm4Kz9B/c4jrzeSXPpKnW7mIfnyqN+hhq1ctW9qPSqodu+fYNhdDxXh5wylml7hnIJzU70vFGrFknZRE2FYk5XvyHg2ImIKDJw= - secure: BeM6mpPEATdeFcAr/222QBZ9vRkZtU2WOi9QQy3mxsuDbWfM8RxYESIEJLipyhW9kXGoe6HGMqm4Kz9B/c4jrzeSXPpKnW7mIfnyqN+hhq1ctW9qPSqodu+fYNhdDxXh5wylml7hnIJzU70vFGrFknZRE2FYk5XvyHg2ImIKDJw=
- secure: RJ5UpdXms9QkraylZ11OBfmcRrmKnb254Yj0yCDAvZmg+n+3jSTwMgGvPY8Ih8X/R1JeW3VTtFDkJXXPnjjfpNg1M91u4CAEUOMPciCudYcoF6GKb8psnOzneTTX5M7zuJSzknGdpv/foldxiPYxiY5Hn5bfjmikhAEl+QX/R0Y= - secure: RJ5UpdXms9QkraylZ11OBfmcRrmKnb254Yj0yCDAvZmg+n+3jSTwMgGvPY8Ih8X/R1JeW3VTtFDkJXXPnjjfpNg1M91u4CAEUOMPciCudYcoF6GKb8psnOzneTTX5M7zuJSzknGdpv/foldxiPYxiY5Hn5bfjmikhAEl+QX/R0Y=
- secure: BXf2buPt/DA09M5ZUdp/LpOWtUuz1mfCBopLyxvHv3Sl3ln+Az57wWsM2+Re+77lUOgihR2f6lXYfNUmQuSUo157rZPunQCqM/DJhK69KhREEB6SJDaJF3FVlnGla+Cwwb1IQUtMopqX9pBYD7w/zyWQFJCi20O57JEVIdfZZS8= - secure: BXf2buPt/DA09M5ZUdp/LpOWtUuz1mfCBopLyxvHv3Sl3ln+Az57wWsM2+Re+77lUOgihR2f6lXYfNUmQuSUo157rZPunQCqM/DJhK69KhREEB6SJDaJF3FVlnGla+Cwwb1IQUtMopqX9pBYD7w/zyWQFJCi20O57JEVIdfZZS8=
addons:
apt:
sources:
- debian-sid
packages:
- shellcheck
install: npm install # <-- yarn still messes up nested bins: https://github.com/yarnpkg/yarn/issues/760 install: npm install # <-- yarn still messes up nested bins: https://github.com/yarnpkg/yarn/issues/760
before_install: before_install:
- if [ "$TRAVIS_OS_NAME" = "osx" ]; then brew update; fi - if [ "$TRAVIS_OS_NAME" = "osx" ]; then brew update; fi

View File

@ -43,6 +43,7 @@ read -r -d '' __helptext <<-'EOF' || true # exits non-zero when EOF encountered
parsed and will be added as-is to the help. parsed and will be added as-is to the help.
EOF EOF
# shellcheck source=./main.sh
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/main.sh" source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/main.sh"

View File

@ -31,37 +31,37 @@ set -o pipefail
# Set magic variables for current file, directory, os, etc. # Set magic variables for current file, directory, os, etc.
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" __dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
__file="${__dir}/$(basename "${BASH_SOURCE[0]}")" __file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
__base="$(basename ${__file} .sh)" __base="$(basename "${__file}" .sh)"
__root="$(cd "$(dirname "${__dir}")" && pwd)" __root="$(cd "$(dirname "${__dir}")" && pwd)"
scenarios="${1:-$(ls ${__dir}/scenario/|egrep -v ^prepare$)}"
__sysTmpDir="${TMPDIR:-/tmp}" __sysTmpDir="${TMPDIR:-/tmp}"
__sysTmpDir="${__sysTmpDir%/}" # <-- remove trailing slash on macosx __sysTmpDir="${__sysTmpDir%/}" # <-- remove trailing slash on macosx
__accptstTmpDir="${__sysTmpDir}/accptst" __accptstTmpDir=$(mktemp -d "${__sysTmpDir}/${__base}.XXXXXX")
mkdir -p "${__accptstTmpDir}"
function cleanup_before_exit () { rm -r "${__accptstTmpDir:?}"; }
trap cleanup_before_exit EXIT
cmdSed=sed
cmdTimeout=timeout
if [[ "${OSTYPE}" == "darwin"* ]]; then if [[ "${OSTYPE}" == "darwin"* ]]; then
cmdSed=gsed cmdSed=gsed
else cmdTimeout=gtimeout
cmdSed=sed
fi fi
if [[ "${OSTYPE}" == "darwin"* ]]; then if [[ ! "$(command -v ${cmdSed})" ]]; then
cmdTimeout="gtimeout --kill-after=6m 5m" echo "Please install ${cmdSed}"
else exit 1
cmdTimeout="timeout --kill-after=6m 5m" fi
if [[ ! "$(command -v ${cmdTimeout})" ]]; then
echo "Please install ${cmdTimeout}"
exit 1
fi fi
__node="$(which node)" __node="$(which node)"
__arch="amd64" __arch="amd64"
if ! which "${cmdSed}" > /dev/null; then
echo "Please install ${cmdSed}"
exit 1
fi
# explicitly setting NO_COLOR to false will make b3bp ignore TERM # explicitly setting NO_COLOR to false will make b3bp ignore TERM
# not being "xterm*" and STDERR not being connected to a terminal # not being "xterm*" and STDERR not being connected to a terminal
# it's the opposite of NO_COLOR="true" - it forces color, no matter what # it's the opposite of NO_COLOR="true" - it forces color, no matter what
@ -70,18 +70,24 @@ export NO_COLOR="false"
# Running prepare before other scenarios is important on Travis, # Running prepare before other scenarios is important on Travis,
# so that stdio can diverge - and we can enforce stricter # so that stdio can diverge - and we can enforce stricter
# stdio comparison on all other tests. # stdio comparison on all other tests.
for scenario in $(echo ${scenarios}); do while IFS=$'\n' read -r scenario; do
scenario="$(dirname "${scenario}")"
scenario="${scenario##${__dir}/scenario/}"
[[ "${scenario}" = "prepare" ]] && continue
[[ "${1:-}" ]] && [[ "${scenario}" != "${1}" ]] && continue
echo "==> Scenario: ${scenario}" echo "==> Scenario: ${scenario}"
pushd "${__dir}/scenario/${scenario}" > /dev/null pushd "${__dir}/scenario/${scenario}" > /dev/null
# Run scenario # Run scenario
(${cmdTimeout} bash ./run.sh \ (${cmdTimeout} --kill-after=6m 5m bash ./run.sh \
> "${__accptstTmpDir}/${scenario}.stdio" 2>&1; \ > "${__accptstTmpDir}/${scenario}.stdio" 2>&1; \
echo "${?}" > "${__accptstTmpDir}/${scenario}.exitcode" \ echo "${?}" > "${__accptstTmpDir}/${scenario}.exitcode" \
) || true ) || true
# Clear out environmental specifics # Clear out environmental specifics
for typ in $(echo stdio exitcode); do for typ in stdio exitcode; do
curFile="${__accptstTmpDir}/${scenario}.${typ}" curFile="${__accptstTmpDir}/${scenario}.${typ}"
"${cmdSed}" -i \ "${cmdSed}" -i \
-e "s@${__node}@{node}@g" "${curFile}" \ -e "s@${__node}@{node}@g" "${curFile}" \
@ -101,7 +107,7 @@ for scenario in $(echo ${scenarios}); do
-e "s@Linux@{os}@g" "${curFile}" \ -e "s@Linux@{os}@g" "${curFile}" \
|| false || false
if [ "$(cat "${curFile}" |grep 'ACCPTST:STDIO_REPLACE_IPS' |wc -l)" -gt 0 ]; then if grep -q 'ACCPTST:STDIO_REPLACE_IPS' "${curFile}"; then
"${cmdSed}" -i \ "${cmdSed}" -i \
-r 's@[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}@{ip}@g' \ -r 's@[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}@{ip}@g' \
"${curFile}" "${curFile}"
@ -112,44 +118,44 @@ for scenario in $(echo ${scenarios}); do
-r 's@\{ip\}\s+@{ip} @g' \ -r 's@\{ip\}\s+@{ip} @g' \
"${curFile}" "${curFile}"
fi fi
if [ "$(cat "${curFile}" |grep 'ACCPTST:STDIO_REPLACE_UUIDS' |wc -l)" -gt 0 ]; then if grep -q 'ACCPTST:STDIO_REPLACE_UUIDS' "${curFile}"; then
"${cmdSed}" -i \ "${cmdSed}" -i \
-r 's@[0-9a-f\-]{32,40}@{uuid}@g' \ -r 's@[0-9a-f\-]{32,40}@{uuid}@g' \
"${curFile}" "${curFile}"
fi fi
if [ "$(cat "${curFile}" |grep 'ACCPTST:STDIO_REPLACE_BIGINTS' |wc -l)" -gt 0 ]; then if grep -q 'ACCPTST:STDIO_REPLACE_BIGINTS' "${curFile}"; then
# Such as: 3811298194 # Such as: 3811298194
"${cmdSed}" -i \ "${cmdSed}" -i \
-r 's@[0-9]{7,64}@{bigint}@g' \ -r 's@[0-9]{7,64}@{bigint}@g' \
"${curFile}" "${curFile}"
fi fi
if [ "$(cat "${curFile}" |grep 'ACCPTST:STDIO_REPLACE_DATETIMES' |wc -l)" -gt 0 ]; then if grep -q 'ACCPTST:STDIO_REPLACE_DATETIMES' "${curFile}"; then
# Such as: 2016-02-10 15:38:44.420094 # Such as: 2016-02-10 15:38:44.420094
"${cmdSed}" -i \ "${cmdSed}" -i \
-r 's@[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}@{datetime}@g' \ -r 's@[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}@{datetime}@g' \
"${curFile}" "${curFile}"
fi fi
if [ "$(cat "${curFile}" |grep 'ACCPTST:STDIO_REPLACE_LONGTIMES' |wc -l)" -gt 0 ]; then if grep -q 'ACCPTST:STDIO_REPLACE_LONGTIMES' "${curFile}"; then
# Such as: 2016-02-10 15:38:44.420094 # Such as: 2016-02-10 15:38:44.420094
"${cmdSed}" -i \ "${cmdSed}" -i \
-r 's@[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{6}@{longtime}@g' \ -r 's@[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{6}@{longtime}@g' \
"${curFile}" "${curFile}"
fi fi
if [ "$(cat "${curFile}" |grep 'ACCPTST:STDIO_REPLACE_DURATIONS' |wc -l)" -gt 0 ]; then if grep -q 'ACCPTST:STDIO_REPLACE_DURATIONS' "${curFile}"; then
# Such as: 0:00:00.001991 # Such as: 0:00:00.001991
"${cmdSed}" -i \ "${cmdSed}" -i \
-r 's@[0-9]{1,2}:[0-9]{2}:[0-9]{2}.[0-9]{6}@{duration}@g' \ -r 's@[0-9]{1,2}:[0-9]{2}:[0-9]{2}.[0-9]{6}@{duration}@g' \
"${curFile}" "${curFile}"
fi fi
if [ "$(cat "${curFile}" |grep 'ACCPTST:STDIO_REPLACE_REMOTE_EXEC' |wc -l)" -gt 0 ]; then if grep -q 'ACCPTST:STDIO_REPLACE_REMOTE_EXEC' "${curFile}"; then
egrep -v 'remote-exec\): [ a-zA-Z]' "${curFile}" > "${__sysTmpDir}/accptst-filtered.txt" egrep -v 'remote-exec\): [ a-zA-Z]' "${curFile}" > "${__sysTmpDir}/accptst-filtered.txt"
mv "${__sysTmpDir}/accptst-filtered.txt" "${curFile}" mv "${__sysTmpDir}/accptst-filtered.txt" "${curFile}"
fi fi
done done
# Save these as new fixtures? # Save these as new fixtures?
if [ "${SAVE_FIXTURES:-}" = "true" ]; then if [[ "${SAVE_FIXTURES:-}" = "true" ]]; then
for typ in $(echo stdio exitcode); do for typ in stdio exitcode; do
curFile="${__accptstTmpDir}/${scenario}.${typ}" curFile="${__accptstTmpDir}/${scenario}.${typ}"
cp -f \ cp -f \
"${curFile}" \ "${curFile}" \
@ -158,13 +164,13 @@ for scenario in $(echo ${scenarios}); do
fi fi
# Compare # Compare
for typ in $(echo stdio exitcode); do for typ in stdio exitcode; do
curFile="${__accptstTmpDir}/${scenario}.${typ}" curFile="${__accptstTmpDir}/${scenario}.${typ}"
echo -n " comparing ${typ}.. " echo -n " comparing ${typ}.. "
if [ "${typ}" = "stdio" ]; then if [[ "${typ}" = "stdio" ]]; then
if [ "$(cat "${curFile}" |grep 'ACCPTST:STDIO_SKIP_COMPARE' |wc -l)" -gt 0 ]; then if grep -q 'ACCPTST:STDIO_SKIP_COMPARE' "${curFile}"; then
echo "skip" echo "skip"
continue continue
fi fi
@ -183,4 +189,41 @@ for scenario in $(echo ${scenarios}); do
done done
popd > /dev/null popd > /dev/null
done done <<< "$(find "${__dir}/scenario" -type f -iname 'run.sh')"
[[ "${1:-}" ]] && exit 0
# finally do some shellcheck linting
if [[ "$(command -v shellcheck)" ]]; then
echo "==> Shellcheck"
pushd "${__root}" > /dev/null
failed="false"
while IFS=$'\n' read -r file; do
lint="false"
[[ "${file}" = "./main.sh" ]] && lint="true"
[[ "${file}" = "./example.sh" ]] && lint="true"
[[ "${file}" = "./test/acceptance.sh" ]] && lint="true"
[[ "${lint}" != "true" ]] && continue
echo -n " ${file}.. "
if ! shellcheck --shell=bash --external-sources "${file}" >> "${__accptstTmpDir}/shellcheck.err"; then
echo "✗"
failed="true"
continue
fi
echo "✓"
done <<< "$(find . -type f -iname '*.sh')"
popd > /dev/null
if [[ "${failed}" = "true" ]]; then
cat "${__accptstTmpDir}/shellcheck.err"
exit 1
fi
fi
exit 0