kward / shunit2

shUnit2 is a xUnit based unit test framework for Bourne based shell scripts.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Assert line numbers work without Macros

alexpop opened this issue · comments

Great tool!

I was trying to get the line numbers to show in the assert errors with macro asserts, but the extra quoting was too much. Like this, based on the README example:

testLineNo() {
  # This assert will have line numbers included (e.g. "ASSERT:[123] ...").
  echo "ae: ${_ASSERT_EQUALS_}"
  ${_ASSERT_EQUALS_} 'not equal' 1 2
}

Then I tried the LINENO variable in the message and voila, it worked. For example, this is my code:

# TEST to ensure that root dirs exist
testForRootDirs() {
  assertContains "$LINENO Directory 'base' should exist." "$ROOT_DIRS" "base"
  echo
}

which contains the line number (172) in the assert error:

testForRootDirs
ASSERT:172 Directory 'base' should exist. not found:<base>

testForFlatDirectoryStructure

If we agree, I can open a PR and replace the line numbering using Macros with this approach.

Also, what do you think about having these line numbers displayed by default without being explicit about them in the tests?

That looks like a good workaround for your environment. Unfortunately, line numbers behave differently depending on the shell, and in some cases they return the line of shUnit2 instead of the line in the script. This is why I've jumped through so many hoops to [try] and make them work.

Regarding showing line numbers by default, I won't try to accomplish this. The matrix of shell variants and behaviours makes this too inconsistent, which distracts from the ability of shUnit2 to behave as consistently as possible across as many shell variants and operating systems as possible.

I've added a FAQ entry that describes your approach so that users can use it if they find it works for them.
https://github.com/kward/shunit2/wiki/FAQ#can-i-use-lineno-to-show-line-numbers