unitest un peu plus propre

This commit is contained in:
hugogogo
2021-12-15 13:12:08 +01:00
parent dd10f34b75
commit 0a8756c200

View File

@@ -1,10 +1,6 @@
#!/bin/bash #!/bin/bash
cd $(dirname $0) cd $(dirname $0)
MINISHELL="../minishell"
TEST_DIR="./tests/"
DEFAULT_DIR="./tests/defaults/"
# COLORS # COLORS
RED="\e[0;31m" RED="\e[0;31m"
GREEN="\e[0;32m" GREEN="\e[0;32m"
@@ -24,24 +20,30 @@ DEFAULT_DIR="./tests/defaults/"
ENDCO="\e[0m" ENDCO="\e[0m"
# copy the executable to current directory
cp $MINISHELL .
# globale variables # globale variables
MINISHELL="../minishell"
TEST_DIR="./tests/"
DEFAULT_DIR="./tests/defaults/"
UNIT_TEST=0 UNIT_TEST=0
SUCCESS_TEST=0 SUCCESS_TEST=0
TOTAL_TEST=0 TOTAL_TEST=0
TOTAL_SUCCESS=0 TOTAL_SUCCESS=0
LINE_NUMBER=0 LINE_NUMBER=0
FILES_BEFORE="$(ls .)" LIST_FILES=""
DEFAULT_FILES=""
ARGS_MAIN=$@
mkdir -p ./logs mkdir -p ./logs
echo "" > ./logs/bash_log.txt echo "" > ./logs/bash_log.txt
echo "" > ./logs/minishell_log.txt echo "" > ./logs/minishell_log.txt
FILES_BEFORE="$(ls .)"
BASH_LOG="./logs/bash_log.txt" BASH_LOG="./logs/bash_log.txt"
MINISHELL_LOG="./logs/minishell_log.txt" MINISHELL_LOG="./logs/minishell_log.txt"
# copy the executable to current directory
cp $MINISHELL .
# default list of files to be use # default list of files to be use
default_files="$( find $DEFAULT_DIR | tail -n+2 )" DEFAULT_FILES="$( find $DEFAULT_DIR | tail -n+2 )"
# print usage # print usage
function print_usage function print_usage
@@ -51,7 +53,7 @@ DEFAULT_DIR="./tests/defaults/"
echo -en $GREEN"\n[help]\n" echo -en $GREEN"\n[help]\n"
echo -en $CYAN"print usage\n" echo -en $CYAN"print usage\n"
echo -en $GREEN"\n[files list ...] if empty, defaults files will be used :\n" echo -en $GREEN"\n[files list ...] if empty, defaults files will be used :\n"
echo -en $CYAN"$default_files" echo -en $CYAN"$DEFAULT_FILES"
echo -en $ENDCO"\n" echo -en $ENDCO"\n"
echo "" echo ""
} }
@@ -59,70 +61,73 @@ DEFAULT_DIR="./tests/defaults/"
# to delete the files created during the script # to delete the files created during the script
function delete_files function delete_files
{ {
FILES_AFTER="$(ls .)" files_after="$(ls .)"
DIFF_FILES="$(comm -3 <(echo "$FILES_BEFORE") <(echo "$FILES_AFTER"))" diff_files="$(comm -3 <(echo "$FILES_BEFORE") <(echo "$files_after"))"
while read -r line_diff while read -r line_diff
do do
rm -rf "$line_diff" rm -rf "$line_diff"
done < <(echo "$DIFF_FILES") done < <(echo "$diff_files")
} }
# check for arguments, like options or files list # check for arguments, like options or files list
# if no file in arguments, default file list is used # if no file in arguments, default file list is used
list_files="$default_files" function parse_arguments
if [ $# -gt 0 ] {
then LIST_FILES="$DEFAULT_FILES"
if [ "$1" == "help" ] if [ $# -gt 0 ]
then then
print_usage if [ "$1" == "help" ]
delete_files then
exit 0 print_usage
else delete_files
list_files="" exit 0
for (( i = 1 ; i <= "$#" ; i++ )) else
do LIST_FILES=""
# the ! is for indirect parameter expansion for (( i = 1 ; i <= "$#" ; i++ ))
# $i expand in integers 1,2,3... do
# $1,$2,$3... expand in arguments of process call # the ! is for indirect parameter expansion
FILE="${!i%.sh}" # $i expand in integers 1,2,3...
FILE="${FILE/%/.sh}" # $1,$2,$3... expand in arguments of process call
if ! [ -e "$FILE" ] file="${!i%.sh}"
then file="${file/%/.sh}"
FILE="${!i/#/$TEST_DIR}" if ! [ -e "$file" ]
FILE="${FILE%.sh}"
FILE="${FILE/%/.sh}"
fi
if ! [ -e "$FILE" ]
then
FILE="${!i/#/$DEFAULT_DIR}"
FILE="${FILE%.sh}"
FILE="${FILE/%/.sh}"
fi
if [ -e "$FILE" ]
then
if [ -n "$list_files" ]
then then
list_files+=$'\n' file="${!i/#/$TEST_DIR}"
file="${file%.sh}"
file="${file/%/.sh}"
fi fi
list_files+="$FILE" if ! [ -e "$file" ]
else then
print_usage file="${!i/#/$DEFAULT_DIR}"
echo " <$FILE> is not a valid file or option, see usage above" file="${file%.sh}"
exit 0 file="${file/%/.sh}"
fi fi
done if [ -e "$file" ]
then
if [ -n "$LIST_FILES" ]
then
LIST_FILES+=$'\n'
fi
LIST_FILES+="$file"
else
print_usage
echo " <$file> is not a valid file or option, see usage above"
exit 0
fi
done
fi
fi fi
fi }
# if print option, print next command # if print option, print next command
function print_next_command function print_next_command
{ {
TEXT=$1 text=$1
CMD=$2 cmd=$2
COLOR=$3 color=$3
INDENT="$(for (( i=1; i<=${#TEXT}; i++ )); do echo -n ' '; done)" indent="$(for (( i=1; i<=${#text}; i++ )); do echo -n ' '; done)"
echo -en $COLOR"$TEXT"$YELLOW echo -en $color"$text"$YELLOW
echo -n "${CMD//$'\n'/$'\n'$INDENT}" echo -n "${cmd//$'\n'/$'\n'$indent}"
echo -e $ENDCO echo -e $ENDCO
} }
@@ -131,7 +136,6 @@ DEFAULT_DIR="./tests/defaults/"
{ {
# execute commands in bash, and logs results # execute commands in bash, and logs results
bash_execution=$( echo "$@" | bash 2>/dev/null ) bash_execution=$( echo "$@" | bash 2>/dev/null )
minishell_execution=$( echo "$@" | ./minishell 2>/dev/null ) minishell_execution=$( echo "$@" | ./minishell 2>/dev/null )
#compare output #compare output
@@ -140,8 +144,8 @@ DEFAULT_DIR="./tests/defaults/"
(( SUCCESS_TEST++ )) (( SUCCESS_TEST++ ))
(( TOTAL_SUCCESS++ )) (( TOTAL_SUCCESS++ ))
else else
ERROR_CMD="ERROR line $(( $LINE_NUMBER - 1 )), command : " error_cmd="ERROR line $(( $LINE_NUMBER - 1 )), command : "
print_next_command "$ERROR_CMD" "$@" "$CYAN" print_next_command "$error_cmd" "$@" "$CYAN"
# print simple log # print simple log
echo -e $B_WHITE"\n\n$@\n-----------"$ENDCO >>$BASH_LOG echo -e $B_WHITE"\n\n$@\n-----------"$ENDCO >>$BASH_LOG
echo "$bash_execution" >> $BASH_LOG echo "$bash_execution" >> $BASH_LOG
@@ -167,25 +171,13 @@ DEFAULT_DIR="./tests/defaults/"
echo -e $B_WHITE"/ $3"$ENDCO echo -e $B_WHITE"/ $3"$ENDCO
} }
# the main loop: go through the files and call the compare func on each commands # function that read the commands of each file
# parse tests files line by line, grouping lines non-separated by an empty line function read_commands
# if line start with # it's ignore {
for i in $list_files
do
filename=$i
echo -e "\n"$B_YELLOW"test file : $i"$ENDCO
command_test=""
LINE_NUMBER=0
LAST_LINE_NUMBER=$(wc -l < $filename)
UNIT_TEST=0
SUCCESS_TEST=0
# write name of file in log files
echo -en $B_YELLOW "\n\n\nfile: $filename\n" $ENDCO >>$BASH_LOG
echo -en $B_YELLOW "\n\n\nfile: $filename\n" $ENDCO >>$MINISHELL_LOG
# do the loop
while read -r line while read -r line
do do
(( LINE_NUMBER++ )) (( LINE_NUMBER++ ))
# concatenate the commands
if [ -n "$line" ] if [ -n "$line" ]
then then
# if line start with # skip it # if line start with # skip it
@@ -198,13 +190,15 @@ DEFAULT_DIR="./tests/defaults/"
command_test+=$'\n' command_test+=$'\n'
command_test+="$line" command_test+="$line"
fi fi
if [ $LINE_NUMBER -eq $LAST_LINE_NUMBER ] # if last line of file, execute here or else next loop will loose it
if [ $LINE_NUMBER -eq $last_line_number ]
then then
(( UNIT_TEST++ )) (( UNIT_TEST++ ))
(( TOTAL_TEST++ )) (( TOTAL_TEST++ ))
test_minishell "$command_test" test_minishell "$command_test"
fi fi
fi fi
# execute (concatenated) commands
elif [ -z "$line" ] && [ -n "$command_test" ] elif [ -z "$line" ] && [ -n "$command_test" ]
then then
(( UNIT_TEST++ )) (( UNIT_TEST++ ))
@@ -213,27 +207,57 @@ DEFAULT_DIR="./tests/defaults/"
command_test="" command_test=""
fi fi
done < $filename done < $filename
print_results $i $SUCCESS_TEST $UNIT_TEST }
done
# the main loop: go through the files and call the compare func on each commands
# parse tests files line by line, grouping lines non-separated by an empty line
# if line start with # it's ignore
function read_files
{
for i in $LIST_FILES
do
filename=$i
echo -e "\n"$B_YELLOW"test file : $i"$ENDCO
command_test=""
LINE_NUMBER=0
last_line_number=$(wc -l < $filename)
UNIT_TEST=0
SUCCESS_TEST=0
# write name of file in log files
echo -en $B_YELLOW "\n\n\nfile: $filename\n" $ENDCO >>$BASH_LOG
echo -en $B_YELLOW "\n\n\nfile: $filename\n" $ENDCO >>$MINISHELL_LOG
read_commands
print_results $i $SUCCESS_TEST $UNIT_TEST
done
}
# print the total results # print the total results
if [ -n "$list_files" ] function print_total_result
then {
echo "" if [ -n "$LIST_FILES" ]
print_results "all" $TOTAL_SUCCESS $TOTAL_TEST then
fi echo ""
print_results "all" $TOTAL_SUCCESS $TOTAL_TEST
fi
}
# ask to show the diff # ask to show the diff
# -rsn1 will stop read after first key pressed # -rsn1 will stop read after first key pressed
function show_diff
if [ $TOTAL_SUCCESS -lt $TOTAL_TEST ] {
then if [ $TOTAL_SUCCESS -lt $TOTAL_TEST ]
read -rsn1 -p 'if you want to see the diff, press "y"' DIFF
if [[ "${DIFF,,}" == y ]]
then then
diff -y --width=100 --color=always "$BASH_LOG" "$MINISHELL_LOG" read -rsn1 -p 'if you want to see the diff, press "y"' DIFF
if [[ "${DIFF,,}" == y ]]
then
diff -y --width=100 --color=always "$BASH_LOG" "$MINISHELL_LOG"
fi
fi fi
fi }
# delete files created during tests # execute script :
parse_arguments $ARGS_MAIN
read_files
print_total_result
show_diff
delete_files delete_files