From 687b1d86313789f2ab0c48ddcf992c999ee58202 Mon Sep 17 00:00:00 2001 From: hugogogo Date: Fri, 3 Dec 2021 07:00:39 +0100 Subject: [PATCH] tests heredocs --- tests/unit_tests/builtins.sh | 1 + tests/unit_tests/exit_status.sh | 1 + tests/unit_tests/expensions.sh | 1 + tests/unit_tests/file.log | 3 + tests/unit_tests/heredocs.sh | 14 +++ tests/unit_tests/pipes.sh | 1 + tests/unit_tests/redirections.sh | 1 + tests/unit_tests/test1.sh | 6 ++ tests/unit_tests/test2.sh | 5 + tests/unit_tests/test3.sh | 10 ++ tests/unit_tests/test_unit.sh | 161 +++++++++++++++++++++++++++++++ tests/unit_tests/vrac.sh | 1 + 12 files changed, 205 insertions(+) create mode 100644 tests/unit_tests/builtins.sh create mode 100644 tests/unit_tests/exit_status.sh create mode 100644 tests/unit_tests/expensions.sh create mode 100644 tests/unit_tests/file.log create mode 100644 tests/unit_tests/heredocs.sh create mode 100644 tests/unit_tests/pipes.sh create mode 100644 tests/unit_tests/redirections.sh create mode 100644 tests/unit_tests/test1.sh create mode 100644 tests/unit_tests/test2.sh create mode 100644 tests/unit_tests/test3.sh create mode 100644 tests/unit_tests/test_unit.sh create mode 100644 tests/unit_tests/vrac.sh diff --git a/tests/unit_tests/builtins.sh b/tests/unit_tests/builtins.sh new file mode 100644 index 0000000..df7d12a --- /dev/null +++ b/tests/unit_tests/builtins.sh @@ -0,0 +1 @@ +echo "builtins" diff --git a/tests/unit_tests/exit_status.sh b/tests/unit_tests/exit_status.sh new file mode 100644 index 0000000..db1c752 --- /dev/null +++ b/tests/unit_tests/exit_status.sh @@ -0,0 +1 @@ +echo "exit status" diff --git a/tests/unit_tests/expensions.sh b/tests/unit_tests/expensions.sh new file mode 100644 index 0000000..d9caf1c --- /dev/null +++ b/tests/unit_tests/expensions.sh @@ -0,0 +1 @@ +echo "expensions" diff --git a/tests/unit_tests/file.log b/tests/unit_tests/file.log new file mode 100644 index 0000000..ca8f9cf --- /dev/null +++ b/tests/unit_tests/file.log @@ -0,0 +1,3 @@ +hulamy@NoName:/home/simplonco/Nextcloud/informatique/42/minishell/minishell/tests s/unit_tests> echo test +test +hulamy@NoName:/home/simplonco/Nextcloud/informatique/42/minishell/minishell/tests s/unit_tests> exit diff --git a/tests/unit_tests/heredocs.sh b/tests/unit_tests/heredocs.sh new file mode 100644 index 0000000..2a3cd63 --- /dev/null +++ b/tests/unit_tests/heredocs.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +COMMANDE=' echo "hugo"' +# cat << EOF +# hello you +# EOF' + +#echo "$COMMANDE" | bash -i &>file.log +echo "$COMMANDE" | ./minishell &>file.log + +#cat << EOF +#hello +#EOF + diff --git a/tests/unit_tests/pipes.sh b/tests/unit_tests/pipes.sh new file mode 100644 index 0000000..81851a8 --- /dev/null +++ b/tests/unit_tests/pipes.sh @@ -0,0 +1 @@ +echo "pipes" diff --git a/tests/unit_tests/redirections.sh b/tests/unit_tests/redirections.sh new file mode 100644 index 0000000..d933bd8 --- /dev/null +++ b/tests/unit_tests/redirections.sh @@ -0,0 +1 @@ +echo "redirections" diff --git a/tests/unit_tests/test1.sh b/tests/unit_tests/test1.sh new file mode 100644 index 0000000..c40b5f9 --- /dev/null +++ b/tests/unit_tests/test1.sh @@ -0,0 +1,6 @@ +echo hello +echo you + +echo "how are" + +echo "you ?" diff --git a/tests/unit_tests/test2.sh b/tests/unit_tests/test2.sh new file mode 100644 index 0000000..54f7176 --- /dev/null +++ b/tests/unit_tests/test2.sh @@ -0,0 +1,5 @@ +echo "ho !" + +echo "it's you again" + +echo "welcome back" diff --git a/tests/unit_tests/test3.sh b/tests/unit_tests/test3.sh new file mode 100644 index 0000000..8f12d98 --- /dev/null +++ b/tests/unit_tests/test3.sh @@ -0,0 +1,10 @@ +echo "let's" +echo "see what" +echo "you've" +echo "got" + +echo "show" +echo "me" +echo "what" +echo "you" +echo "goooot" diff --git a/tests/unit_tests/test_unit.sh b/tests/unit_tests/test_unit.sh new file mode 100644 index 0000000..acb2f8f --- /dev/null +++ b/tests/unit_tests/test_unit.sh @@ -0,0 +1,161 @@ +#!/bin/bash + +## explanation : +## 1. check for arguments (files for tests) +## if found, check for validity, and launch test on them +## if not found, launch test on default file list +## 2. execute the main for-loop +## it will iterate through the commands of each files +## if commands are not separated by empty line, it concat them +## then it calls the compare function "test_minishell" with commands +## and it calls the print function with the results +## 3. the compare function "test_minishell" +## it takes the commands in argument +## it launch bash and minishell with the commands +## if multiple commands, they are executed in a row before exit the shell +## if error, it prints right away the two outputs +## 4. the print function, print results +## it's use after each files +## and at the end + +# COLORS + RED="\001\e[0;31m\002" + GREEN="\001\e[0;32m\002" + YELLOW="\001\e[0;33m\002" + BLUE="\001\e[0;34m\002" + MAGENTA="\001\e[0;35m\002" + CYAN="\001\e[0;36m\002" + WHITE="\001\e[0;37m\002" + + B_RED="\001\e[1;31m\002" + B_GREEN="\001\e[1;32m\002" + B_YELLOW="\001\e[1;33m\002" + B_BLUE="\001\e[1;34m\002" + B_MAGENTA="\001\e[1;35m\002" + B_CYAN="\001\e[1;36m\002" + B_WHITE="\001\e[1;37m\002" + + ENDCO="\001\e[0m\002" + +# copy the executable to current directory + cp ../../minishell . + +# list of tests files, by default ot in parameters + if [ $# == 0 ] + then + list_files=" + vrac.sh + pipes.sh + expensions.sh + redirections.sh + heredocs.sh + exit_status.sh + builtins.sh + " + else + list_files="" + for (( i = 1 ; i <= "$#" ; i++ )) + do + if [ -e "${!i}" ] + then + if [ -n "$list_files" ] + then + list_files+=$'\n' + fi + list_files+="${!i}" + else + echo "${!i} is not a valid file" + fi + done + fi + +# globale variables + UNIT_TEST=0 + SUCCESS_TEST=0 + TOTAL_TEST=0 + TOTAL_SUCCESS=0 + +# function that will launch the command in bash and minishell and compare them + function test_minishell + { + # execute commands in bash and minishell + bash_execution=$( echo "$@" | bash 2>/dev/null ) + minishell_execution=$( echo "$@" | ./minishell 2>/dev/null ) + #compare output + if [ "$bash_execution" = "$minishell_execution" ] + then + (( SUCCESS_TEST++ )) + (( TOTAL_SUCCESS++ )) + else + echo -e $B_RED"ERROR :"$ENDCO + echo "" + echo -e $B_MAGENTA"[bash execution] :"$ENDCO + echo "$@" | bash -i + echo "" + echo -e $B_MAGENTA"[minishell execution] :"$ENDCO + echo "$@" | ./minishell + echo "" + 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 + for i in $list_files + do + filename=$i + echo -e "\n"$B_YELLOW"test file : $i"$ENDCO + command_test="" + last_line="$(tail -1 $i)" + UNIT_TEST=0 + SUCCESS_TEST=0 + while read line + do + if [ "$line" != "" ] + then + if [ "$command_test" == "" ] + then + command_test="$line" + else + command_test+=$'\n' + command_test+="$line" + fi + if [ "$line" == "$last_line" ] + then + (( UNIT_TEST++ )) + (( TOTAL_TEST++ )) + test_minishell "$command_test" + fi + elif [ "$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 diff --git a/tests/unit_tests/vrac.sh b/tests/unit_tests/vrac.sh new file mode 100644 index 0000000..8769af4 --- /dev/null +++ b/tests/unit_tests/vrac.sh @@ -0,0 +1 @@ +echo "vrac"