Skip to content

Commit 96a207a

Browse files
committed
fix: make test doubles strict mode compatible
Add error handling to spy/mock file operations to prevent failures in strict mode (set -e): - cat commands use || echo 0 fallback - sed/tail commands use || true fallback - read command uses || true for empty input Also restore --parallel in CI strict mode job.
1 parent bb7821b commit 96a207a

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,4 @@ jobs:
120120

121121
- name: Run Tests with strict mode
122122
run: |
123-
./bashunit --no-parallel --simple --strict tests/
123+
./bashunit --parallel --simple --strict tests/

src/test_doubles.sh

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ function bashunit::spy() {
6060
done
6161
serialized=\${serialized%$'\\x1f'}
6262
printf '%s\x1e%s\\n' \"\$raw\" \"\$serialized\" >> '$params_file'
63-
local _c=\$(cat '$times_file')
63+
local _c
64+
_c=\$(cat '$times_file' 2>/dev/null || echo 0)
6465
_c=\$((_c+1))
6566
echo \"\$_c\" > '$times_file'
6667
}"
@@ -77,7 +78,7 @@ function assert_have_been_called() {
7778
local file_var="${variable}_times_file"
7879
local times=0
7980
if [[ -f "${!file_var-}" ]]; then
80-
times=$(cat "${!file_var}")
81+
times=$(cat "${!file_var}" 2>/dev/null || echo 0)
8182
fi
8283
local label="${2:-$(bashunit::helper::normalize_test_function_name "${FUNCNAME[1]}")}"
8384

@@ -108,14 +109,14 @@ function assert_have_been_called_with() {
108109
local line=""
109110
if [[ -f "${!file_var-}" ]]; then
110111
if [[ -n $index ]]; then
111-
line=$(sed -n "${index}p" "${!file_var}")
112+
line=$(sed -n "${index}p" "${!file_var}" 2>/dev/null || true)
112113
else
113-
line=$(tail -n 1 "${!file_var}")
114+
line=$(tail -n 1 "${!file_var}" 2>/dev/null || true)
114115
fi
115116
fi
116117

117118
local raw
118-
IFS=$'\x1e' read -r raw _ <<<"$line"
119+
IFS=$'\x1e' read -r raw _ <<<"$line" || true
119120

120121
if [[ "$expected" != "$raw" ]]; then
121122
bashunit::state::add_assertions_failed
@@ -135,7 +136,7 @@ function assert_have_been_called_times() {
135136
local file_var="${variable}_times_file"
136137
local times=0
137138
if [[ -f "${!file_var-}" ]]; then
138-
times=$(cat "${!file_var}")
139+
times=$(cat "${!file_var}" 2>/dev/null || echo 0)
139140
fi
140141
local label="${3:-$(bashunit::helper::normalize_test_function_name "${FUNCNAME[1]}")}"
141142
if [[ $times -ne $expected_count ]]; then

0 commit comments

Comments
 (0)