diff --git a/problem.sh b/problem.sh new file mode 100755 index 0000000..e4d069c --- /dev/null +++ b/problem.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +set -eu + +# Demonstrates the difficulty of reporting a proper exit code when referencing +# an unset variable and using a cleanup trap. +# +# If you switch from failure_without_exit_code to failure_with_exit_code in the +# main block, you'll find that the trap proxies the error code correctly for a +# "normal" return code but not for one generated by an unset variable. + +main() { + trap cleanup EXIT + failure_without_exit_code +} + +cleanup() { + rv=$? + echo "CLEANUP" + exit $rv +} + +failure_without_exit_code() { + echo ${UNSET_VARIABLE} +} + +failure_with_exit_code() { + rm nonexistent_file_1234 +} + +main diff --git a/sharness/test_build_static_site.t b/sharness/test_build_static_site.t index abc6db9..a802096 100755 --- a/sharness/test_build_static_site.t +++ b/sharness/test_build_static_site.t @@ -97,6 +97,11 @@ test_expect_success "should fail with multiple cname values" ' printf "redacted\n" | test_cmp - important_dir/.wallet.dat ' +test_expect_success "should not error when SOURCECRED_DIRECTORY is unset" ' + unset SOURCECRED_DIRECTORY && + run --target build_output/output_sourcecred_directory_unset +' + # # Now, actually generate output in two cases: one with repositories, and # one with no repositories. We can only do this if we have a token.