TypedDevs / bashunit

A simple testing library for bash scripts. Test your bash scripts in the fastest and simplest way, discover the most modern bash testing library.
https://bashunit.typeddevs.com
MIT License
271 stars 21 forks source link

feat: alternative to set -e #189

Closed antonio-gg-dev closed 8 months ago

antonio-gg-dev commented 8 months ago

πŸ“š Description

"The objective of this PR is to change the behavior of how an error is detected within a test to mark it as failed.

Currently, the tests detected these errors using return codes along with set -e. The set -e makes a script (the test in this case) interrupt if any execution within the script returns an error code other than 0. This in theory sounds good since if something went wrong, the test execution was interrupted and marked as failed, but in practice, it is not so, since if a line executes several functions, only the return of the last one is counted, therefore for this common situation:

function test_a_non_existing_function_assert_empty() {
  assert_empty "$(a_non_existing_function)"
}

The test no longer failed, since although a_non_existing_function returns an error 127, the assert_empty always returns 0.

With the version of this PR, instead of using the returns of the functions, the error output is used, that is, if any execution within the script writes something in stderr, it will be considered as failed.

This changes the behavior of the runner a bit since now the tests will run to the end even if something failed at the beginning, this might seem strange but right now if one of our tests has 2 assertions and the first one fails, the second one is still executed. However, as soon as something fails within the test, it is completely evaluated as failed (even if valid assertions are given afterward), so we could conclude that we have homogenized the runner's behavior.

Another change that has been made is when printing the tests that fail due to an error, previously the name of the test was printed followed by a "with error X", which went against our error reporting standard, since in the line of the test name we never printed anything else.

imagen

Now, since error codes are not used for anything, instead what is done is printing on a new line, indented and in gray (with a format very similar to the rest of the assertions) the actual error that is occurring, therefore if for example a non-existing function is called, literally in the test it will say that it has failed because a non-existing function was called."

πŸ”– Changes

βœ… To-do list

antonio-gg-dev commented 8 months ago

Closes #187