aoc_2024/simpletest.sh

128 lines
3.2 KiB
Bash
Raw Normal View History

#!/bin/bash
# Function to show usage
usage() {
2024-12-05 13:43:19 +00:00
echo "Usage: $0 [-l] [write|validate]"
echo " -l Use local input files (e.g., day01input.txt)"
exit 1
}
2024-12-05 13:43:19 +00:00
# Parse arguments
LOCAL_MODE=0
while getopts ":l" opt; do
case $opt in
l) LOCAL_MODE=1 ;;
*) usage ;;
esac
done
shift $((OPTIND - 1))
if [[ $# -ne 1 ]]; then
usage
fi
MODE=$1
2024-12-05 13:43:19 +00:00
# Function to fetch input
fetch_input() {
local script=$1
local dayname
dayname=$(basename "$script" .kts | tr 'a-z' 'A-Z')
if [[ $LOCAL_MODE -eq 1 ]]; then
# Fetch input from local files
local input_file="${dayname,,}input.txt"
if [[ -f "$input_file" ]]; then
cat "$input_file"
else
echo "Error: Local input file $input_file not found" >&2
exit 1
fi
else
# Fetch input from environment variable
local env_var="${dayname}INPUT"
if [[ -z "${!env_var}" ]]; then
echo "Error: Environment variable $env_var not set" >&2
exit 1
fi
echo "${!env_var}"
fi
}
# Write mode
if [[ "$MODE" == "write" ]]; then
echo "Running in write mode..."
for script in *.kts; do
test_file="${script}.testvalue"
echo "Processing $script..."
2024-12-05 13:43:19 +00:00
input=$(fetch_input "$script")
output=$(echo "$input" | kotlin "$script" 2>&1)
if [[ -f "$test_file" ]]; then
if [[ "$output" == "$(cat "$test_file")" ]]; then
echo "No change: $script"
else
echo "Output changed for $script"
diff -u "$test_file" <(echo "$output")
read -p "Update test value? (y/n) " update
if [[ "$update" == "y" ]]; then
echo "$output" > "$test_file"
echo "Updated $test_file"
fi
fi
else
echo "$output" > "$test_file"
echo "Created $test_file"
fi
done
for test_file in *.kts.testvalue; do
script="${test_file%.testvalue}"
if [[ ! -f "$script" ]]; then
echo "Test file $test_file does not have a matching script."
read -p "Delete $test_file? (y/n) " delete
if [[ "$delete" == "y" ]]; then
rm "$test_file"
echo "Deleted $test_file"
fi
fi
done
# Validate mode
elif [[ "$MODE" == "validate" ]]; then
echo "Running in validate mode..."
pass=0
fail=0
for test_file in *.kts.testvalue; do
script="${test_file%.testvalue}"
if [[ -f "$script" ]]; then
2024-12-05 13:43:19 +00:00
input=$(fetch_input "$script")
output=$(echo "$input" | kotlin "$script" 2>&1)
if [[ "$output" == "$(cat "$test_file")" ]]; then
echo "PASS: $script"
((pass++))
else
echo "FAIL: $script"
diff -u "$test_file" <(echo "$output")
((fail++))
fi
else
echo "Orphaned test file: $test_file (no matching script)"
((fail++))
fi
done
echo "Summary: $pass pass, $fail fail"
if [[ $fail -gt 0 ]]; then
exit 1
else
exit 0
fi
else
usage
fi