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

Add shlib/lib/assertions.sh and related tests #237

Merged
merged 6 commits into from
Sep 13, 2022
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
7 changes: 7 additions & 0 deletions shlib/lib/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package(default_visibility = ["//visibility:public"])

sh_library(
name = "assertions",
testonly = True,
srcs = ["assertions.sh"],
)
99 changes: 99 additions & 0 deletions shlib/lib/assertions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/usr/bin/env bash

cgrindel marked this conversation as resolved.
Show resolved Hide resolved
# Fail a test with the specified message.
#
# Args:
# err_msg: The message to print to stderr.
#
# Outputs:
# stdout: None.
# stderr: The error message.
fail() {
local err_msg="${1:-}"
if [[ -z "${err_msg:-}" ]]; then
err_msg="Unspecified error occurred."
fi
echo >&2 "${err_msg}"
exit 1
}

make_err_msg() {
local err_msg="${1}"
local prefix="${2:-}"
if [[ -n "${prefix:-}" ]]; then
err_msg="${prefix} ${err_msg}"
fi
echo "${err_msg}"
}


# Asserts that the actual value equals the expected value.
#
# Args:
# expected: The expected value.
# actual: The actual value.
# err_msg: Optional. The error message to print if the assertion fails.
#
# Outputs:
# stdout: None.
# stderr: None.
assert_equal() {
local expected="${1}"
local actual="${2}"
local err_msg
err_msg="$(\
make_err_msg \
"Expected to be equal. expected: ${expected}, actual: ${actual}" "${3:-}" \
)"
if [[ "${expected}" != "${actual}" ]]; then
fail "${err_msg}"
fi
}

# Asserts that the actual value contains the specified regex pattern.
#
# Args:
# pattern: The expected pattern.
# actual: The actual value.
# err_msg: Optional. The error message to print if the assertion fails.
#
# Outputs:
# stdout: None.
# stderr: None.
assert_match() {
local pattern=${1}
local actual="${2}"
local err_msg
err_msg="$(\
make_err_msg \
"Expected to match. pattern: ${pattern}, actual: ${actual}" "${3:-}" \
)"
if [[ ! "${actual}" =~ ${pattern} ]]; then
fail "${err_msg}"
fi
}

# Asserts that the actual value does not contain the specified regex pattern.
#
# Args:
# pattern: The expected pattern.
# actual: The actual value.
# err_msg: Optional. The error message to print if the assertion fails.
#
# Outputs:
# stdout: None.
# stderr: None.
assert_no_match() {
local pattern=${1}
local actual="${2}"
local err_msg
err_msg="$(\
make_err_msg \
"Expected not to match. pattern: ${pattern}, actual: ${actual}" "${3:-}" \
)"
if [[ "${actual}" =~ ${pattern} ]]; then
fail "${err_msg}"
fi
# Because this is a negative test, we need to end on a positive note if all is well.
echo ""
}
35 changes: 35 additions & 0 deletions shlib/tests/lib_tests/assertions_tests/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
sh_library(
name = "assert_fail",
testonly = True,
srcs = ["assert_fail.sh"],
)

sh_test(
name = "assert_equal_test",
srcs = ["assert_equal_test.sh"],
deps = [
":assert_fail",
"//shlib/lib:assertions",
"@bazel_tools//tools/bash/runfiles",
],
)

sh_test(
name = "assert_match_test",
srcs = ["assert_match_test.sh"],
deps = [
":assert_fail",
"//shlib/lib:assertions",
"@bazel_tools//tools/bash/runfiles",
],
)

sh_test(
name = "assert_no_match_test",
srcs = ["assert_no_match_test.sh"],
deps = [
":assert_fail",
"//shlib/lib:assertions",
"@bazel_tools//tools/bash/runfiles",
],
)
43 changes: 43 additions & 0 deletions shlib/tests/lib_tests/assertions_tests/assert_equal_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env bash

# --- begin runfiles.bash initialization v2 ---
# Copy-pasted from the Bazel Bash runfiles library v2.
set -o nounset -o pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash
# shellcheck disable=SC1090
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
source "$0.runfiles/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
# --- end runfiles.bash initialization v2 ---

assertions_sh_location=aspect_bazel_lib/shlib/lib/assertions.sh
assertions_sh="$(rlocation "${assertions_sh_location}")" || \
(echo >&2 "Failed to locate ${assertions_sh_location}" && exit 1)
# shellcheck source=SCRIPTDIR/../../../lib/assertions.sh
source "${assertions_sh}"

assert_fail_sh_location=aspect_bazel_lib/shlib/tests/lib_tests/assertions_tests/assert_fail.sh
assert_fail_sh="$(rlocation "${assert_fail_sh_location}")" || \
(echo >&2 "Failed to locate ${assert_fail_sh_location}" && exit 1)
# shellcheck source=SCRIPTDIR/assert_fail.sh
source "${assert_fail_sh}"


# MARK - Test assert_equal

