Skip to content

Commit 6af4184

Browse files
committed
Make it work with mocks and stubs
1 parent 004e707 commit 6af4184

File tree

5 files changed

+68
-2
lines changed

5 files changed

+68
-2
lines changed

load.bash

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# Preserve path at the time this file was sourced
2+
# This prevents using of user-defined mocks/stubs that modify the PATH
3+
4+
_BATSLIB_PATH="$PATH"
5+
16
source "$(dirname "${BASH_SOURCE[0]}")/src/output.bash"
27
source "$(dirname "${BASH_SOURCE[0]}")/src/error.bash"
38
source "$(dirname "${BASH_SOURCE[0]}")/src/lang.bash"

src/output.bash

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ batslib_err() {
3838
{ if (( $# > 0 )); then
3939
echo "$@"
4040
else
41-
cat -
41+
( PATH="$_BATSLIB_PATH"; command cat -; )
4242
fi
4343
} >&2
4444
}
@@ -273,7 +273,7 @@ batslib_mark() {
273273
batslib_decorate() {
274274
echo
275275
echo "-- $1 --"
276-
cat -
276+
( PATH="$_BATSLIB_PATH"; command cat -; )
277277
echo '--'
278278
echo
279279
}

test/50-output-10-batslib_err.bats

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,30 @@ load test_helper
1414
[ "$status" -eq 0 ]
1515
[ "$output" == 'm1 m2' ]
1616
}
17+
18+
@test 'batslib_err() works with modified path' {
19+
export PATH="$BATS_TEST_DIRNAME:$PATH"
20+
echo 'm1 m2' | {
21+
# Verify stub
22+
run which cat
23+
[ "$status" -eq 0 ]
24+
[ "$output" = "$BATS_TEST_DIRNAME/cat" ]
25+
# Should still work
26+
run batslib_err
27+
[ "$status" -eq 0 ]
28+
[ "$output" == 'm1 m2' ]
29+
}
30+
}
31+
32+
@test 'batslib_err() works with mock function' {
33+
echo 'm1 m2' | {
34+
function cat {
35+
echo "Mocked cat"
36+
}
37+
[ "$(cat)" = "Mocked cat" ]
38+
# Should still work
39+
run batslib_err
40+
[ "$status" -eq 0 ]
41+
[ "$output" == 'm1 m2' ]
42+
}
43+
}

test/50-output-19-batslib_decorate.bats

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,36 @@ load test_helper
1111
[ "${lines[1]}" == 'body' ]
1212
[ "${lines[2]}" == '--' ]
1313
}
14+
15+
@test 'batslib_decorate() works with modified path' {
16+
export PATH="$BATS_TEST_DIRNAME:$PATH"
17+
echo body | {
18+
# Verify stub
19+
run which cat
20+
[ "$status" -eq 0 ]
21+
[ "$output" = "$BATS_TEST_DIRNAME/cat" ]
22+
# Should still work
23+
run batslib_decorate 'title'
24+
[ "$status" -eq 0 ]
25+
[ "${#lines[@]}" -eq 3 ]
26+
[ "${lines[0]}" == '-- title --' ]
27+
[ "${lines[1]}" == 'body' ]
28+
[ "${lines[2]}" == '--' ]
29+
}
30+
}
31+
32+
@test 'batslib_decorate() works with mock function' {
33+
echo body | {
34+
function cat {
35+
echo "Mocked cat"
36+
}
37+
[ "$(cat)" = "Mocked cat" ]
38+
# Should still work
39+
run batslib_decorate 'title'
40+
[ "$status" -eq 0 ]
41+
[ "${#lines[@]}" -eq 3 ]
42+
[ "${lines[0]}" == '-- title --' ]
43+
[ "${lines[1]}" == 'body' ]
44+
[ "${lines[2]}" == '--' ]
45+
}
46+
}

test/cat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Dummy file stubbing a stub/mock

0 commit comments

Comments
 (0)