Uh oh!
There was an error while loading. Please reload this page.
docs(assert): document how to assert a captured exit code - #1170
Merged
Conversation
assert_successful_code, assert_unsuccessful_code, assert_general_error and assert_command_not_found read $?. Under set -e -- which --strict turns on for every test -- that cannot be used directly: the non-zero exit aborts the test before the assertion runs, so the code must be captured, and capturing it sets $? to zero. Both assert_general_error assert_general_error "$code" then assert the wrong thing silently, the second being the natural spelling. The working form takes the code as the third argument, and was undocumented. Appended at the end of each section rather than after the intro line: the `bashunit doc` renderer truncates a section, so inserting earlier dropped the existing assert_exec tip from CLI output. With the block at the end the doc snapshot is byte-identical. Not changing $1 to mean the captured code: assert_general_error "$(cmd)" is an established idiom here that relies on $?, and it would silently change meaning whenever that output is a bare integer. Closes#1169
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for freeto join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🤔 Background
Related #1169
The exit-code assertions read
$?. Underset -e— which--strictturns onfor every test — that idiom cannot be used directly: the non-zero exit aborts
the test before the assertion runs, so the code has to be captured, and
capturing it sets
$?to zero.Both silently assert the wrong thing, and the second is the natural spelling.
The working form —
assert_general_error "" "" "$code"— was undocumented.💡 Changes
assert_exit_codetakes its expected code in$1bashunit docrenderer truncates a section, and inserting earlier silently dropped the existingassert_exectip from CLI output. With the block at the end the doc snapshot is byte-identicalNot changing
Making
$1the captured code would fix the natural spelling, butassert_general_error "$(some_cmd)"is an established idiom in this repo thatrelies on
$?and would silently change meaning for numeric output. That is acontract change for a maintainer.