Files
42_INT_07_minishell/tests/test_unit.sh
2021-12-02 15:29:16 +01:00

119 lines
2.3 KiB
Bash

#!/bin/bash
cp ../minishell .
RED="\001\e[0;31m\002"
GREEN="\001\e[0;32m\002"
YELLOW="\001\e[0;33m\002"
BLUE="\001\e[0;34m\002"
MAGENTA="\001\e[0;35m\002"
CYAN="\001\e[0;36m\002"
WHITE="\001\e[0;37m\002"
B_RED="\001\e[1;31m\002"
B_GREEN="\001\e[1;32m\002"
B_YELLOW="\001\e[1;33m\002"
B_BLUE="\001\e[1;34m\002"
B_MAGENTA="\001\e[1;35m\002"
B_CYAN="\001\e[1;36m\002"
B_WHITE="\001\e[1;37m\002"
ENDCO="\001\e[0m\002"
list_files="\
tunit_0.sh"
# tunit_1.sh \
# tunit_2.sh \
# tunit_3.sh \
# "
TOTAL_TEST=0
SUCCESS_TEST=0
function test_bash
{
# execute commands in bash and minishell
bash_execution=$( echo "$@" | bash -i 2>&1 )
minishell_execution=$( echo "$@" | ./minishell )
#echo "minishell_execution :"
#echo "$minishell_execution"
#echo "$minishell_execution" | cat -e
echo "bash_execution :"
echo "$bash_execution"
echo "$bash_execution" | cat -e
echo ""
# extract informations
first_command=$( head -1 <<< "$@" )
echo "first_command:"
echo "$first_command"
echo ""
#f_cmd_length=${#first_command}
#bash_prompt=$( head -1 <<< "$bash_execution" | sed "s/${first_command}//g")
bash_prompt=$( head -1 <<< "$bash_execution")
#bash_prompt=${bash_prompt::-$f_cmd_length}
echo "bash_prompt :"
echo "$bash_prompt"
echo "$bash_prompt" | cat -e
echo ""
echo ""
myoutput=$( grep -v "simplonco" <<< "$bash_execution" )
echo "myoutput :"
echo "$myoutput"
echo ""
((SUCCESS_TEST++))
}
function print_results
{
if [ $SUCCESS_TEST -eq 0 ]
then
echo -en $B_RED $SUCCESS_TEST $ENDCO
elif [ $SUCCESS_TEST -eq $TOTAL_TEST ]
then
echo -en $B_GREEN $SUCCESS_TEST $ENDCO
else
echo -en $B_MAGENTA $SUCCESS_TEST $ENDCO
fi
echo -e $B_WHITE"/ $TOTAL_TEST"$ENDCO
}
# parse tests files line by line, grouping lines non-separated by an empty line
for i in $list_files
do
filename=$i
echo -e "\n"$B_YELLOW"test file : $i"$ENDCO
command_test=""
last_line="$(tail -1 $i)"
((TOTAL_TEST=0))
((SUCCESS_TEST=0))
while read line
do
if [ "$line" != "" ]
then
if [ "$command_test" == "" ]
then
command_test="$line"
else
command_test+=$'\n'
command_test+="$line"
fi
if [ "$line" == "$last_line" ]
then
((TOTAL_TEST++))
test_bash "$command_test"
fi
elif [ "$command_test" != "" ]
then
((TOTAL_TEST++))
test_bash "$command_test"
command_test=""
fi
done < $filename
print_results
done