File tree Expand file tree Collapse file tree 5 files changed +68
-2
lines changed
Expand file tree Collapse file tree 5 files changed +68
-2
lines changed Original file line number Diff line number Diff line change 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+
16source " $( dirname " ${BASH_SOURCE[0]} " ) /src/output.bash"
27source " $( dirname " ${BASH_SOURCE[0]} " ) /src/error.bash"
38source " $( dirname " ${BASH_SOURCE[0]} " ) /src/lang.bash"
Original file line number Diff line number Diff 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() {
273273batslib_decorate () {
274274 echo
275275 echo " -- $1 --"
276- cat -
276+ ( PATH= " $_BATSLIB_PATH " ; command cat -; )
277277 echo ' --'
278278 echo
279279}
Original file line number Diff line number Diff 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+ }
Original file line number Diff line number Diff 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+ }
Original file line number Diff line number Diff line change 1+ # Dummy file stubbing a stub/mock
You can’t perform that action at this time.
0 commit comments