diff options
Diffstat (limited to '42sh/tests/functional/testsuite.sh')
| -rwxr-xr-x | 42sh/tests/functional/testsuite.sh | 639 |
1 files changed, 639 insertions, 0 deletions
diff --git a/42sh/tests/functional/testsuite.sh b/42sh/tests/functional/testsuite.sh new file mode 100755 index 0000000..a063083 --- /dev/null +++ b/42sh/tests/functional/testsuite.sh @@ -0,0 +1,639 @@ +#!/bin/sh + +REF_OUT="ref.out" +REF_REDIR="ref.red" +REF_ERR="ref.err" +TEST_OUT="test.out" +TEST_REDIR="test.red" +TEST_ERR="test.err" + +score=0 +testcount=0 +failures="" + +# COLORS +RED='\033[0;31m' +GREEN='\033[0;32m' +CYAN='\033[0;36m' +LIGHT_PURPLE='\033[1;35m' +NO_COLOR='\033[0m' + +SEPARATOR='==================================================================' + +debug() +{ + echo "REF stderr (exit code $1) :" + cat "$REF_ERR" + echo + echo "TEST stderr (exit code $2) :" + cat "$TEST_ERR" + echo +} + +testbasic() +{ + echo ${SEPARATOR} + testcount=$((testcount + 1)) + printf "Testcase (%d) ${CYAN}\'%s\'${NO_COLOR}\n" "$testcount" "$1" + bash --posix -c "$1" > "$REF_OUT" 2>"$REF_ERR" + ref_code=$(echo $?) + "$BIN_PATH" -c "$1" > "$TEST_OUT" 2>"$TEST_ERR" + test_code=$(echo $?) + [ "$DEBUG" ] && debug "$ref_code" "$test_code" + [ "$test_code" -eq "$ref_code" ] && diff "$REF_OUT" "$TEST_OUT" && score=$((score + 1)) && printf "${GREEN}Success${NO_COLOR}\n" && return 0 + printf "${RED}Failure${NO_COLOR}\n" && failures="$failures $testcount" +} + +testredir() +{ + echo ${SEPARATOR} + testcount=$((testcount + 1)) + printf "Testcase (%d) ${CYAN}\'%s\'${NO_COLOR}\n" "$testcount" "$1$2$3" + bash --posix -c "$1$2$3" + ref_code=$(echo $?) + cat "$3" > "$REF_OUT" + "$BIN_PATH" -c "$1$2$3" + test_code=$(echo $?) + cat "$3" > "$TEST_OUT" + [ "$test_code" -eq "$ref_code" ] && diff "$REF_OUT" "$TEST_OUT" && score=$((score + 1)) && printf "${GREEN}Success${NO_COLOR}\n" && return 0 + printf "${RED}Failure${NO_COLOR}\n" && failures="$failures $testcount" +} + +testloop() +{ + echo ${SEPARATOR} + testcount=$((testcount + 1)) + printf "Testcase (%d) ${CYAN}\'%s\'${NO_COLOR}\n" "$testcount" "$1" + timeout --preserve-status 1s bash --posix -c "$1" 2>"$REF_ERR" > "$REF_OUT" + ref_code=$(echo $?) + timeout --preserve-status 1s "$BIN_PATH" -c "$1" 2>"$REF_ERR" > "$REF_OUT" + test_code=$(echo $?) + [ "$DEBUG" ] && debug "$ref_code" "$test_code" + [ "$test_code" -eq "$ref_code" ] && diff "$REF_OUT" "$TEST_OUT" > /dev/null && score=$((score + 1)) && printf "${GREEN}Success${NO_COLOR}\n" && return 0 + printf "${RED}Failure${NO_COLOR}\n" && failures="$failures $testcount" +} + +testerror() +{ + echo ${SEPARATOR} + testcount=$((testcount + 1)) + printf "Testcase (%d) ${CYAN}\'%s\'${NO_COLOR}\n" "$testcount" "$1" + timeout --preserve-status 1s bash --posix -c "$1" > "$REF_OUT" 2>"$REF_ERR" + ref_code=$(echo $?) + timeout --preserve-status 1s "$BIN_PATH" -c "$1" > "$TEST_OUT" 2>"$TEST_ERR" + test_code=$(echo $?) + [ "$DEBUG" ] && debug "$ref_code" "$test_code" + [ "$test_code" -eq "$ref_code" ] && score=$((score + 1)) && printf "${GREEN}Success${NO_COLOR}\n" && return 0 + printf "${RED}Failure${NO_COLOR}\n" && failures="$failures $testcount" +} + +testbadusage() +{ + echo ${SEPARATOR} + testcount=$((testcount + 1)) + printf "Testcase (%d) ${CYAN}\'Bad Usage\'${NO_COLOR}\n" "$testcount" + timeout --preserve-status 1s bash --posix -notaflag "$1" > "$REF_OUT" 2>"$REF_ERR" + ref_code=$(echo $?) + timeout --preserve-status 1s "$BIN_PATH" -notaflag "$1" > "$TEST_OUT" 2>"$TEST_ERR" + test_code=$(echo $?) + [ "$DEBUG" ] && debug "$ref_code" "$test_code" + [ "$test_code" -eq "$ref_code" ] && score=$((score + 1)) && printf "${GREEN}Success${NO_COLOR}\n" && return 0 + printf "${RED}Failure${NO_COLOR}\n" && failures="$failures $testcount" +} + +testprettyprint() +{ + echo ${SEPARATOR} + testcount=$((testcount + 1)) + printf "Testcase (%d) ${CYAN}\'%s\'${NO_COLOR}\n" "$testcount" "$1" + bash --posix -c "$1" > "$REF_OUT" 2>"$REF_ERR" + ref_code=$(echo $?) + "$BIN_PATH" --pretty-print -c "$1" > "$TEST_OUT" 2>"$TEST_ERR" + test_code=$(echo $?) + [ "$DEBUG" ] && debug "$ref_code" "$test_code" + [ "$test_code" -eq "$ref_code" ] && diff "$REF_OUT" "$TEST_OUT" && score=$((score + 1)) && printf "${GREEN}Success${NO_COLOR}\n" && return 0 + printf "${RED}Failure${NO_COLOR}\n" && failures="$failures $testcount" +} + +testasscript() +{ + echo ${SEPARATOR} + testcount=$((testcount + 1)) + printf "Testcase (as script) (%d) ${CYAN}\'%s\'${NO_COLOR}\n" "$testcount" "$1" + echo "$1" > 'as_script.sh' + bash --posix 'as_script.sh' eee.sh > "$REF_OUT" 2>"$REF_ERR" + ref_code=$(echo $?) + "$BIN_PATH" 'as_script.sh' eee.sh > "$TEST_OUT" 2>"$TEST_ERR" + test_code=$(echo $?) + [ "$DEBUG" ] && debug "$ref_code" "$test_code" + [ "$test_code" -eq "$ref_code" ] && diff "$REF_OUT" "$TEST_OUT" && score=$((score + 1)) && printf "${GREEN}Success${NO_COLOR}\n" && return 0 + printf "${RED}Failure${NO_COLOR}\n" && failures="$failures $testcount" +} + +testasargument() +{ + echo ${SEPARATOR} + testcount=$((testcount + 1)) + printf "Testcase (as script) (%d) ${CYAN}\'%s\'${NO_COLOR}\n" "$testcount" "$1" + echo "$1" > 'as_script.sh' + bash --posix < 'as_script.sh' eee.sh > "$REF_OUT" 2>"$REF_ERR" + ref_code=$(echo $?) + "$BIN_PATH" < 'as_script.sh' eee.sh > "$TEST_OUT" 2>"$TEST_ERR" + test_code=$(echo $?) + [ "$DEBUG" ] && debug "$ref_code" "$test_code" + [ "$test_code" -eq "$ref_code" ] && diff "$REF_OUT" "$TEST_OUT" && score=$((score + 1)) && printf "${GREEN}Success${NO_COLOR}\n" && return 0 + printf "${RED}Failure${NO_COLOR}\n" && failures="$failures $testcount" +} + +print_header() +{ + printf "\n" + printf "${LIGHT_PURPLE}>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n" + printf "%s\n" "$1" + printf "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<${NO_COLOR}\n" +} + +clean() +{ + rm "$REF_OUT" "$TEST_OUT" "$TEST_ERR" "$REF_ERR" "$TEST_REDIR" "22" "as_script.sh" "le_test.oui" +} + +### Valid tests +VALID_TESTS="Valid tests" +print_header ${VALID_TESTS} + +## Builtins +# true +testbasic 'true' +testbasic 'true -c -e' + +# false +testbasic 'false' +testbasic 'false -c -e' + +# echo +# echo no flag +testbasic 'echo' +testbasic 'echo OK' +testbasic 'echo A AA AAA A AA AAA' +testbasic 'echo !' +testbasic "echo Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Egestas purus viverra accumsan in nisl nisi. At quis risus sed vulputate. Neque laoreet suspendisse interdum consectetur libero. Et molestie ac feugiat sed lectus vestibulum mattis. Tristique nulla aliquet enim tortor at auctor. Aliquet porttitor lacus luctus accumsan tortor. Tellus cras adipiscing enim eu turpis. Mattis ullamcorper velit sed ullamcorper morbi tincidunt ornare. Nec sagittis aliquam malesuada bibendum arcu vitae elementum. Consequat id porta nibh venenatis cras. Dolor magna eget est lorem ipsum. Vivamus at augue eget arcu dictum varius duis. Aliquam eleifend mi in nulla. Cursus risus at ultrices mi tempus imperdiet nulla malesuada. Adipiscing elit ut aliquam purus sit amet luctus venenatis. Risus at ultrices mi tempus imperdiet nulla malesuada pellentesque elit. Mauris cursus mattis molestie a iaculis at erat pellentesque adipiscing." +testbasic "echo if then do fi elif while for done until else in !" +# echo flag -n +testbasic 'echo -n' +testbasic 'echo -n OK' +testbasic 'echo -n A AA AAA A AA AAA' +testbasic 'Echo -n Ugly presentation innit?' +# echo flag -e +testbasic 'echo -e' +testbasic 'echo -e OK' +testbasic 'echo -e A AA AAA A AA AAA' +testbasic 'echo -e \n\t\e' +testbasic 'echo -e \n \t \e \\ \f' +testbasic 'echo -e some stuff \\ is in here' +testbasic 'echo -e Cutni\n half' +testbasic 'echo -e Testing the tab \t Just like this, see?\n' +# echo flag -E +testbasic 'echo -E' +testbasic 'echo -E OK' +testbasic 'echo -E A AA AAA A AA AAA' +testbasic 'echo -E \n\t\e' +testbasic 'echo -E \n \t \e \\ \f' +testbasic 'echo -E Nothing to see here' +# echo multiple flags +testbasic 'echo -E -n' +testbasic 'echo -E -n OK' +testbasic 'echo -E -n A AA AAA A AA AAA' +testbasic 'echo -E -e \n\t\e' +testbasic 'echo -e -E \n\t\e' +testbasic 'echo -e -E -n \n\t\e' +testbasic 'echo -E -e -n \n \t \e \\ \f' +testbasic 'echo -e -E \n \t \e \\ \f' +testbasic "echo -e -n Something\'s wrong\nI can \\feel\\ it\n" +testbasic 'echo -E -n Still nothing to \tsee here\n' +testbasic 'echo -e -n Testing the tab \t Just like this, see?\n Abd I also force a newline\n' +testbasic 'echo -e -n Something'\''s wrong\nI can \feel\\ it\n' +testbasic 'echo -E -e does this \t works ??' + +# dot +testbasic '.' +testbasic '. ./tests/resources/dot/script1.sh' +testbasic '. ./tests/resources/dot/script2.sh' + +# exit +testbasic 'exit' +testbasic 'exit -e' +testbasic 'exit 5' +testbasic '. ./tests/resources/exit/script1.sh' + +# cd +testbasic 'cd' +testbasic 'cd -' +testbasic 'cd /usr/' +testbasic 'cd ..' + +# export +testbasic 'export' +testbasic 'export A=42' +testbasic 'export A' + +# unset +testbasic 'export A=42; unset A' +testbasic 'unset A' + +# break & continue +testbasic 'break' +testbasic 'break 5' +testbasic 'continue' +testbasic 'continue 5' + +## Simple Command +testbasic 'ls' +testbasic 'cat /etc/passwd' +testbasic 'git status' +testbasic 'git log --all --decorate --oneline --graph' +testbasic 'tree' +testbasic 'date' +testbasic 'echo Testsuite now functional!' +testbasic 'echo if' +testbasic 'echo then' +testbasic 'ls then' +testbasic 'cat fi' +testbasic ' cat for a while' + +## Command List +testbasic 'true; false;' +testbasic 'false; true;' +testbasic 'true; true;' +testbasic 'false; false;' +testbasic 'echo aaa bbb ccc; true; ls;' +testbasic 'echo aaa bbb ccc; true; false; if true; then echo ok; fi' +testbasic 'echo a; echo b; echo c; echo ; echo ; echo ; echo d; echo ls; ls;' +testbasic 'true; true; true; true; true; true; true; true; true; true; false' +testbasic 'ls;ls .;ls ./;ls;ls;ls;ls ..;ls;ls;ls ../;ls;ls;' +testbasic 'true; true; true; true; echo aaa; true; true; false' +testbasic 'false; false; false; false; echo aaa; true' + +## Single Quote +testbasic "'echo' OK" +testbasic "'ech'o OK" +testbasic "cat 'fi'" +testbasic "l'''s'" +testbasic "'c''a''t' '/etc/pa'sswd" +testbasic "g'i't s'tat'us" +testbasic "g'i't l'o'g --all --decorate --oneline --graph" +testbasic "'tree'" +testbasic "'d''a''t''e'" +testbasic "'d''''a''''''t''''''''e'" +testbasic "'e''c'ho 'test aaaa'" + +## Comment +testbasic "echo \'# comment ça ???\'" +testbasic "echo \'comment ça ???\' \# comment ça \'????\'" +testbasic "echo \# et là ?" +testbasic "echo '\# et là ?'" +testbasic "echo han ? \# '\# et là ?'" +testbasic "echo \# '\# et là ?'" +testbasic "#######################################" +testbasic "echo \#Inexistant" +testbasic "echo Existe \#pas" + +## If then elif else +testbasic "if echo test; false; then echo ok; else echo ko; fi" +testbasic "if false; then echo ok; else echo ko; fi" +testbasic "if false; then echo ok; elif true; then echo bogos binted; else echo ko; fi" +testbasic "if false; then echo ok; elif ls; then echo bogos binted; else echo ko; fi" +testbasic "if false; then echo KO1; elif false; then echo KO2; elif false; then echo KO3; elif false; then echo KO4; elif false; then echo KO5; fi" +testbasic "if false; then echo KO1; elif false; then echo KO2; elif false; then echo KO3; elif false; then echo KO4; elif false; then echo KO5; else echo OK; fi" +testbasic "if false; then echo ok; elif true; then echo bogos binted; else echo ko; fi" +testbasic "if false; then \n\n\n\n\n\n\n\n echo ok; else echo ko\n\n\n\n; fi" +testbasic "if 'true' ; then e'ch'o O'K' ; else e'ch'o K'O'; fi" +testbasic "if 'tr'ue ; then 'echo' OK ; else 'echo' KO; fi" +testbasic "if true ; then echo OK ; elif false; then echo KO; elif false; then echo KO; fi" +testbasic "if false; then echo ok; else echo ko; fi ; if false; then echo ok; else echo ko; fi" +testbasic "if echo count with me; then echo 1 2 3 4 5 6 7 8 9 10 | wc; else echo too bad; fi" +testbasic "if echo -n many comms; then echo a; echo b; echo c; else echo too bad; fi" +testbasic "if echo testing redirs in the condition | wc; then echo it works; else echo it breakes; fi" +testbasic "if echo testing compound lists; echo another one; echo and another one; false; then echo problem; else echo fine; fi" +testbasic "if echo testing compound lists; echo another one; echo and another one; false; echo this is actually true; then echo fine; else echo problem; fi" + +## Pipeline +testbasic "true | false" +testbasic "false | true" +testbasic "find -iname \"*.[ch]\" | grep '.*test.*'" +testbasic "echo aaaaa | cat -e | sed 's/$/€/g'" +testbasic "echo Bonjour je suis là | cut --characters o | sed 's/o/a/g'" +testbasic "echo aaa | cat" +testbasic "echo aaa | cat -e" +testbasic "cat testsuite.sh | wc -l | cat -e" +testbasic "cat testsuite.sh | wc -l | cat | wc -c" +testbasic "cat testsuite.sh | wc -l | cat | wc -l" +testbasic "cat testsuite.sh | wc -l | cat | wc | cat -e" +testbasic "echo ./ | ls" +testbasic "echo ./TrustMeBroItJustWorks | ls" +testbasic "ls | ls" +testbasic "echo | echo" + +## Redirection +testredir "echo bonjour" '>' "$TEST_REDIR" +testredir "echo bonjour" '<' "$TEST_REDIR" +testredir "echo bonjour" '>>' "$TEST_REDIR" +testredir "echo bonjour" '<>' "$TEST_REDIR" +testredir "echo bonjour" '>|' "$TEST_REDIR" +testredir "echo -a bonjour" '>&' "$TEST_REDIR" +testredir "echo bonjour" '<&' "$TEST_REDIR" + +## And Or Not (Logical) +testbasic "! echo" +testbasic "! true" +testbasic "! false" +testbasic "! ! echo Invalid double negation" +testbasic "if ! false; then echo OK; else echo KO; fi" +testbasic "! true; echo aaa" +testbasic "true && false" +testbasic "true || false" +testbasic "false && true" +testbasic "! false && true" +testbasic "echo aa && true" +testbasic "if test -z $aaaaaa && echo doesn\'t exist; then echo OK; fi" +testbasic "false || ! false" +testbasic "if ! true || true ; then echo KO ; else echo OK ; fi" +testbasic "true || false || false || false || false || false" +testbasic "false && false && false && false && false && false" +testbasic "! true || ! false || ! false || ! false || ! false || ! false" +testbasic "if false && ! true && ! true || false; then echo KO; else echo OK; fi" + +## While +testbasic 'while false ; do echo LOOOOOOP ; done' +testbasic 'while invalid ; do echo LOOOOOOP ; done' + +## Until +testbasic 'until true ; do echo LOOOOOOP ; done' +testbasic 'until cat /etc/passwd ; do echo LOOOOOOP ; done' + +## Variable expansion +# Basic var +testbasic 'A=42 echo $A sh' +testbasic 'A=42; echo $A sh' +testbasic 'echo $A; A=42' +testbasic 'A=42 B=43 C=44 echo $A $B $C' +testbasic 'A=42 B=43 C=44 echo; $A $B $C' +testbasic 'A=42; B=43; C=44; echo; $A $B $C' +testbasic 'A=42 echo $A Nope; echo $A YES' +testbasic 'A=42 ;echo $A; A=43; echo $A; A=44; echo $A' +testbasic 'A=42 A=43 A=44; echo $A' +testbasic 'echo; echo; A=42; echo; echo; echo $A' +testbasic 'A=42 B=43 A=43;echo $A $B' +testbasic 'A=42 B=42 C=43 D=44 E=45 F=46 G=47 H=48 I=49 K=50; echo $A $B $C $D $E $F $G $H $I $J $K' +testbasic 'A=42; B=$A ; echo $B' + +# Special var +testbasic 'echo $@' +testbasic 'echo $*' +testbasic 'echo $?' +testbasic 'echo $$' +testbasic 'echo $1' +testbasic 'echo $2 $3' +testbasic 'echo $#' +testbasic 'echo $RANDOM' +testbasic 'echo $UID' +testbasic 'echo $OLDPWD' +testbasic 'echo $PWD' +testbasic 'echo $IFS' +testbasic 'echo ${PWD}' + +## For +testbasic 'for i in foo bar baz ; do echo $i ; done' +testbasic 'for i in foo "bar baz" ; do echo $i ; done' +testbasic 'for i; do echo $i ; done' + +## Command Substitution +testbasic 'echo $(echo toto)' +testbasic 'echo $(echo toto; echo tata)' +testbasic 'echo $(echo toto | cat -e)' +testbasic 'echo $(if true; then echo OK; else echo KO; fi)' +testbasic 'echo $(true)' +testbasic 'echo$(echo " toto")' +testbasic 'echo `echo $(printf " coucou")`' +testbasic 'echo `echo $(printf " coucou%s" $(echo aaaaaaaaaaaaaaa))`' +# This test will print stuff on STDERR but still succedes with exit code 0 +testbasic 'echo `echo $(printf " coucou%s" $(aaaaaaaaaaaaaaa))`' +testbasic 'echo "$(echo OK)"' + +## Subshells +testbasic 'echo aaa; (echo bbb); echo ccc;' +testbasic 'echo aaa; (A=42; echo $A sh);' +testbasic 'echo aaa; (A=42; echo $A sh); echo ccc' +testbasic 'A=41; (echo $A; A=42); echo $A' +testbasic 'A=41; (A=42; echo $A); echo $A' +testbasic 'A=41; (A=42 echo $A); echo $A' +testbasic '(A=42); echo $A' +testbasic '(A=1;(A=2;(A=3; echo $A); echo $A); echo $A)' +testbasic '(A=1); (A=2); (echo $A)' +testbasic '(A=1; (B=2)); echo $A $B' +testbasic '(A=1); (B=1); (C=1); (echo $A $B $C)' +testbasic 'A=1; B=2; C=3; (echo $A $B $C)' +testbasic '((A=1);(B=2); echo $A $B)' + +## Command Block +testredir "{ echo bonjour ; echo au revoir; }" '>' "$TEST_REDIR" +testredir "{ echo bonjour ; echo au revoir; }" '<' "$TEST_REDIR" +testredir "{ echo bonjour ; echo au revoir; }" '>>' "$TEST_REDIR" +testredir "{ echo bonjour ; echo au revoir; }" '<>' "$TEST_REDIR" +testredir "{ echo bonjour ; echo au revoir; }" '>|' "$TEST_REDIR" +testredir "{ echo bonjour ; echo au revoir; }" '>&' "$TEST_REDIR" +testredir "{ echo bonjour ; echo au revoir; }" '<&' "$TEST_REDIR" + +## Mixed tests +testbasic "echo a; echo b; echo c | wc | cat; echo d; echo e" +testbasic "echo a; echo b; if true; then echo OK; else echo KO; fi; echo c; echo d" +testbasic "ls; echo a && t'ru'e || false; echo conditionals are now over | wc; if true; then echo OK; else echo KO; fi" +testbasic "echo aaa | wc | cat; echo bbb | cat | ls; echo ccc | echo" + +## As Script +testasscript "echo a; echo b; echo c | wc | cat; echo d; echo e" +testasscript "echo a; echo b; if true; then echo OK; else echo KO; fi; echo c; echo d" +testasscript "ls; echo a && t'ru'e || false; echo conditionals are now over | wc; if true; then echo OK; else echo KO; fi" +testasscript "echo aaa | wc | cat; echo bbb | cat | ls; echo ccc | echo" + +## As Argument +testasargument "echo a; echo b; echo c | wc | cat; echo d; echo e" +testasargument "echo a; echo b; if true; then echo OK; else echo KO; fi; echo c; echo d" +testasargument "ls; echo a && t'ru'e || false; echo conditionals are now over | wc; if true; then echo OK; else echo KO; fi" +testasargument "echo aaa | wc | cat; echo bbb | cat | ls; echo ccc | echo" + +# Error tests +ERROR_TESTS="Error tests" +print_header ${ERROR_TESTS} + +testerror "ghsdfloerijg" +testerror "if echo test; false; then\n echo ok;\n else\n echo ko;\n fi" +testerror "if djunkleschkog; then echo bear; else echo plushie; fi" +testerror "if true; then nope; else echo you sure?; fi" +testerror "if false; then echo Seriously?; else depression; fi" +testerror "if false; then echo not again; elif insanity; then echo Nope; else echo still nope; fi" +testerror "if false; then echo stop; elif true; then nope; else echo really, stop; fi" +testerror "if false; then echo stop; elif false; then echo STOP DAMMIT; else adog; fi" +testerror "ech'o a'aa" +testerror "'echo aaa'" +testerror "if true; echo OK ; else echo KO; fi" +testerror "if bobby; then echo GREEDY; else echo fine; fi" +testerror "'if' true; 'then' echo OK; 'else' echo KO; 'fi'" +testerror "if true; 'then' echo OK; 'else' echo KO; 'fi'" +testerror "e'cho aaa" +testerror "echo aa ><" +testerror "while while true; do echo ok else echo false" +testerror "while true; echo missing; done" +testerror "while true; do echo missing again;" +testerror "while ; do echo no cond; done" +testerror "while true; do echo no end done" +testerror "while echo infinity; do do echo incorrect; done" +testerror "while invalid; do echo no end; done" +testerror "while invalid; do incorrect; done" +testerror "until while true; do echo ok else echo false" +testerror "until true; echo missing; done" +testerror "until true; do echo missing again;" +testerror "until ; do echo no cond; done" +testerror "until true; do echo no end done" +testerror "until echo infinity; do do echo incorrect; done" +testerror "until invalid; do echo no end; done" +testerror "until invalid; do incorrect; done" + +# Logic Combined with Pipelines +testerror "true && echo This | grep This || echo Fail" +testerror "false || echo Fallback | grep Fall" +testerror "echo Hello | grep H && echo Found || echo Not Found" +testerror "echo Hello | grep Z && echo Found || echo Not Found" +testerror "false || echo aaaaa | cat | sed 's/a/b/g'" +testerror "true && echo OK | cat -e && echo Done" +testerror "true || echo This should not print | grep This" +testerror "false && echo This should not print | grep This" + +# Complex Conditions with Pipelines +testerror "if echo Hello | grep H; then echo OK; else echo KO; fi" +testerror "if echo Bonjour | grep Z; then echo OK; else echo KO; fi" +testerror "if echo Foo | grep F && echo Bar | grep B; then echo OK; else echo KO; fi" +testerror "if false || echo Hello | grep H; then echo OK; else echo KO; fi" + +# Invalid Pipelines +testerror "| echo Leading pipe" +testerror "echo Trailing pipe |" +testerror "echo Double || pipe" +testerror "echo || echo || Invalid logic" +testerror "echo Missing command | | grep H" + +# Invalid Redirections +testerror "echo Test >" +testerror "echo Test >< file" +testerror "echo Test ><< file" +# testerror 'echo Test >!< file' +testerror "echo Test > > file" +testerror "cat < nonexistentfile" +testerror "ls 2> /nonexistentdirectory/file" + +# Invalid Logic +testerror '!echo' +testerror '!true' +testerror '!false' +testerror "true && && echo Invalid and" +testerror "true || || echo Invalid or" + +# Micellanious +testerror ')' +testerror '}' +testerror '||' +testerror '&&' +testerror '$' +testerror '&' +testerror '|' + +# If loops +testerror 'if' +testerror 'if true; echo aaa' +testerror 'if ; echo aaa' +testerror 'if true; then echo aaa' +testerror 'if true; then echo aaa; else' +testerror 'if true; then echo aaa; else echo bbb' +testerror 'if true; then echo aaa; else echo bbb; elif' +testerror 'if true; then echo aaa; elif' +testerror 'if true; then echo aaa; elif false; echo ccc' +testerror 'if true; then echo aaa; elif false; then echo ccc' +testerror 'if true; then echo aaa; elif false; then echo ccc;' +testerror 'if rue; then echo aaa' +testerror 'if true; then cho aaa; else' +testerror 'if true; then cho aaa; else cho bbb' +testerror 'if true; then cho aaa; else cho bbb; elif' +testerror 'if true; then cho aaa; elif' +testerror 'if true; then cho aaa; elif false; cho ccc' +testerror 'if true; then cho aaa; elif false; then cho ccc' +testerror 'if true; then cho aaa; elif false; then cho ccc;' + +# While loops +testerror 'while' +testerror 'while false' +testerror 'while false; echo aaa' +testerror 'while false; do echo aaa;' + +# Until loops +testerror 'until' +testerror 'until true' +testerror 'until true; echo aaa' +testerror 'until true; do echo aaa;' + +# For loops +testerror 'for' +testerror 'for a' +testerror 'for abc def' +testerror 'for a in ;' +testerror 'for (' +testerror 'for a ;' +testerror 'for a in b c d e f g h ;' +testerror 'for i in range; do' +testerror 'for j in range; asd' +testerror 'for k in range; do echo aaa;' +testerror 'for l in range; do echo aaa' +testerror 'for in in in' + +# Function declaration +testerror 'test (' +testerror 'test (0123456789' +testerror 'test ()' +testerror 'test &' +testerror 'test () {' + +# Redirection +testerror '2' +testerror 'wipuevbhqipie 2>&' +testerror 'echo aaa 2>&' +testerror 'echo > >' +testerror 'echo aaa >& (' +testerror 'echo aaa >& )' +testerror '>22' +testerror '>>>>>>>>>' +testerror '() > ()' + +# Bad Usage +testbadusage + +## Pretty print +testprettyprint 'echo OK' +testprettyprint 'ls' +testprettyprint 'if true ; then exit ; else . ./tests/resources/dot/script1.sh' +testprettyprint 'while false ; do your mom ; done' +testprettyprint 'until true ; do nothing ; done' +testprettyprint 'for i in foo bar baz ; do echo $i ; done' +testprettyprint 'A=42 ; echo $A' +testprettyprint 'find . -name "*.[hc]" | xargs wc -l' +testprettyprint 'if ! false && true || true ; then echo OK ; else echo KO ; fi' +testprettyprint 'echo OK > le_test.oui && rm -f le_test.oui' + +RESULTS="Results" +print_header ${RESULTS} +printf "%d%% ($score/$testcount) of the tests passed!\n" $((score * 100 / testcount)) +[ "$failures" ] && echo "Failing tests ($((testcount - score))/$testcount): $failures" +echo ${SEPARATOR} +[ "$OUTPUT_FILE" ] && echo $((score * 100 / testcount)) > "$OUTPUT_FILE" + +clean + +exit 0 |
