diff --git a/cronic b/cronic index 1bb3849..1644dcf 100755 --- a/cronic +++ b/cronic @@ -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 } diff --git a/tests/cronic-test/cronic-test.sh b/tests/cronic-test/cronic-test.sh index 52f89ee..6d8f9fb 100755 --- a/tests/cronic-test/cronic-test.sh +++ b/tests/cronic-test/cronic-test.sh @@ -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" @@ -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"