Skip to content

Conversation

@cameel
Copy link
Collaborator

@cameel cameel commented Oct 14, 2021

While working on #12141 I was really missing a quick way to assert stuff and the lack of a proper stack traces when a script fails makes debugging really annoying. This PR adds a rudimentary helper that can print a stack trace and a function that we can get an assert-like failure.

@cameel cameel requested a review from Marenz October 14, 2021 16:49
@cameel cameel self-assigned this Oct 14, 2021
@cameel cameel force-pushed the bash-assert-and-stack-traces branch from bbea48f to 5a33f46 Compare October 14, 2021 16:55
@cameel cameel force-pushed the bash-assert-and-stack-traces branch from 5a33f46 to 0280c8d Compare October 14, 2021 16:56
fi
else
exit 1
[[ $INTERACTIVE == false ]] || assertFail
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially I implemented a full assert that you would invoke like this:

assert "[[ $INTERACTIVE == false ]]"

It could print the condition if you did not provide a message, which was nice.

The problem is that it required eval to check the condition and also that quoting was tricky - you had to quote the whole condition with "" (otherwise the variables would be evaluated inside the assert rather than in the current function) and then use '' over any variable used inside that could evaluate to an empty string.

In the end I thought that just a simple assertFail that prints a stack trace is enough. It still clearly indicates that the condition you check is an assertion and condition evaluation has no quirks (at least not more than usually in Bash). Also, the condition itself can be seen in the stack trace.

@cameel cameel merged commit 2162039 into develop Oct 26, 2021
@cameel cameel deleted the bash-assert-and-stack-traces branch October 26, 2021 08:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants