]> git.rocketbowman.com Git - belfry.git/commitdiff
feat: check for container before running tests
authorKyle Bowman <kyle+github@rocketbowman.com>
Sun, 30 Mar 2025 15:49:45 +0000 (11:49 -0400)
committerKyle Bowman <kyle+github@rocketbowman.com>
Sun, 30 Mar 2025 15:49:45 +0000 (11:49 -0400)
Dockerfile
tests/test_example.bats

index 315f6d24131e2b9478e8a59b82385f57740d457f..df3d76dce72b0249341bc8c2a33ca3ec4c0310ec 100644 (file)
@@ -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
index 3d7bcd94d28d681551668f4a58d424cab97a6a32..2a61dda0785ccd725083d62cf212821d50405b60 100644 (file)
@@ -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
 }