Skip to content

sourcing a file with set -e causes the tests to always succeed #390

@meleu

Description

@meleu
Q A
OS Linux
Shell bash v5.1.16
bashunit version 0.18.0

Summary

If I use set -e in my script and source it to test the behavior of a function, the test always succeed. Even when it clearly should fail.

NOTE: this only happens when I source the script.

IMO this is a serious bug that puts into question the reliability of bashunit, as it reports a failing test as successful (it would be slightly less serious if it were the other way around).

How to reproduce

Here's my hello.sh script:

#!/usr/bin/env bash

# NOTE: if this 'set -e' is removed, the bug reported here doesn't happen!
set -e

hello() {
  echo "Hello, World!"
}

[[ "${BASH_SOURCE[0]}" == "${0}" ]] && hello

Here's the hello_test.sh (note that both are supposed to fail, X != "Hello, World!"):

#!/usr/bin/env bash

test_calling_hello.sh() {
  # 👍 this assertion fails, which is expected
  assert_same "X" "$(./hello.sh)"
}

test_source_hello.sh_and_call_the_hello_function() {
  # 👎 this assertion passes, which is an error
  source ./hello.sh
  assert_same "X" "$(hello)"
}

When I run this test I have this:

$ bashunit ./hello_test.sh
bashunit - 0.18.0
Running hello_test.sh
✗ Failed: Calling hello.sh
    Expected 'X'
    but got  'Hello, World!'
✓ Passed: Source hello.sh and call the hello function
# ...

Again: if I remove the set -e in hello.sh this buggy success does NOT happen..

Expected behavior

Do not report a failing test behavior as success, even if the script has set -e and is sourced.

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status
    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions