@@ -295,16 +295,54 @@ split_bats_output_into_lines() {
295295# The script is written as `$BATS_TEST_BINDIR/$cmd_name`. `$BATS_TEST_BINDIR` is
296296# added to `PATH` and exported if it isn't already present.
297297#
298+ # You may need to call `restore_program_in_path` immediately after `run` to
299+ # avoid side-effects in the rest of your test program, especially when using the
300+ # `--in-process` option.
301+ #
302+ # Options:
303+ # --in-process Set this when calling `run` on an in-process function
304+ #
298305# Arguments:
299306# cmd_name: Name of the command from PATH to stub
300307# ...: Lines comprising the stub script
301308stub_program_in_path () {
302309 local bindir_pattern=" ^${BATS_TEST_BINDIR} :"
310+ local in_process
311+
312+ if [[ " $1 " == ' --in-process' ]]; then
313+ in_process=' true'
314+ shift
315+ fi
303316
304317 if [[ ! " $PATH " =~ $bindir_pattern ]]; then
305318 export PATH=" $BATS_TEST_BINDIR :$PATH "
306319 fi
307320 create_bats_test_script " ${BATS_TEST_BINDIR# $BATS_TEST_ROOTDIR / } /$1 " " ${@: 2} "
321+
322+ if [[ -n " $in_process " ]]; then
323+ hash " $1 "
324+ fi
325+ }
326+
327+ # Removes a stub program from `PATH`
328+ #
329+ # This will return an error if the stub doesn't exist, to help avoid errors
330+ # when the `cmd_name` arguments to `stub_program_in_path` and this function
331+ # don't match.
332+ #
333+ # Arguments:
334+ # cmd_name: Name of the command from PATH to stub
335+ #
336+ # Returns:
337+ # Zero if the stub program exists and is removed, nonzero otherwise
338+ restore_program_in_path () {
339+ if [[ -e " $BATS_TEST_BINDIR /$1 " ]]; then
340+ rm -f " $BATS_TEST_BINDIR /$1 "
341+ hash " $1 "
342+ else
343+ printf " Bats test stub program doesn't exist: %s\n" " $1 "
344+ return 1
345+ fi
308346}
309347
310348# --------------------------------
0 commit comments