ajout vpath et execute dans dossier temp et option -p

This commit is contained in:
hugogogo
2021-12-17 20:35:38 +01:00
parent 87510f1e3e
commit 1a90821b1d

View File

@@ -25,39 +25,37 @@ cd $(dirname $0)
cp $MINISHELL .
# globale variables
TEST_DIR="./tests/"
DEFAULT_DIR="./tests/defaults/"
CURRENT_D="$(pwd)"
VPATH=" $CURRENT_D/
$CURRENT_D/tests/
$CURRENT_D/tests/defaults/
"
DEFAULT_DIR="$CURRENT_D/tests/defaults/"
UNIT_TEST=0
SUCCESS_TEST=0
TOTAL_TEST=0
TOTAL_SUCCESS=0
LINE_NUMBER=0
PRINT=0
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"
# FILES_BEFORE="$(ls .)"
BASH_LOG="$CURRENT_D/logs/bash_log.txt"
MINISHELL_LOG="$CURRENT_D/logs/minishell_log.txt"
# to delete the files created during the script
function delete_files
{
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")
}
# move project to a temp file
mkdir -p tmp
cd tmp
# handle sigint signal
function handler_sigint
{
delete_files
cd $CURRENT_D
rm -r $CURRENT_D/tmp/
exit 0
}
trap 'handler_sigint' 2
@@ -68,16 +66,29 @@ cd $(dirname $0)
# print usage
function print_usage
{
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"usage :\n"
echo -en $CYAN"bash unitest.sh [option] [files list ...]\n"
echo -en $GREEN"\n[options]\n"
echo -en $CYAN"help : print usage\n"
echo -en $CYAN" -p : print tests commands\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 ""
}
# function to find the path of a file in argument
function find_path
{
file_ori="$file"
for x in $VPATH
do
file="${file_ori/#/$x}"
file="${file%.sh}"
file="${file/%/.sh}"
done
}
# check for arguments, like options or files list
# if no file in arguments, default file list is used
function parse_arguments
@@ -88,7 +99,8 @@ cd $(dirname $0)
if [ "$1" == "help" ]
then
print_usage
delete_files
cd $CURRENT_D
rm -r $CURRENT_D/tmp/
exit 0
else
LIST_FILES=""
@@ -97,20 +109,13 @@ cd $(dirname $0)
# 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" ]
file="${!i}"
if [ "$file" = "-p" ]
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}"
PRINT=1
continue
fi
find_path
if [ -e "$file" ]
then
if [ -n "$LIST_FILES" ]
@@ -145,18 +150,27 @@ cd $(dirname $0)
{
# execute commands in bash, and logs results
bash_execution=$( echo "$@" | bash -i 2>/dev/null )
delete_files
minishell_execution=$( echo "$@" | ./minishell 2>/dev/null )
delete_files
rm -rf $CURRENT_D/tmp/*
minishell_execution=$( echo "$@" | $CURRENT_D/minishell 2>/dev/null )
rm -rf $CURRENT_D/tmp/*
#compare output
if [ "$bash_execution" = "$minishell_execution" ]
then
(( SUCCESS_TEST++ ))
(( TOTAL_SUCCESS++ ))
# if uncommented, print all commands in case of option -p
# if [ $PRINT -eq 1 ]
# then
# success_cmd="success line $(( $LINE_NUMBER - 1 )), command : "
# print_next_command "$success_cmd" "$@" "$CYAN"
# fi
else
error_cmd="ERROR line $(( $LINE_NUMBER - 1 )), command : "
print_next_command "$error_cmd" "$@" "$CYAN"
if [ $PRINT -eq 1 ]
then
failure_cmd="FAILURE line $(( $LINE_NUMBER - 1 )), command : "
print_next_command "$failure_cmd" "$@" "$MAGENTA"
fi
# print simple log
echo -e $B_WHITE"\n\n$@\n-----------"$ENDCO >>$BASH_LOG
echo "$bash_execution" >> $BASH_LOG
@@ -271,4 +285,5 @@ cd $(dirname $0)
read_files
print_total_result
show_diff
delete_files
cd $CURRENT_D
rm -r $CURRENT_D/tmp/