Skip to content

Commit

Permalink
Fix shellcheck errors (slsa-framework#338)
Browse files Browse the repository at this point in the history
* Fix shellcheck issues in assert.sh

* Fix shellcheck issues
  • Loading branch information
ianlewis committed Jun 17, 2022
1 parent 8958ae8 commit 9e71f01
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 179 deletions.
312 changes: 158 additions & 154 deletions .github/workflows/scripts/assert.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
## Function list based on:
## http://junit.sourceforge.net/javadoc/org/junit/Assert.html
## Log methods : inspired by
## - https://natelandau.com/bash-scripting-utilities/
## - https://natelandau.com/bash-scripting-utilities/
## author: Mark Torok
##
## date: 07. Dec. 2016
Expand All @@ -22,229 +22,233 @@
# Note: from https://github.com/torokmark/assert.sh/blob/main/assert.sh.

if command -v tput &>/dev/null && tty -s; then
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
MAGENTA=$(tput setaf 5)
NORMAL=$(tput sgr0)
BOLD=$(tput bold)
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
MAGENTA=$(tput setaf 5)
NORMAL=$(tput sgr0)
BOLD=$(tput bold)
else
RED=$(echo -en "\e[31m")
GREEN=$(echo -en "\e[32m")
MAGENTA=$(echo -en "\e[35m")
NORMAL=$(echo -en "\e[00m")
BOLD=$(echo -en "\e[01m")
RED=$(echo -en "\e[31m")
GREEN=$(echo -en "\e[32m")
MAGENTA=$(echo -en "\e[35m")
NORMAL=$(echo -en "\e[00m")
BOLD=$(echo -en "\e[01m")
fi

log_header() {
printf "\n${BOLD}${MAGENTA}========== %s ==========${NORMAL}\n" "$@" >&2
printf "\n${BOLD}${MAGENTA}========== %s ==========${NORMAL}\n" "$@" >&2
}

log_success() {
printf "${GREEN}✔ %s${NORMAL}\n" "$@" >&2
printf "${GREEN}✔ %s${NORMAL}\n" "$@" >&2
}

log_failure() {
printf "${RED}✖ %s${NORMAL}\n" "$@" >&2
printf "${RED}✖ %s${NORMAL}\n" "$@" >&2
}


assert_eq() {
local expected="$1"
local actual="$2"
local msg="${3-}"

if [ "$expected" == "$actual" ]; then
return 0
else
[ "${#msg}" -gt 0 ] && log_failure "$expected == $actual :: $msg" || true
return 1
fi
local expected="$1"
local actual="$2"
local msg="${3-}"

if [ "$expected" == "$actual" ]; then
return 0
else
([ "${#msg}" -gt 0 ] && log_failure "$expected == $actual :: $msg") || true
return 1
fi
}

assert_not_eq() {
local expected="$1"
local actual="$2"
local msg="${3-}"

if [ ! "$expected" == "$actual" ]; then
return 0
else
[ "${#msg}" -gt 0 ] && log_failure "$expected != $actual :: $msg" || true
return 1
fi
local expected="$1"
local actual="$2"
local msg="${3-}"

if [ ! "$expected" == "$actual" ]; then
return 0
else
([ "${#msg}" -gt 0 ] && log_failure "$expected != $actual :: $msg") || true
return 1
fi
}

assert_true() {
local actual="$1"
local msg="${2-}"
local actual="$1"
local msg="${2-}"

assert_eq true "$actual" "$msg"
return "$?"
assert_eq true "$actual" "$msg"
return "$?"
}

assert_false() {
local actual="$1"
local msg="${2-}"
local actual="$1"
local msg="${2-}"

assert_eq false "$actual" "$msg"
return "$?"
assert_eq false "$actual" "$msg"
return "$?"
}

assert_array_eq() {
# There is a bug in a shellcheck type check that bleeds across function
# scope so these variables need to have a different name.
# See: https://github.com/koalaman/shellcheck/issues/1225
declare -a expecteda=("${!1-}")
declare -a actuala=("${!2}")

declare -a expected=("${!1-}")
# echo "AAE ${expected[@]}"

declare -a actual=("${!2}")
# echo "AAE ${actual[@]}"
local msg="${3-}"

local msg="${3-}"
local return_code=0
if [ ! "${#expecteda[@]}" == "${#actuala[@]}" ]; then
return_code=1
fi

local return_code=0
if [ ! "${#expected[@]}" == "${#actual[@]}" ]; then
return_code=1
fi
local i
for ((i = 1; i < ${#expecteda[@]} + 1; i += 1)); do
if [ ! "${expecteda[$i - 1]}" == "${actuala[$i - 1]}" ]; then
return_code=1
break
fi
done

local i
for (( i=1; i < ${#expected[@]} + 1; i+=1 )); do
if [ ! "${expected[$i-1]}" == "${actual[$i-1]}" ]; then
return_code=1
break
if [ "$return_code" == 1 ]; then
([ "${#msg}" -gt 0 ] && log_failure "(${expecteda[*]}) != (${actuala[*]}) :: $msg") || true
fi
done

if [ "$return_code" == 1 ]; then
[ "${#msg}" -gt 0 ] && log_failure "(${expected[*]}) != (${actual[*]}) :: $msg" || true
fi

return "$return_code"
return "$return_code"
}

assert_array_not_eq() {
# There is a bug in a shellcheck type check that bleeds across function
# scope so these variables need to have a different name.
# See: https://github.com/koalaman/shellcheck/issues/1225
declare -a expecteda=("${!1-}")
declare -a actuala=("${!2}")

declare -a expected=("${!1-}")
declare -a actual=("${!2}")
local msg="${3-}"

local msg="${3-}"
local return_code=1
if [ ! "${#expecteda[@]}" == "${#actuala[@]}" ]; then
return_code=0
fi

local return_code=1
if [ ! "${#expected[@]}" == "${#actual[@]}" ]; then
return_code=0
fi
local i
for ((i = 1; i < ${#expecteda[@]} + 1; i += 1)); do
if [ ! "${expecteda[$i - 1]}" == "${actuala[$i - 1]}" ]; then
return_code=0
break
fi
done

local i
for (( i=1; i < ${#expected[@]} + 1; i+=1 )); do
if [ ! "${expected[$i-1]}" == "${actual[$i-1]}" ]; then
return_code=0
break
if [ "$return_code" == 1 ]; then
([ "${#msg}" -gt 0 ] && log_failure "(${expecteda[*]}) == (${actuala[*]}) :: $msg") || true
fi
done

if [ "$return_code" == 1 ]; then
[ "${#msg}" -gt 0 ] && log_failure "(${expected[*]}) == (${actual[*]}) :: $msg" || true
fi

return "$return_code"
return "$return_code"
}

assert_empty() {
local actual=$1
local msg="${2-}"
local actual=$1
local msg="${2-}"

assert_eq "" "$actual" "$msg"
return "$?"
assert_eq "" "$actual" "$msg"
return "$?"
}

assert_not_empty() {
local actual=$1
local msg="${2-}"
local actual=$1
local msg="${2-}"

assert_not_eq "" "$actual" "$msg"
return "$?"
assert_not_eq "" "$actual" "$msg"
return "$?"
}

assert_contain() {
local haystack="$1"
local needle="${2-}"
local msg="${3-}"
local haystack="$1"
local needle="${2-}"
local msg="${3-}"

if [ -z "${needle:+x}" ]; then
return 0;
fi
if [ -z "${needle:+x}" ]; then
return 0
fi

if [ -z "${haystack##*$needle*}" ]; then
return 0
else
[ "${#msg}" -gt 0 ] && log_failure "$haystack doesn't contain $needle :: $msg" || true
return 1
fi
# needle is used as a search pattern.
# shellcheck disable=SC2295
if [ -z "${haystack##*$needle*}" ]; then
return 0
else
([ "${#msg}" -gt 0 ] && log_failure "$haystack doesn't contain $needle :: $msg") || true
return 1
fi
}

assert_not_contain() {
local haystack="$1"
local needle="${2-}"
local msg="${3-}"
local haystack="$1"
local needle="${2-}"
local msg="${3-}"

if [ -z "${needle:+x}" ]; then
return 0;
fi
if [ -z "${needle:+x}" ]; then
return 0
fi

if [ "${haystack##*$needle*}" ]; then
return 0
else
[ "${#msg}" -gt 0 ] && log_failure "$haystack contains $needle :: $msg" || true
return 1
fi
# needle is used as a search pattern.
# shellcheck disable=SC2295
if [ "${haystack##*$needle*}" ]; then
return 0
else
([ "${#msg}" -gt 0 ] && log_failure "$haystack contains $needle :: $msg") || true
return 1
fi
}

assert_gt() {
local first="$1"
local second="$2"
local msg="${3-}"

if [[ "$first" -gt "$second" ]]; then
return 0
else
[ "${#msg}" -gt 0 ] && log_failure "$first > $second :: $msg" || true
return 1
fi
local first="$1"
local second="$2"
local msg="${3-}"

if [[ "$first" -gt "$second" ]]; then
return 0
else
([ "${#msg}" -gt 0 ] && log_failure "$first > $second :: $msg") || true
return 1
fi
}

assert_ge() {
local first="$1"
local second="$2"
local msg="${3-}"

if [[ "$first" -ge "$second" ]]; then
return 0
else
[ "${#msg}" -gt 0 ] && log_failure "$first >= $second :: $msg" || true
return 1
fi
local first="$1"
local second="$2"
local msg="${3-}"

if [[ "$first" -ge "$second" ]]; then
return 0
else
([ "${#msg}" -gt 0 ] && log_failure "$first >= $second :: $msg") || true
return 1
fi
}

assert_lt() {
local first="$1"
local second="$2"
local msg="${3-}"

if [[ "$first" -lt "$second" ]]; then
return 0
else
[ "${#msg}" -gt 0 ] && log_failure "$first < $second :: $msg" || true
return 1
fi
local first="$1"
local second="$2"
local msg="${3-}"

if [[ "$first" -lt "$second" ]]; then
return 0
else
([ "${#msg}" -gt 0 ] && log_failure "$first < $second :: $msg") || true
return 1
fi
}

assert_le() {
local first="$1"
local second="$2"
local msg="${3-}"

if [[ "$first" -le "$second" ]]; then
return 0
else
[ "${#msg}" -gt 0 ] && log_failure "$first <= $second :: $msg" || true
return 1
fi
local first="$1"
local second="$2"
local msg="${3-}"

if [[ "$first" -le "$second" ]]; then
return 0
else
([ "${#msg}" -gt 0 ] && log_failure "$first <= $second :: $msg") || true
return 1
fi
}
Loading

0 comments on commit 9e71f01

Please sign in to comment.