#!/bin/bash # Test Tool # # The Translators - Spring 2025 # RED='\033[0;31m' GREEN='\033[0;32m' ORANGE='\033[0;33m' BLUE='\033[0;34m' PURPLE='\033[0;35m' CYAN='\033[0;36m' LIGHTGRAY='\033[0;37m' DARKGRAY='\033[1;30m' LIGHTRED='\033[1;31m' LIGHTGREEN='\033[1;32m' YELLOW='\033[1;33m' LIGHTBLUE='\033[1;34m' LIGHTPURPLE='\033[1;35m' LIGHTCYAN='\033[1;36m' WHITE='\033[1;37m' if [ ! -f "./alpha" ]; then echo -e "${RED}[ALERT] ${YELLOW}File ./alpha not found! Generating with 'make'.${WHITE}" make fi if [ ! -d "./out" ]; then mkdir -p out fi if [ $# -eq 0 ]; then help fi help() { echo -e "${WHITE}-----{ ${BLUE}Test.sh Help ${WHITE}}-----\n" echo -e "${YELLOW} Arguments: ${WHITE}" echo -e "${GREEN} -exp ${WHITE} Generate expected output files" echo -e "${GREEN} -diff ${WHITE} Compare output files with expected output files" echo -e "${GREEN} -help ${WHITE} Show this help message\n" echo -e "${YELLOW} Usage: ${WHITE}" echo -e "${GREEN} ./test.sh [flags] ${WHITE} Run the test on a single file" echo -e "${GREEN} ./test.sh [flags] ${WHITE} Run the test on all files in a directory\n" echo -e "${YELLOW} Examples: ${WHITE}" echo -e "${GREEN} ./test.sh test.alpha ${WHITE} Runs test flags in header on test.alpha" echo -e "${GREEN} ./test.sh test/ ${WHITE} Runs test flags in header on all .alpha files in test/" echo -e "${GREEN} ./test.sh test/ -exp ${WHITE} Runs test flags in header on all .alpha files in test/ and generates expected output files" echo -e "${GREEN} ./test.sh test/ -diff ${WHITE} Runs test flags in header on all .alpha files in test/ and compares output files with expected output files" echo -e "${GREEN} ./test.sh test/ -exp -diff ${WHITE} Runs test flags in header on all .alpha files in test/ and generates expected output files and compares output files with expected output files\n" echo -e "${YELLOW} Notes: ${WHITE}" echo -e "${ORANGE} To create a test file, on the first line of the .alpha file, add:" echo -e "${LIGHTBLUE} (* TEST: [ ] *)" echo -e "${ORANGE} where are the alpha flags to be used. Ex:" echo -e "${LIGHTBLUE} (* TEST: [ -debug -asc -tc ] *)\n" echo -e "${WHITE}-----------------------------------------\n" exit 1 } run() { filename=$(basename -- "$1") first_line=$(head -n 1 "$1") if [[ "$first_line" == "(* TEST: ["*"] *)"* ]]; then bracket_content=$(echo "$first_line" | sed -n 's/.*\[\(.*\)\].*/\1/p') sed -i '1d' "$1" ./alpha ${bracket_content} "$1" sed -i "1s/^/$first_line\n/" "$1" if [ $# -ge 2 ]; then path=$(dirname "$1") filename_noext=${filename:0:${#filename}-6} if [[ "$2" == "-exp" || "$3" == "-exp" ]]; then for file in out/${filename_noext}.*; do if [[ "$file" == *".asc" || "$file" == *".s" || "$file" == *".st" || "$file" == *".tok" || "$file" == *".ir" ]]; then for_filename=$(basename -- "$file") for_filename="${for_filename}.exp" if [ -f "${path}/../expected/${for_filename}" ]; then echo -e "${RED}[ALERT] ${YELLOW}File ${path}/../expected/${for_filename} already exists! Overwriting...${WHITE}" fi cp "$file" "${path}/../expected/${for_filename}" else echo -e "${RED}[ALERT] ${YELLOW}Unexpected file found: $file ${WHITE}" fi done fi if [[ "$2" == "-diff" || "$3" == "-diff" ]]; then for file in out/${filename_noext}.*; do if [[ "$file" == *".asc" || "$file" == *".s" || "$file" == *".st" || "$file" == *".tok" || "$file" == *".ir" ]]; then for_filename=$(basename -- "$file") for_filename="${for_filename}.exp" if [ -f "${path}/../expected/${for_filename}" ]; then exp_basename=$(basename -- "$for_filename") diff -q "${path}/../expected/${for_filename}" "$file" if [ $? -eq 0 ]; then echo -e "${GREEN}[SUCCESS] ${YELLOW}Test ${WHITE}$file ${YELLOW}passed ${WHITE}$exp_basename${WHITE}" else echo -e "${RED}[ERROR] ${YELLOW}Test ${WHITE}$file ${YELLOW}failed ${WHITE}$exp_basename${WHITE}" fi else echo -e "${RED}[ALERT] ${YELLOW}File ${path}/../expected/${for_filename} not found!${WHITE}" fi else echo -e "${RED}[ALERT] ${YELLOW}Unexpected file found: $file ${WHITE}" fi done fi fi else echo -e "${RED}[ERROR] ${YELLOW}File $1 is not a valid .alpha test file!${WHITE}" fi } if [[ "$1" == *"/" ]]; then if [ $# -ge 2 ]; then for file in "$1"*; do if [[ "$file" == *.alpha ]]; then if [[ "$2" == "-exp" || "$3" == "-exp" || "$2" == "-diff" || "$3" == "-diff" ]]; then run "$file" "$2" "$3" else echo -e "${RED}[ERROR] ${YELLOW}Invalid argument $2!${WHITE}" help fi fi done else for file in "$1"*; do if [[ "$file" == *.alpha ]]; then run "$file" else echo -e "${RED}[ERROR] ${YELLOW}File $file is not a .alpha file!${WHITE}" fi done fi exit 0 fi if [ -f "$1" ]; then if [[ "$1" == *.alpha ]]; then if [ $# -ge 2 ]; then if [[ "$2" == "-exp" || "$3" == "-exp" || "$2" == "-diff" || "$3" == "-diff" ]]; then run "$1" "$2" "$3" else echo -e "${RED}[ERROR] ${YELLOW}Invalid argument $2!${WHITE}" help fi else run "$1" fi else echo -e "${RED}[ERROR] ${YELLOW}File $1 is not a .alpha file!${WHITE}" help fi else help fi