]> git.ipfire.org Git - thirdparty/systemd.git/commit
test: fix error handling with `set -e`
authorFrantisek Sumsal <frantisek@sumsal.cz>
Sat, 18 Sep 2021 19:44:38 +0000 (21:44 +0200)
committerFrantisek Sumsal <frantisek@sumsal.cz>
Sun, 19 Sep 2021 11:46:55 +0000 (13:46 +0200)
commitabfa9a0e7de2352ad3bdc7109843ffe083d3df4e
treed50e90ee72cd14b82128ed280093ae985defa52c
parentb1471e559e8dc4ea78c896ed365f5b043c2d6ce0
test: fix error handling with `set -e`

Unfortunately, when checking the return/exit code using &&, ||, if,
while, etc., `set -e` is disabled for all nested functions as well,
which leads to incorrectly ignored errors, *sigh*.

Example:

```
set -eu
set -o pipefail

task() {
    echo "task init"
    echo "this should fail"
    false
    nonexistentcommand
    echo "task end (we shouldn't be here)"
}

if ! task; then
    echo >&2 "The task failed"
    exit 1
else
    echo "The task passed"
fi
```

```
$ bash test.sh
task init
this should fail
test.sh: line 10: nonexistentcommand: command not found
task end (we shouldn't be here)
The task passed
$ echo $?
0
```

But without the `if`, everything works "as expected":

```
set -eu
set -o pipefail

task() {
    echo "task init"
    echo "this should fail"
    false
    nonexistentcommand
    echo "task end (we shouldn't be here)"
}

task
```

```
$ bash test.sh
task init
this should fail
$ echo $?
1
```

Wonderful.
test/TEST-64-UDEV-STORAGE/test.sh