When writing Bazel tests using sh_test(), I often find myself needing to compare
two collections for equivalence. For example, I might compare a directory listing
against a set of expected files or directories, or the list of files and directories
in a .tar file against a set of expected items. This blog post provides some tips
and tricks as to how to do so.
Dealing with Bazel runfiles is one of the most annoying things about using Bazel.
Fortunately, Bazel provides a library to make resolving runfiles from Bash scripts
easy.
sh_test is my most commonly used test rule by far. It is the easiest way to
write quick-and-dirty tests and works nearly everywhere. For anything beyond
the most trivial tests, I use Bazel’s Bash unit test framework. This explains
what the framework is and how to use it.
In Bazel, a successful build should be a quiet build. While build failures
should, of course, print ample information to stderr to aide in troubleshooting,
any custom Bazel code you write should not output progress information to stdout
or stderr. Let Bazel be responsible for overall build progress reporting.
One tends to write a lot of Bash scripts when using Bazel. In
order to make these scripts more robust, enable
Bash’s unofficial strict mode by starting all scripts
like this: