From 118391d8ea542be5ad530eec2f40efa751925375 Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Fri, 9 Aug 2019 09:54:55 +1000 Subject: [PATCH] Fixed coding standards in shell scripts and enabled shellcheck exit code in CI. --- .travis.yml | 2 +- libexec/bats-core/bats | 2 +- libexec/bats-core/bats-exec-suite | 2 ++ libexec/bats-core/bats-exec-test | 13 ++++++++++++- libexec/bats-core/bats-format-tap-stream | 12 +++++++----- libexec/bats-core/bats-preprocess | 2 +- 6 files changed, 24 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index 110879aa..df6a62e7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -30,7 +30,7 @@ script: - | if [[ "${TRAVIS_OS_NAME:-}" == 'linux' ]]; then # @todo: Remove "|| true" once all coding standards issues are fixed. - ./shellcheck.sh || true + ./shellcheck.sh fi if [[ "${TRAVIS_OS_NAME:-}" == 'linux' && -n "${BASHVER}" ]]; then diff --git a/libexec/bats-core/bats b/libexec/bats-core/bats index 8bc0aa01..337f2f17 100755 --- a/libexec/bats-core/bats +++ b/libexec/bats-core/bats @@ -60,7 +60,7 @@ expand_path() { printf -v "$result" '%s/%s' "$dirname" "${path##*/}" } -BATS_LIBEXEC="$(dirname "$(expand_link "$BASH_SOURCE")")" +BATS_LIBEXEC="$(dirname "$(expand_link "${BASH_SOURCE[0]}")")" export BATS_CWD="$PWD" export BATS_TEST_PATTERN="^[[:blank:]]*@test[[:blank:]]+(.*[^[:blank:]])[[:blank:]]+\{(.*)\$" export BATS_TEST_FILTER= diff --git a/libexec/bats-core/bats-exec-suite b/libexec/bats-core/bats-exec-suite index d76fdf8c..39f815df 100755 --- a/libexec/bats-core/bats-exec-suite +++ b/libexec/bats-core/bats-exec-suite @@ -23,6 +23,7 @@ while [[ "$#" -ne 0 ]]; do num_jobs="$1" ;; -x) + # shellcheck disable=SC2034 extended_syntax_flag='-x' flags+=('-x') ;; @@ -34,6 +35,7 @@ while [[ "$#" -ne 0 ]]; do done if ( type -p parallel &>/dev/null ); then + # shellcheck disable=SC2034 have_gnu_parallel=1 elif [[ "$num_jobs" != 1 ]]; then printf 'bats: cannot execute "%s" jobs without GNU parallel\n' "$num_jobs" >&2 diff --git a/libexec/bats-core/bats-exec-test b/libexec/bats-core/bats-exec-test index 3bbd9758..2d18f18a 100755 --- a/libexec/bats-core/bats-exec-test +++ b/libexec/bats-core/bats-exec-test @@ -1,6 +1,7 @@ #!/usr/bin/env bash set -eET +# Variables used in other scripts. BATS_COUNT_ONLY='' BATS_TEST_FILTER='' BATS_EXTENDED_SYNTAX='' @@ -8,10 +9,12 @@ BATS_EXTENDED_SYNTAX='' while [[ "$#" -ne 0 ]]; do case "$1" in -c) + # shellcheck disable=SC2034 BATS_COUNT_ONLY=1 ;; -f) shift + # shellcheck disable=SC2034 BATS_TEST_FILTER="$1" ;; -x) @@ -52,6 +55,8 @@ load() { exit 1 fi + # Dynamically loaded user files provided outside of Bats. + # shellcheck disable=SC1090 source "${filename}" } @@ -59,8 +64,12 @@ run() { local origFlags="$-" set +eET local origIFS="$IFS" + # 'output', 'status', 'lines' are global variables available to tests. + # shellcheck disable=SC2034 output="$("$@" 2>&1)" + # shellcheck disable=SC2034 status="$?" + # shellcheck disable=SC2034,SC2206 IFS=$'\n' lines=($output) IFS="$origIFS" set "-$origFlags" @@ -211,7 +220,7 @@ bats_trim_filename() { } bats_debug_trap() { - if [[ "$BASH_SOURCE" != "$1" ]]; then + if [[ "${BASH_SOURCE[0]}" != "$1" ]]; then # The last entry in the stack trace is not useful when en error occured: # It is either duplicated (kinda correct) or has wrong line number (Bash < 4.4) # Therefore we capture the stacktrace but use it only after the next debug @@ -365,6 +374,8 @@ bats_evaluate_preprocessed_source() { if [[ -z "$BATS_TEST_SOURCE" ]]; then BATS_TEST_SOURCE="${BATS_PARENT_TMPNAME}.src" fi + # Dynamically loaded user files provided outside of Bats. + # shellcheck disable=SC1090 source "$BATS_TEST_SOURCE" } diff --git a/libexec/bats-core/bats-format-tap-stream b/libexec/bats-core/bats-format-tap-stream index 11ce72ba..c78d860d 100755 --- a/libexec/bats-core/bats-format-tap-stream +++ b/libexec/bats-core/bats-format-tap-stream @@ -20,7 +20,7 @@ fi update_screen_width() { screen_width="$(tput cols)" - count_column_left=$(( $screen_width - $count_column_width )) + count_column_left=$(( screen_width - count_column_width )) } trap update_screen_width WINCH @@ -28,7 +28,7 @@ update_screen_width begin() { go_to_column 0 - buffer_with_truncation $(( $count_column_left - 1 )) ' %s' "$name" + buffer_with_truncation $(( count_column_left - 1 )) ' %s' "$name" clear_to_end_of_line go_to_column $count_column_left buffer "%${#count}s/${count}" "$index" @@ -92,10 +92,11 @@ buffer_with_truncation() { shift local string + # shellcheck disable=SC2059 printf -v 'string' -- "$@" if [[ "${#string}" -gt "$width" ]]; then - buffer '%s...' "${string:0:$(( $width - 4 ))}" + buffer '%s...' "${string:0:$(( width - 4 ))}" else buffer '%s' "$string" fi @@ -103,7 +104,7 @@ buffer_with_truncation() { go_to_column() { local column="$1" - buffer '\x1B[%dG' $(( $column + 1 )) + buffer '\x1B[%dG' $(( column + 1 )) } clear_to_end_of_line() { @@ -123,7 +124,7 @@ set_color() { if [[ "$2" == 'bold' ]]; then weight=1 fi - buffer '\x1B[%d;%dm' "$(( 30 + $color ))" "$weight" + buffer '\x1B[%d;%dm' "$(( 30 + color ))" "$weight" } clear_color() { @@ -134,6 +135,7 @@ _buffer= buffer() { local content + # shellcheck disable=SC2059 printf -v content -- "$@" _buffer+="$content" } diff --git a/libexec/bats-core/bats-preprocess b/libexec/bats-core/bats-preprocess index 8e31705c..4cdd879a 100755 --- a/libexec/bats-core/bats-preprocess +++ b/libexec/bats-core/bats-preprocess @@ -42,7 +42,7 @@ tests=() name="${name%[\'\"]}" body="${BASH_REMATCH[2]}" bats_encode_test_name "$name" 'encoded_name' - printf '%s() { bats_test_begin "%s"; %s\n' "$encoded_name" "$name" "$body" || : + printf '%s() { bats_test_begin "%s"; %s\n' "${encoded_name:?}" "$name" "$body" || : if [[ -z "$BATS_TEST_FILTER" || "$name" =~ $BATS_TEST_FILTER ]]; then tests+=("$encoded_name")