Skip to content

Commit

Permalink
Merge pull request sstephenson#242 from AlexSkrypnyk/feature/fix-codi…
Browse files Browse the repository at this point in the history
…ng-standards

Fixed coding standards in shell scripts and enabled shellcheck exit code in CI.
  • Loading branch information
sublimino authored Aug 27, 2019
2 parents 1c83a1b + 118391d commit e582ef5
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion libexec/bats-core/bats
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 2 additions & 0 deletions libexec/bats-core/bats-exec-suite
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ while [[ "$#" -ne 0 ]]; do
num_jobs="$1"
;;
-x)
# shellcheck disable=SC2034
extended_syntax_flag='-x'
flags+=('-x')
;;
Expand All @@ -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
Expand Down
13 changes: 12 additions & 1 deletion libexec/bats-core/bats-exec-test
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
#!/usr/bin/env bash
set -eET

# Variables used in other scripts.
BATS_COUNT_ONLY=''
BATS_TEST_FILTER=''
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)
Expand Down Expand Up @@ -52,15 +55,21 @@ load() {
exit 1
fi

# Dynamically loaded user files provided outside of Bats.
# shellcheck disable=SC1090
source "${filename}"
}

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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
}

Expand Down
12 changes: 7 additions & 5 deletions libexec/bats-core/bats-format-tap-stream
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ 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
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"
Expand Down Expand Up @@ -92,18 +92,19 @@ 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
}

go_to_column() {
local column="$1"
buffer '\x1B[%dG' $(( $column + 1 ))
buffer '\x1B[%dG' $(( column + 1 ))
}

clear_to_end_of_line() {
Expand All @@ -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() {
Expand All @@ -134,6 +135,7 @@ _buffer=

buffer() {
local content
# shellcheck disable=SC2059
printf -v content -- "$@"
_buffer+="$content"
}
Expand Down
2 changes: 1 addition & 1 deletion libexec/bats-core/bats-preprocess
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit e582ef5

Please sign in to comment.