bashtest 0.6.1

bashtest.sh - A Bazel shell test runner.

This shell test library provides Bazel macro rules to simplify shell testing.

The library is tested with continuous integration: Test.

Bashtest

Run one of the following commands to get detailed information on the actual bashtest.sh script:

The flags can be used on the bazel run and bazel test commands (the latter requiring --test_arg=...).

Use --keep-tmpdir=never|failure|always to control scratch retention; the default is failure.
A retained path is printed at shutdown. Bazel itself may still remove a sandbox unless the test runs
locally or with --sandbox_debug.

Functionality

Example

  1. Write a test that sources bashtest.
set -euo pipefail

# shellcheck disable=SC1090,SC1091,SC2154
source "${mboworks_bashtest}"

test::my_test() {
  expect_ne "Hello" "World"
  # Your tests go here...
}

# More tests go here...

test_runner
  1. Write or extend a BUILD file
load("@mboworks_bashtest//bashtest:bashtest.bzl", "bashtest")

bashtest(
    name = "sh_test",
    srcs = ["sh_test.sh"],
)

Matching command output

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 idiom printf '%s' "${output}" | grep -qE '...' is a
footgun there: grep -q exits on the first match, the producing command gets
SIGPIPE, and pipefail turns that into a non-zero exit — a flaky failure on
large output. Feed the text via a here-string (grep -qE -- '...' <<<"${output}")
or, better, use expect_matches, which relies on bash's built-in [[ =~ ]]
and spawns no subprocess at all.

Installation and requirements

This repository requires bash to work (Linux, MacOs).

MODULE.bazel

Check Releases for details. All that is needed is a bazel_dep instruction with the correct version.

bazel_dep(name = "mboworks_bashtest", version = "0.0.0")