Merge a6771a6fb7642fb2a801eebb5e6fd0cb3663dcbb into b01fc435806635555d44cf815208247f7138292a

This commit is contained in:
brainchild0 2023-04-04 10:45:58 -05:00 committed by GitHub
commit 860e6a37b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 370 additions and 30 deletions

17
meta-tests.txt Normal file
View File

@ -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

6
meta-tests/base.sh Executable file
View File

@ -0,0 +1,6 @@
#!/usr/bin/env bash
source ./mo
if (( FAIL )); then
exit 1
fi

17
meta-tests/function-export.sh Executable file
View File

@ -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)

15
meta-tests/function-list.sh Executable file
View File

@ -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)

16
meta-tests/function-unexport.sh Executable file
View File

@ -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')

14
meta-tests/function-unload.sh Executable file
View File

@ -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')

View File

@ -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

9
meta-tests/subshell-clean.sh Executable file
View File

@ -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

View File

@ -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

9
meta-tests/subshell-declare.sh Executable file
View File

@ -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

View File

@ -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

11
meta-tests/subshell-export.sh Executable file
View File

@ -0,0 +1,11 @@
#!/usr/bin/env bash
source ./mo
moExport
bash << "EOF"
source ./run-basic-tests
if (( FAIL )); then
exit 1
fi
EOF

9
meta-tests/subshell-simple.sh Executable file
View File

@ -0,0 +1,9 @@
#!/usr/bin/env bash
source ./mo
bash << "EOF"
source ./run-basic-tests
if ! (( FAIL )); then
exit 1
fi
EOF

8
meta-tests/subshell-source.sh Executable file
View File

@ -0,0 +1,8 @@
#!/usr/bin/env bash
bash << "EOF"
source ./mo
source ./run-basic-tests
if (( FAIL )); then
exit 1
fi

View File

@ -0,0 +1,8 @@
#!/usr/bin/env bash
bash << "EOF"
source ./run-basic-tests
if ! (( FAIL )); then
exit 1
fi
EOF

View File

@ -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

11
meta-tests/subshell-unexport.sh Executable file
View File

@ -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

4
meta-tests/subshell-usage.sh Executable file
View File

@ -0,0 +1,4 @@
#!/usr/bin/env bash
source ./mo
diff <(moUsage) <((moDeclare; echo moUsage) | bash)

102
mo
View File

@ -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 <<EOF
mo
moCallFunction
moFindEndTag
moFindString
moFullTagName
moGetContent
moIndentLines
moIndirect
moIndirectArray
moIsArray
moIsFunction
moIsStandalone
moJoin
moLoadFile
moLoop
moParse
moPartial
moShow
moSplit
moStandaloneAllowed
moStandaloneDenied
moTest
moTestVarSet
moTrimChars
moTrimWhitespace
moUsage
moListFuncs
moListVars
moUnload
moExport
moUnexport
moDeclare
EOF
}
# Produce a list of all shell variables declared by mo, and brought
# into any shell that had sourced the file. Each name is given on one
# line of standard output.
moListVars() {
cat <<EOF
MO_ORIGINAL_COMMAND
MO_VERSION
MO_EXPORT
EOF
}
# Clear all shell functions and variables for names used by mo,
# restoring the shell to a state as though mo had never been sourced
# (assuming no naming collisions).
moUnload() {
unset -v $(moListVars)
unset -f $(moListFuncs)
}
# Mark for export all shell functions and variables used by mo, such
# that any child (or descendant) Bash process may call mo as though
# itself having sourced the file.
moExport() {
export $(moListVars)
export -f $(moListFuncs)
MO_EXPORT=1
}
# Unmark for export all shell functions and variables used by, such
# that any new child (or descendant) Bash process would not inherit
# them, regardless of any past operations.
moUnexport() {
export -n $(moListVars)
export -fn $(moListFuncs)
MO_EXPORT=
}
# Print declaration of all shell functions and variables used by mo,
# suitable for processing by another Bash shell. Any Bash instance
# processing the output of this function may afterward call mo as
# though itself having sourced the file.
moDeclare() {
local functions="$(moListFuncs)"
if ! [ -z "${functions}" ]; then
declare -fp ${functions}
fi
local vars="$(moListVars)"
if ! [ -z "${vars}" ]; then
declare -p ${vars}
fi
if ! [ -z "${MO_EXPORT}" ]; then
echo moExport
fi
}
# Save the original command's path for usage later
MO_ORIGINAL_COMMAND="$(cd "${BASH_SOURCE[0]%/*}" || exit 1; pwd)/${BASH_SOURCE[0]##*/}"
MO_VERSION="2.2.0"
MO_EXPORT=
# If sourced, load all functions.
# If executed, perform the actions as expected.

33
run-basic-tests Executable file
View File

@ -0,0 +1,33 @@
#!/usr/bin/env bash
cd "$(dirname "$0")" || exit 1
for TEST in tests/*.expected; do
export BASE="${TEST%.expected}"
export MO_FALSE_IS_EMPTY=
echo -n "basic:${BASE#tests/} ... "
(
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

23
run-meta-tests Normal file
View File

@ -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

View File

@ -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