Interview Prep/Shell Scripting/ShellCheck, debugging, testing, portability and security

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.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on ShellCheck, debugging, testing, portability and security