reset_fail_err_msgs
assert_equal "hello" "goodbye"
assert_fail "Expected to be equal."
reset_fail_err_msgs

reset_fail_err_msgs
assert_equal "hello" "goodbye" "Custom prefix."
assert_fail "Custom prefix. Expected to be equal."
reset_fail_err_msgs

reset_fail_err_msgs
assert_equal "hello" "hello"
assert_no_fail
reset_fail_err_msgs
36 changes: 36 additions & 0 deletions shlib/tests/lib_tests/assertions_tests/assert_fail.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env bash

FAIL_ERR_MSGS=()

fail() {
local err_msg="${1:-"Unspecified error occurred."}"
FAIL_ERR_MSGS+=( "${err_msg}" )
}

reset_fail_err_msgs() {
FAIL_ERR_MSGS=()
}

new_fail(){
local err_msg="${1:-}"
[[ -n "${err_msg}" ]] || err_msg="Unspecified error occurred."
cgrindel marked this conversation as resolved.
Show resolved Hide resolved
echo >&2 "${err_msg}"
exit 1
}

assert_fail() {
local pattern=${1}
[[ ${#FAIL_ERR_MSGS[@]} == 0 ]] && \
new_fail "Expected a failure. None found. pattern: ${pattern}"
[[ ${#FAIL_ERR_MSGS[@]} -gt 1 ]] && \
new_fail "Expected a single failure. Found ${#FAIL_ERR_MSGS[@]}. pattern: ${pattern}"
[[ "${FAIL_ERR_MSGS[0]}" =~ ${pattern} ]] || \
new_fail "Unexpected failure. Found '${FAIL_ERR_MSGS[0]}'. pattern: ${pattern}"
}

assert_no_fail(){
[[ ${#FAIL_ERR_MSGS[@]} == 0 ]] || ( \
err_msg=$("${FAIL_ERR_MSGS[@]}") && \
new_fail "Expected no failures. Found ${#FAIL_ERR_MSGS[@]}. '${err_msg}'" \
)
}
37 changes: 37 additions & 0 deletions shlib/tests/lib_tests/assertions_tests/assert_match_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env bash

# --- begin runfiles.bash initialization v2 ---
# Copy-pasted from the Bazel Bash runfiles library v2.
set -o nounset -o pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash
# shellcheck disable=SC1090
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
source "$0.runfiles/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
# --- end runfiles.bash initialization v2 ---

assertions_sh_location=aspect_bazel_lib/shlib/lib/assertions.sh
assertions_sh="$(rlocation "${assertions_sh_location}")" || \
(echo >&2 "Failed to locate ${assertions_sh_location}" && exit 1)
# shellcheck source=SCRIPTDIR/../../../lib/assertions.sh
source "${assertions_sh}"

assert_fail_sh_location=aspect_bazel_lib/shlib/tests/lib_tests/assertions_tests/assert_fail.sh
assert_fail_sh="$(rlocation "${assert_fail_sh_location}")" || \
(echo >&2 "Failed to locate ${assert_fail_sh_location}" && exit 1)
# shellcheck source=SCRIPTDIR/assert_fail.sh
source "${assert_fail_sh}"

# MARK - Test assert_match

reset_fail_err_msgs
assert_match ^Begin "Begin with hello"
assert_no_fail
reset_fail_err_msgs

reset_fail_err_msgs
assert_match ^Begin "Not Begin with hello"
assert_fail "Expected to match."
reset_fail_err_msgs
37 changes: 37 additions & 0 deletions shlib/tests/lib_tests/assertions_tests/assert_no_match_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env bash

# --- begin runfiles.bash initialization v2 ---
# Copy-pasted from the Bazel Bash runfiles library v2.
set -o nounset -o pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash
# shellcheck disable=SC1090
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
source "$0.runfiles/$f" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
# --- end runfiles.bash initialization v2 ---

assertions_sh_location=aspect_bazel_lib/shlib/lib/assertions.sh
assertions_sh="$(rlocation "${assertions_sh_location}")" || \
(echo >&2 "Failed to locate ${assertions_sh_location}" && exit 1)
# shellcheck source=SCRIPTDIR/../../../lib/assertions.sh
source "${assertions_sh}"

assert_fail_sh_location=aspect_bazel_lib/shlib/tests/lib_tests/assertions_tests/assert_fail.sh
assert_fail_sh="$(rlocation "${assert_fail_sh_location}")" || \
(echo >&2 "Failed to locate ${assert_fail_sh_location}" && exit 1)
# shellcheck source=SCRIPTDIR/assert_fail.sh
source "${assert_fail_sh}"

# MARK - Test assert_no_match

reset_fail_err_msgs
assert_no_match ^Begin "Begin with hello"
assert_fail "Expected not to match."
reset_fail_err_msgs

reset_fail_err_msgs
assert_no_match ^Begin "Not Begin with hello"
assert_no_fail
reset_fail_err_msgs