Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create unit tests for BATS helper functions #5134

Merged
merged 2 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bats/scripts/bats-lint.pl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
# - $assert_success
# - $ {assert}_success
# - if [ $status -eq 0 ]
if (/(\$\{?)?(assert|refute|output\b|status\b)/) {
if (/(\$\{?)? (assert | refute | \b output \b | \b status \b)/x) {
IsaSih marked this conversation as resolved.
Show resolved Hide resolved
undef $run;
}
# Doesn't match on:
Expand Down
2 changes: 1 addition & 1 deletion bats/tests/helpers/load.bash
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ call_local_function() {
local func
func="local_$(calling_function)"
if [ "$(type -t "$func")" = "function" ]; then
eval "$func"
"$func"
fi
}

Expand Down
40 changes: 20 additions & 20 deletions bats/tests/helpers/utils.bash
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,15 @@ is_true() {
# case-insensitive check; false values: '', '0', 'no', and 'false'
local value
value=$(to_lower "$1")
if [[ $value =~ ^(0|no|false)?$ ]]; then
false
else
true
fi
[[ ! $value =~ ^(0|no|false)?$ ]]
}

is_false() {
! is_true "$1"
}

bool() {
if eval "$1"; then
if "$@"; then
echo "true"
else
echo "false"
Expand All @@ -35,7 +31,7 @@ validate_enum() {
local var=$1
shift
for value in "$@"; do
if [ "${!var}" = "$value" ]; then
if [[ ${!var} == "$value" ]]; then
return
fi
done
Expand Down Expand Up @@ -90,7 +86,7 @@ join_map() {
local elem
local result=""
for elem in "$@"; do
elem=$(eval "$map" '"$elem"')
elem=$(eval "$map" '"$elem"') || return
IsaSih marked this conversation as resolved.
Show resolved Hide resolved
if [[ -z $result ]]; then
result=$elem
else
Expand All @@ -101,7 +97,12 @@ join_map() {
}

jq_output() {
jq -r "$@" <<<"${output}"
run jq -r "$@" <<<"${output}"
echo "$output"
if [[ $output == null ]]; then
status=1
fi
return "$status"
}

get_setting() {
Expand Down Expand Up @@ -151,10 +152,10 @@ try() {
shift
done

local count
for ((count = 0; count < max; ++count)); do
local count=0
while true; do
run "$@"
if ((status == 0)); then
if ((status == 0 || ++count >= max)); then
break
fi
sleep "$delay"
Expand All @@ -167,7 +168,7 @@ image_without_tag_as_json_string() {
local image=$1
# If the tag looks like a port number and follows something that looks
# like a domain name, then don't strip the tag (e.g. foo.io:5000).
if [[ ${image##*:} =~ ^[0-9]+$ && ${image%:*} =~ \.[a-z]+$ ]]; then
if [[ ${image##*:} =~ ^[0-9]+(/|$) && ${image%:*} =~ \.[a-z]+$ ]]; then
json_string "$image"
else
json_string "${image%:*}"
Expand Down Expand Up @@ -209,23 +210,22 @@ unique_filename() {
local suffix=""

while true; do
local filename="$basename$suffix$extension"
if [ ! -e "$filename" ]; then
local filename="${basename}${suffix}${extension}"
if [[ ! -e $filename ]]; then
IsaSih marked this conversation as resolved.
Show resolved Hide resolved
echo "$filename"
return
fi
index=$((index + 1))
suffix="_$index"
suffix="_$((++index))"
done
}

capture_logs() {
if capturing_logs && [ -d "$PATH_LOGS" ]; then
if capturing_logs && [[ -d $PATH_LOGS ]]; then
IsaSih marked this conversation as resolved.
Show resolved Hide resolved
local logdir
logdir=$(unique_filename "${PATH_BATS_LOGS}/${RD_TEST_FILENAME}")
mkdir -p "$logdir"
cp -LR "$PATH_LOGS/" "$logdir"
echo "${BATS_TEST_DESCRIPTION:-teardown}" >"$logdir/test_description"
cp -LR "${PATH_LOGS}/" "$logdir"
echo "${BATS_TEST_DESCRIPTION:-teardown}" >"${logdir}/test_description"
# Capture settings.json
cp "$PATH_CONFIG_FILE" "$logdir"
foreach_profile export_profile "$logdir"
Expand Down
Loading