simplification unitests, suppression options -p1,2,3, logs affiche seulement les tests rates

This commit is contained in:
hugogogo
2021-12-14 16:56:38 +01:00
parent f845b342a2
commit 989c8de771
2 changed files with 623 additions and 214 deletions

View File

@@ -33,8 +33,6 @@ DEFAULT_DIR="./tests/defaults/"
TOTAL_TEST=0
TOTAL_SUCCESS=0
LINE_NUMBER=0
OPTION=0
PRINT=0
FILES_BEFORE="$(ls .)"
mkdir -p ./logs
echo "" > ./logs/bash_log.txt
@@ -48,41 +46,14 @@ DEFAULT_DIR="./tests/defaults/"
# print usage
function print_usage
{
echo -en "$GREEN"
echo " usage :"
echo -en "$ENDCO"
echo -en "$CYAN"
echo " > bash test_unit.sh [options] [list files...]"
echo -en "$ENDCO"
echo ""
echo -en "$GREEN"
echo " if no files are given in parameters, the defaults will be used"
echo " defaults files :"
echo -en "$ENDCO"
echo -en "$CYAN"
echo "$default_files"
echo -en "$ENDCO"
echo ""
echo -en "$GREEN"
echo " options :"
echo -en "$ENDCO"
echo -en "$CYAN"
echo " help - print usage"
echo " -p1 - prints errors"
echo " -p2 - prints errors and commands"
echo " -p3 - prints errors and commands and errors output"
echo -en "$ENDCO"
echo ""
echo -en "$GREEN"
echo " tests files are formated with the following rules :"
echo -en "$ENDCO"
echo -en "$CYAN"
echo " - a line starting with # is skipped"
echo " - an empty line separate two sets of commands"
echo " - other lines are commands"
echo " - multiples lines in a row are executed as following commands"
echo -en "$ENDCO"
echo ""
echo -en $GREEN"usage : "
echo -en $CYAN"bash unitest.sh [help] [files list ...]\n"
echo -en $GREEN"\n[help]\n"
echo -en $CYAN"print usage\n"
echo -en $GREEN"\n[files list ...] if empty, defaults files will be used :\n"
echo -en $CYAN"$default_files"
echo -en $ENDCO"\n"
echo ""
}
# to delete the files created during the script
@@ -101,38 +72,14 @@ DEFAULT_DIR="./tests/defaults/"
list_files="$default_files"
if [ $# -gt 0 ]
then
START=1
if [ "$1" == "help" ]
then
print_usage
delete_files
exit 0
elif [ "$1" == "-p1" ]
then
PRINT=1
OPTION=1
elif [ "$1" == "-p2" ]
then
PRINT=2
OPTION=1
elif [ "$1" == "-p3" ]
then
PRINT=3
OPTION=1
fi
if [ $OPTION -eq 1 ]
then
if [ $# -eq 1 ]
then
START=0
else
START=2
fi
fi
if [ $START -gt 0 ]
then
else
list_files=""
for (( i = $START ; i <= "$#" ; i++ ))
for (( i = 1 ; i <= "$#" ; i++ ))
do
# the ! is for indirect parameter expansion
# $i expand in integers 1,2,3...
@@ -150,6 +97,7 @@ DEFAULT_DIR="./tests/defaults/"
else
print_usage
echo " <$FILE> is not a valid file or option, see usage above"
exit 0
fi
done
fi
@@ -170,20 +118,10 @@ DEFAULT_DIR="./tests/defaults/"
# function that will launch the command in bash and minishell and compare them
function test_minishell
{
# if -p2|3, print which command is about to be executed
if [ $PRINT -gt 1 ]
then
NEXT_CMD="command line $(( $LINE_NUMBER - 1 )) : "
print_next_command "$NEXT_CMD" "$@" "$CYAN"
fi
# execute commands in bash, and logs results
bash_execution=$( echo "$@" | bash 2>/dev/null )
echo -e $B_WHITE"\n\n$@\n-----------"$ENDCO >>$BASH_LOG
echo "$bash_execution" >> $BASH_LOG
minishell_execution=$( echo "$@" | ./minishell 2>/dev/null )
echo -e $B_WHITE"\n\n$@\n-----------"$ENDCO >>$MINISHELL_LOG
echo "$minishell_execution" >> $MINISHELL_LOG
#compare output
if [ "$bash_execution" = "$minishell_execution" ]
@@ -191,24 +129,13 @@ DEFAULT_DIR="./tests/defaults/"
(( SUCCESS_TEST++ ))
(( TOTAL_SUCCESS++ ))
else
# if -p1..3 print error for command
if [ $PRINT -eq 1 ]
then
ERROR_CMD="ERROR line $(( $LINE_NUMBER - 1 )), command : "
print_next_command "$ERROR_CMD" "$@" "$B_RED"
elif [ $PRINT -eq 2 ]
then
echo -e $B_RED" "'\'" ERROR"$ENDCO
elif [ $PRINT -eq 3 ]
then
echo -e $B_RED" "'\'" ERROR"$ENDCO
echo -e $B_MAGENTA"[bash execution] :"$ENDCO
echo "$@" | bash -i
echo ""
echo -e $B_MAGENTA"[minishell execution] :"$ENDCO
echo "$@" | ./minishell
echo ""
fi
ERROR_CMD="ERROR line $(( $LINE_NUMBER - 1 )), command : "
print_next_command "$ERROR_CMD" "$@" "$CYAN"
# print simple log
echo -e $B_WHITE"\n\n$@\n-----------"$ENDCO >>$BASH_LOG
echo "$bash_execution" >> $BASH_LOG
echo -e $B_WHITE"\n\n$@\n-----------"$ENDCO >>$MINISHELL_LOG
echo "$minishell_execution" >> $MINISHELL_LOG
fi
}