Skip to content
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

allow a forced unstub mode without plans validation #49

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ Finally, when the stub is removed with `unstub`,
there is a final check to ensure there are no remaining un-met plans
(which would indicated an expected invocation that did not occur).

You can force to `unstub` without validation of expected plans with
`unstub <cmd> true`.

### Unstubbing

Once the test case is done,
Expand Down
5 changes: 4 additions & 1 deletion stub.bash
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,17 @@ stub_repeated() {

unstub() {
local program="$1"
local force="${2:-false}"
local prefix
prefix="$(echo "$program" | tr a-z- A-Z_)"
local path="${BATS_MOCK_BINDIR}/${program}"

export "${prefix}_STUB_END"=1

local STATUS=0
"$path" || STATUS="$?"
if [ "$force" != 'true' ]; then
"$path" || STATUS="$?"
fi

rm -f "$path"
rm -f "${BATS_MOCK_TMPDIR}/${program}-stub-plan" "${BATS_MOCK_TMPDIR}/${program}-stub-run"
Expand Down
37 changes: 37 additions & 0 deletions test/stub.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env bats

# this is the "code under test"
# it would normally be in another file
format_date() {
date -r 222
}

setup() {
load '../load'
_DATE_ARGS='-r 222'
}

@test "date format util formats date with expected arguments" {
stub date \
"${_DATE_ARGS} : echo 'I am stubbed!'" \
"${_DATE_ARGS} : echo 'Wed Dec 31 18:03:42 CST 1969'"

result="$(format_date)"
[ "$result" == 'I am stubbed!' ]

result="$(format_date)"
[ "$result" == 'Wed Dec 31 18:03:42 CST 1969' ]

unstub date
}

@test "unstub forced not enough invocations" {
stub date \
"${_DATE_ARGS} : echo 'I am stubbed!'" \
"${_DATE_ARGS} : echo 'Wed Dec 31 18:03:42 CST 1969'"

result="$(format_date)"
[ "$result" == 'I am stubbed!' ]

unstub date true
}