build/tests/helpers.bash

68 lines
1.6 KiB
Bash
Executable File

#! /usr/bin/env bats
# Helper function, returns successfuly when the two values
# passed as positional arguments are equal ($1 = $2), and
# fails otherwise, printing out the two mismatching values
eq() {
if [ "$1" != "$2" ]; then
echo "\"$1\" != \"$2\"" >&2
return 1
fi
}
# Returns successfully when the two values aren't equal,
# and fails otherwise printing out the two matching values
neq() {
if [ "$1" = "$2" ]; then
echo "\"$1\" = \"$2\"" >&2
return 1
fi
}
# Emulate translation, without actual escape codes (colors),
# variable substitution, or actual translations
warn() {
echo "warning${1}"
}
info() {
if [[ "$LOG" = "info" ]] || [[ "$LOG" = "debug" ]] || [[ "$LOG" = "" ]]; then
echo "info${1}"
fi
}
debug() {
[[ "$LOG" = "debug" ]] && echo "debug${1}"
}
error() {
echo "error${1}"
}
# Helper function which returns the index in an array
# of lines which matches the string given as $1
# If $2 is provided, it is the line where we start looking
# (to avoid previous matches)
# Takes the full input as $CONTEXT env variable
# Exits with 1 when the string was not found
find_line() {
if [ $# -gt 1 ]; then
i=$2
else
i=0
fi
run echo "$CONTEXT"
while [ "${lines[i]}" != "" ]; do
if [ "${lines[i]}" = "$1" ]; then
# We've found the line, break out of the loop
break;
fi
((i++))
done
# If the current line is an empty line, we haven't matched
if [ "${lines[i]}" = "" ]; then exit 1; fi
# Output the matching line number
echo $i
}