From 88252e085e8d62136fda3243457cf2b905664246 Mon Sep 17 00:00:00 2001 From: Rainer Poisel Date: Tue, 10 May 2022 00:18:44 +0200 Subject: [PATCH] Add example that demonstrates how to mock commands called by functions under test --- doc/contributors.md | 1 + examples/lib.inc | 12 ++++++ examples/mock_cmd_complex_test.sh | 68 +++++++++++++++++++++++++++++++ examples/mock_cmd_simple_test.sh | 50 +++++++++++++++++++++++ 4 files changed, 131 insertions(+) create mode 100644 examples/lib.inc create mode 100755 examples/mock_cmd_complex_test.sh create mode 100755 examples/mock_cmd_simple_test.sh diff --git a/doc/contributors.md b/doc/contributors.md index 7adae22..8fc7a81 100644 --- a/doc/contributors.md +++ b/doc/contributors.md @@ -13,3 +13,4 @@ contributed in some way or another to shunit2. - Rocky Bernstein - [rugk](https://github.com/rugk) - wood4321 (of code.google.com) +- [Rainer Poisel](https://github.com/rpoisel) diff --git a/examples/lib.inc b/examples/lib.inc new file mode 100644 index 0000000..dceb380 --- /dev/null +++ b/examples/lib.inc @@ -0,0 +1,12 @@ +# available as examples/lib.inc + +myFunc() { + local msg="${1}" + + local log + log="[$(date)] ${msg}" + echo "${log}" + if [ -n "${LOG_FILE_PATH}" ]; then + echo "${log}" 2>/dev/null >> "${LOG_FILE_PATH}" + fi +} diff --git a/examples/mock_cmd_complex_test.sh b/examples/mock_cmd_complex_test.sh new file mode 100755 index 0000000..8dc296c --- /dev/null +++ b/examples/mock_cmd_complex_test.sh @@ -0,0 +1,68 @@ +#!/bin/sh +# +# shUnit2 example for mocking commands. +# +# This example demonstrates a more flexible mechanism for mocking external +# dependencies such as commands or functions. +# +# In this example, the behavior of the mocking function `date` can be changed +# by defining the logic in a global variable `__ACTION`. Please note that this +# approach would not work when executing tests in parallel due to race conditions +# when reading/writing the global `__ACTION` variable. + +. ./lib.inc + +__ACTION="true" + +# dynamic mock (overrides original dependency) +date() { + eval "${__ACTION}" +} + +#dedicated function implementing the mock logic +dying_date_func() { + exit 1 +} + +testMyFuncDateDies() { + __ACTION="dying_date_func" + LOG_FILE_PATH="/tmp/01.log" + + local result rc exists contents + result=$(myFunc "some message") + rc=$? + + assertEquals 0 "${rc}" + assertEquals "[] some message" "${result}" + + exists=0 + [ -e "/tmp/01.log" ] && exists=1 + assertEquals 1 "${exists}" + contents=$(cat /tmp/01.log) + assertEquals "[] some message" "${contents}" +} + +testMyFuncDoesSomethingMeaningful() { + __ACTION="echo \"now\"" + LOG_FILE_PATH="/tmp/02.log" + + local result rc exists contents + result=$(myFunc "some message") + rc=$? + + assertEquals 0 "${rc}" + assertEquals "[now] some message" "${result}" + + exists=0 + [ -e "/tmp/02.log" ] && exists=1 + assertEquals 1 "${exists}" + contents=$(cat /tmp/02.log) + assertEquals "[now] some message" "${contents}" +} + +setUp() { + cp /dev/null /tmp/01.log + cp /dev/null /tmp/02.log +} + +. ../shunit2 diff --git a/examples/mock_cmd_simple_test.sh b/examples/mock_cmd_simple_test.sh new file mode 100755 index 0000000..630bbad --- /dev/null +++ b/examples/mock_cmd_simple_test.sh @@ -0,0 +1,50 @@ +#!/bin/sh +# +# shUnit2 example for mocking commands. +# +# This example demonstrates a simple mechanism for mocking external +# dependencies such as commands or functions. +# +# In this example, the mocking function `date` behaves the same for +# all tests. + +. ./lib.inc + +# static mock (overrides original dependency) +date() { + echo "now" +} + +testMyFuncMissingPath() { + unset LOG_FILE_PATH + + local result rc + result=$(myFunc "some message") + rc=$? + + assertEquals 0 "${rc}" + assertEquals "[now] some message" "${result}" +} + +testMyFuncHappy() { + LOG_FILE_PATH="/tmp/01.log" + + local result rc exists contents + result=$(myFunc "some message") + rc=$? + + assertEquals 0 "${rc}" + assertEquals "[now] some message" "${result}" + + exists=0 + [ -e "/tmp/01.log" ] && exists=1 + assertEquals 1 "${exists}" + contents=$(cat /tmp/01.log) + assertEquals "[now] some message" "${contents}" +} + +setUp() { + cp /dev/null /tmp/01.log +} + +. ../shunit2