diff --git a/meta-tests.txt b/meta-tests.txt new file mode 100644 index 0000000..e94b65c --- /dev/null +++ b/meta-tests.txt @@ -0,0 +1,17 @@ +base +subshell-usage +subshell-simple +subshell-undeclared +subshell-source +subshell-clean +subshell-clean-undeclared +subshell-export +subshell-export-nested +subshell-unexport +subshell-unexport-nested +subshell-declare +subshell-declare-nested +function-list +function-unload +function-export +function-unexport diff --git a/meta-tests/base.sh b/meta-tests/base.sh new file mode 100755 index 0000000..64b6008 --- /dev/null +++ b/meta-tests/base.sh @@ -0,0 +1,6 @@ +#!/usr/bin/env bash + +source ./mo +if (( FAIL )); then + exit 1 +fi diff --git a/meta-tests/function-export.sh b/meta-tests/function-export.sh new file mode 100755 index 0000000..fb6ba3c --- /dev/null +++ b/meta-tests/function-export.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash + +list_function_names() { + declare -F | while read l; do echo ${l/#* /}; done | sort +} +declare -xf list_function_names + +f_orig=$(list_function_names) + +source ./mo +moExport + +f=$(bash -c list_function_names) + +f_new=$(comm -13 <(echo $f_orig | tr ' ' '\n') <(echo $f | tr ' ' '\n')) + +diff <(echo $f_new | tr ' ' '\n') <(moListFuncs | sort) diff --git a/meta-tests/function-list.sh b/meta-tests/function-list.sh new file mode 100755 index 0000000..cf38e4d --- /dev/null +++ b/meta-tests/function-list.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +list_function_names() { + declare -F | while read l; do echo ${l/#* /}; done | sort +} + +f_orig=$(list_function_names) + +source ./mo + +f=$(list_function_names) + +f_new=$(comm -13 <(echo $f_orig | tr ' ' '\n') <(echo $f | tr ' ' '\n')) + +diff <(echo $f_new | tr ' ' '\n') <(moListFuncs | sort) diff --git a/meta-tests/function-unexport.sh b/meta-tests/function-unexport.sh new file mode 100755 index 0000000..5190c49 --- /dev/null +++ b/meta-tests/function-unexport.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash + +list_function_names() { + declare -F | while read l; do echo ${l/#* /}; done | sort +} +declare -xf list_function_names + +f_orig=$(list_function_names) + +source ./mo +moExport +moUnexport + +f=$(bash -c list_function_names) + +diff <(echo $f_orig | tr ' ' '\n') <(echo $f | tr ' ' '\n') diff --git a/meta-tests/function-unload.sh b/meta-tests/function-unload.sh new file mode 100755 index 0000000..263c8d7 --- /dev/null +++ b/meta-tests/function-unload.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash + +list_function_names() { + declare -F | while read l; do echo ${l/#* /}; done | sort +} + +f_orig=$(list_function_names) + +source ./mo +moUnload + +f=$(list_function_names) + +diff <(echo $f_orig | tr ' ' '\n') <(echo $f | tr ' ' '\n') diff --git a/meta-tests/subshell-clean-undeclared.sh b/meta-tests/subshell-clean-undeclared.sh new file mode 100755 index 0000000..56ba131 --- /dev/null +++ b/meta-tests/subshell-clean-undeclared.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +source ./mo +moExport +env --ignore-environment << "EOF" +source ./run-basic-tests +if ! (( FAIL )); then + exit 1 +fi +EOF diff --git a/meta-tests/subshell-clean.sh b/meta-tests/subshell-clean.sh new file mode 100755 index 0000000..c8fa996 --- /dev/null +++ b/meta-tests/subshell-clean.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +source ./mo +cat <(moDeclare) - << "EOF" | env --ignore-environment bash +source ./run-basic-tests +if (( FAIL )); then + exit 1 +fi +EOF diff --git a/meta-tests/subshell-declare-nested.sh b/meta-tests/subshell-declare-nested.sh new file mode 100755 index 0000000..5b385a9 --- /dev/null +++ b/meta-tests/subshell-declare-nested.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +source ./mo +cat <(moDeclare) - << "EOF" | bash +cat <(moDeclare) - << "EOF2" | bash +cat <(moDeclare) - << "EOF3" | bash +cat <(moDeclare) - << "EOF4" | bash +source ./run-basic-tests +if (( FAIL )); then + exit 1 +fi +EOF4 +EOF3 +EOF2 +EOF diff --git a/meta-tests/subshell-declare.sh b/meta-tests/subshell-declare.sh new file mode 100755 index 0000000..9b6db40 --- /dev/null +++ b/meta-tests/subshell-declare.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +source ./mo +cat <(moDeclare) - << "EOF" | bash +source ./run-basic-tests +if (( FAIL )); then + exit 1 +fi +EOF diff --git a/meta-tests/subshell-export-nested.sh b/meta-tests/subshell-export-nested.sh new file mode 100755 index 0000000..e4cbd89 --- /dev/null +++ b/meta-tests/subshell-export-nested.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash + +source ./mo +moExport +bash << "EOF" +bash << "EOF2" +bash << "EOF3" +bash << "EOF4" +source ./run-basic-tests +if (( FAIL )); then +exit 1 +fi +EOF4 +EOF3 +EOF2 +EOF diff --git a/meta-tests/subshell-export.sh b/meta-tests/subshell-export.sh new file mode 100755 index 0000000..83147e0 --- /dev/null +++ b/meta-tests/subshell-export.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +source ./mo +moExport +bash << "EOF" +source ./run-basic-tests +if (( FAIL )); then + exit 1 +fi +EOF + diff --git a/meta-tests/subshell-simple.sh b/meta-tests/subshell-simple.sh new file mode 100755 index 0000000..6e874bd --- /dev/null +++ b/meta-tests/subshell-simple.sh @@ -0,0 +1,9 @@ +#!/usr/bin/env bash + +source ./mo +bash << "EOF" +source ./run-basic-tests +if ! (( FAIL )); then + exit 1 +fi +EOF diff --git a/meta-tests/subshell-source.sh b/meta-tests/subshell-source.sh new file mode 100755 index 0000000..6edb3d9 --- /dev/null +++ b/meta-tests/subshell-source.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +bash << "EOF" +source ./mo +source ./run-basic-tests +if (( FAIL )); then + exit 1 +fi diff --git a/meta-tests/subshell-undeclared.sh b/meta-tests/subshell-undeclared.sh new file mode 100755 index 0000000..da66a32 --- /dev/null +++ b/meta-tests/subshell-undeclared.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +bash << "EOF" +source ./run-basic-tests +if ! (( FAIL )); then + exit 1 +fi +EOF diff --git a/meta-tests/subshell-unexport-nested.sh b/meta-tests/subshell-unexport-nested.sh new file mode 100755 index 0000000..9d0f2df --- /dev/null +++ b/meta-tests/subshell-unexport-nested.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +source ./mo +moExport +bash << "EOF" +moUnexport +bash << "EOF2" +source ./run-basic-tests +if ! (( FAIL )); then + exit 1 +fi +EOF2 +EOF diff --git a/meta-tests/subshell-unexport.sh b/meta-tests/subshell-unexport.sh new file mode 100755 index 0000000..b0c1fe5 --- /dev/null +++ b/meta-tests/subshell-unexport.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +source ./mo +moExport +moUnexport +bash << "EOF" +source ./run-basic-tests +if ! (( FAIL )); then + exit 1 +fi +EOF diff --git a/meta-tests/subshell-usage.sh b/meta-tests/subshell-usage.sh new file mode 100755 index 0000000..32fc9c1 --- /dev/null +++ b/meta-tests/subshell-usage.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +source ./mo +diff <(moUsage) <((moDeclare; echo moUsage) | bash) diff --git a/mo b/mo index c928d7c..38d18bb 100755 --- a/mo +++ b/mo @@ -1095,9 +1095,111 @@ moUsage() { } +# Produce a list of all shell function declared by mo, and brought +# into any shell that had sourced the file. Each name is given on one +# line of standard output. +moListFuncs() { + cat < "${BASE}.diff" + + statusCode=$? + + if [[ $statusCode -ne 0 ]]; then + echo "FAIL (status code $statusCode)" + FAIL=$(( FAIL + 1 )) + else + echo "ok" + PASS=$(( PASS + 1 )) + rm "${BASE}.diff" + fi +done diff --git a/run-meta-tests b/run-meta-tests new file mode 100644 index 0000000..3af69f0 --- /dev/null +++ b/run-meta-tests @@ -0,0 +1,23 @@ +#!/usr/bin/env bash + +cd "$(dirname "$0")" || exit 1 + +for TEST in $(cat meta-tests.txt); do + + echo -n "meta:$TEST ... " + + ( + meta-tests/${TEST}.sh > /dev/null 2> /dev/null + ) + + statusCode=$? + + if [[ $statusCode -ne 0 ]]; then + echo "FAIL (status code $statusCode)" + FAIL=$(( FAIL + 1 )) + else + echo "ok" + PASS=$(( PASS + 1 )) + fi + +done diff --git a/run-tests b/run-tests index b1a0331..33889b7 100755 --- a/run-tests +++ b/run-tests @@ -1,44 +1,18 @@ #!/usr/bin/env bash -cd "${0%/*}" || exit 1 +cd "$(dirname "$0")" || exit 1 # shellcheck disable=SC1091 . ./mo PASS=0 FAIL=0 -for TEST in tests/*.expected; do - export BASE="${TEST%.expected}" - export MO_FALSE_IS_EMPTY= +source ./run-basic-tests - echo -n "$BASE ... " - - ( - if [[ -f "${BASE}.sh" ]]; then - # Run a shell script if one exists - "${BASE}.sh" - else - # Fall back to using .env and .template - # shellcheck disable=SC1090 - . "${BASE}.env" - echo "Do not read this input" | mo "${BASE}.template" - fi - ) | diff -U5 - "${TEST}" > "${BASE}.diff" - - statusCode=$? - - if [[ $statusCode -ne 0 ]]; then - echo "FAIL (status code $statusCode)" - FAIL=$(( FAIL + 1 )) - else - echo "ok" - PASS=$(( PASS + 1 )) - rm "${BASE}.diff" - fi -done +source ./run-meta-tests echo "" -echo "Pass: $PASS" +echo "Pass: $PASS" echo "Fail: $FAIL" if [[ $FAIL -gt 0 ]]; then exit 1