Skip to content

Commit

Permalink
feat: ✨ support regenerating the results of specified tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Equationzhao committed May 12, 2024
1 parent 8074de9 commit f4e000b
Showing 1 changed file with 52 additions and 3 deletions.
55 changes: 52 additions & 3 deletions script/reproduce_test_result.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,32 @@
# Reproduce the test result from the test script
# This script will overwrite the test result in tests/*.stdout

error() {
printf '\033[1;31m%s\033[0m\n' "$1"
}

success() {
printf '\033[1;32m%s\033[0m\n' "$1"
}

warn() {
printf '\033[1;33m%s\033[0m\n' "$1"
}

check_input(){
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
}

bye(){
echo "Bye👋"
exit 0
}

# PRINT WARNING
printf '\033[1;33mThis script will overwrite the test result in tests/*.stdout\033[0m\n'
warn 'This script will overwrite the test result in tests/*.stdout'

read -p "Are you sure? (y/N) " -n 1 -r
check_input
Expand All @@ -21,13 +38,45 @@ read -p "Are you sure??? (y/N) " -n 1 -r
check_input

echo "Well, you asked for it..."
echo "Reproducing test result..."

printf 'which one do you want to reproduce?(name/all/none)\n'

read -p "Enter the name(s) of the test script(s): " -r
if [ "$REPLY" == "all" ]; then
echo "Reproducing all test result..."
for sh_file in tests/*.sh; do
name="${sh_file%.*}"
first_line=$(head -n 1 "$sh_file")
eval "$first_line"
# output is assigned in the test script
echo "$output" > "$name.stdout"
done
success "Test result reproduced successfully.🎉"
exit 0
fi

if [ "$REPLY" == "none" ]; then
error "No test result will be reproduced."
bye
exit 0
fi

# split the input by comma

IFS=',' read -r -a test_names <<< "$REPLY"
for test_name in "${test_names[@]}"; do
sh_file="tests/$test_name.sh"
echo "Reproducing $sh_file..."
if [ ! -f "$sh_file" ]; then
error "Test script not found.😭"
exit 1
fi

for sh_file in tests/*.sh; do
name="${sh_file%.*}"
first_line=$(head -n 1 "$sh_file")
eval "$first_line"
# output is assigned in the test script
echo "$output" > "$name.stdout"
done

success "Test result reproduced successfully.🎉"

0 comments on commit f4e000b

Please sign in to comment.