databuild/databuild/test/test_job_helper_test.sh

46 lines
1.2 KiB
Bash
Executable file

#!/bin/bash
set -e
HELPER="$1"
# Test 1: Default behavior - exit code 0, default stdout
OUTPUT=$("$HELPER")
if [ "$OUTPUT" != "test executed" ]; then
echo "Test 1 failed: Expected 'test executed', got '$OUTPUT'"
exit 1
fi
echo "Test 1 passed: Default stdout"
# Test 2: Custom exit code
DATABUILD_TEST_EXIT_CODE=42 "$HELPER" || EXIT_CODE=$?
if [ "$EXIT_CODE" != "42" ]; then
echo "Test 2 failed: Expected exit code 42, got $EXIT_CODE"
exit 1
fi
echo "Test 2 passed: Custom exit code"
# Test 3: Custom stdout message
OUTPUT=$(DATABUILD_TEST_STDOUT="custom message" "$HELPER")
if [ "$OUTPUT" != "custom message" ]; then
echo "Test 3 failed: Expected 'custom message', got '$OUTPUT'"
exit 1
fi
echo "Test 3 passed: Custom stdout"
# Test 4: Write to file
TEMP_FILE=$(mktemp)
DATABUILD_TEST_OUTPUT_FILE="$TEMP_FILE" DATABUILD_TEST_FILE_CONTENT="hello world" "$HELPER" > /dev/null
if [ ! -f "$TEMP_FILE" ]; then
echo "Test 4 failed: Output file not created"
exit 1
fi
CONTENT=$(cat "$TEMP_FILE")
if [ "$CONTENT" != "hello world" ]; then
echo "Test 4 failed: Expected 'hello world' in file, got '$CONTENT'"
rm "$TEMP_FILE"
exit 1
fi
rm "$TEMP_FILE"
echo "Test 4 passed: Write to file"
echo "All tests passed!"