Merge pull request #51 from bats-core/feature/add_shellcheck
Add shellcheck
diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml
index 7c3edeb..bbf450d 100644
--- a/.github/workflows/tests.yml
+++ b/.github/workflows/tests.yml
@@ -10,6 +10,11 @@
branches: [ master ]
jobs:
+ shellcheck:
+ runs-on: ubuntu-latest
+ steps:
+ - uses: actions/checkout@v2
+ - run: shellcheck ./src/*
build:
strategy:
matrix:
diff --git a/src/file.bash b/src/file.bash
index 021ffe2..6bea4da 100644
--- a/src/file.bash
+++ b/src/file.bash
@@ -258,7 +258,7 @@
assert_files_equal() {
local -r file1="$1"
local -r file2="$2"
- if ! `cmp -s "$file1" "$file2"` ; then
+ if ! cmp -s "$file1" "$file2" >/dev/null ; then
local -r rem="${BATSLIB_FILE_PATH_REM-}"
local -r add="${BATSLIB_FILE_PATH_ADD-}"
batslib_print_kv_single 4 'path' "${file1/$rem/$add}" 'path' "${file2/$rem/$add}" \
@@ -274,14 +274,11 @@
_bats_get_file_owner() {
local -r output_var=$1
local -r file=$2
- case "$OSTYPE" in
- "darwin"*)
+ if [[ "$OSTYPE" == darwin* ]]; then
local -ra cmd_params=(-f %Su)
- ;;
- "linux-gnu"*)
+ else
local -ra cmd_params=(-c %U)
- ;;
- esac
+ fi
printf -v "$output_var" "%s" "$(stat "${cmd_params[@]}" "$file")"
}
@@ -332,24 +329,20 @@
assert_file_permission() {
local -r permission="$1"
local -r file="$2"
- if [[ `uname` == "Darwin" ]]; then
- if [ `stat -f '%A' "$file"` -ne "$permission" ]; then
- local -r rem="${BATSLIB_FILE_PATH_REM-}"
- local -r add="${BATSLIB_FILE_PATH_ADD-}"
- batslib_print_kv_single 4 'path' "${file/$rem/$add}" \
- | batslib_decorate "file does not have permissions $permission" \
- | fail
- fi
- elif [[ `uname` == "Linux" ]]; then
- if [ `stat -c "%a" "$file"` -ne "$permission" ]; then
- local -r rem="${BATSLIB_FILE_PATH_REM-}"
- local -r add="${BATSLIB_FILE_PATH_ADD-}"
- batslib_print_kv_single 4 'path' "${file/$rem/$add}" \
- | batslib_decorate "file does not have permissions $permission" \
- | fail
+
+ if [[ "$OSTYPE" == darwin* ]]; then
+ local -r actual_permission=$(stat -f '%A' "$file")
+ else
+ local -r actual_permission=$(stat -c "%a" "$file")
fi
-fi
+ if [[ "$actual_permission" != "$permission" ]]; then
+ local -r rem="${BATSLIB_FILE_PATH_REM-}"
+ local -r add="${BATSLIB_FILE_PATH_ADD-}"
+ batslib_print_kv_single 4 'path' "${file/$rem/$add}" \
+ | batslib_decorate "file does not have permissions $permission" \
+ | fail
+ fi
}
# Fail if file is not zero byte. This
@@ -367,25 +360,14 @@
# STDERR - details, on failure
assert_size_zero() {
local -r file="$1"
- if [[ `uname` == "Darwin" ]]; then
- mkfile 2k ${TEST_FIXTURE_ROOT}/dir/notzerobyte
- if [ -s "$file" ]; then
+
+ if [ -s "$file" ]; then
local -r rem="${BATSLIB_FILE_PATH_REM-}"
local -r add="${BATSLIB_FILE_PATH_ADD-}"
batslib_print_kv_single 4 'path' "${file/$rem/$add}" \
| batslib_decorate 'file is greater than 0 byte' \
| fail
fi
- elif [[ `uname` == "Linux" ]]; then
- fallocate -l 2k ${TEST_FIXTURE_ROOT}/dir/notzerobyte
- if [ -s "$file" ]; then
- local -r rem="${BATSLIB_FILE_PATH_REM-}"
- local -r add="${BATSLIB_FILE_PATH_ADD-}"
- batslib_print_kv_single 4 'path' "${file/$rem/$add}" \
- | batslib_decorate 'file is greater than 0 byte' \
- | fail
- fi
-fi
}
# Fail if group if is not set on file. This
@@ -460,6 +442,24 @@
fi
}
+_bats_file_readlinkf_macos() {
+ local TARGET_FILE=$1
+ cd "$(dirname "$TARGET_FILE")" || return
+ TARGET_FILE=$(basename "$TARGET_FILE")
+ # Iterate down a (possible) chain of symlinks
+ while [ -L "$TARGET_FILE" ]
+ do
+ TARGET_FILE=$(readlink "$TARGET_FILE")
+ cd "$(dirname "$TARGET_FILE")" || return
+ TARGET_FILE=$(basename "$TARGET_FILE")
+ done
+ # Compute the canonicalized name by finding the physical path
+ # for the directory we're in and appending the target file.
+ local -r PHYS_DIR=$(pwd -P)
+
+ printf "%s/%s\n" "$PHYS_DIR" "$TARGET_FILE"
+}
+
# Fail and display path of the file (or directory) if it is not a symlink to given destination.
# function is the logical complement of `assert_not_symlink_to`
# $1 - source
@@ -472,58 +472,29 @@
assert_symlink_to() {
local -r sourcefile="$1"
local -r link="$2"
- # If OS is linux
- if [[ `uname` == "Linux" ]]; then
- if [ ! -L $link ]; then
- local -r rem="${BATSLIB_FILE_PATH_REM-}"
- local -r add="${BATSLIB_FILE_PATH_ADD-}"
- batslib_print_kv_single 4 'path' "${link/$rem/$add}" \
- | batslib_decorate 'file is not a symbolic link' \
- | fail
- fi
- local -r realsource=$( readlink -f "$link" )
- if [ ! "$realsource" = "$sourcefile" ]; then
- local -r rem="${BATSLIB_FILE_PATH_REM-}"
- local -r add="${BATSLIB_FILE_PATH_ADD-}"
- batslib_print_kv_single 4 'path' "${link/$rem/$add}" \
- | batslib_decorate 'symbolic link does not have the correct target' \
- | fail
- fi
- # If OS is OSX
- elif [[ `uname` == "Darwin" ]]; then
- function readlinkf() {
- TARGET_FILE=$1
- cd `dirname $TARGET_FILE`
- TARGET_FILE=`basename $TARGET_FILE`
- # Iterate down a (possible) chain of symlinks
- while [ -L "$TARGET_FILE" ]
- do
- TARGET_FILE=`readlink $TARGET_FILE`
- cd `dirname $TARGET_FILE`
- TARGET_FILE=`basename $TARGET_FILE`
- done
- # Compute the canonicalized name by finding the physical path
- # for the directory we're in and appending the target file.
- PHYS_DIR=`pwd -P`
- RESULT=$PHYS_DIR/$TARGET_FILE
- echo $RESULT
- }
- if [ ! -L $link ]; then
+ # If OS is OSX, emulate readlink -f
+ if [[ $OSTYPE == "darwin"* ]]; then
+ local -ra readlink_command=(_bats_file_readlinkf_macos)
+ else
+ local -ra readlink_command=(readlink -f)
+ fi
+
+ if [ ! -L "$link" ]; then
local -r rem="${BATSLIB_FILE_PATH_REM-}"
local -r add="${BATSLIB_FILE_PATH_ADD-}"
batslib_print_kv_single 4 'path' "${link/$rem/$add}" \
| batslib_decorate 'file is not a symbolic link' \
| fail
- fi
- local -r realsource=$( readlinkf "$link" )
- if [ ! "$realsource" = "$sourcefile" ]; then
- local -r rem="${BATSLIB_FILE_PATH_REM-}"
- local -r add="${BATSLIB_FILE_PATH_ADD-}"
- batslib_print_kv_single 4 'path' "${link/$rem/$add}" \
+ fi
+
+ local -r realsource=$( "${readlink_command[@]}" "$link" )
+ if [ ! "$realsource" = "$sourcefile" ]; then
+ local -r rem="${BATSLIB_FILE_PATH_REM-}"
+ local -r add="${BATSLIB_FILE_PATH_ADD-}"
+ batslib_print_kv_single 4 'path' "${link/$rem/$add}" \
| batslib_decorate 'symbolic link does not have the correct target' \
| fail
- fi
fi
}
# Fail and display path of the file (or directory) if it does not match a size.
@@ -597,7 +568,7 @@
{ local -ir width=8
batslib_print_kv_single "$width" 'path' "${file/$rem/$add}"
batslib_print_kv_single_or_multi "$width" \
- 'output' "$(cat $file)"
+ 'output' "$(cat "$file")"
} | batslib_decorate 'file is not empty' \
| fail
fi
@@ -864,24 +835,20 @@
assert_not_file_permission() {
local -r permission="$1"
local -r file="$2"
- if [[ `uname` == "Darwin" ]]; then
- if [ `stat -f '%A' "$file"` -eq "$permission" ]; then
- local -r rem="${BATSLIB_FILE_PATH_REM-}"
- local -r add="${BATSLIB_FILE_PATH_ADD-}"
- batslib_print_kv_single 4 'path' "${file/$rem/$add}" \
- | batslib_decorate "file has permissions $permission, but it was expected not to have" \
- | fail
- fi
- elif [[ `uname` == "Linux" ]]; then
- if [ `stat -c "%a" "$file"` -eq "$permission" ]; then
- local -r rem="${BATSLIB_FILE_PATH_REM-}"
- local -r add="${BATSLIB_FILE_PATH_ADD-}"
- batslib_print_kv_single 4 'path' "${file/$rem/$add}" \
- | batslib_decorate "file has permissions $permission, but it was expected not to have" \
- | fail
- fi
+
+ if [[ "$OSTYPE" == darwin* ]]; then
+ local -r actual_permission=$(stat -f '%A' "$file")
+ else
+ local -r actual_permission=$(stat -c "%a" "$file")
fi
+ if [ "$actual_permission" -eq "$permission" ]; then
+ local -r rem="${BATSLIB_FILE_PATH_REM-}"
+ local -r add="${BATSLIB_FILE_PATH_ADD-}"
+ batslib_print_kv_single 4 'path' "${file/$rem/$add}" \
+ | batslib_decorate "file has permissions $permission, but it was expected not to have" \
+ | fail
+ fi
}
# This function is the logical complement of `assert_files_equal'.
@@ -895,7 +862,7 @@
assert_files_not_equal() {
local -r file1="$1"
local -r file2="$2"
- if `cmp -s "$file1" "$file2"` ; then
+ if cmp -s "$file1" "$file2" >/dev/null ; then
local -r rem="${BATSLIB_FILE_PATH_REM-}"
local -r add="${BATSLIB_FILE_PATH_ADD-}"
batslib_print_kv_single 4 'path' "${file1/$rem/$add}" 'path' "${file2/$rem/$add}" \
@@ -919,8 +886,6 @@
# STDERR - details, on failure
assert_size_not_zero() {
local -r file="$1"
- if [[ `uname` == "Darwin" ]]; then
- mkfile 2k ${TEST_FIXTURE_ROOT}/dir/notzerobyte
if [[ ! -s "$file" ]]; then
local -r rem="${BATSLIB_FILE_PATH_REM-}"
local -r add="${BATSLIB_FILE_PATH_ADD-}"
@@ -928,16 +893,6 @@
| batslib_decorate 'file is 0 byte, but it was expected not to be' \
| fail
fi
- elif [[ `uname` == "Linux" ]]; then
- fallocate -l 2k ${TEST_FIXTURE_ROOT}/dir/notzerobyte
- if [[ ! -s "$file" ]]; then
- local -r rem="${BATSLIB_FILE_PATH_REM-}"
- local -r add="${BATSLIB_FILE_PATH_ADD-}"
- batslib_print_kv_single 4 'path' "${file/$rem/$add}" \
- | batslib_decorate 'file is 0 byte, but it was expected not to be' \
- | fail
- fi
-fi
}
@@ -1026,54 +981,27 @@
assert_not_symlink_to() {
local -r sourcefile="$1"
local -r link="$2"
- # If OS is linux
- if [[ `uname` == "Linux" ]]; then
- if [ -L $link ]; then
- local -r rem="${BATSLIB_FILE_PATH_REM-}"
- local -r add="${BATSLIB_FILE_PATH_ADD-}"
- batslib_print_kv_single 4 'path' "${link/$rem/$add}" \
- | batslib_decorate 'file is a symbolic link' \
- | fail
- fi
- local -r realsource=$( readlink -f "$link" )
- if [ "$realsource" = "$sourcefile" ]; then
- batslib_print_kv_single 4 'path' "${link/$rem/$add}" \
- | batslib_decorate 'symbolic link does have the correct target' \
- | fail
- fi
- # If OS is OSX
- elif [[ `uname` == "Darwin" ]]; then
- function readlinkf() {
- TARGET_FILE=$1
- cd `dirname $TARGET_FILE`
- TARGET_FILE=`basename $TARGET_FILE`
- # Iterate down a (possible) chain of symlinks
- while [ -L "$TARGET_FILE" ]
- do
- TARGET_FILE=`readlink $TARGET_FILE`
- cd `dirname $TARGET_FILE`
- TARGET_FILE=`basename $TARGET_FILE`
- done
- # Compute the canonicalized name by finding the physical path
- # for the directory we're in and appending the target file.
- PHYS_DIR=`pwd -P`
- RESULT=$PHYS_DIR/$TARGET_FILE
- echo $RESULT
- }
-
- if [ -L $link ]; then
+
+
+ if [[ $OSTYPE == darwin* ]]; then
+ local -ra readlink_command=(_bats_file_readlinkf_macos)
+ else
+ local -ra readlink_command=(readlink -f)
+ fi
+
+ if [ -L "$link" ]; then
local -r rem="${BATSLIB_FILE_PATH_REM-}"
local -r add="${BATSLIB_FILE_PATH_ADD-}"
batslib_print_kv_single 4 'path' "${link/$rem/$add}" \
| batslib_decorate 'file is a symbolic link' \
| fail
- fi
- local -r realsource=$( readlinkf "$link" )
- if [ "$realsource" = "$sourcefile" ]; then
- batslib_print_kv_single 4 'path' "${link/$rem/$add}" \
- | batslib_decorate 'symbolic link does have the correct target' \
- | fail
- fi
+ fi
+
+ local -r realsource=$( "${readlink_command[@]}" "$link" )
+ if [ "$realsource" = "$sourcefile" ]; then
+ batslib_print_kv_single 4 'path' "${link/$rem/$add}" \
+ | batslib_decorate 'symbolic link does have the correct target' \
+ | fail
fi
}
# Fail and display path of the file (or directory) if it is empty. This
diff --git a/src/temp.bash b/src/temp.bash
index db0daaa..f7a9af0 100755
--- a/src/temp.bash
+++ b/src/temp.bash
@@ -101,8 +101,7 @@
template+='-XXXXXXXXXX'
local path
- path="$(mktemp -d -- "${BATS_TMPDIR}/${template}" 2>&1)"
- if (( $? )); then
+ if ! path="$(mktemp -d -- "${BATS_TMPDIR}/${template}" 2>&1)"; then
echo "$path" \
| batslib_decorate 'ERROR: temp_make' \
| fail
@@ -172,8 +171,7 @@
# Delete directory.
local result
- result="$(rm -r -- "$path" 2>&1 </dev/null)"
- if (( $? )); then
+ if ! result="$(rm -r -- "$path" 2>&1 >/dev/null )"; then
echo "$result" \
| batslib_decorate 'ERROR: temp_del' \
| fail
diff --git a/test/61-assert-10-size_zero.bats b/test/61-assert-10-size_zero.bats
index e2047f0..5a11e6d 100644
--- a/test/61-assert-10-size_zero.bats
+++ b/test/61-assert-10-size_zero.bats
@@ -4,7 +4,11 @@
fixtures 'exist'
setup () {
- touch ${TEST_FIXTURE_ROOT}/dir/zerobyte
+ readonly ZERO_FILE=${TEST_FIXTURE_ROOT}/dir/zerobyte
+ touch "$ZERO_FILE"
+
+ readonly NOTZERO_FILE="${TEST_FIXTURE_ROOT}/dir/notzerobyte"
+ echo "not empty" > "$NOTZERO_FILE"
}
teardown () {
@@ -14,19 +18,17 @@
# Correctness
@test 'assert_size_zero() <file>: returns 0 if <file> file is 0 byte' {
- local -r file="${TEST_FIXTURE_ROOT}/dir/zerobyte"
- run assert_size_zero "$file"
+ run assert_size_zero "$ZERO_FILE"
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 0 ]
}
@test 'assert_size_zero() <file>: returns 1 and displays path if <file> file is greater than 0 byte' {
- local -r file="${TEST_FIXTURE_ROOT}/dir/notzerobyte"
- run assert_size_zero "$file"
+ run assert_size_zero "$NOTZERO_FILE"
[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 3 ]
[ "${lines[0]}" == '-- file is greater than 0 byte --' ]
- [ "${lines[1]}" == "path : $file" ]
+ [ "${lines[1]}" == "path : $NOTZERO_FILE" ]
[ "${lines[2]}" == '--' ]
}
@@ -36,7 +38,7 @@
@test 'assert_size_zero() <file>: replace prefix of displayed path' {
local -r BATSLIB_FILE_PATH_REM="#${TEST_FIXTURE_ROOT}"
local -r BATSLIB_FILE_PATH_ADD='..'
- run assert_size_zero "${TEST_FIXTURE_ROOT}/dir/notzerobyte"
+ run assert_size_zero "$NOTZERO_FILE"
[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 3 ]
[ "${lines[0]}" == '-- file is greater than 0 byte --' ]
@@ -47,7 +49,7 @@
@test 'assert_size_zero() <file>: replace suffix of displayed path' {
local -r BATSLIB_FILE_PATH_REM='%dir/notzerobyte'
local -r BATSLIB_FILE_PATH_ADD='..'
- run assert_size_zero "${TEST_FIXTURE_ROOT}/dir/notzerobyte"
+ run assert_size_zero "$NOTZERO_FILE"
[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 3 ]
[ "${lines[0]}" == '-- file is greater than 0 byte --' ]
@@ -58,7 +60,7 @@
@test 'assert_size_zero() <file>: replace infix of displayed path' {
local -r BATSLIB_FILE_PATH_REM='dir/notzerobyte'
local -r BATSLIB_FILE_PATH_ADD='..'
- run assert_size_zero "${TEST_FIXTURE_ROOT}/dir/notzerobyte"
+ run assert_size_zero "$NOTZERO_FILE"
[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 3 ]
[ "${lines[0]}" == '-- file is greater than 0 byte --' ]
diff --git a/test/61-assert-11-size_not_zero.bats b/test/61-assert-11-size_not_zero.bats
index bd80a61..70be0af 100644
--- a/test/61-assert-11-size_not_zero.bats
+++ b/test/61-assert-11-size_not_zero.bats
@@ -4,29 +4,30 @@
fixtures 'exist'
setup () {
- touch ${TEST_FIXTURE_ROOT}/dir/zerobyte
+ readonly ZERO_FILE="${TEST_FIXTURE_ROOT}/dir/zerobyte"
+ touch "$ZERO_FILE"
+ readonly NOTZERO_FILE="${TEST_FIXTURE_ROOT}/dir/notzerobyte"
+ echo "not empty" > "$NOTZERO_FILE"
}
teardown () {
- rm -f ${TEST_FIXTURE_ROOT}/dir/zerobyte ${TEST_FIXTURE_ROOT}/dir/notzerobyte
+ rm -f "$ZERO_FILE" "$NOTZERO_FILE"
}
# Correctness
@test 'assert_size_not_zero() <file>: returns 0 if <file> file is greater than 0 byte' {
- local -r file="${TEST_FIXTURE_ROOT}/dir/notzerobyte"
- run assert_size_not_zero "$file"
+ run assert_size_not_zero "$NOTZERO_FILE"
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 0 ]
}
@test 'assert_size_not_zero() <file>: returns 1 and displays path if <file> file is 0 byte, but it was expected not to be' {
- local -r file="${TEST_FIXTURE_ROOT}/dir/zerobyte"
- run assert_size_not_zero "$file"
+ run assert_size_not_zero "$ZERO_FILE"
[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 3 ]
[ "${lines[0]}" == '-- file is 0 byte, but it was expected not to be --' ]
- [ "${lines[1]}" == "path : $file" ]
+ [ "${lines[1]}" == "path : $ZERO_FILE" ]
[ "${lines[2]}" == '--' ]
}
@@ -35,7 +36,7 @@
@test 'assert_size_not_zero() <file>: replace prefix of displayed path' {
local -r BATSLIB_FILE_PATH_REM="#${TEST_FIXTURE_ROOT}"
local -r BATSLIB_FILE_PATH_ADD='..'
- run assert_size_not_zero "${TEST_FIXTURE_ROOT}/dir/zerobyte"
+ run assert_size_not_zero "$ZERO_FILE"
[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 3 ]
[ "${lines[0]}" == '-- file is 0 byte, but it was expected not to be --' ]
@@ -46,7 +47,7 @@
@test 'assert_size_not_zero() <file>: replace suffix of displayed path' {
local -r BATSLIB_FILE_PATH_REM='%dir/zerobyte'
local -r BATSLIB_FILE_PATH_ADD='..'
- run assert_size_not_zero "${TEST_FIXTURE_ROOT}/dir/zerobyte"
+ run assert_size_not_zero "$ZERO_FILE"
[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 3 ]
[ "${lines[0]}" == '-- file is 0 byte, but it was expected not to be --' ]
@@ -57,7 +58,7 @@
@test 'assert_size_not_zero() <file>: replace infix of displayed path' {
local -r BATSLIB_FILE_PATH_REM='dir/zerobyte'
local -r BATSLIB_FILE_PATH_ADD='..'
- run assert_size_not_zero "${TEST_FIXTURE_ROOT}/dir/zerobyte"
+ run assert_size_not_zero "$ZERO_FILE"
[ "$status" -eq 1 ]
[ "${#lines[@]}" -eq 3 ]
[ "${lines[0]}" == '-- file is 0 byte, but it was expected not to be --' ]