Skip to content

bats/helpers: Reset executable hash after stubbing #194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 1, 2017
Merged

Conversation

mbland
Copy link
Owner

@mbland mbland commented Aug 31, 2017

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

`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
@mbland mbland added the bug label Aug 31, 2017
@mbland mbland added this to the v1.7.0 milestone Aug 31, 2017
@mbland mbland self-assigned this Aug 31, 2017
@coveralls
Copy link

Coverage Status

Coverage increased (+0.002%) to 94.619% when pulling 67fe813 on stub-hash-reset into 94cabe2 on master.

@mbland mbland merged commit e6d646d into master Sep 1, 2017
@mbland mbland deleted the stub-hash-reset branch September 1, 2017 01:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants