diff --git a/unitests.sh b/unitests.sh new file mode 100644 index 0000000..2588016 --- /dev/null +++ b/unitests.sh @@ -0,0 +1,277 @@ +#!/bin/bash +cd $(dirname $0) + +# COLORS + RED="\e[0;31m" + GREEN="\e[0;32m" + YELLOW="\e[0;33m" + BLUE="\e[0;34m" + MAGENTA="\e[0;35m" + CYAN="\e[0;36m" + WHITE="\e[0;37m" + + B_RED="\e[1;31m" + B_GREEN="\e[1;32m" + B_YELLOW="\e[1;33m" + B_BLUE="\e[1;34m" + B_MAGENTA="\e[1;35m" + B_CYAN="\e[1;36m" + B_WHITE="\e[1;37m" + + ENDCO="\e[0m" + +# copy the executable to current directory + cp ../../minishell . + +# globale variables + UNIT_TEST=0 + SUCCESS_TEST=0 + TOTAL_TEST=0 + TOTAL_SUCCESS=0 + LINE_NUMBER=0 + OPTION=0 + PRINT=0 + mkdir -p ./logs + echo "" > ./logs/bash_log.txt + echo "" > ./logs/minishell_log.txt + BASH_LOG="./logs/bash_log.txt" + MINISHELL_LOG="./logs/minishell_log.txt" + +# default list of files to be use + default_files="\ + ut_builtins.sh + ut_exit_status.sh + ut_expensions.sh + ut_heredocs.sh + ut_pipes.sh + ut_redirections.sh + ut_vrac.sh" + +# print usage + function print_usage + { + echo -en "$GREEN" + echo " usage :" + echo -en "$ENDCO" + echo -en "$CYAN" + echo " > bash test_unit.sh [options] [list files...]" + echo -en "$ENDCO" + echo "" + echo -en "$GREEN" + echo " if no files are given in parameters, the defaults will be used" + echo " defaults files :" + echo -en "$ENDCO" + echo -en "$CYAN" + echo "$default_files" + echo -en "$ENDCO" + echo "" + echo -en "$GREEN" + echo " options :" + echo -en "$ENDCO" + echo -en "$CYAN" + echo " help - print usage" + echo " -p1 - prints errors" + echo " -p2 - prints errors and commands" + echo " -p3 - prints errors and commands and errors output" + echo -en "$ENDCO" + echo "" + echo -en "$GREEN" + echo " tests files are formated with the following rules :" + echo -en "$ENDCO" + echo -en "$CYAN" + echo " - a line starting with # is skipped" + echo " - an empty line separate two sets of commands" + echo " - other lines are commands" + echo " - multiples lines in a row are executed as following commands" + echo -en "$ENDCO" + echo "" + } + +# 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 + START=1 + if [ "$1" == "help" ] + then + print_usage + START=0 + elif [ "$1" == "-p1" ] + then + PRINT=1 + OPTION=1 + elif [ "$1" == "-p2" ] + then + PRINT=2 + OPTION=1 + elif [ "$1" == "-p3" ] + then + PRINT=3 + OPTION=1 + fi + if [ $OPTION -eq 1 ] + then + if [ $# -eq 1 ] + then + START=0 + else + START=2 + fi + fi + if [ $START -gt 0 ] + then + list_files="" + for (( i = $START ; i <= "$#" ; i++ )) + do + if [ -e "${!i}" ] + then + if [ -n "$list_files" ] + then + list_files+=$'\n' + fi + list_files+="${!i}" + else + print_usage + echo " \"${!i}\" is not a valid file or option, see usage above" + fi + done + 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}" + echo -e $ENDCO + } + +# function that will launch the command in bash and minishell and compare them + function test_minishell + { + # if -p2|3, print which command is about to be executed + if [ $PRINT -gt 1 ] + then + NEXT_CMD="command line $(( $LINE_NUMBER - 1 )) : " + print_next_command "$NEXT_CMD" "$@" "$CYAN" + fi + # execute commands in bash, and logs results + bash_execution=$( echo "$@" | bash 2>/dev/null ) + echo -e $B_WHITE"\n\n$@\n-----------"$ENDCO >>$BASH_LOG + echo "$bash_execution" >> $BASH_LOG + + minishell_execution=$( echo "$@" | ./minishell 2>/dev/null ) + echo -e $B_WHITE"\n\n$@\n-----------"$ENDCO >>$MINISHELL_LOG + echo "$minishell_execution" >> $MINISHELL_LOG + + #compare output + if [ "$bash_execution" = "$minishell_execution" ] + then + (( SUCCESS_TEST++ )) + (( TOTAL_SUCCESS++ )) + else + # if -p1..3 print error for command + if [ $PRINT -eq 1 ] + then + ERROR_CMD="ERROR line $(( $LINE_NUMBER - 1 )), command : " + print_next_command "$ERROR_CMD" "$@" "$B_RED" + elif [ $PRINT -eq 2 ] + then + echo -e $B_RED" "'\'" ERROR"$ENDCO + elif [ $PRINT -eq 3 ] + then + echo -e $B_RED" "'\'" ERROR"$ENDCO + echo -e $B_MAGENTA"[bash execution] :"$ENDCO + echo "$@" | bash -i + echo "" + echo -e $B_MAGENTA"[minishell execution] :"$ENDCO + echo "$@" | ./minishell + echo "" + fi + fi + } + +# function to print the results +# receive parameters : file name, numerator, denominator + function print_results + { + echo -en $B_WHITE"results for $1 : "$ENDCO + if [ $2 -eq 0 ] + then + echo -en $B_RED $2 $ENDCO + elif [ $2 -eq $3 ] + then + echo -en $B_GREEN $2 $ENDCO + else + echo -en $B_MAGENTA $2 $ENDCO + fi + 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 + while read line + do + (( LINE_NUMBER++ )) + if [ -n "$line" ] + then + # if line start with # skip it + if [ "${line:0:1}" != "#" ] + then + if [ "$command_test" == "" ] + then + command_test="$line" + else + command_test+=$'\n' + command_test+="$line" + fi + if [ $LINE_NUMBER -eq $LAST_LINE_NUMBER ] + then + (( UNIT_TEST++ )) + (( TOTAL_TEST++ )) + test_minishell "$command_test" + fi + fi + elif [ -z "$line" ] && [ -n "$command_test" ] + then + (( UNIT_TEST++ )) + (( TOTAL_TEST++ )) + test_minishell "$command_test" + command_test="" + fi + done < $filename + 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 + +# ask to show the diff + read -p 'you want to see the diff ? (y?)' DIFF + if [[ "${DIFF,,}" =~ y|yes ]] + then + diff -y --width=100 --color=always "$BASH_LOG" "$MINISHELL_LOG" + fi diff --git a/ut_bonus.sh b/ut_bonus.sh new file mode 100644 index 0000000..f71d00b --- /dev/null +++ b/ut_bonus.sh @@ -0,0 +1,11 @@ +echo bonjour ; | + +echo bonjour |; + +; + +echo coucou | ; + +echo ;; + +echo bonjour \; ls diff --git a/ut_builtins.sh b/ut_builtins.sh new file mode 100644 index 0000000..e9827e6 --- /dev/null +++ b/ut_builtins.sh @@ -0,0 +1,108 @@ +# +# ENV +# + +env + +# +# EXPORT +# + +export var + +export var=test + +export var ="cat Makefile | grep >" + +export "test=ici"=coucou + +export LOL=lala ROR=rara + +export "HI= hi" + +export "HI =hi" + +export +env +# display is different for both commands + +env +export +env + +# +# UNSET +# + +export BLOU= +env +unset BLOU +env + +unset LOL ROR +env + +unset blablabla +env + +export "" +env +unset "" +env + +# +# ECHO +# + +echo + +echo simple + +echo -n simple + +echo -n -n -nnnn -nnnnm + +# +# PWD +# + +pwd a + +pwd a b c d + +# +# CD +# + +cd +pwd + +cd . +pwd + +cd ~ +pwd + +cd / +pwd + +cd no_file +pwd + +cd a b c d +pwd + +cd $HOME/Documents +pwd + +cd ../../../../../.. +pwd + +# +# EXIT +# + +exit -10 + +exit +10 + diff --git a/ut_exit_status.sh b/ut_exit_status.sh new file mode 100644 index 0000000..9a2968f --- /dev/null +++ b/ut_exit_status.sh @@ -0,0 +1,17 @@ +echo $? + +cat Makefile | grep pr | head -n 5 | cd test (mybin) +$? + +cat Makefile | grep pr | head -n 5 | cat test (bin) +$? + +cat Makefile | grep pr | head -n 5 | hello (NA) +$? + +cat diufosgid +$? + +exit +$? + diff --git a/ut_expensions.sh b/ut_expensions.sh new file mode 100644 index 0000000..20c3409 --- /dev/null +++ b/ut_expensions.sh @@ -0,0 +1,52 @@ +echo "bip | bip ; coyotte > < \" " + +export vat=at +c$var Makefile + +$blablabla + +$LESS$VAR + +echo '\' + +echo "\" + +echo "\\" + +echo "\n \n \n" + +echo "\n \\n \\\n" + +echo hi";" hihi + +echo hi " ; " hihi + +echo "\s" + +echo "\\s" + +echo \> + +echo "$HOME" + +echo '$HOME' + +echo \$HOME + +export A="s -la" +l$A + +export A=p +export B=w +$A"$B"d +"$A"'$B'd + +echo "ls -la b" + +echo ok "" ok + +echo ok "" "" "" "" "" "" "" "" "" ok + +export OK="ok ok" +echo $OK + diff --git a/ut_heredocs.sh b/ut_heredocs.sh new file mode 100644 index 0000000..69e931f --- /dev/null +++ b/ut_heredocs.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +#cat << EOF +#hello +#EOF + diff --git a/ut_pipes.sh b/ut_pipes.sh new file mode 100644 index 0000000..cd17612 --- /dev/null +++ b/ut_pipes.sh @@ -0,0 +1,48 @@ +echo bonjour ; | + +echo bonjour | | + +| + +echo bonjour |; + +cat Makefile | grep pr | head -n 5 | cd test (mybin) + +cat Makefile | grep pr | head -n 5 | cat test (bin) + +cat Makefile | grep pr | head -n 5 | hello (NA) + +cat | cat | cat | ls + +echo hudifg d | | hugdfihd + +echo | + +| echo + +sort | ls + +exit 0 | exit 1 + +exit 1 | exit 0 + +cat < Makefile | grep gcc > output +ls +rm output + +rm output + +sleep 5 | exit + +ls | cat + +cat | ls + +ulimit -n + +ls /proc/self/fd + +ls | ls | ls /proc/self/fd + +ls | ls | ls | ls | ls | ls /proc/self/fd + diff --git a/ut_redirections.sh b/ut_redirections.sh new file mode 100644 index 0000000..74f3818 --- /dev/null +++ b/ut_redirections.sh @@ -0,0 +1,100 @@ +echo bonjour > $test +ls +rm '$test' + +> log echo coucou +ls +rm log + +cat < > + +cat < < + +cat > > + +> a ls > b < Makefile +ls +rm a b + +echo > a Hello World! +ls +rm a + +> a echo Hello World! +ls +rm a + +echo bonjour > test\ 1 +ls +rm test + +echo bonjour >>> test + +echo bonjour > > out +ls +rm out + +echo 2 >> out1 > out2 +ls +rm out1 out2 + +echo 2 > out1 >> out2 +ls +rm out1 out2 + +cat < blablabla + +echo test > file test1 +ls +rm file + +not_cmd bonjour > salut +ls +rm salut + +>testt +ls + +rm testt +>>testt +ls + +rm testt +c >d + +echo hello >a +echo byebye >b +cat c >d +cat a +cat b +cat c +cat d +rm a b c d + + +cat a | < b cat | cat > c | cat +ls +rm a b c + +echo hello >a +echo byebye >b +echo ouf >c +cat a | < b cat | cat > c | cat +ls +cat a +cat b +cat c +rm a b c + diff --git a/ut_to_do_manually.sh b/ut_to_do_manually.sh new file mode 100644 index 0000000..cdfab63 --- /dev/null +++ b/ut_to_do_manually.sh @@ -0,0 +1,11 @@ + +cat /dev/random | head -n 1 | cat -e + +ctrl-C . 130 sur bin(ex : sleep 10)&line vide + +ctrl-\ .131 sur bin + +/bin/ls +write something the press ctrl+c +write something then press ctrl+d +write something then press ctrl+\ diff --git a/ut_vrac.sh b/ut_vrac.sh new file mode 100644 index 0000000..b6e3588 --- /dev/null +++ b/ut_vrac.sh @@ -0,0 +1,32 @@ +$ + +file_name_in_current_dir + +/bin/echo bonjour + +not_cmd + +echo bonjour > $test w/ t +ls +rm '$test' + +"exit retour a la ligne" + +minishell +# binary not in path without "./" before + +l^Ds + +touch testt +chmod 000 testt +ls +./testt +rm -rf testt + +mkdir testt +./testt +pwd +cd ../ +rm -rf testt + +cat < ../