Files
drharness/tests/test_auto_temp_file.sh
darthrubik 44db144bfb Auto-generate temp file with -n flag
- Added UUID v4 generation using <random> and <filesystem> headers
- When -n is specified without -F, create a temp file in system temp dir
- Temp file is named harness-<uuid>.json
- Print temp file path to stderr when session exits
- Updated help message to reflect new behavior
- Added test_auto_temp_file.sh to verify:
  - Temp file creation with -n alone
  - Path printed to stderr
  - Existing -F <file> -n behavior preserved
  - UUID uniqueness
2026-05-21 16:49:19 -05:00

63 lines
2.3 KiB
Bash
Executable File

#!/bin/bash
# test_auto_temp_file.sh - Validates auto-generation of temp files with -n flag
# Test 1: -n alone creates a temp file and prints path to stderr
OUTPUT=$("${DRHARNESS}" -n -m "test" -d <<< "$(be_done 'Test response from mock')" 2> stderr_output)
# Check that we got the expected response
if [ "${OUTPUT}" != "Test response from mock" ]; then
echo "FAIL: Expected 'Test response from mock', got '${OUTPUT}'"
exit 1
fi
# Check that stderr contains a path matching harness-*.json pattern
if ! grep -qE 'harness-[0-9a-f-]+\.json' stderr_output; then
echo "FAIL: Expected temp file path in stderr, got: $(cat stderr_output)"
exit 1
fi
# Extract the temp file path from stderr (last line)
TEMP_FILE=$(grep -E 'harness-[0-9a-f-]+\.json' stderr_output | tail -1)
# Verify the temp file exists
if [ ! -f "${TEMP_FILE}" ]; then
echo "FAIL: Temp file '${TEMP_FILE}' does not exist"
exit 1
fi
# Verify the temp file has content (the harness state was written to it)
FILE_SIZE=$(wc -c < "${TEMP_FILE}")
if [ "${FILE_SIZE}" -eq 0 ]; then
echo "FAIL: Temp file is empty, expected harness state to be written"
exit 1
fi
echo "PASS: auto_temp_file - temp file created and path printed to stderr"
# Test 2: -F <file> -n still works as before (preserves existing behavior)
touch "${TEST_TEMP_DIR}/existing.json"
EXISTING_OUTPUT=$("${DRHARNESS}" -F existing.json -n -m "test" -d <<< "$(be_done 'Existing file test')" 2> existing_stderr_output)
if [ "${EXISTING_OUTPUT}" != "Existing file test" ]; then
echo "FAIL: Expected 'Existing file test', got '${EXISTING_OUTPUT}'"
exit 1
fi
# The existing file should not have been modified if it was empty initially
# (except for the harness state being written to it)
# Test 3: Verify two -n calls create different files
FIRST_OUTPUT=$("${DRHARNESS}" -n -m "test" -d <<< "$(be_done 'First')" 2> first_stderr)
SECOND_OUTPUT=$("${DRHARNESS}" -n -m "test" -d <<< "$(be_done 'Second')" 2> second_stderr)
FIRST_FILE=$(grep -E 'harness-[0-9a-f-]+\.json' first_stderr | tail -1)
SECOND_FILE=$(grep -E 'harness-[0-9a-f-]+\.json' second_stderr | tail -1)
if [ "${FIRST_FILE}" = "${SECOND_FILE}" ]; then
echo "FAIL: Two -n calls created the same file: ${FIRST_FILE}"
exit 1
fi
echo "PASS: auto_temp_file - UUID uniqueness verified"
echo "PASS: auto_temp_file -F behavior preserved"