unitest un peu plus propre
This commit is contained in:
212
unitests.sh
212
unitests.sh
@@ -1,10 +1,6 @@
|
||||
#!/bin/bash
|
||||
cd $(dirname $0)
|
||||
|
||||
MINISHELL="../minishell"
|
||||
TEST_DIR="./tests/"
|
||||
DEFAULT_DIR="./tests/defaults/"
|
||||
|
||||
# COLORS
|
||||
RED="\e[0;31m"
|
||||
GREEN="\e[0;32m"
|
||||
@@ -24,24 +20,30 @@ DEFAULT_DIR="./tests/defaults/"
|
||||
|
||||
ENDCO="\e[0m"
|
||||
|
||||
# copy the executable to current directory
|
||||
cp $MINISHELL .
|
||||
|
||||
# globale variables
|
||||
MINISHELL="../minishell"
|
||||
TEST_DIR="./tests/"
|
||||
DEFAULT_DIR="./tests/defaults/"
|
||||
UNIT_TEST=0
|
||||
SUCCESS_TEST=0
|
||||
TOTAL_TEST=0
|
||||
TOTAL_SUCCESS=0
|
||||
LINE_NUMBER=0
|
||||
FILES_BEFORE="$(ls .)"
|
||||
LIST_FILES=""
|
||||
DEFAULT_FILES=""
|
||||
ARGS_MAIN=$@
|
||||
mkdir -p ./logs
|
||||
echo "" > ./logs/bash_log.txt
|
||||
echo "" > ./logs/minishell_log.txt
|
||||
FILES_BEFORE="$(ls .)"
|
||||
BASH_LOG="./logs/bash_log.txt"
|
||||
MINISHELL_LOG="./logs/minishell_log.txt"
|
||||
|
||||
# copy the executable to current directory
|
||||
cp $MINISHELL .
|
||||
|
||||
# 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
|
||||
function print_usage
|
||||
@@ -51,7 +53,7 @@ DEFAULT_DIR="./tests/defaults/"
|
||||
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 $CYAN"$DEFAULT_FILES"
|
||||
echo -en $ENDCO"\n"
|
||||
echo ""
|
||||
}
|
||||
@@ -59,70 +61,73 @@ DEFAULT_DIR="./tests/defaults/"
|
||||
# to delete the files created during the script
|
||||
function delete_files
|
||||
{
|
||||
FILES_AFTER="$(ls .)"
|
||||
DIFF_FILES="$(comm -3 <(echo "$FILES_BEFORE") <(echo "$FILES_AFTER"))"
|
||||
files_after="$(ls .)"
|
||||
diff_files="$(comm -3 <(echo "$FILES_BEFORE") <(echo "$files_after"))"
|
||||
while read -r line_diff
|
||||
do
|
||||
rm -rf "$line_diff"
|
||||
done < <(echo "$DIFF_FILES")
|
||||
done < <(echo "$diff_files")
|
||||
}
|
||||
|
||||
# check for arguments, like options or files list
|
||||
# if no file in arguments, default file list is used
|
||||
list_files="$default_files"
|
||||
if [ $# -gt 0 ]
|
||||
then
|
||||
if [ "$1" == "help" ]
|
||||
function parse_arguments
|
||||
{
|
||||
LIST_FILES="$DEFAULT_FILES"
|
||||
if [ $# -gt 0 ]
|
||||
then
|
||||
print_usage
|
||||
delete_files
|
||||
exit 0
|
||||
else
|
||||
list_files=""
|
||||
for (( i = 1 ; i <= "$#" ; i++ ))
|
||||
do
|
||||
# the ! is for indirect parameter expansion
|
||||
# $i expand in integers 1,2,3...
|
||||
# $1,$2,$3... expand in arguments of process call
|
||||
FILE="${!i%.sh}"
|
||||
FILE="${FILE/%/.sh}"
|
||||
if ! [ -e "$FILE" ]
|
||||
then
|
||||
FILE="${!i/#/$TEST_DIR}"
|
||||
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" ]
|
||||
if [ "$1" == "help" ]
|
||||
then
|
||||
print_usage
|
||||
delete_files
|
||||
exit 0
|
||||
else
|
||||
LIST_FILES=""
|
||||
for (( i = 1 ; i <= "$#" ; i++ ))
|
||||
do
|
||||
# the ! is for indirect parameter expansion
|
||||
# $i expand in integers 1,2,3...
|
||||
# $1,$2,$3... expand in arguments of process call
|
||||
file="${!i%.sh}"
|
||||
file="${file/%/.sh}"
|
||||
if ! [ -e "$file" ]
|
||||
then
|
||||
list_files+=$'\n'
|
||||
file="${!i/#/$TEST_DIR}"
|
||||
file="${file%.sh}"
|
||||
file="${file/%/.sh}"
|
||||
fi
|
||||
list_files+="$FILE"
|
||||
else
|
||||
print_usage
|
||||
echo " <$FILE> is not a valid file or option, see usage above"
|
||||
exit 0
|
||||
fi
|
||||
done
|
||||
if ! [ -e "$file" ]
|
||||
then
|
||||
file="${!i/#/$DEFAULT_DIR}"
|
||||
file="${file%.sh}"
|
||||
file="${file/%/.sh}"
|
||||
fi
|
||||
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
|
||||
}
|
||||
|
||||
# if print option, print next command
|
||||
function print_next_command
|
||||
{
|
||||
TEXT=$1
|
||||
CMD=$2
|
||||
COLOR=$3
|
||||
INDENT="$(for (( i=1; i<=${#TEXT}; i++ )); do echo -n ' '; done)"
|
||||
echo -en $COLOR"$TEXT"$YELLOW
|
||||
echo -n "${CMD//$'\n'/$'\n'$INDENT}"
|
||||
text=$1
|
||||
cmd=$2
|
||||
color=$3
|
||||
indent="$(for (( i=1; i<=${#text}; i++ )); do echo -n ' '; done)"
|
||||
echo -en $color"$text"$YELLOW
|
||||
echo -n "${cmd//$'\n'/$'\n'$indent}"
|
||||
echo -e $ENDCO
|
||||
}
|
||||
|
||||
@@ -131,7 +136,6 @@ DEFAULT_DIR="./tests/defaults/"
|
||||
{
|
||||
# execute commands in bash, and logs results
|
||||
bash_execution=$( echo "$@" | bash 2>/dev/null )
|
||||
|
||||
minishell_execution=$( echo "$@" | ./minishell 2>/dev/null )
|
||||
|
||||
#compare output
|
||||
@@ -140,8 +144,8 @@ DEFAULT_DIR="./tests/defaults/"
|
||||
(( SUCCESS_TEST++ ))
|
||||
(( TOTAL_SUCCESS++ ))
|
||||
else
|
||||
ERROR_CMD="ERROR line $(( $LINE_NUMBER - 1 )), command : "
|
||||
print_next_command "$ERROR_CMD" "$@" "$CYAN"
|
||||
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
|
||||
@@ -167,25 +171,13 @@ DEFAULT_DIR="./tests/defaults/"
|
||||
echo -e $B_WHITE"/ $3"$ENDCO
|
||||
}
|
||||
|
||||
# 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
|
||||
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
|
||||
# function that read the commands of each file
|
||||
function read_commands
|
||||
{
|
||||
while read -r line
|
||||
do
|
||||
(( LINE_NUMBER++ ))
|
||||
# concatenate the commands
|
||||
if [ -n "$line" ]
|
||||
then
|
||||
# if line start with # skip it
|
||||
@@ -198,13 +190,15 @@ DEFAULT_DIR="./tests/defaults/"
|
||||
command_test+=$'\n'
|
||||
command_test+="$line"
|
||||
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
|
||||
(( UNIT_TEST++ ))
|
||||
(( TOTAL_TEST++ ))
|
||||
test_minishell "$command_test"
|
||||
fi
|
||||
fi
|
||||
# execute (concatenated) commands
|
||||
elif [ -z "$line" ] && [ -n "$command_test" ]
|
||||
then
|
||||
(( UNIT_TEST++ ))
|
||||
@@ -213,27 +207,57 @@ DEFAULT_DIR="./tests/defaults/"
|
||||
command_test=""
|
||||
fi
|
||||
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
|
||||
if [ -n "$list_files" ]
|
||||
then
|
||||
echo ""
|
||||
print_results "all" $TOTAL_SUCCESS $TOTAL_TEST
|
||||
fi
|
||||
function print_total_result
|
||||
{
|
||||
if [ -n "$LIST_FILES" ]
|
||||
then
|
||||
echo ""
|
||||
print_results "all" $TOTAL_SUCCESS $TOTAL_TEST
|
||||
fi
|
||||
}
|
||||
|
||||
# ask to show the diff
|
||||
# -rsn1 will stop read after first key pressed
|
||||
|
||||
if [ $TOTAL_SUCCESS -lt $TOTAL_TEST ]
|
||||
then
|
||||
read -rsn1 -p 'if you want to see the diff, press "y"' DIFF
|
||||
if [[ "${DIFF,,}" == y ]]
|
||||
function show_diff
|
||||
{
|
||||
if [ $TOTAL_SUCCESS -lt $TOTAL_TEST ]
|
||||
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
|
||||
}
|
||||
|
||||
# delete files created during tests
|
||||
# execute script :
|
||||
parse_arguments $ARGS_MAIN
|
||||
read_files
|
||||
print_total_result
|
||||
show_diff
|
||||
delete_files
|
||||
|
||||
Reference in New Issue
Block a user