Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,11 @@ function bashunit::helper::normalize_variable_name() {
normalized_string="${input_string//[^a-zA-Z0-9_]/_}"

local _re='^[a-zA-Z_]'
if [ "$(echo "$normalized_string" | "$GREP" -cE "$_re" || true)" -eq 0 ]; then
if [ "$(builtin echo "$normalized_string" | "$GREP" -cE "$_re" || true)" -eq 0 ]; then
normalized_string="_$normalized_string"
fi

echo "$normalized_string"
builtin echo "$normalized_string"
}

function bashunit::helper::get_provider_data() {
Expand Down
16 changes: 8 additions & 8 deletions src/test_doubles.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function bashunit::mock() {
if [ $# -gt 0 ]; then
eval "function $command() { $* \"\$@\"; }"
else
eval "function $command() { echo \"$($CAT)\" ; }"
eval "function $command() { builtin echo \"$($CAT)\" ; }"
fi

export -f "${command?}"
Expand All @@ -61,14 +61,14 @@ function bashunit::spy() {
local serialized=\"\"
local arg
for arg in \"\$@\"; do
serialized=\"\$serialized\$(printf '%q' \"\$arg\")$'\\x1f'\"
serialized=\"\$serialized\$(builtin printf '%q' \"\$arg\")$'\\x1f'\"
done
serialized=\${serialized%$'\\x1f'}
printf '%s\x1e%s\\n' \"\$raw\" \"\$serialized\" >> '$params_file'
builtin printf '%s\x1e%s\\n' \"\$raw\" \"\$serialized\" >> '$params_file'
local _c
_c=\$(cat '$times_file' 2>/dev/null || echo 0)
_c=\$(cat '$times_file' 2>/dev/null || builtin echo 0)
_c=\$((_c+1))
echo \"\$_c\" > '$times_file'
builtin echo \"\$_c\" > '$times_file'
}"

export -f "${command?}"
Expand All @@ -83,7 +83,7 @@ function assert_have_been_called() {
local file_var="${variable}_times_file"
local times=0
if [ -f "${!file_var-}" ]; then
times=$(cat "${!file_var}" 2>/dev/null || echo 0)
times=$(cat "${!file_var}" 2>/dev/null || builtin echo 0)
fi
local label="${2:-$(bashunit::helper::normalize_test_function_name "${FUNCNAME[1]}")}"

Expand Down Expand Up @@ -141,7 +141,7 @@ function assert_have_been_called_times() {
local file_var="${variable}_times_file"
local times=0
if [ -f "${!file_var-}" ]; then
times=$(cat "${!file_var}" 2>/dev/null || echo 0)
times=$(cat "${!file_var}" 2>/dev/null || builtin echo 0)
fi
local label="${3:-$(bashunit::helper::normalize_test_function_name "${FUNCNAME[1]}")}"
if [ "$times" -ne "$expected_count" ]; then
Expand Down Expand Up @@ -170,7 +170,7 @@ function assert_have_been_called_nth_with() {

local times=0
if [ -f "${!times_file_var-}" ]; then
times=$(cat "${!times_file_var}" 2>/dev/null || echo 0)
times=$(cat "${!times_file_var}" 2>/dev/null || builtin echo 0)
fi

if [ "$nth" -gt "$times" ]; then
Expand Down
18 changes: 18 additions & 0 deletions tests/functional/doubles_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,21 @@ function test_mock_mktemp_does_not_break_spy_creation() {
assert_have_been_called_times 1 rm
assert_have_been_called_with rm "-f" "/tmp/mocked_temp_file"
}

function test_spy_on_echo_does_not_hang() {
source ./tests/functional/fixtures/echo_function.sh
bashunit::spy echo

write_message "hello world"

assert_have_been_called echo
}

function test_spy_on_printf_does_not_hang() {
source ./tests/functional/fixtures/printf_function.sh
bashunit::spy printf

format_message "hello world"

assert_have_been_called printf
}
5 changes: 5 additions & 0 deletions tests/functional/fixtures/echo_function.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

function write_message() {
echo "message: $*"
}
5 changes: 5 additions & 0 deletions tests/functional/fixtures/printf_function.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

function format_message() {
printf "formatted: %s\n" "$*"
}
2 changes: 2 additions & 0 deletions tests/unit/test_doubles_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,5 @@ function test_unsuccessful_spy_nth_called_with_invalid_index() {
"expected call" "at index 5 but" "only called 1 times")" \
"$(assert_have_been_called_nth_with 5 ps "first")"
}


Loading