From 32b19a12fdaa42755440bca3844e41f5d65a9685 Mon Sep 17 00:00:00 2001 From: zawz Date: Mon, 20 Dec 2021 15:59:42 +0100 Subject: [PATCH] chore: add tests --- .gitignore | 6 +- run-tests.sh | 233 +++++++++++++++++++++++++++++++++++++++++++ test/a.bash | 43 ++++++++ test/arithmetic.sh | 4 + test/array.bash | 38 +++++++ test/backtick.sh | 7 ++ test/brace.sh | 5 + test/braceexp.bash | 8 ++ test/case.sh | 17 ++++ test/comment.sh | 5 + test/complex.sh | 22 ++++ test/debashify.bash | 43 ++++++++ test/dequote.sh | 76 ++++++++++++++ test/echo.bash | 12 +++ test/err/err.bash | 12 +++ test/err/err.sh | 66 ++++++++++++ test/fct.sh | 11 ++ test/for.sh | 18 ++++ test/heredocument.sh | 48 +++++++++ test/if.sh | 11 ++ test/include.sh | 9 ++ test/manip.sh | 15 +++ test/pipe.sh | 8 ++ test/prompt.sh | 3 + test/redir.sh | 5 + test/resolve.sh | 9 ++ test/subshell.sh | 10 ++ test/var.sh | 31 ++++++ test/while.sh | 23 +++++ 29 files changed, 794 insertions(+), 4 deletions(-) create mode 100755 run-tests.sh create mode 100644 test/a.bash create mode 100644 test/arithmetic.sh create mode 100644 test/array.bash create mode 100644 test/backtick.sh create mode 100644 test/brace.sh create mode 100644 test/braceexp.bash create mode 100644 test/case.sh create mode 100644 test/comment.sh create mode 100644 test/complex.sh create mode 100644 test/debashify.bash create mode 100644 test/dequote.sh create mode 100644 test/echo.bash create mode 100644 test/err/err.bash create mode 100644 test/err/err.sh create mode 100644 test/fct.sh create mode 100644 test/for.sh create mode 100644 test/heredocument.sh create mode 100644 test/if.sh create mode 100644 test/include.sh create mode 100644 test/manip.sh create mode 100644 test/pipe.sh create mode 100644 test/prompt.sh create mode 100644 test/redir.sh create mode 100644 test/resolve.sh create mode 100644 test/subshell.sh create mode 100644 test/var.sh create mode 100644 test/while.sh diff --git a/.gitignore b/.gitignore index 5acec18..178a384 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,8 @@ /obj -/test -/run-tests.sh /Zmakefile -/TODO +TODO /lxsh -/gmon.out +gmon.out /profiling/* /profiling.* /include/g_* diff --git a/run-tests.sh b/run-tests.sh new file mode 100755 index 0000000..ba92f72 --- /dev/null +++ b/run-tests.sh @@ -0,0 +1,233 @@ +#!/bin/bash + +bin=${1-./lxsh} + +echo_red() +{ + printf "\033[1;31m%s\033[0m\n" "$*" + exit 1 +} + +err=0 + +# $1 = file , $2 = extra print +# _LXSH_OPT : lxsh options +compile_test() +{ + printf "%s%s: " "$1" "$2" + if errout=$($bin $_LXSH_OPT "$1" 2>&1 >/dev/null) + then + echo "Ok" + else + echo_red "Error" + echo "$errout" + return 1 + fi +} + +# $1 = runner , $2 = file , $3 = extra print , $4 = runtime for lxsh +# _LXSH_OPT : lxsh options +exec_test() +{ + run=$1 + lxshrun=${4-$run} + ret1=$($run "$2") + stat1=$? + ret2=$($bin $_LXSH_OPT "$2" | $lxshrun) + stat2=$? + printf "%s%s: " "$2" "$3" + if [ "$ret1" = "$ret2" ] && [ $stat1 -eq $stat2 ] + then echo "Ok" + else + echo_red "Error" + echo ">> original stat $stat1 +$ret1 +>> compiled stat $stat2 +$ret2" + return 1 + fi +} + +size_test() +{ + shebang=$(head -n1 "$1" | grep '^#!') + c1=$($bin --no-shebang -m "$1" | wc -c) + c2=$($bin -m "$1" | shfmt -mn | wc -c) + printf "%s%s: " "$1" "$2" + if [ $c1 -lt $c2 ] + then echo "Ok" + else + echo_red "Too big" + return 1 + fi +} + +# $1 = file , $2 = extra print , $3 = list , $@ = run options +list_test() +{ + printf "%s%s: " "$1" "$2" + file=$1 + varlist=$3 + shift 3 + diffout=$(diff <($bin "$file" "$@" | sort -k2) <(echo "$varlist" | sed '/^$/d' | sort -k2) ) + if [ -z "$diffout" ] ; then + echo "Ok" + else + echo_red "Variable mismatch" + echo "$diffout" + return 1 + fi +} + +resolve="test/include.sh test/resolve.sh" +exec_exclude="test/prompt.sh $resolve" + + +echo " +============ +| sh | +============ +" + +echo "== Parse ==" +for I in test/*.sh +do + compile_test "$I" || err=$((err+1)) +done + +echo "== Exec ==" +for I in $( echo test/*.sh $exec_exclude | tr -s ' \n' '\n' | sort | uniq -u ) +do + exec_test sh "$I" || err=$((err+1)) + _LXSH_OPT=-M exec_test sh "$I" " (minify)" || err=$((err+1)) +done + +echo "== Size ==" +for I in test/*.sh +do + size_test "$I" || err=$((err+1)) +done + +echo "== Resolve ==" + +for I in $resolve +do + printf "%s: " "$I" + if errmsg=$($bin "$I" | sh 2>&1 >/dev/null) && [ -z "$errmsg" ] + then echo "Ok" + else + echo_red "Error" + echo ">> stderr +$errmsg" + err=$((err+1)) + fi +done + +varlist=" +2 nul +2 ABCD +1 AYE +1 BAR +3 FOO +2 TATA +1 TITI +4 TOTO +1 TUTU +4 somevar +" + +vardefs=" +1 ABCD +1 BAR +2 FOO +1 TATA +1 TOTO +1 TUTU +1 nul +2 somevar +" + +varcalls=" +1 AYE +1 ABCD +1 FOO +1 TATA +1 TITI +3 TOTO +1 nul +2 somevar +" + +varlist_used=" +1 AYE +2 ABCD +3 FOO +2 TATA +1 TITI +4 TOTO +2 nul +4 somevar +" + +echo "== Variables ==" +{ + list_test test/var.sh " (list)" "$varlist" --list-var || err=$((err+1)) + list_test test/var.sh " (list-def)" "$vardefs" --list-var-def || err=$((err+1)) + list_test test/var.sh " (list-call)" "$varcalls" --list-var-call || err=$((err+1)) + list_test test/var.sh " (remove unused)" "$varlist_used" --remove-unused --list-var || err=$((err+1)) +} + +fctlist=" +1 toto +1 tata +" + +fctlist_used=" +1 toto +" + +cmdlist=" +2 echo +1 toto +" + +echo "== Functions ==" +{ + list_test test/fct.sh " (list-fct)" "$fctlist" --list-fct || err=$((err+1)) + list_test test/fct.sh " (list-cmd)" "$cmdlist" --list-cmd || err=$((err+1)) + list_test test/fct.sh " (remove unused)" "$fctlist_used" --remove-unused --list-fct || err=$((err+1)) +} + +echo " +============ +| bash | +============ +" + +echo "== Parse ==" +for I in test/*.bash +do + compile_test "$I" || err=$((err+1)) +done + +echo "== Exec ==" +for I in test/*.bash +do + exec_test bash "$I" || err=$((err+1)) + _LXSH_OPT=-m exec_test bash "$I" " (minify)" || err=$((err+1)) +done + +echo "== Size ==" +for I in test/*.bash +do + size_test "$I" || err=$((err+1)) +done + +echo "== Debashify ==" +for I in test/{debashify.bash,array.bash,echo.bash} +do + _LXSH_OPT=--debashify exec_test bash "$I" "" sh || err=$((err+1)) + _LXSH_OPT="-m --debashify" exec_test bash "$I" " (minify)" sh || err=$((err+1)) +done + +exit $err diff --git a/test/a.bash b/test/a.bash new file mode 100644 index 0000000..85dd7ed --- /dev/null +++ b/test/a.bash @@ -0,0 +1,43 @@ +#!/usr/bin/env bash + +diff <(echo a) <(echo b) + +write_to_file() { echo "$2" > "$1"; } +write_to_file >(grep bar) bar +wait $! + +echo a &> /tmp/foo +echo b >& /tmp/bar +echo c &>> /tmp/foo + +cat /tmp/bar /tmp/foo +rm /tmp/bar /tmp/foo + + +TOTO="foo +bar" +grep ar <<< ar$TOTO + +declare -a A +A=("fo o" bar) +echo ${A[1]} + +declare -A B +B[foo]=ta +B[bar]=tu +echo ${B[foo]} +echo ${B[bar]} +echo ${B[*]} + +C=([foo]=bar [bar]=foo) +echo ${C[foo]} +echo ${C[bar]} +echo ${C[*]} + +BAR=FOO +echo ${!BAR} + +[[ $DEBUG == true ]] && echo debug + +a=a +[[ $a = a && foo = fo* && bar =~ b.r || 2 < 3 ]] diff --git a/test/arithmetic.sh b/test/arithmetic.sh new file mode 100644 index 0000000..01edf19 --- /dev/null +++ b/test/arithmetic.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +echo $(( (2+1)*3 )) +echo $(( $((2+1))*3 )) diff --git a/test/array.bash b/test/array.bash new file mode 100644 index 0000000..45737db --- /dev/null +++ b/test/array.bash @@ -0,0 +1,38 @@ +#!/bin/bash + +TOTO=(toto tata) +TOTO[0]=titi +TOTO[1]+=tu +echo ${TOTO[0]} +echo ${TOTO[1]} +echo ${TOTO[*]} + +TOTO+=(2) +echo $((TOTO[2]+1)) +echo $((${TOTO[2]}+2)) + +declare -a TUTU +TUTU=(titi "tu tu") +echo ${TUTU[0]} +echo ${TUTU[1]} +echo ${TUTU[*]} +echo "${TUTU[*]}" + +declare -A A +A[to]=ta +A[ti]=tu +echo ${A[to]} +echo ${A[ti]} +echo ${A[*]} + +declare -A B +B=([to]=ta [ti]=tu) +echo ${B[to]} +echo ${B[ti]} +echo ${B[*]} +echo "${B[*]}" + +toto=tata +C=() +C+=($toto) +echo ${C[@]} diff --git a/test/backtick.sh b/test/backtick.sh new file mode 100644 index 0000000..8bb8b7c --- /dev/null +++ b/test/backtick.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +echo $(echo toto) +echo $(printf %s\\n tata) +echo `echo titi` +echo `printf '%s\\n' tutu` + diff --git a/test/brace.sh b/test/brace.sh new file mode 100644 index 0000000..11e3f57 --- /dev/null +++ b/test/brace.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +{ echo tata ; echo a; } | sed 's|a|toto|g' + +echo a | { grep a && echo b; } diff --git a/test/braceexp.bash b/test/braceexp.bash new file mode 100644 index 0000000..174e064 --- /dev/null +++ b/test/braceexp.bash @@ -0,0 +1,8 @@ +#!/usr/bin/bash + +echo tot{a,o,i} +echo {1..10} +echo {0..10..2} +echo {1..10..2} +echo tot{a,o,i}{1..3}tata +echo :{{a..z},{A..Z}} diff --git a/test/case.sh b/test/case.sh new file mode 100644 index 0000000..5f1afb4 --- /dev/null +++ b/test/case.sh @@ -0,0 +1,17 @@ + +echo "$*" | case $1 in + to*) echo toto ;; + tata) echo wew tata ;; + a | b) + echo titi + ;; + *) + cat ;; +esac + +case foo in bar)echo a;;foo)echo b;esac +case foo in bar)echo a;;foo)echo b +esac + +case far in foo) echo a;;bar) +esac diff --git a/test/comment.sh b/test/comment.sh new file mode 100644 index 0000000..574f962 --- /dev/null +++ b/test/comment.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +echo toto# +echo toto#tata +echo toto #tata diff --git a/test/complex.sh b/test/complex.sh new file mode 100644 index 0000000..a2d953c --- /dev/null +++ b/test/complex.sh @@ -0,0 +1,22 @@ +while [ -z "$I" ] +do +case $U in +*)echo toto;I=y +esac +done +echo "$I" + +case toto in + tutu) ;; + + toto) +cat << EOF +toto +EOF +;; + tata) +esac + +echo to 2>&1 +echo to2 >&1 +echo to$(echo 2) >&1 diff --git a/test/debashify.bash b/test/debashify.bash new file mode 100644 index 0000000..f722c62 --- /dev/null +++ b/test/debashify.bash @@ -0,0 +1,43 @@ +#!/bin/bash + +readonly tutu titi=tata +echo "$tutu $titi" + +diff <(echo a) <(echo b) + +write_to_file() { echo "$2" > "$1"; } +write_to_file >(grep tutu) tutu +wait $! + +echo a &> /tmp/toto +echo b >& /tmp/tata +echo c &>> /tmp/toto + +cat /tmp/tata /tmp/toto +rm /tmp/tata /tmp/toto + + +TOTO="ta +to" +grep ta <<< toto$TOTO + +TATA=ti +TATA+=tu +echo $TATA + +[[ $DEBUG == true ]] && echo debug + +[ $((RANDOM+RANDOM)) -gt 0 ] +echo randomstat: $? + +a=a +[[ $a = a && foo = fo* && bar =~ b.r || 2 < 3 ]] +echo $? + +N=1 +TOTO=tatitu +echo "${TOTO:2}" +echo "${TOTO:$N:2}" + +echo ${TOTO:-tutu} +echo ${TITI:-bar} diff --git a/test/dequote.sh b/test/dequote.sh new file mode 100644 index 0000000..a14d244 --- /dev/null +++ b/test/dequote.sh @@ -0,0 +1,76 @@ +#!/bin/sh + + +toto=tutu +tata=titi + +echo "toto tata titi" +echo "toto" + +echo "tata"titi + +echo "ta ta" + +echo "$toto"tata + +echo "$toto" + +echo $toto"tata" + +echo $"toto" + +toto="$toto" + +toto="$toto"tata +echo "$toto" +toto="tata"$tata +echo "$toto" +toto=$toto"tata" +echo "$toto" +toto="$toto".tata +echo "$toto" + +tata="ta ta" +echo "$tata" + +echo "$" +echo \$ + +toto=tutu +tata=titi + +echo 'toto tata titi' +echo 'toto' + +echo 'tata'titi + +echo 'ta ta' + +echo '$toto'tata + +echo '$toto' + +echo $toto'tata' + +echo $'toto' + +toto='$toto' + +toto='$toto'tata +echo '$toto' +toto='tata'$tata +echo '$toto' +toto=$toto'tata' +echo '$toto' +toto='$toto'.tata +echo '$toto' + +tata='ta ta' +echo '$tata' + +echo '$' + + +cat << EOF +"toto" +EOF diff --git a/test/echo.bash b/test/echo.bash new file mode 100644 index 0000000..09650a5 --- /dev/null +++ b/test/echo.bash @@ -0,0 +1,12 @@ +#!/bin/bash + +echo -n titi: +echo -e 'tata\n' +echo -E 'tutu\n' + +echo -n tata tutu tete + +toto="to to" +echo $toto + +echo to $toto diff --git a/test/err/err.bash b/test/err/err.bash new file mode 100644 index 0000000..a843c48 --- /dev/null +++ b/test/err/err.bash @@ -0,0 +1,12 @@ +#!/bin/bash + +var[a +read var+=a +export var+=a +export var=() + +[[ a = b ]] toto + +echo >() <() + +function toto-titi{ true; } diff --git a/test/err/err.sh b/test/err/err.sh new file mode 100644 index 0000000..ae34d6b --- /dev/null +++ b/test/err/err.sh @@ -0,0 +1,66 @@ +#!/bin/sh + +read var=a +var+=a +var=(foo) + +$((var~2)) + +${!var} +${~} +${#var-a} +`echo \`echo\` ` + +$(( (var) ) + +>& /dev/null +echo &> /dev/null + +cat 2< file +cat <<< var +echo > + +echo &| cat +echo |& cat + +[[ a = b ]] foo + +() + +fct() abc +{ ; } + +fct() { } + +typeset var +var=val read var +case foo ; esac +case foo in aiae ; esac +case foo in ) ; esac +case foo in a) ; b) esac + +for 2 in a ; do true ; done +for foo do ; do true ; done +for foo & ; do true ; done +for I in ; true ; done + +while +do true ; done + +while true ; do +done + +if true ;then +fi + +if +then true ; fi + +if true ; then true ; else +fi + +fct-foo() { true; } + +function foo { true; } + +{ foo; } bar diff --git a/test/fct.sh b/test/fct.sh new file mode 100644 index 0000000..dcce4cb --- /dev/null +++ b/test/fct.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +toto() { + echo toto +} + +tata () { + echo tata +} + +toto diff --git a/test/for.sh b/test/for.sh new file mode 100644 index 0000000..142b395 --- /dev/null +++ b/test/for.sh @@ -0,0 +1,18 @@ +#!/bin/sh + +__for_fct() { + for I in ; do + echo $I + done +} + +for N +do + echo $N +done + +for I in $(seq 1 10); do + echo "toto $I" +done + +__for_fct toto tata diff --git a/test/heredocument.sh b/test/heredocument.sh new file mode 100644 index 0000000..1d97d45 --- /dev/null +++ b/test/heredocument.sh @@ -0,0 +1,48 @@ +#!/bin/sh + +cat << EOF +toto +tata +EOF + +toto=toto +cat << EOF | grep toto +$toto +tata +EOF + +cat << EOF +' +EOF + +cat << EOF | +azjeha +kijejaze +ljksdjk +EOF +cut -c1 + +grep -q toto << EOF && +toto +EOF +echo found toto + +{ cat << EOF | grep toto; } +toto +tata +EOF + +( cat << EOF | grep toto ) +toto +tata +EOF + +{ cat << EOF | grep toto && echo true; echo eyy; } +toto +tata +EOF + +( cat << EOF | grep toto && echo true ; echo eyy ) +toto +tata +EOF diff --git a/test/if.sh b/test/if.sh new file mode 100644 index 0000000..f88f29b --- /dev/null +++ b/test/if.sh @@ -0,0 +1,11 @@ +#!/bin/sh + +if [ -n "$DEBUG" ] +then + echo "set" +elif [ -n "$TOTO" ] +then + echo "toto lol" +else + echo "not set" +fi diff --git a/test/include.sh b/test/include.sh new file mode 100644 index 0000000..196ec14 --- /dev/null +++ b/test/include.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +RAW="$(%include -f pipe.sh brace.sh)" + +echo "$RAW" + +{ %include -f pipe.sh; } | grep -q arch && echo "btw i use arch" + +%include *.sh diff --git a/test/manip.sh b/test/manip.sh new file mode 100644 index 0000000..25729c1 --- /dev/null +++ b/test/manip.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +var="marijuana" +echo ${#var} + +echo ${var-foo} +echo ${var+foo} +echo ${foo-foo} +echo ${foo+foo} + +echo ${var#*a} +echo ${var##*a} +echo ${var%a*} +echo ${var%%a*} + diff --git a/test/pipe.sh b/test/pipe.sh new file mode 100644 index 0000000..df5163f --- /dev/null +++ b/test/pipe.sh @@ -0,0 +1,8 @@ +#!/bin/sh + +grep "^ID=" /etc/os-release | cut -d '=' -f2- + +echo toto | # +grep to + +echo '#toto' | grep '#toto' diff --git a/test/prompt.sh b/test/prompt.sh new file mode 100644 index 0000000..1ec5e52 --- /dev/null +++ b/test/prompt.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +read -r prompt || echo $? diff --git a/test/redir.sh b/test/redir.sh new file mode 100644 index 0000000..5f59320 --- /dev/null +++ b/test/redir.sh @@ -0,0 +1,5 @@ +{ echo toto >&2; } 2>&1 + +{ echo tata; }>/dev/null + +grep abc < test/redir.sh diff --git a/test/resolve.sh b/test/resolve.sh new file mode 100644 index 0000000..bb53586 --- /dev/null +++ b/test/resolve.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +echo "a$(%resolve echo tata titi)b" + +echo $(%resolve cut -d ' ' -f1 /proc/uptime)s + +%resolve echo "TIME=$(cut -d ' ' -f1 /proc/uptime)s;echo This was compiled at \${TIME}s uptime" + +FOO=$(%resolve echo bar) echo foo diff --git a/test/subshell.sh b/test/subshell.sh new file mode 100644 index 0000000..66e6d1f --- /dev/null +++ b/test/subshell.sh @@ -0,0 +1,10 @@ +TOTO=toto +(TOTO=tata; echo $TOTO; echo a) | sed 's|a|titi|g' +echo $TOTO + +echo a | ( grep a && echo b ) + +echo ab | ( grep a ) +pwd +(cd /) +pwd diff --git a/test/var.sh b/test/var.sh new file mode 100644 index 0000000..60df736 --- /dev/null +++ b/test/var.sh @@ -0,0 +1,31 @@ +#!/bin/sh + +foo() +{ + echo $FOO +} + +TOTO=tata +TATA=titi +echo $TOTO +echo "$TOTO $TATA$TITI" +echo "${AYE-aye}" + +export TUTU=ta + +foo +FOO=bar foo +BAR=foo foo +ABCD=$(FOO=true foo) +echo $ABCD +nul=/dev/null +echo toto > "$nul" + +somevar=val +echo $somevar +unset somevar +echo $somevar + +cat << EOF +$TOTO +EOF diff --git a/test/while.sh b/test/while.sh new file mode 100644 index 0000000..3ff8e79 --- /dev/null +++ b/test/while.sh @@ -0,0 +1,23 @@ +#!/bin/sh + +I=0 +while [ $I -lt 10 ] +do + I=$((I+1)) + echo "$I" +done + +I=0 +until [ $I -eq 10 ] +do + I=$((I+1)) + echo "$I" +done + +I=0 +while + I=$((I+1)) + echo "$I" + [ $I -lt 10 ] +do true +done