Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/bats-core/bats-support.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Grund <alexander.grund@tu-dresden.de>2019-06-14 18:45:20 +0300
committerAlexander Grund <alexander.grund@tu-dresden.de>2019-06-14 19:05:14 +0300
commit6af41847af3cb820d7e8a4a02cdced726d5e7b55 (patch)
treeefd5c1489f195cece17f644b7704c3b9d3450bea
parent004e707638eedd62e0481e8cdc9223ad471f12ee (diff)
Make it work with mocks and stubs
-rw-r--r--load.bash5
-rw-r--r--src/output.bash4
-rwxr-xr-xtest/50-output-10-batslib_err.bats27
-rwxr-xr-xtest/50-output-19-batslib_decorate.bats33
-rwxr-xr-xtest/cat1
5 files changed, 68 insertions, 2 deletions
diff --git a/load.bash b/load.bash
index 0727aeb..62c4622 100644
--- a/load.bash
+++ b/load.bash
@@ -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"
diff --git a/src/output.bash b/src/output.bash
index c6cf6a6..6d260b1 100644
--- a/src/output.bash
+++ b/src/output.bash
@@ -38,7 +38,7 @@ batslib_err() {
{ if (( $# > 0 )); then
echo "$@"
else
- cat -
+ ( PATH="$_BATSLIB_PATH"; command cat -; )
fi
} >&2
}
@@ -273,7 +273,7 @@ batslib_mark() {
batslib_decorate() {
echo
echo "-- $1 --"
- cat -
+ ( PATH="$_BATSLIB_PATH"; command cat -; )
echo '--'
echo
}
diff --git a/test/50-output-10-batslib_err.bats b/test/50-output-10-batslib_err.bats
index 8c27fd1..89b9db1 100755
--- a/test/50-output-10-batslib_err.bats
+++ b/test/50-output-10-batslib_err.bats
@@ -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' ]
+ }
+}
diff --git a/test/50-output-19-batslib_decorate.bats b/test/50-output-19-batslib_decorate.bats
index 02d55ad..a4d4fcb 100755
--- a/test/50-output-19-batslib_decorate.bats
+++ b/test/50-output-19-batslib_decorate.bats
@@ -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]}" == '--' ]
+ }
+}
diff --git a/test/cat b/test/cat
new file mode 100755
index 0000000..c45d58f
--- /dev/null
+++ b/test/cat
@@ -0,0 +1 @@
+# Dummy file stubbing a stub/mock