Skip to content

Commit

Permalink
Make it work with mocks and stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
Flamefire committed Jun 14, 2019
1 parent 004e707 commit 25a1306
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 2 deletions.
5 changes: 5 additions & 0 deletions load.bash
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Preserve path at the time this file was sourced
# This prevents using of user-defined mocks/stubs that modify the PATH

_BATSLIB_PATH="$PATH"

source "$(dirname "${BASH_SOURCE[0]}")/src/output.bash"
source "$(dirname "${BASH_SOURCE[0]}")/src/error.bash"
source "$(dirname "${BASH_SOURCE[0]}")/src/lang.bash"
6 changes: 4 additions & 2 deletions src/output.bash
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ batslib_err() {
{ if (( $# > 0 )); then
echo "$@"
else
cat -
PATH="$_BATSLIB_PATH"
command cat -
fi
} >&2
}
Expand Down Expand Up @@ -273,7 +274,8 @@ batslib_mark() {
batslib_decorate() {
echo
echo "-- $1 --"
cat -
PATH="$_BATSLIB_PATH"
command cat -
echo '--'
echo
}
27 changes: 27 additions & 0 deletions test/50-output-10-batslib_err.bats
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,30 @@ load test_helper
[ "$status" -eq 0 ]
[ "$output" == 'm1 m2' ]
}

@test 'batslib_err() works with modified path' {
export PATH="$BATS_TEST_DIRNAME:$PATH"
echo 'm1 m2' | {
# Verify stub
run which cat
[ "$status" -eq 0 ]
[ "$output" = "$BATS_TEST_DIRNAME/cat" ]
# Should still work
run batslib_err
[ "$status" -eq 0 ]
[ "$output" == 'm1 m2' ]
}
}

@test 'batslib_err() works with mock function' {
echo 'm1 m2' | {
function cat {
echo "Mocked cat"
}
[ "$(cat)" = "Mocked cat" ]
# Should still work
run batslib_err
[ "$status" -eq 0 ]
[ "$output" == 'm1 m2' ]
}
}
33 changes: 33 additions & 0 deletions test/50-output-19-batslib_decorate.bats
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,36 @@ load test_helper
[ "${lines[1]}" == 'body' ]
[ "${lines[2]}" == '--' ]
}

@test 'batslib_decorate() works with modified path' {
export PATH="$BATS_TEST_DIRNAME:$PATH"
echo body | {
# Verify stub
run which cat
[ "$status" -eq 0 ]
[ "$output" = "$BATS_TEST_DIRNAME/cat" ]
# Should still work
run batslib_decorate 'title'
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 3 ]
[ "${lines[0]}" == '-- title --' ]
[ "${lines[1]}" == 'body' ]
[ "${lines[2]}" == '--' ]
}
}

@test 'batslib_decorate() works with mock function' {
echo body | {
function cat {
echo "Mocked cat"
}
[ "$(cat)" = "Mocked cat" ]
# Should still work
run batslib_decorate 'title'
[ "$status" -eq 0 ]
[ "${#lines[@]}" -eq 3 ]
[ "${lines[0]}" == '-- title --' ]
[ "${lines[1]}" == 'body' ]
[ "${lines[2]}" == '--' ]
}
}
1 change: 1 addition & 0 deletions test/cat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Dummy file stubbing a stub/mock

0 comments on commit 25a1306

Please sign in to comment.