From fd12b9737ee1fd124956e82c50d45ad77a7c2313 Mon Sep 17 00:00:00 2001 From: Kyle Bowman Date: Sun, 30 Mar 2025 11:49:45 -0400 Subject: [PATCH] feat: check for container before running tests --- Dockerfile | 1 + tests/test_example.bats | 25 ++++++++++++++++++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index 315f6d2..df3d76d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,4 @@ FROM debian:bookworm-20250317-slim +ENV IN_CONTAINER=true RUN apt update \ && apt install -y bats \ No newline at end of file diff --git a/tests/test_example.bats b/tests/test_example.bats index 3d7bcd9..2a61dda 100644 --- a/tests/test_example.bats +++ b/tests/test_example.bats @@ -1,18 +1,33 @@ #!/usr/bin/env bats -setup() { - # Run before each test case - # Example: start container, prepare environment +#################### +# Setup & Teardown # +#################### +# setup_file is run once for this whole file +setup_file() { + + # Ensure sandboxed if the script has side effects. + if [ -z "$IN_CONTAINER" ]; then + echo "This script has side effects and must be run in a container." > /dev/stderr + exit 1 + fi +} + +# setup is run once per test case +setup () { + # Source library LIB_DIR="$(dirname $(dirname "$BATS_TEST_FILENAME"))/lib" . "$LIB_DIR"/example.sh } +# teardown is run after each test case teardown() { - # Run after each test case - # Example: stop/remove container, clean up resources echo "Teardown running" } +######### +# Tests # +######### @test "ensure echo_hello runs" { echo_hello } -- 2.39.5