ajout option log et ignore comments in tests

This commit is contained in:
hugogogo
2021-12-03 11:22:24 +01:00
parent 294a818924
commit 6ac25b11ae
10 changed files with 538 additions and 53 deletions

View File

@@ -6,6 +6,7 @@
## if not found, launch test on default file list
## 2. execute the main for-loop
## it will iterate through the commands of each files
## if a line start with a # it's skipped
## if commands are not separated by empty line, it concat them
## then it calls the compare function "test_minishell" with commands
## and it calls the print function with the results
@@ -38,42 +39,100 @@
ENDCO="\001\e[0m\002"
# copy the executable to current directory
rm -rf ./minishell
cp ../../minishell .
# list of tests files, by default ot in parameters
if [ $# == 0 ]
then
list_files="
vrac.sh
pipes.sh
expensions.sh
redirections.sh
heredocs.sh
exit_status.sh
builtins.sh
"
else
list_files=""
for (( i = 1 ; i <= "$#" ; i++ ))
do
if [ -e "${!i}" ]
then
if [ -n "$list_files" ]
then
list_files+=$'\n'
fi
list_files+="${!i}"
else
echo "${!i} is not a valid file"
fi
done
fi
# globale variables
UNIT_TEST=0
SUCCESS_TEST=0
TOTAL_TEST=0
TOTAL_SUCCESS=0
LOG=0
mkdir -p ./logs
echo "" > ./logs/bash_log.txt
echo "" > ./logs/minishell_log.txt
BASH_LOG="./logs/bash_log.txt"
MINISHELL_LOG="./logs/minishell_log.txt"
# default list of files to be use
default_files="\
vrac.sh
pipes.sh
expensions.sh
redirections.sh
exit_status.sh
builtins.sh"
#heredocs.sh
# 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 " log - log output into logs/<logs_files.txt>"
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 ""
}
# list of tests files, by default ot in parameters
if [ $# == 0 ]
then
list_files="$default_files"
else
if [ "$1" == "help" ]
then
print_usage
else
START=1
if [ "$1" == "log" ]
then
LOG=1
START=2
fi
list_files=""
for (( i = $START ; i <= "$#" ; i++ ))
do
if [ -e "${!i}" ]
then
if [ -n "$list_files" ]
then
list_files+=$'\n'
fi
list_files+="${!i}"
else
echo "${!i} is not a valid file"
fi
done
fi
fi
# function that will launch the command in bash and minishell and compare them
function test_minishell
@@ -81,6 +140,12 @@
# execute commands in bash and minishell
bash_execution=$( echo "$@" | bash 2>/dev/null )
minishell_execution=$( echo "$@" | ./minishell 2>/dev/null )
# if log options, log results
if [ "$LOG" == 1 ]
then
echo "$@" | bash -i &>>$BASH_LOG
echo "$@" | ./minishell &>>$MINISHELL_LOG
fi
#compare output
if [ "$bash_execution" = "$minishell_execution" ]
then
@@ -117,6 +182,7 @@
# 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
for i in $list_files
do
filename=$i
@@ -127,22 +193,26 @@
SUCCESS_TEST=0
while read line
do
if [ "$line" != "" ]
if [ -n "$line" ]
then
if [ "$command_test" == "" ]
# if line start with # skip it
if [ "${line:0:1}" != "#" ]
then
command_test="$line"
else
command_test+=$'\n'
command_test+="$line"
if [ "$command_test" == "" ]
then
command_test="$line"
else
command_test+=$'\n'
command_test+="$line"
fi
if [ "$line" == "$last_line" ]
then
(( UNIT_TEST++ ))
(( TOTAL_TEST++ ))
test_minishell "$command_test"
fi
fi
if [ "$line" == "$last_line" ]
then
(( UNIT_TEST++ ))
(( TOTAL_TEST++ ))
test_minishell "$command_test"
fi
elif [ "$command_test" != "" ]
elif [ -z "$line" ] && [ -n "$command_test" ]
then
(( UNIT_TEST++ ))
(( TOTAL_TEST++ ))