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: RJ5UpdXms9QkraylZ11OBfmcRrmKnb254Yj0yCDAvZmg+n+3jSTwMgGvPY8Ih8X/R1JeW3VTtFDkJXXPnjjfpNg1M91u4CAEUOMPciCudYcoF6GKb8psnOzneTTX5M7zuJSzknGdpv/foldxiPYxiY5Hn5bfjmikhAEl+QX/R0Y=
- 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
before_install:
- 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.
EOF
# shellcheck source=./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.
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
__file="${__dir}/$(basename "${BASH_SOURCE[0]}")"
__base="$(basename ${__file} .sh)"
__base="$(basename "${__file}" .sh)"
__root="$(cd "$(dirname "${__dir}")" && pwd)"
scenarios="${1:-$(ls ${__dir}/scenario/|egrep -v ^prepare$)}"
__sysTmpDir="${TMPDIR:-/tmp}"
__sysTmpDir="${__sysTmpDir%/}" # <-- remove trailing slash on macosx
__accptstTmpDir="${__sysTmpDir}/accptst"
mkdir -p "${__accptstTmpDir}"
__accptstTmpDir=$(mktemp -d "${__sysTmpDir}/${__base}.XXXXXX")
function cleanup_before_exit () { rm -r "${__accptstTmpDir:?}"; }
trap cleanup_before_exit EXIT
cmdSed=sed
cmdTimeout=timeout
if [[ "${OSTYPE}" == "darwin"* ]]; then
cmdSed=gsed
else
cmdSed=sed
cmdTimeout=gtimeout
fi
if [[ "${OSTYPE}" == "darwin"* ]]; then
cmdTimeout="gtimeout --kill-after=6m 5m"
else
cmdTimeout="timeout --kill-after=6m 5m"
if [[ ! "$(command -v ${cmdSed})" ]]; then
echo "Please install ${cmdSed}"
exit 1
fi
if [[ ! "$(command -v ${cmdTimeout})" ]]; then
echo "Please install ${cmdTimeout}"
exit 1
fi
__node="$(which node)"
__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
# 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
@ -70,18 +70,24 @@ export NO_COLOR="false"
# Running prepare before other scenarios is important on Travis,
# so that stdio can diverge - and we can enforce stricter
# 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}"
pushd "${__dir}/scenario/${scenario}" > /dev/null
# Run scenario
(${cmdTimeout} bash ./run.sh \
(${cmdTimeout} --kill-after=6m 5m bash ./run.sh \
> "${__accptstTmpDir}/${scenario}.stdio" 2>&1; \
echo "${?}" > "${__accptstTmpDir}/${scenario}.exitcode" \
) || true
# Clear out environmental specifics
for typ in $(echo stdio exitcode); do
for typ in stdio exitcode; do
curFile="${__accptstTmpDir}/${scenario}.${typ}"
"${cmdSed}" -i \
-e "s@${__node}@{node}@g" "${curFile}" \
@ -101,7 +107,7 @@ for scenario in $(echo ${scenarios}); do
-e "s@Linux@{os}@g" "${curFile}" \
|| 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 \
-r 's@[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}@{ip}@g' \
"${curFile}"
@ -112,44 +118,44 @@ for scenario in $(echo ${scenarios}); do
-r 's@\{ip\}\s+@{ip} @g' \
"${curFile}"
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 \
-r 's@[0-9a-f\-]{32,40}@{uuid}@g' \
"${curFile}"
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
"${cmdSed}" -i \
-r 's@[0-9]{7,64}@{bigint}@g' \
"${curFile}"
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
"${cmdSed}" -i \
-r 's@[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}@{datetime}@g' \
"${curFile}"
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
"${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' \
"${curFile}"
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
"${cmdSed}" -i \
-r 's@[0-9]{1,2}:[0-9]{2}:[0-9]{2}.[0-9]{6}@{duration}@g' \
"${curFile}"
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"
mv "${__sysTmpDir}/accptst-filtered.txt" "${curFile}"
fi
done
# Save these as new fixtures?
if [ "${SAVE_FIXTURES:-}" = "true" ]; then
for typ in $(echo stdio exitcode); do
if [[ "${SAVE_FIXTURES:-}" = "true" ]]; then
for typ in stdio exitcode; do
curFile="${__accptstTmpDir}/${scenario}.${typ}"
cp -f \
"${curFile}" \
@ -158,13 +164,13 @@ for scenario in $(echo ${scenarios}); do
fi
# Compare
for typ in $(echo stdio exitcode); do
for typ in stdio exitcode; do
curFile="${__accptstTmpDir}/${scenario}.${typ}"
echo -n " comparing ${typ}.. "
if [ "${typ}" = "stdio" ]; then
if [ "$(cat "${curFile}" |grep 'ACCPTST:STDIO_SKIP_COMPARE' |wc -l)" -gt 0 ]; then
if [[ "${typ}" = "stdio" ]]; then
if grep -q 'ACCPTST:STDIO_SKIP_COMPARE' "${curFile}"; then
echo "skip"
continue
fi
@ -183,4 +189,41 @@ for scenario in $(echo ${scenarios}); do
done
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