Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
meleu opened this issue Dec 30, 2024 · 0 comments
Open

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

meleu opened this issue Dec 30, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@meleu
Copy link

meleu commented Dec 30, 2024

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

@meleu meleu added the bug Something isn't working label Dec 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: No status
Development

No branches or pull requests

1 participant