From d879ae1fe0ef65c5e483bd10221dc2bd64bcae2f Mon Sep 17 00:00:00 2001 From: Manuel Streuhofer Date: Fri, 10 Feb 2017 20:08:51 +0100 Subject: [PATCH 1/6] fixup example.sh like 2.3.0 main.sh __i_am_main_script logic fixed for example.sh added double-sourced test --- example.sh | 29 +++++++++++++++++++++++------ main.sh | 16 +++++++++++----- test/fixture/double-source.exitcode | 1 + test/fixture/double-source.stdio | 23 +++++++++++++++++++++++ test/fixture/main-debug.stdio | 1 + test/fixture/main-nocolor.stdio | 1 + test/scenario/double-source/run.sh | 15 +++++++++++++++ test/scenario/main-debug/run.sh | 2 +- test/scenario/main-nocolor/run.sh | 2 +- 9 files changed, 77 insertions(+), 13 deletions(-) create mode 100644 test/fixture/double-source.exitcode create mode 100644 test/fixture/double-source.stdio create mode 100644 test/scenario/double-source/run.sh diff --git a/example.sh b/example.sh index 907c7b6..c1c69e5 100755 --- a/example.sh +++ b/example.sh @@ -7,7 +7,7 @@ # # LOG_LEVEL=7 ./example.sh -f /tmp/x -d (change this for your script) # -# Based on a template by BASH3 Boilerplate v2.2.0 +# Based on a template by BASH3 Boilerplate v2.3.0 # http://bash3boilerplate.sh/#authors # # The MIT License (MIT) @@ -47,6 +47,25 @@ EOF source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/main.sh" +### Signal trapping and backtracing +############################################################################## + +function __b3bp_cleanup_before_exit () { + info "Cleaning up. Done" +} +trap __b3bp_cleanup_before_exit EXIT + +# requires `set -o errtrace` +__b3bp_err_report() { + local error_code + error_code=${?} + error "Error in ${__file} in function ${1} on line ${2}" + exit ${error_code} +} +# Uncomment the following line for always providing an error backtrace +# trap '__b3bp_err_report "${FUNCNAME:-.}" ${LINENO}' ERR + + ### Command-line argument switches (like -d for debugmode, -h for showing helppage) ############################################################################## @@ -54,6 +73,8 @@ source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/main.sh" if [[ "${arg_d:?}" = "1" ]]; then set -o xtrace LOG_LEVEL="7" + # Enable error backtracing + trap '__b3bp_err_report "${FUNCNAME:-.}" ${LINENO}' ERR fi # verbose mode @@ -83,11 +104,7 @@ fi ### Runtime ############################################################################## -function cleanup_before_exit () { - info "Cleaning up. Done" -} -trap cleanup_before_exit EXIT - +info "__i_am_main_script: ${__i_am_main_script}" info "__file: ${__file}" info "__dir: ${__dir}" info "__base: ${__base}" diff --git a/main.sh b/main.sh index 7bab26a..6aa8bcb 100755 --- a/main.sh +++ b/main.sh @@ -27,11 +27,18 @@ set -o pipefail # set -o xtrace if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then + __i_am_main_script="0" # false + if [[ "${__usage+x}" ]]; then + if [[ "${BASH_SOURCE[1]}" = "${0}" ]]; then + __i_am_main_script="1" # true + fi + __b3bp_external_usage="true" __b3bp_tmp_source_idx=1 fi else + __i_am_main_script="1" # true [[ "${__usage+x}" ]] && unset -v __usage [[ "${__helptext+x}" ]] && unset -v __helptext fi @@ -40,11 +47,7 @@ fi __dir="$(cd "$(dirname "${BASH_SOURCE[${__b3bp_tmp_source_idx:-0}]}")" && pwd)" __file="${__dir}/$(basename "${BASH_SOURCE[${__b3bp_tmp_source_idx:-0}]}")" __base="$(basename "${__file}" .sh)" -if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then - __i_am_main_script="0" # false -else - __i_am_main_script="1" # true -fi + # Define the environment variables (and their defaults) that this script depends on LOG_LEVEL="${LOG_LEVEL:-6}" # 7 = debug -> 0 = emergency @@ -308,6 +311,7 @@ if [[ "${__b3bp_external_usage:-}" = "true" ]]; then return fi + ### Signal trapping and backtracing ############################################################################## @@ -326,6 +330,7 @@ __b3bp_err_report() { # Uncomment the following line for always providing an error backtrace # trap '__b3bp_err_report "${FUNCNAME:-.}" ${LINENO}' ERR + ### Command-line argument switches (like -d for debugmode, -h for showing helppage) ############################################################################## @@ -364,6 +369,7 @@ fi ### Runtime ############################################################################## +info "__i_am_main_script: ${__i_am_main_script}" info "__file: ${__file}" info "__dir: ${__dir}" info "__base: ${__base}" diff --git a/test/fixture/double-source.exitcode b/test/fixture/double-source.exitcode new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/test/fixture/double-source.exitcode @@ -0,0 +1 @@ +1 diff --git a/test/fixture/double-source.stdio b/test/fixture/double-source.stdio new file mode 100644 index 0000000..e136b9e --- /dev/null +++ b/test/fixture/double-source.stdio @@ -0,0 +1,23 @@ +ACCPTST:STDIO_REPLACE_DATETIMES +{datetime} UTC [ debug] cli arg arg_f = () -> {tmpdir}/x +{datetime} UTC [ info] __i_am_main_script: 0 +{datetime} UTC [ info] __file: {root}/example.sh +{datetime} UTC [ info] __dir: {root} +{datetime} UTC [ info] __base: example +{datetime} UTC [ info] OSTYPE: {OSTYPE} +{datetime} UTC [ info] arg_f: {tmpdir}/x +{datetime} UTC [ info] arg_d: 0 +{datetime} UTC [ info] arg_v: 0 +{datetime} UTC [ info] arg_h: 0 +{datetime} UTC [ info] multiple lines example - line #1 +{datetime} UTC [ info] multiple lines example - line #2 +{datetime} UTC [ info] imagine logging the output of 'ls -al /path/' +{datetime} UTC [ debug] Info useful to developers for debugging the application, not useful during operations. +{datetime} UTC [ info] Normal operational messages - may be harvested for reporting, measuring throughput, etc. - no action required. +{datetime} UTC [ notice] Events that are unusual but not error conditions - might be summarized in an email to developers or admins to spot potential problems - no immediate action required. +{datetime} UTC [ warning] Warning messages, not an error, but indication that an error will occur if action is not taken, e.g. file system 85% full - each item must be resolved within a given time. This is a debug message +{datetime} UTC [ error] Non-urgent failures, these should be relayed to developers or admins; each item must be resolved within a given time. +{datetime} UTC [ critical] Should be corrected immediately, but indicates failure in a primary system, an example is a loss of a backup ISP connection. +{datetime} UTC [ alert] Should be corrected immediately, therefore notify staff who can fix the problem. An example would be the loss of a primary ISP connection. +{datetime} UTC [emergency] A "panic" condition usually affecting multiple apps/servers/sites. At this level it would usually notify all tech staff on call. +{datetime} UTC [ info] Cleaning up. Done diff --git a/test/fixture/main-debug.stdio b/test/fixture/main-debug.stdio index b473bf5..ec65b64 100644 --- a/test/fixture/main-debug.stdio +++ b/test/fixture/main-debug.stdio @@ -1,5 +1,6 @@ ACCPTST:STDIO_REPLACE_DATETIMES {datetime} UTC [ debug] cli arg arg_f = () -> {tmpdir}/x +{datetime} UTC [ info] __i_am_main_script: 1 {datetime} UTC [ info] __file: {root}/main.sh {datetime} UTC [ info] __dir: {root} {datetime} UTC [ info] __base: main diff --git a/test/fixture/main-nocolor.stdio b/test/fixture/main-nocolor.stdio index e13a404..2cb3a6b 100644 --- a/test/fixture/main-nocolor.stdio +++ b/test/fixture/main-nocolor.stdio @@ -1,5 +1,6 @@ ACCPTST:STDIO_REPLACE_DATETIMES {datetime} UTC [ debug] cli arg arg_f = () -> {tmpdir}/x +{datetime} UTC [ info] __i_am_main_script: 1 {datetime} UTC [ info] __file: {root}/main.sh {datetime} UTC [ info] __dir: {root} {datetime} UTC [ info] __base: main diff --git a/test/scenario/double-source/run.sh b/test/scenario/double-source/run.sh new file mode 100644 index 0000000..29d5ccb --- /dev/null +++ b/test/scenario/double-source/run.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +set -o pipefail +set -o errexit +set -o nounset +# set -o xtrace + +__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +__root="$(cd "$(dirname "$(dirname "$(dirname "${__dir}")")")" && pwd)" + +export LOG_LEVEL=7 + +echo "ACCPTST:STDIO_REPLACE_DATETIMES" + +# shellcheck source=example.sh +source "${__root}/example.sh" -f /tmp/x diff --git a/test/scenario/main-debug/run.sh b/test/scenario/main-debug/run.sh index eda5bce..a7407e4 100644 --- a/test/scenario/main-debug/run.sh +++ b/test/scenario/main-debug/run.sh @@ -9,4 +9,4 @@ __root="$(cd "$(dirname "$(dirname "$(dirname "${__dir}")")")" && pwd)" echo "ACCPTST:STDIO_REPLACE_DATETIMES" -env LOG_LEVEL=8 bash "${__root}/main.sh" -f /tmp/x +env LOG_LEVEL=7 bash "${__root}/main.sh" -f /tmp/x diff --git a/test/scenario/main-nocolor/run.sh b/test/scenario/main-nocolor/run.sh index 8abf1cd..a7971bf 100644 --- a/test/scenario/main-nocolor/run.sh +++ b/test/scenario/main-nocolor/run.sh @@ -9,4 +9,4 @@ __root="$(cd "$(dirname "$(dirname "$(dirname "${__dir}")")")" && pwd)" echo "ACCPTST:STDIO_REPLACE_DATETIMES" -env LOG_LEVEL=8 NO_COLOR=true bash "${__root}/main.sh" -f /tmp/x +env LOG_LEVEL=7 NO_COLOR=true bash "${__root}/main.sh" -f /tmp/x From 7eea26c7b4803f36d220c4d0daf461101cf9e9e6 Mon Sep 17 00:00:00 2001 From: Izaak Beekman Date: Fri, 10 Feb 2017 14:53:11 -0500 Subject: [PATCH 2/6] Attempt fix for OS X CI build --- .travis.yml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index 40a7ad7..3d45600 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,14 +18,14 @@ addons: - 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 -- if [ "$TRAVIS_OS_NAME" = "osx" ]; then which gsed || brew install gnu-sed; fi -- if [ "$TRAVIS_OS_NAME" = "osx" ]; then which gtimeout || brew install coreutils; - fi -- bash --version -- awk --version + - if [ "$TRAVIS_OS_NAME" = "osx" ]; then brew update; fi + - if [ "$TRAVIS_OS_NAME" = "osx" ]; then which gsed || brew install gnu-sed; fi + - if [ "$TRAVIS_OS_NAME" = "osx" ]; then which gtimeout || brew install coreutils; fi + - if [ "$TRAVIS_OS_NAME" = "osx" ]; then which bundle || gem install bundler; fi + - bash --version + - awk --version +install: npm install # <-- yarn still messes up nested bins: https://github.com/yarnpkg/yarn/issues/760 before_cache: - rm -f ./node_modules/.bin/which # <-- workaround for https://github.com/travis-ci/travis-ci/issues/5092 cache: From 0d1f488365ef4622e41af7aeea01c2bf0c503442 Mon Sep 17 00:00:00 2001 From: Izaak Beekman Date: Fri, 10 Feb 2017 12:16:05 -0500 Subject: [PATCH 3/6] Fix shellchk and show usage for new magic var Update acceptance tests --- main.sh | 1 + test/fixture/main-debug.stdio | 1 + test/fixture/main-nocolor.stdio | 1 + 3 files changed, 3 insertions(+) diff --git a/main.sh b/main.sh index 6aa8bcb..5e675d3 100755 --- a/main.sh +++ b/main.sh @@ -374,6 +374,7 @@ info "__file: ${__file}" info "__dir: ${__dir}" info "__base: ${__base}" info "OSTYPE: ${OSTYPE}" +info "Script called directly? $([[ "${__i_am_main_script}" = "1" ]] && echo "yes" || echo "no")" info "arg_f: ${arg_f}" info "arg_d: ${arg_d}" diff --git a/test/fixture/main-debug.stdio b/test/fixture/main-debug.stdio index ec65b64..7f40ee2 100644 --- a/test/fixture/main-debug.stdio +++ b/test/fixture/main-debug.stdio @@ -5,6 +5,7 @@ ACCPTST:STDIO_REPLACE_DATETIMES {datetime} UTC [ info] __dir: {root} {datetime} UTC [ info] __base: main {datetime} UTC [ info] OSTYPE: {OSTYPE} +{datetime} UTC [ info] Script called directly? yes {datetime} UTC [ info] arg_f: {tmpdir}/x {datetime} UTC [ info] arg_d: 0 {datetime} UTC [ info] arg_v: 0 diff --git a/test/fixture/main-nocolor.stdio b/test/fixture/main-nocolor.stdio index 2cb3a6b..bbc459c 100644 --- a/test/fixture/main-nocolor.stdio +++ b/test/fixture/main-nocolor.stdio @@ -5,6 +5,7 @@ ACCPTST:STDIO_REPLACE_DATETIMES {datetime} UTC [ info] __dir: {root} {datetime} UTC [ info] __base: main {datetime} UTC [ info] OSTYPE: {OSTYPE} +{datetime} UTC [ info] Script called directly? yes {datetime} UTC [ info] arg_f: {tmpdir}/x {datetime} UTC [ info] arg_d: 0 {datetime} UTC [ info] arg_v: 0 From 986191210abf55978a14c493961e791b58d58247 Mon Sep 17 00:00:00 2001 From: Izaak Beekman Date: Fri, 10 Feb 2017 13:59:57 -0500 Subject: [PATCH 4/6] Update FAQ website instructions `npm run start` didn't work for me... I had to get yarn and do `yarn install` first. I'm guessing this will be true for others. --- FAQ.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/FAQ.md b/FAQ.md index de6aff6..b91801f 100644 --- a/FAQ.md +++ b/FAQ.md @@ -96,9 +96,10 @@ Somewhat inconsistent – but true to Unix ancestry – the abbreviation for our ## How can I locally develop and preview the b3bp website? -You should have a working Node.js >=10 and Ruby >=2 install on your workstation. When that is the case, you can run: +You should have a working Node.js >=10, Ruby >=2 and [YARN](https://yarnpkg.com) install on your workstation. When that is the case, you can run: ```bash +yarn install npm run start ``` From 2d5506cb36f00dc9a7792490ac2ab5dd8957dee9 Mon Sep 17 00:00:00 2001 From: Izaak Beekman Date: Fri, 10 Feb 2017 14:33:29 -0500 Subject: [PATCH 5/6] Perform syntax checking with all available bashes Add tests to help catch bash4-isms and ensure syntax is checked with all bashes on PATH in a given system. --- test/acceptance.sh | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/test/acceptance.sh b/test/acceptance.sh index 33565d8..c4d31bf 100755 --- a/test/acceptance.sh +++ b/test/acceptance.sh @@ -193,7 +193,41 @@ done <<< "$(find "${__dir}/scenario" -type f -iname 'run.sh')" [[ "${1:-}" ]] && exit 0 -# finally do some shellcheck linting +# Ensure correct syntax with all available bashes + +if bashes=($(which -a bash 2> /dev/null )); then + for bash in "${bashes[@]}"; do + # shellcheck disable=SC2016 + echo "==> ${bash} -n $(${bash} -c 'echo "(${BASH_VERSION})"')" + pushd "${__root}" > /dev/null + + failed="false" + + while IFS=$'\n' read -r file; do + [[ "${file}" =~ ^\./node_modules/ ]] && continue + [[ "${file}" =~ ^\./website/\.lanyon/ ]] && continue + + echo -n " ${file}.. " + + if ! "${bash}" -n "${file}" 2>> "${__accptstTmpDir}/${bash//\//.}.err"; then + echo "✗" + failed="true" + continue + fi + + echo "✓" + done <<< "$(find . -type f -iname '*.sh')" + + popd > /dev/null + + if [[ "${failed}" = "true" ]]; then + cat "${__accptstTmpDir}/${bash//\//.}.err" + exit 1 + fi + done +fi + +# do some shellcheck linting if [[ "$(command -v shellcheck)" ]]; then echo "==> Shellcheck" pushd "${__root}" > /dev/null From cb29b2f9843644a31be097f6ff47f9eb93aa0667 Mon Sep 17 00:00:00 2001 From: Izaak Beekman Date: Fri, 10 Feb 2017 19:00:00 -0500 Subject: [PATCH 6/6] Restore @mstreuhofer's changes that I clobbered --- main.sh | 1 - test/fixture/main-debug.stdio | 1 - test/fixture/main-nocolor.stdio | 1 - 3 files changed, 3 deletions(-) diff --git a/main.sh b/main.sh index 5e675d3..6aa8bcb 100755 --- a/main.sh +++ b/main.sh @@ -374,7 +374,6 @@ info "__file: ${__file}" info "__dir: ${__dir}" info "__base: ${__base}" info "OSTYPE: ${OSTYPE}" -info "Script called directly? $([[ "${__i_am_main_script}" = "1" ]] && echo "yes" || echo "no")" info "arg_f: ${arg_f}" info "arg_d: ${arg_d}" diff --git a/test/fixture/main-debug.stdio b/test/fixture/main-debug.stdio index 7f40ee2..ec65b64 100644 --- a/test/fixture/main-debug.stdio +++ b/test/fixture/main-debug.stdio @@ -5,7 +5,6 @@ ACCPTST:STDIO_REPLACE_DATETIMES {datetime} UTC [ info] __dir: {root} {datetime} UTC [ info] __base: main {datetime} UTC [ info] OSTYPE: {OSTYPE} -{datetime} UTC [ info] Script called directly? yes {datetime} UTC [ info] arg_f: {tmpdir}/x {datetime} UTC [ info] arg_d: 0 {datetime} UTC [ info] arg_v: 0 diff --git a/test/fixture/main-nocolor.stdio b/test/fixture/main-nocolor.stdio index bbc459c..2cb3a6b 100644 --- a/test/fixture/main-nocolor.stdio +++ b/test/fixture/main-nocolor.stdio @@ -5,7 +5,6 @@ ACCPTST:STDIO_REPLACE_DATETIMES {datetime} UTC [ info] __dir: {root} {datetime} UTC [ info] __base: main {datetime} UTC [ info] OSTYPE: {OSTYPE} -{datetime} UTC [ info] Script called directly? yes {datetime} UTC [ info] arg_f: {tmpdir}/x {datetime} UTC [ info] arg_d: 0 {datetime} UTC [ info] arg_v: 0