Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exporting engine definition to other bash instances #50

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
e42e4fc
Add functions for listing names of defined functions and variables
brainchild0 Jan 24, 2022
2f382c5
Add functions for handling defined functions and variables
brainchild0 Jan 24, 2022
47941c8
Fix formatting of white space in test output
brainchild0 Jan 24, 2022
3431357
Fortify detection of base directory for tests
brainchild0 Jan 24, 2022
ed2ab8f
Move loop over tests to separate file
brainchild0 Jan 24, 2022
67bd586
Print prefix `basic` before test name instead of file relative path
brainchild0 Jan 24, 2022
3cd32a4
Add `meta` tests, to validate handling of functions for subshells
brainchild0 Jan 24, 2022
8aeb20b
Add trivial test, for basic integrity of framework
brainchild0 Jan 25, 2022
61151b3
Add test that functions are not normally inherited
brainchild0 Jan 24, 2022
0036b48
Add trivial test, that processing fails when tool is not invoked
brainchild0 Jan 25, 2022
9ad445e
Add test that functions run in child if explicitly imported
brainchild0 Jan 24, 2022
cec3502
Add test that functions run in child if exported by parent
brainchild0 Jan 24, 2022
61d6b0c
Add test that listed functions are the same as actually defined
brainchild0 Jan 25, 2022
9a81e1d
Add test that defined functions are properly unloaded
brainchild0 Jan 25, 2022
747bb59
Add test that all declared functions are inherited when marked for ex…
brainchild0 Jan 25, 2022
88e7f09
Add test that unmarking for export prevents inheritence by child shell
brainchild0 Jan 25, 2022
c4cd099
Add test that subshell may process automatically-generated declarations
brainchild0 Jan 25, 2022
79231fa
Add test that declarations are not dependent on inherited environment
brainchild0 Jan 25, 2022
84edc13
Add test that functions are not inherited into clean environment
brainchild0 Jan 25, 2022
b1d679b
Add test that unmarking functions for export indeed prevents exportation
brainchild0 Jan 25, 2022
772eec9
Add test that exported functions are inherited by descendants
brainchild0 Jan 25, 2022
e1b775a
Add test that unmarking for export works in child shell from original
brainchild0 Jan 25, 2022
5e04b21
Add test that declarations may be propagated to descedant shells
brainchild0 Jan 25, 2022
a6771a6
Add test that generated usage function is equivalent to original
brainchild0 Jan 25, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions meta-tests.txt
Original file line number Diff line number Diff line change
@@ -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 changes: 6 additions & 0 deletions meta-tests/base.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

source ./mo
if (( FAIL )); then
exit 1
fi
17 changes: 17 additions & 0 deletions meta-tests/function-export.sh
Original file line number Diff line number Diff line change
@@ -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 changes: 15 additions & 0 deletions meta-tests/function-list.sh
Original file line number Diff line number Diff line change
@@ -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 changes: 16 additions & 0 deletions meta-tests/function-unexport.sh
Original file line number Diff line number Diff line change
@@ -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 changes: 14 additions & 0 deletions meta-tests/function-unload.sh
Original file line number Diff line number Diff line change
@@ -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')
10 changes: 10 additions & 0 deletions meta-tests/subshell-clean-undeclared.sh
Original file line number Diff line number Diff line change
@@ -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 changes: 9 additions & 0 deletions meta-tests/subshell-clean.sh
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions meta-tests/subshell-declare-nested.sh
Original file line number Diff line number Diff line change
@@ -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 changes: 9 additions & 0 deletions meta-tests/subshell-declare.sh
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions meta-tests/subshell-export-nested.sh
Original file line number Diff line number Diff line change
@@ -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 changes: 11 additions & 0 deletions meta-tests/subshell-export.sh
Original file line number Diff line number Diff line change
@@ -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 changes: 9 additions & 0 deletions meta-tests/subshell-simple.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

source ./mo
bash << "EOF"
source ./run-basic-tests
if ! (( FAIL )); then
exit 1
fi
EOF
8 changes: 8 additions & 0 deletions meta-tests/subshell-source.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

bash << "EOF"
source ./mo
source ./run-basic-tests
if (( FAIL )); then
exit 1
fi
8 changes: 8 additions & 0 deletions meta-tests/subshell-undeclared.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash

bash << "EOF"
source ./run-basic-tests
if ! (( FAIL )); then
exit 1
fi
EOF
13 changes: 13 additions & 0 deletions meta-tests/subshell-unexport-nested.sh
Original file line number Diff line number Diff line change
@@ -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 changes: 11 additions & 0 deletions meta-tests/subshell-unexport.sh
Original file line number Diff line number Diff line change
@@ -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 changes: 4 additions & 0 deletions meta-tests/subshell-usage.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

source ./mo
diff <(moUsage) <((moDeclare; echo moUsage) | bash)
102 changes: 102 additions & 0 deletions mo
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
33 changes: 33 additions & 0 deletions run-basic-tests
Original file line number Diff line number Diff line change
@@ -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 changes: 23 additions & 0 deletions run-meta-tests
Original file line number Diff line number Diff line change
@@ -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
Loading