From f81f480746546e31da15a1ae79b915969dcfc298 Mon Sep 17 00:00:00 2001 From: Hugo LAMY Date: Wed, 22 Dec 2021 22:30:23 +0100 Subject: [PATCH] ajout tests valgrind, option -o --- .valgrindrc | 4 ++ unitests.sh | 105 +++++++++++++++++++++++++++-------------- valgrind_readline.supp | 13 +++++ 3 files changed, 86 insertions(+), 36 deletions(-) create mode 100644 .valgrindrc create mode 100644 valgrind_readline.supp diff --git a/.valgrindrc b/.valgrindrc new file mode 100644 index 0000000..b75e382 --- /dev/null +++ b/.valgrindrc @@ -0,0 +1,4 @@ +--suppressions=./valgrind_readline.supp +--leak-check=full +--show-reachable=yes +--track-fds=yes diff --git a/unitests.sh b/unitests.sh index 48a2468..aa3a7c9 100644 --- a/unitests.sh +++ b/unitests.sh @@ -21,9 +21,9 @@ cd $(dirname $0) ENDCO="\e[0m" # copy the executable to current directory - MINISHELL="../minishell" - make -C ../ &>/dev/null - cp $MINISHELL . + MINISHELL_DIR="../" + make -C $MINISHELL_DIR &>/dev/null + cp $MINISHELL_DIR/minishell . # globale variables CURRENT_D="$(pwd)" @@ -38,15 +38,20 @@ cd $(dirname $0) TOTAL_TEST=0 TOTAL_SUCCESS=0 LINE_NUMBER=0 - PRINT=0 - SCRIPT=0 + CMD_NBR=0 + # options + ERROR=0 + VALGR=0 + OMMIT=0 LIST_FILES="" DEFAULT_FILES="" DEFAULT_FILES_USAGE="" + VALGRIND_OUTPUT_REF="" ARGS_MAIN=$@ mkdir -p ./logs echo "" > ./logs/bash_log.txt echo "" > ./logs/minishell_log.txt + echo "" > ./logs/valgrind.log BASH_LOG="$CURRENT_D/logs/bash_log.txt" MINISHELL_LOG="$CURRENT_D/logs/minishell_log.txt" @@ -86,8 +91,9 @@ cd $(dirname $0) echo -en $CYAN"sudo 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 $CYAN" -s : (only in non-sudo) test in script, not interactif\n" + echo -en $CYAN" -e : print tests commands\n" + echo -en $CYAN" -v : test valgrin, don't compare with bash\n" + echo -en $CYAN" -o : ommit following files\n" echo -en $GREEN"\n[files list ...] if empty, defaults files will be used :\n" echo -en $CYAN"$DEFAULT_FILES_USAGE" echo -en $ENDCO"\n" @@ -131,31 +137,40 @@ cd $(dirname $0) # $i expand in integers 1,2,3... # $1,$2,$3... expand in arguments of process call file="${!i}" - if [ "$file" = "-p" ] + if [ "$file" = "-e" ] then - PRINT=1 + ERROR=1 if [ $# -eq 1 ] then LIST_FILES="$DEFAULT_FILES" break fi - elif [ "$file" = "-s" ] + elif [ "$file" = "-v" ] then - SCRIPT=1 + VALGR=1 if [ $# -eq 1 ] then LIST_FILES="$DEFAULT_FILES" break fi + elif [ "$file" = "-o" ] + then + OMMIT=1 + LIST_FILES="$DEFAULT_FILES" else find_path if [ -e "$file" ] then - if [ -n "$LIST_FILES" ] + if [ $OMMIT -eq 1 ] then - LIST_FILES+=$'\n' + LIST_FILES="$( echo "$LIST_FILES" | grep -v "$file")" + else + if [ -n "$LIST_FILES" ] + then + LIST_FILES+=$'\n' + fi + LIST_FILES+="$file" fi - LIST_FILES+="$file" else print_usage echo " <$file> is not a valid file or option, see usage above" @@ -192,8 +207,8 @@ cd $(dirname $0) (( SUCCESS_TEST++ )) (( TOTAL_SUCCESS++ )) else - # print failed commands in case of option -p - if [ $PRINT -eq 1 ] + # print failed commands in case of option -e + if [ $ERROR -eq 1 ] && [ $VALGR -eq 0 ] then failure_cmd="FAILURE line $(( $LINE_NUMBER - 1 )), command : " print_command "$failure_cmd" "$3" "$MAGENTA" @@ -234,7 +249,7 @@ cd $(dirname $0) done } -# function test minishell in interactive mode and handle signals and ctrl-d +# function test minishell in classic script mode and handle signals and ctrl-d function test_minishell_signals { # in this order, so that it's not the minishell or bash that goes in background @@ -244,17 +259,8 @@ cd $(dirname $0) rm -rf $CURRENT_D/tmp/* } -# function test minishell in script mode +# function test minishell in classic script mode, but cannot handle signals and ctrl-d function test_minishell_script - { - bash_execution=$( echo "$commands" | bash 2>/dev/null ) - rm -rf $CURRENT_D/tmp/* - minishell_execution=$( echo "$commands" | $CURRENT_D/minishell 2>/dev/null ) - rm -rf $CURRENT_D/tmp/* - } - -# function test minishell in interactiv mode, but cannot handle signals and ctrl-d - function test_minishell_normal { bash_execution=$( bash <<<"$commands" 2>/dev/null ) rm -rf $CURRENT_D/tmp/* @@ -262,6 +268,24 @@ cd $(dirname $0) rm -rf $CURRENT_D/tmp/* } +# function test minishell in classic script mode, but cannot handle signals and ctrl-d + function test_minishell_valgrind + { + valgrind="valgrind --suppressions=../valgrind_readline.supp --leak-check=full --show-reachable=yes --track-fds=yes" + minishell_execution=$( $valgrind $CURRENT_D/minishell <<<"$commands" 2>../logs/valgrind.log ) + valgrind_output="$( cat ../logs/valgrind.log | grep -e "ERROR SUMMARY: " -e "definitely lost: " -e "indirectly lost: " -e "possibly lost: " -e "still reachable: " -e "FILE DESCRIPTORS: " )" + valgrind_output="$( cut -c 13- <<< "$valgrind_output" )" + + valgrind_error="$( echo "$valgrind_output" | grep [1-9] )" + if [ -n "$valgrind_error" ] + then + echo -e $B_RED"line: $LINE_NUMBER, command: "$YELLOW$commands$ENDCO + echo "$valgrind_output" + fi + + rm -rf $CURRENT_D/tmp/* + } + # function that will launch the command in bash and minishell and compare them # receive parameters : $1 commands to execute, $2 name of the file function test_minishell @@ -272,14 +296,14 @@ cd $(dirname $0) minishell_execution="" # execute commands, and logs results - if [ "$ROOT" -eq 1 ] + if [ $ROOT -eq 1 ] then test_minishell_signals - elif [ "$SCRIPT" -eq 1 ] + elif [ $VALGR -eq 1 ] then - test_minishell_script + test_minishell_valgrind else - test_minishell_normal + test_minishell_script fi # for env, special treatment @@ -289,7 +313,10 @@ cd $(dirname $0) minishell_execution="$( echo "$minishell_execution" | sort | grep -v -e "LINES=" -e "COLUMNS=" -e "_=" -e"LESSCLOSE=" -e "LESSOPEN=" -e "SHLVL=" )" fi - compare_output "$bash_execution" "$minishell_execution" "$commands" + if [ "$VALGR" -eq 0 ] + then + compare_output "$bash_execution" "$minishell_execution" "$commands" + fi } # function to print the results @@ -377,7 +404,10 @@ cd $(dirname $0) echo -en $B_YELLOW "\n\n\nfile: ${filename##*/}\n" $ENDCO >>$MINISHELL_LOG read_commands "$filename" "$last_line_number" # '##' print from the right untill... '*/' s slash - print_results ${filename##*/} $SUCCESS_TEST $UNIT_TEST + if [ "$VALGR" -eq 0 ] + then + print_results ${filename##*/} $SUCCESS_TEST $UNIT_TEST + fi done } @@ -407,8 +437,11 @@ cd $(dirname $0) # execute script : parse_arguments $ARGS_MAIN - read_files - print_total_result - show_diff + read_files + if [ "$VALGR" -eq 0 ] + then + print_total_result + show_diff + fi cd $CURRENT_D rm -r $CURRENT_D/tmp/ diff --git a/valgrind_readline.supp b/valgrind_readline.supp new file mode 100644 index 0000000..f1f3bc4 --- /dev/null +++ b/valgrind_readline.supp @@ -0,0 +1,13 @@ +{ + + Memcheck:Leak + ... + fun:readline +} + +{ + + Memcheck:Leak + ... + fun:add_history +}