Attempt to test for failure #1226

This commit attempts to modify sharness/test_build_static_site.t so as
to catch the error described in #1226. Unfortunately, the test does not
work as intended: the test passes even when the script is crashing.

The reason is that the exit trap in build_static_site.sh traps the
failure, and as such the script returns with exit code 0. Based on
googling, it should be possible to catch the actual exit code and
re-exit with it during the cleanup trap, and this indeed works for
"regular" exit codes, but not for the `1` that comes from referencing
an unset variable.

See the included script `problem.sh` for a reproduction.
This commit is contained in:
Dandelion Mané 2019-07-14 17:36:30 +01:00
parent 4ba9fe3a8c
commit a575638fa4
2 changed files with 36 additions and 0 deletions

31
problem.sh Executable file
View File

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

View File

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