This shell test library provides Bazel macro rules to simplify shell testing.
The library is tested with continuous integration: .
Run one of the following commands to get detailed information on the actual bashtest.sh script:
bazel run //bashtest:bashtest_helpbazel run //bashtest:bashtest_help | pandoc -s -t man | man -l -bazel run //bashtest:bashtest_help | pandoc | lynx -stdinThe flags can be used on the bazel run and bazel test commands (the latter requiring --test_arg=...).
test_has_error: Returns whether a test function has had an expectation error. This is reset for every test function.test_has_failed_tests: Returns whether a test program had previous failing test functions.expect_eq "${LHS}" "${RHS}": Asserts that two strings are the same.expect_ne "${LHS}" "${RHS}": Asserts that two strings are different.expect_files_eq "${LHS}" "${RHS}": Asserts that two file are the same (supports golden updates).expect_contains "${EXPECTED}" "${ARRAY[@]}": Assert that one string is present in an array.expect_not_contains "${EXPECTED}" "${ARRAY[@]}": Assert that one string is not present in an array.expect_output_contains "${SUBSTRING}" "${TEXT}": Assert that a text contains a literal substring.expect_output_not_contains "${SUBSTRING}" "${TEXT}": Assert that a text does not contain a literal substring.expect_matches "${REGEX}" "${TEXT}": Assert that a text matches an extended regular expression (bash built-in; ^/$ anchor the whole text).expect_not_matches "${REGEX}" "${TEXT}": Assert that a text does not match an extended regular expression.expect_pcre_matches "${REGEX}" "${TEXT}": Assert that a text matches a Perl Compatible Regular Expression (requires an external PCRE tool: grep -P, pcre2grep, or pcregrep).expect_pcre_not_matches "${REGEX}" "${TEXT}": Assert that a text does not match a Perl Compatible Regular Expression.skip_test "${REASON}": Marks the current test as skipped and reports the reason. Use skip_test "reason" && return to end the test body.test::test_init: If present, then this function runs first! Tests will only be executed if it succeeds.test::test_done: If present, then this function runs last!set -euo pipefail
# shellcheck disable=SC1090,SC1091,SC2154
source "${helly25_bashtest}"
test::my_test() {
expect_ne "Hello" "World"
# Your tests go here...
}
# More tests go here...
test_runner
load("@helly25_bashtest//bashtest:bashtest.bzl", "bashtest")
bashtest(
name = "sh_test",
srcs = ["sh_test.sh"],
)
To assert on captured command output, prefer the built-in matchers
(expect_output_contains, expect_matches, expect_pcre_matches) over
hand-rolled pipelines.
[!WARNING] bashtest runs under
set -o pipefail(and recommends the same for your test scripts). The common idiomprintf '%s' "${output}" | grep -qE '...'is a footgun there:grep -qexits on the first match, the producing command getsSIGPIPE, andpipefailturns that into a non-zero exit — a flaky failure on large output. Feed the text via a here-string (grep -qE -- '...' <<<"${output}") or, better, useexpect_matches, which relies on bash's built-in[[ =~ ]]and spawns no subprocess at all.
This repository requires bash to work (Linux, MacOs).
Check Releases for details. All that is needed is a bazel_dep instruction with the correct version.
bazel_dep(name = "helly25_bashtest", version = "0.0.0")