| Q |
A |
| OS |
Linux (Cygwin on Windows) |
| Shell & version |
bash 5.2.21(1)-release |
| bashunit version |
0.33.0 |
Summary
Mocking mktemp breaks creation of spies
Current behavior
Creating a mock of mktemp breaks creation of spies after the bashunit::mock call
How to reproduce
function test_thing() {
tempfile="$(mktemp -t)"
mock_mktemp() {
echo "$tempfile"
}
bashunit::mock mktemp mock_mktemp
bashunit::spy rm
__function_being_tested
assert_exit_code 0
assert_have_been_called_times 1 rm
assert_have_been_called with rm '-f' "$tempfile"
}
Error from bashunit:
|./lib/bashunit: line 5428: function rm() {
| local raw="$*"
| local serialized=""
| local arg
| for arg in "$@"; do
| serialized="$serialized$(printf '%q' "$arg")$''"
| done
| serialized=${serialized%$''}
| printf '%s%s
|' "$raw" "$serialized" >> '/tmp/tmp.SurPLXBvsn'
| local _c
| _c=$(cat '/tmp/tmp.SurPLXBvsn' 2>/dev/null || echo 0)
| _c=$((_c+1))
| echo "$_c" > '/tmp/tmp.SurPLXBvsn'
| }$'function rm() {
| local raw="$*"
| local serialized=""
| local arg
| for arg in "$@"; do
| serialized="$serialized$(printf \'%q\' "$arg")$\'\x1f\'"
| done
| serialized=${serialized%$\'\x1f\'}
| printf \'%s\x1e%s\n\' "$raw" "$serialized" >> \'/tmp/tmp.SurPLXBvsn\'
| local _c
| _c=$(cat \'/tmp/tmp.SurPLXBvsn\' 2>/dev/null || echo 0)
| _c=$((_c+1))
| echo "$_c" > \'/tmp/tmp.SurPLXBvsn\'
| }'$'': syntax error in expression (error token is "rm() {
| local raw="$*"
| local serialized=""
| local arg
| for arg in "$@"; do
| serialized="$serialized$(printf '%q' "$arg")$''"
| done
| serialized=${serialized%$''}
| printf '%s%s
|' "$raw" "$serialized" >> '/tmp/tmp.SurPLXBvsn'
| local _c
| _c=$(cat '/tmp/tmp.SurPLXBvsn' 2>/dev/null || echo 0)
| _c=$((_c+1))
| echo "$_c" > '/tmp/tmp.SurPLXBvsn'
| }$'function rm() {
| local raw="$*"
| local serialized=""
| local arg
| for arg in "$@"; do
| serialized="$serialized$(printf \'%q\' "$arg")$\'\x1f\'"
| done
| serialized=${serialized%$\'\x1f\'}
| printf \'%s\x1e%s\n\' "$raw" "$serialized" >> \'/tmp/tmp.SurPLXBvsn\'
| local _c
| _c=$(cat \'/tmp/tmp.SurPLXBvsn\' 2>/dev/null || echo 0)
| _c=$((_c+1))
| echo "$_c" > \'/tmp/tmp.SurPLXBvsn\'
| }'$''")
Likely cause
bashunit is making unprotected calls to mktemp without referencing the full path to the program, e.g.:
# problematic behavior
mktemp "$file"
# less (non?) problematic behavior
/usr/bin/mktemp "$file"
#or
"$(command -pv mktemp)" "$file"
Expected behavior
I can mock any standard program and not have bashunit break
Summary
Mocking
mktempbreaks creation of spiesCurrent behavior
Creating a mock of
mktempbreaks creation of spies after thebashunit::mockcallHow to reproduce
Error from bashunit:
Likely cause
bashunitis making unprotected calls tomktempwithout referencing the full path to the program, e.g.:Expected behavior
I can mock any standard program and not have bashunit break