mbland / go-script-bash

Framework for writing modular, discoverable, testable Bash scripts
ISC License
95 stars 16 forks source link

bats/helpers: Reset executable hash after stubbing #194

Closed mbland closed 7 years ago

mbland commented 7 years ago

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

coveralls commented 7 years ago

Coverage Status

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