diff --git a/unitests.sh b/unitests.sh index 2d72a2f..b259faf 100644 --- a/unitests.sh +++ b/unitests.sh @@ -49,6 +49,16 @@ cd $(dirname $0) BASH_LOG="$CURRENT_D/logs/bash_log.txt" MINISHELL_LOG="$CURRENT_D/logs/minishell_log.txt" +# check ROOT access : + ROOT=$( whoami ) + if [ "$ROOT" == "root" ] + then + ROOT=1 + else + ROOT=0 + fi + + # default list of files to be use DEFAULT_FILES="$( find $DEFAULT_DIR | tail -n+2 )" DEFAULT_FILES_USAGE="$( find $DEFAULT_DIR_USAGE | tail -n+2 )" @@ -147,7 +157,8 @@ cd $(dirname $0) } # if print option, print next command - function print_next_command +# receive parameters : $1 text presenting command, $2 command itself, $3 color for text + function print_command { text=$1 cmd=$2 @@ -158,64 +169,138 @@ cd $(dirname $0) echo -e $ENDCO } -# function that will launch the command in bash and minishell and compare them - function test_minishell +# compare the execution output and mark and print the success or failure +# receive parameters : $1 bash execution output, $2 minishell execution output + function compare_output { - # execute commands in bash, and logs results - bash_execution=$( echo "$@" | bash -i 2>/dev/null ) - rm -rf $CURRENT_D/tmp/* - minishell_execution=$( echo "$@" | $CURRENT_D/minishell 2>/dev/null ) - rm -rf $CURRENT_D/tmp/* - - # for env, special treatment - bash_execution="$( echo "$bash_execution" | sort | grep -v -e "LINES=" -e "COLUMNS=" -e "_=" -e"LESSCLOSE=" -e "LESSOPEN=" )" - minishell_execution="$( echo "$minishell_execution" | sort | grep -v -e "LINES=" -e "COLUMNS=" -e "_=" -e"LESSCLOSE=" -e "LESSOPEN=" )" - - #compare output - if [ "$bash_execution" = "$minishell_execution" ] + bash_output="$1" + minishell_output="$2" + if [ "$bash_output" = "$minishell_output" ] 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 + # print failed commands in case of option -p if [ $PRINT -eq 1 ] then failure_cmd="FAILURE line $(( $LINE_NUMBER - 1 )), command : " - print_next_command "$failure_cmd" "$@" "$MAGENTA" + print_command "$failure_cmd" "$@" "$MAGENTA" fi # print simple log echo -e $B_WHITE"\n\n$@\n-----------"$ENDCO >>$BASH_LOG - echo "$bash_execution" >> $BASH_LOG + echo "$bash_output" >> $BASH_LOG echo -e $B_WHITE"\n\n$@\n-----------"$ENDCO >>$MINISHELL_LOG - echo "$minishell_execution" >> $MINISHELL_LOG + echo "$minishell_output" >> $MINISHELL_LOG fi } +# WIP +# function that send the command to fd-0 + function send_command + { + # find pid of programm (in a loop in case it didn't start already) + PID="" + while [ -z "$PID" ] + do + PID="$( pidof minishell )" + done + + #VAR="\4" + #kill -s INT `pidof minishell` + while read -r line + do + printf "${line}\n" | ./tiocsti >/proc/"$PID"/fd/0 + #printf "$VAR" | ./tiocsti >/proc/"$PID"/fd/0 + done < <(echo "$CONCAT") + + # kill the process when programm is done + FINISH=0 + while [ ! "$FINISH" -eq 1 ] + do + kill -0 "$PID" 2>/dev/null + FINISH=$( echo $? ) + done + } + +# function test minishell in script mode, it cannot handle signals and ctrl-d + function test_minishell_interactif + { + # in this order, so that it's not the minishell or bash that goes in background + bash_execution=$( send_command & bash 2>/dev/null ) + rm -rf $CURRENT_D/tmp/* + minishell_execution=$( send_command & $CURRENT_D/minishell 2>/dev/null ) + rm -rf $CURRENT_D/tmp/* + } + +# function test minishell in script mode, it cannot handle signals and ctrl-d +# receive parameters : $1 commands to execute, $2 name of the file + 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 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 + { + commands="$1" + in_file="$2" + bash_execution="" + minishell_execution="" + + # execute commands, and logs results + if [ "$ROOT" -eq 1 ] + then + test_minishell_interactif + else + test_minishell_script + fi + + # for env, special treatment + if [ "${in_file: -6}" == "env.sh" ] + then + bash_execution="$( echo "$bash_execution" | sort | grep -v -e "LINES=" -e "COLUMNS=" -e "_=" -e"LESSCLOSE=" -e "LESSOPEN=" )" + minishell_execution="$( echo "$minishell_execution" | sort | grep -v -e "LINES=" -e "COLUMNS=" -e "_=" -e"LESSCLOSE=" -e "LESSOPEN=" )" + fi + + compare_output "$bash_execution" "$minishell_execution" + } + # function to print the results -# receive parameters : file name, numerator, denominator +# receive parameters : $1 file name, $2 numerator, $3 denominator function print_results { - echo -en $B_WHITE"results for $1 : "$ENDCO - if [ $2 -eq 0 ] + the_file="$1" + successful_test="$2" + num_of_test="$3" + echo -en $B_WHITE"results for "$the_file" : "$ENDCO + if [ "$successful_test" -eq 0 ] then - echo -en $B_RED $2 $ENDCO - elif [ $2 -eq $3 ] + if [ "$num_of_test" -eq 0 ] + then + echo -en $B_WHITE "$successful_test" $ENDCO + else + echo -en $B_RED "$successful_test" $ENDCO + fi + elif [ "$successful_test" -eq "$num_of_test" ] then - echo -en $B_GREEN $2 $ENDCO + echo -en $B_GREEN "$successful_test" $ENDCO else - echo -en $B_MAGENTA $2 $ENDCO + echo -en $B_MAGENTA "$successful_test" $ENDCO fi - echo -e $B_WHITE"/ $3"$ENDCO + echo -e $B_WHITE"/ "$num_of_test""$ENDCO } # function that read the commands of each file +# receive parameters : $1 file name, $2 last line number function read_commands { + file="$1" + last_line="$2" + command_test="" while read -r line do (( LINE_NUMBER++ )) @@ -233,11 +318,11 @@ cd $(dirname $0) command_test+="$line" fi # if last line of file, execute here or else next loop will loose it - if [ $LINE_NUMBER -eq $last_line_number ] + if [ $LINE_NUMBER -eq $last_line ] then (( UNIT_TEST++ )) (( TOTAL_TEST++ )) - test_minishell "$command_test" + test_minishell "$command_test" "$file" fi fi # execute (concatenated) commands @@ -245,10 +330,10 @@ cd $(dirname $0) then (( UNIT_TEST++ )) (( TOTAL_TEST++ )) - test_minishell "$command_test" + test_minishell "$command_test" "$file" command_test="" fi - done < $filename + done < $file } # the main loop: go through the files and call the compare func on each commands @@ -260,7 +345,6 @@ cd $(dirname $0) do filename=$i echo -e "\n"$B_YELLOW"test file : ${filename##*/}"$ENDCO - command_test="" LINE_NUMBER=0 last_line_number=$(wc -l < $filename) UNIT_TEST=0 @@ -268,7 +352,7 @@ cd $(dirname $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 + read_commands "$filename" "$last_line_number" # '##' print from the right untill... '*/' s slash print_results ${filename##*/} $SUCCESS_TEST $UNIT_TEST done