Skip to content

Commit af36aa4

Browse files
authored
Merge pull request #163 from mbland/in-process-stub
bats/help: Implement in-process stub, restore
2 parents 1e2338e + 1a82f4f commit af36aa4

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

lib/bats/helpers

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
301308
stub_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
# --------------------------------

tests/bats-helpers.bats

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ __check_dirs_exist() {
236236
assert_lines_equal '' '' 'foo' '' 'bar' '' 'baz'
237237
}
238238

239-
@test "$SUITE: stub_program_in_path" {
239+
@test "$SUITE: stub_program_in_path for testing external program" {
240240
local bats_bindir_pattern="^${BATS_TEST_BINDIR}:"
241241
fail_if matches "$bats_bindir_pattern" "$PATH"
242242

@@ -246,3 +246,20 @@ __check_dirs_exist() {
246246
run git Hello, World!
247247
assert_success 'Hello, World!'
248248
}
249+
250+
@test "$SUITE: {stub,restore}_program_in_path for testing in-process function" {
251+
local orig_path="$(command -v mkdir)"
252+
253+
stub_program_in_path --in-process 'mkdir' 'echo "$@"'
254+
run command -v mkdir
255+
assert_success "$BATS_TEST_BINDIR/mkdir"
256+
257+
restore_program_in_path 'mkdir'
258+
run command -v mkdir
259+
assert_success "$orig_path"
260+
}
261+
262+
@test "$SUITE: restore_program_in_path fails when stub doesn't exist" {
263+
run restore_program_in_path 'foobar'
264+
assert_failure "Bats test stub program doesn't exist: foobar"
265+
}

0 commit comments

Comments
 (0)