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