Skip to content

Commit

Permalink
Add support for test library function prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
eth-p committed Apr 6, 2020
1 parent f66c4e0 commit 025ed31
Show file tree
Hide file tree
Showing 12 changed files with 166 additions and 36 deletions.
1 change: 1 addition & 0 deletions bin/best.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export TEST_ENV_TMPDIR="${TEST_ENV_TMPDIR:-${TMPDIR}}"
export TEST_ENV_HOME="${TEST_ENV_HOME:-${HOME}}"
export TEST_ENV_TERM="${TEST_ENV_TERM:-xterm-color}"

export TEST_LIB_PREFIX="${TEST_LIB_PREFIX}"
export TEST_LIB_DIR="${TEST_LIB_DIR:-${LIB}/test}"
export TEST_SHIM_PATH="${TEST_SHIM_PATH:+${TEST_SHIM_PATH}:}${ROOT}/share/shim"
export TEST_DIR="${TEST_DIR:-${PWD}/test}"
Expand Down
4 changes: 2 additions & 2 deletions lib/manager.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ suite_tests_parse() {
END { print "TESTS="(n+1) };
$2 == "()" { p=0 };
$2 == "()" && /^test([A-Z:_])/ { p=1; n++; print "TESTS_NAME["n"]=\""$1"\"" };
/^( |\t)(description|snapshot).*/ {
/^( |\t)('"${TEST_LIB_PREFIX}"')(description|snapshot).*/ {
if (p) {
var=toupper($1);
var=substr(toupper($1), 1+'"${#TEST_LIB_PREFIX}"');
$1="";
val=$0;
Expand Down
1 change: 1 addition & 0 deletions lib/runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ runner() {
cd "${TEST_PWD}" && env -i \
"${env_passthrough[@]}" \
TEST_LIB_DIR="$TEST_LIB_DIR" \
TEST_LIB_PREFIX="$TEST_LIB_PREFIX" \
TEST_SHIM_PATH="$TEST_SHIM_PATH" \
BEST_VERSION="$BEST_VERSION" \
BEST_RUNNER_QUIET="${BEST_RUNNER_QUIET:-false}" \
Expand Down
4 changes: 2 additions & 2 deletions lib/test/shim.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Issues: https://github.com/eth-p/best/issues
# ----------------------------------------------------------------------------------------------------------------------

use_shim() {
:PREFIX:use_shim() {
local dir
while read -r -d ':' dir; do
if [[ -f "${dir}/$1.sh" ]]; then
Expand All @@ -15,7 +15,7 @@ use_shim() {
done <<< "$TEST_SHIM_PATH:"

if [[ "$__BEST_INSIDE_TEST" = true ]]; then
fail "Could not find shim: %s" "$1"
:PREFIX:fail "Could not find shim: %s" "$1"
else
__best_ipc_send_crash "Could not find shim: $1"
exit 1
Expand Down
2 changes: 1 addition & 1 deletion lib/test/snapshot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
#
# snapshot stdout
#
snapshot() {
:PREFIX:snapshot() {
__best_ipc_send "SNAPSHOT" "$(tr '[:lower:]' '[:upper:]' <<< "$1")"
}
26 changes: 13 additions & 13 deletions lib/test/test_assert.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# assert [ "true" = false ]
# assert ! false
#
assert() {
:PREFIX:assert() {
if [[ "$1" = "!" ]]; then
if ! "$@" &>/dev/null; then
return 0
Expand All @@ -42,8 +42,8 @@ assert() {
#
# assert_equal 2 2
#
assert_equal() {
assert [ "$1" = "$2" ]
:PREFIX:assert_equal() {
:PREFIX:assert [ "$1" = "$2" ]
return $?
}

Expand All @@ -57,8 +57,8 @@ assert_equal() {
#
# assert_not_equal 1 2
#
assert_not_equal() {
assert [ "$1" != "$2" ]
:PREFIX:assert_not_equal() {
:PREFIX:assert [ "$1" != "$2" ]
return $?
}

Expand All @@ -72,8 +72,8 @@ assert_not_equal() {
#
# assert_less 1 2
#
assert_less() {
assert [ "$1" -lt "$2" ]
:PREFIX:assert_less() {
:PREFIX:assert [ "$1" -lt "$2" ]
return $?
}

Expand All @@ -87,8 +87,8 @@ assert_less() {
#
# assert_less_or_equal 2 2
#
assert_less_or_equal() {
assert [ "$1" -le "$2" ]
:PREFIX:assert_less_or_equal() {
:PREFIX:assert [ "$1" -le "$2" ]
return $?
}

Expand All @@ -102,8 +102,8 @@ assert_less_or_equal() {
#
# assert_greater 5 2
#
assert_greater() {
assert [ "$1" -gt "$2" ]
:PREFIX:assert_greater() {
:PREFIX:assert [ "$1" -gt "$2" ]
return $?
}

Expand All @@ -117,7 +117,7 @@ assert_greater() {
#
# assert_greater_or_equal 2 2
#
assert_greater_or_equal() {
assert [ "$1" -ge "$2" ]
:PREFIX:assert_greater_or_equal() {
:PREFIX:assert [ "$1" -ge "$2" ]
return $?
}
26 changes: 13 additions & 13 deletions lib/test/test_expect.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# expect [ "true" = false ]
# assert ! false
#
expect() {
:PREFIX:expect() {
if [[ "$1" = "!" ]]; then
if ! "$@" &>/dev/null; then
return 0
Expand All @@ -41,8 +41,8 @@ expect() {
#
# expect_equal 2 2
#
expect_equal() {
expect [ "$1" = "$2" ]
:PREFIX:expect_equal() {
:PREFIX:expect [ "$1" = "$2" ]
return $?
}

Expand All @@ -56,8 +56,8 @@ expect_equal() {
#
# expect_not_equal 1 2
#
expect_not_equal() {
expect [ "$1" != "$2" ]
:PREFIX:expect_not_equal() {
:PREFIX:expect [ "$1" != "$2" ]
return $?
}

Expand All @@ -71,8 +71,8 @@ expect_not_equal() {
#
# expect_less 1 2
#
expect_less() {
expect [ "$1" -lt "$2" ]
:PREFIX:expect_less() {
:PREFIX:expect [ "$1" -lt "$2" ]
return $?
}

Expand All @@ -86,8 +86,8 @@ expect_less() {
#
# expect_less_or_equal 2 2
#
expect_less_or_equal() {
expect [ "$1" -le "$2" ]
:PREFIX:expect_less_or_equal() {
:PREFIX:expect [ "$1" -le "$2" ]
return $?
}

Expand All @@ -101,8 +101,8 @@ expect_less_or_equal() {
#
# expect_greater 5 2
#
expect_greater() {
expect [ "$1" -gt "$2" ]
:PREFIX:expect_greater() {
:PREFIX:expect [ "$1" -gt "$2" ]
return $?
}

Expand All @@ -116,7 +116,7 @@ expect_greater() {
#
# expect_greater_or_equal 2 2
#
expect_greater_or_equal() {
expect [ "$1" -ge "$2" ]
:PREFIX:expect_greater_or_equal() {
:PREFIX:expect [ "$1" -ge "$2" ]
return $?
}
4 changes: 2 additions & 2 deletions lib/test/test_result.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#
# fail "Could not find '%s'" "bash"
#
fail() {
:PREFIX:fail() {
if [[ $# -eq 0 ]]; then
__best_test_abort "$__BEST_RESULT_ENUM_FAIL" "Test called 'fail' function."
else
Expand All @@ -35,7 +35,7 @@ fail() {
#
# skip "Test disabled on %s." "$(uname -s)"
#
skip() {
:PREFIX:skip() {
# shellcheck disable=SC2059
if [[ $# -gt 0 ]]; then
__best_test_abort "$__BEST_RESULT_ENUM_SKIP" "$(printf "$@")"
Expand Down
2 changes: 1 addition & 1 deletion lib/test/util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#
# array_contains "world" in "${MY_ARRAY[@]}"
#
array_contains() {
:PREFIX:array_contains() {
if [[ "$2" != "in" ]]; then
echo "array_contains: expected string 'in' for second argument" 1>&2
return 2
Expand Down
2 changes: 1 addition & 1 deletion lib/test/void.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
# This file contains a list of empty functions that serve no purpose during test execution.
# The functions themselves will not be documented here.

description() { :; }
:PREFIX:description() { :; }
128 changes: 128 additions & 0 deletions lib/test_runner/preprocess.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# ----------------------------------------------------------------------------------------------------------------------
# best | Copyright (C) 2020 eth-p | MIT License
#
# Repository: https://github.com/eth-p/best
# Issues: https://github.com/eth-p/best/issues
# ----------------------------------------------------------------------------------------------------------------------

# Preprocesses a test library.
# This will replace function prefixes.
#
# Input:
# The library script.
#
# Output:
# The processed library script.
#
# Replacements:
# :PREFIX: -> {{$TEST_LIB_PREFIX}}
__best_lib_preprocess() {
sed 's/:PREFIX:/'"${TEST_LIB_PREFIX}"'/'
}

# Sends a TEST_OUTPUT IPC message.
# This represents a file containing one of the test's outputs.
#
# Arguments:
# $1 [string] -- The output type.
# $2 [string] -- The output file.
#
__best_ipc_send_test_output() {
__best_ipc_send "TEST_OUTPUT" "$1 $2"
}

# Sends a TEST_TIMESTAMP IPC message.
# This represents a timestamp that can be used to calculate the test duration.
#
# Arguments:
# $1 [string] -- The timestamp type.
# $2 [string] -- The timestamp.
#
__best_ipc_send_test_timestamp() {
__best_ipc_send "TEST_TIMESTAMP" "$1 $2"
}

# Sends a TEST_COMPLETE IPC message.
# This represents the exit code of the test.
#
# Arguments:
# $1 [number] -- The exit code.
#
__best_ipc_send_test_complete() {
__best_ipc_send "TEST_COMPLETE" "$1"
}

# Sends a RESULT IPC message.
# This represents the final result of the test.
#
# Arguments:
# $1 ["FAIL"|"PASS"|"SKIP"] -- The result type.
#
__best_ipc_send_test_result() {
__best_ipc_send "RESULT" "$1"
}

# Sends a RESULT_MSG IPC message.
# This represents a message that explains why the test result ended up the way it did.
#
# Arguments:
# $1 [string] -- The result message printf pattern.
#
__best_ipc_send_test_result_message() {
__best_ipc_send "RESULT_MSG" "$1"
}

# Sends a RESULT_MSG_DATA IPC message.
# This represents a message that explains why the test result ended up the way it did.
#
# Arguments:
# $1 [string] -- The result printf argument.
#
__best_ipc_send_test_result_message_data() {
__best_ipc_send "RESULT_MSG_DATA" "$1"
}

# Sends a RUNNER_MSG IPC message.
# This represents a generic user message, and should not be interpreted for reports.
#
# Arguments:
# $1 [string] -- The result message.
#
__best_ipc_send_message() {
__best_ipc_send "RUNNER_MSG" "$1"
}

# Sends a RUNNER_CRASH IPC message.
# This represents a fatal crash that indicates the runner failed in some way.
#
# Arguments:
# $1 [string] -- The crash message.
#
__best_ipc_send_crash() {
__best_ipc_send "RUNNER_CRASH" "$1"
}

# Sends a EXECUTING IPC message.
# This represents the previous command sent to the REPL.
#
# Arguments:
# $1 [string] -- The command.
# $2 [string] -- The command data.
#
__best_ipc_send_executing_message() {
__best_ipc_send "EXECUTING" "$1 $2"
}

# Sends a raw best-runner IPC message.
#
# Arguments:
# $1 [string] -- The message name.
# $2 [string] -- The message data.
#
# Example:
#
# __best_ipc_send "TEST_RESULT" "FAIL"
#
__best_ipc_send() {
printf "%s %s\n" "$1" "$2" 1>&3
}
2 changes: 1 addition & 1 deletion libexec/best-runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ __BEST_RUNNER_LIB="${__BEST_ROOT}/lib/test_runner"
{
shopt -s nullglob
for __file in "${__BEST_RUNNER_LIB}"/*.sh; do source "$__file"; done
for __file in "${TEST_LIB_DIR}"/*; do source "$__file"; done
for __file in "${TEST_LIB_DIR}"/*; do source <(__best_lib_preprocess < "$__file"); done
shopt -u nullglob
}
# ----------------------------------------------------------------------------------------------------------------------
Expand Down

0 comments on commit 025ed31

Please sign in to comment.