What is bats, and how do you test a script that calls external commands?
IntermediateConceptShellCheck, debugging, testing, portability and security
Answer
bats (Bash Automated Testing System, bats-core) runs @test blocks; run cmd captures $status and $output.
@test "alert fails with exit 2 on missing title" {
run ./bin/opskit-alert -s critical
[ "$status" -eq 2 ]
[[ "$output" == *"-t TITLE is required"* ]]
}
To isolate external commands (curl, aws, systemctl), put stub scripts in a directory that comes first on PATH during tests; stubs record their arguments and return canned output. Each test gets $BATS_TEST_TMPDIR for files. The hands-on lab uses exactly this pattern.
More on ShellCheck, debugging, testing, portability and security
What is ShellCheck and how do you use it in a team?BeginnerHow do you disable a ShellCheck warning, and when is that acceptable?IntermediateName the ShellCheck warnings you see most and what each means.IntermediateHow do you debug a Bash script?BeginnerHow do you make `set -x` output more useful with `PS4`?IntermediateWhat is `shfmt` and why use it alongside ShellCheck?Beginner