bats/helpers: Reset executable hash after stubbing#194
Merged
Conversation
`stub_program_in_path` now calls `hash` on the new stub to ensure it is
discovered instead of the implementation it's intended to replace.
Previously, if `stub_program_in_path` was invoked for a program that
had already been executed by the test, the original program would still
get invoked rather than the stub (unless `hash` was called or `PATH` was
updated elsewhere).
Per the comments from the 'bats-helpers: {stub,restore}_program_in_path
trigger Bash command rehash' test case:
`restore_program_in_path` unconditionally updates `PATH`, which resets
Bash's executable path hash table. I didn't realize this until calling
`stub_program_in_path` on `rm` and finding that the `rm` stub was only
found when it was the first in a series of programs to be stubbed. After
some trial and error, I realized this was because the
`create_bats_test_script` call invokes `rm` and `stub_program_in_path`
only modifies `PATH` on the first call.
This isn't documented in the Bash man page, but once I figured out what
was happening, my hypothesis was confirmed by:
https://superuser.com/a/1000317
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 free
to 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.
stub_program_in_pathnow callshashon the new stub to ensure it is discovered instead of the implementation it's intended to replace. Previously, ifstub_program_in_pathwas invoked for a program that had already been executed by the test, the original program would still get invoked rather than the stub (unlesshashwas called orPATHwas updated elsewhere).Per the comments from the 'bats-helpers: {stub,restore}_program_in_path trigger Bash command rehash' test case:
restore_program_in_pathunconditionally updatesPATH, which resets Bash's executable path hash table. I didn't realize this until callingstub_program_in_pathonrmand finding that thermstub was only found when it was the first in a series of programs to be stubbed. After some trial and error, I realized this was because thecreate_bats_test_scriptcall invokesrmandstub_program_in_pathonly modifiesPATHon the first call.This isn't documented in the Bash man page, but once I figured out what was happening, my hypothesis was confirmed by: https://superuser.com/a/1000317