bats-core / bats-assert

Common assertions for Bats
Creative Commons Zero v1.0 Universal
94 stars 39 forks source link

Assert reports wrong failing command #32

Closed thorstenkampe closed 2 years ago

thorstenkampe commented 3 years ago

see below example: bats-core reports that false failed (correct) while assert reports true failed (incorrect).

I suspect that assert always reports the previous command as failing while a test can include more than just one run and one assert.

load /usr/local/libexec/bats-assert/load.bash
load /usr/local/libexec/bats-support/load.bash

@test core {
    true
    false
}

@test assert {
    true
    assert false
}
 ✗ core
   (in test file .\mytest.bats, line 8)
     `false' failed
 ✗ assert
   (in test file .\mytest.bats, line 12)
     `true' failed

   -- assertion failed --
   expression : false
   --
flickerfly commented 3 years ago

false is just a string in bash. A non-empty string is true. Therefore, this is working properly. You need to manage boolean logic differently in your bash.

Related discussion: https://unix.stackexchange.com/questions/105935/understanding-boolean-operators-in-bash-script

thorstenkampe commented 3 years ago

@flickerfly quoting from the documentation... | assert - Fail if the given expression evaluates to false. ...that means the false string is evaluated (to boolean false).

But that was just an example of a failing assertion. The issue is that it reports the true statement as failing while it's the assertion that is failing.

hyperupcall commented 2 years ago

I just ran the same example, and everything seems to work properly. I didn't see any code changes, so it was possible there was a fix within Bats. You might be also using an older version of Bash, it may be useful to know what it is

 ✗ core
   (in test file 32.sh, line 8)
     `false' failed
 ✗ assert
   (from function `assert' in file ../bats-assert/src/assert.bash, line 40,
    in test file 32.sh, line 13)
     `assert false' failed

   -- assertion failed --
   expression : false
   --

I would suspect that the error boiled down to accessing the wrong line number (off by one indexing error), or something along those lines. At least with the current code, the "false being a string" isn't particularly relevant, because all parameters are passed as strings - the actual execution occurs after being passed into assert.

thorstenkampe commented 2 years ago

@hyperupcall can't confirm your observation. The result is still the same for me with Bats 1.3.0.

Which version of Bats are you using and how did you install it?

hyperupcall commented 2 years ago

Hmmm, I'm using Bats version 1.4.1 and Bash version 5.0.17(1)-release (x86_64-pc-linux-gnu). I installed it with my Bash package manager, which effectively is the downloaded tarball of release v1.4.1 on the Bats release page

martin-schulze-vireso commented 2 years ago

@thorstenkampe I can't reproduce this, even with Bats 1.3.0. Please try to provide more detail about your environment and post this at bats-core, since error line reporting comes from over there.