Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions cronic
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ cronic_help() {
echo "stderr, from which lines that start with PS4 are removed. That removal"
echo "is skipped if PS4 contains \"\$\", a backquote, a backslash, or a newline,"
echo "because such a PS4 can expand differently on each line."
echo "Do not export the verbose option (\"set -v\") via SHELLOPTS: bash would echo"
echo "the lines of this script and of a bash COMMAND to stderr, so every run would"
echo "produce output."
exit 0
}

Expand Down
48 changes: 20 additions & 28 deletions tests/cronic-test/cronic-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -341,51 +341,43 @@ check "EXIT trap writes to stdout" 0 silent trap 'echo "from the trap"' EXIT
check "builtin with an unset variable" 0 silent \
eval ': "$cronic_test_unset_variable"'
# When the caller exports SHELLOPTS, a bash script runs with the caller's
# options rather than with `cronic`'s own `set -eu`. BASH_ENV is empty so that
# the script runs no startup file.
# options rather than with `cronic`'s own `set -eu`.
# Creates "$work/cronic-with-NAME", which runs `cronic` with SHELLOPTS exported,
# after `set OPTION` if OPTION is given.
# after `set OPTION` if OPTION is given. The wrapper unsets BASH_ENV, so that
# neither `cronic` nor a bash script that it runs reads the user's startup
# file. The wrapper's own trace goes to /dev/null, so that with `set -x`, any
# trace in the output comes from `cronic`.
make_cronic_wrapper() { # NAME [OPTION]
{
echo "#!/bin/bash"
if [ $# -gt 1 ]; then
echo "set $2"
fi
echo "export SHELLOPTS"
echo "exec \"$CRONIC\" \"\$@\""
} > "$work/cronic-with-$1"
chmod +x "$work/cronic-with-$1"
}
make_cronic_wrapper shellopts
make_cronic_wrapper nounset -u
make_cronic_wrapper errexit -e
make_cronic_wrapper allexport -a
# The wrapper's own trace goes to /dev/null, and `cronic` reads no startup
# file, so that any trace in the output comes from `cronic`.
cat > "$work/cronic-with-xtrace" << EOF
cat > "$work/cronic-with-$1" << EOF
#!/bin/bash
unset BASH_ENV
exec 3>&2 2> /dev/null
set -x
${2:+set $2}
export SHELLOPTS
exec "$CRONIC" "\$@" 2>&3 3>&-
EOF
chmod +x "$work/cronic-with-xtrace"
chmod +x "$work/cronic-with-$1"
}
make_cronic_wrapper shellopts
make_cronic_wrapper nounset -u
make_cronic_wrapper errexit -e
make_cronic_wrapper allexport -a
make_cronic_wrapper xtrace -x
saved_cronic="$CRONIC"
CRONIC="$work/cronic-with-shellopts"
# shellcheck disable=SC2016 # The expansion is for the wrapped command to do.
check "bash script with an unset variable, SHELLOPTS exported" 0 silent \
env BASH_ENV= bash -c ': "$cronic_test_unset_variable"'
bash -c ': "$cronic_test_unset_variable"'
check "bash script with a failing command, SHELLOPTS exported" 0 silent \
env BASH_ENV= bash -c 'false; true'
bash -c 'false; true'
CRONIC="$work/cronic-with-nounset"
# shellcheck disable=SC2016 # The expansion is for the wrapped command to do.
check "bash script with an unset variable, caller's nounset exported" 127 \
"report:cronic_test_unset_variable" \
env BASH_ENV= bash -c ': "$cronic_test_unset_variable"'
bash -c ': "$cronic_test_unset_variable"'
CRONIC="$work/cronic-with-errexit"
check "bash script with a failing command, caller's errexit exported" 1 report \
env BASH_ENV= bash -c 'false; true'
bash -c 'false; true'
# When the caller exports allexport, `cronic` does not export its own variables
# to the command, but a variable that the command assigns is exported.
CRONIC="$work/cronic-with-allexport"
Expand All @@ -397,9 +389,9 @@ check "command's variable exported, caller's allexport exported" 0 silent \
# When the caller exports xtrace, `cronic` traces only the command, not itself.
CRONIC="$work/cronic-with-xtrace"
check "bash script, caller's xtrace exported" 0 silent \
env BASH_ENV= bash -c 'true'
bash -c 'true'
check "bash script that fails, caller's xtrace exported" 1 "report:^+ false$" \
env BASH_ENV= bash -c 'false'
bash -c 'false'
if grep -q cronic_ "$work/output"; then
echo "FAIL: caller's xtrace exported: the report traces cronic itself:"
cat "$work/output"
Expand Down
Loading