Skip to content

Commit 352e7d8

Browse files
committed
test: verify post-increment fix under set -e (#618)
Add tests that run get_executable_lines, report_lcov, and str::rpad in subshells with set -e to ensure ((++var)) does not cause silent early exit when the counter starts at zero.
1 parent dc7b89f commit 352e7d8

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

tests/unit/coverage_core_test.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,29 @@ EOF
174174
rm -f "$temp_file"
175175
}
176176

177+
function test_coverage_get_executable_lines_does_not_exit_under_set_e() {
178+
local temp_file
179+
temp_file=$(mktemp)
180+
181+
cat >"$temp_file" <<'EOF'
182+
#!/usr/bin/env bash
183+
echo "line 1"
184+
echo "line 2"
185+
EOF
186+
187+
# ((var++)) when var=0 evaluates to 0 (falsy) causing exit code 1;
188+
# under set -e this silently terminates the function (#618)
189+
local result
190+
result=$(
191+
set -e
192+
bashunit::coverage::get_executable_lines "$temp_file"
193+
)
194+
195+
assert_equals "2" "$result"
196+
197+
rm -f "$temp_file"
198+
}
199+
177200
function test_coverage_record_line_writes_to_file() {
178201
BASHUNIT_COVERAGE="true"
179202
BASHUNIT_COVERAGE_PATHS="/"

tests/unit/coverage_reporting_test.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,40 @@ EOF
146146
rm -f "$temp_file" "$report_file"
147147
}
148148

149+
function test_coverage_report_lcov_completes_under_set_e() {
150+
BASHUNIT_COVERAGE="true"
151+
bashunit::coverage::init
152+
153+
local temp_file
154+
temp_file=$(mktemp)
155+
cat >"$temp_file" <<'EOF'
156+
#!/usr/bin/env bash
157+
echo "line 1"
158+
echo "line 2"
159+
EOF
160+
161+
echo "$temp_file" >"$_BASHUNIT_COVERAGE_TRACKED_FILES"
162+
163+
local report_file
164+
report_file=$(mktemp)
165+
166+
# ((lineno++)) when lineno=0 returns exit code 1 under set -e
167+
# causing incomplete LCOV output (#618)
168+
(
169+
set -e
170+
bashunit::coverage::report_lcov "$report_file"
171+
)
172+
173+
local content
174+
content=$(cat "$report_file")
175+
176+
assert_contains "end_of_record" "$content"
177+
assert_contains "DA:2," "$content"
178+
assert_contains "DA:3," "$content"
179+
180+
rm -f "$temp_file" "$report_file"
181+
}
182+
149183
function test_coverage_report_text_shows_no_files_message() {
150184
BASHUNIT_COVERAGE="true"
151185
bashunit::coverage::init

tests/unit/str_test.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ function test_rpad_custom_width_padding_text_too_long_and_special_chars() {
5151
"$actual"
5252
}
5353

54+
function test_rpad_does_not_exit_under_set_e() {
55+
# ((i++)) when i=0 evaluates to 0 (falsy) causing exit code 1;
56+
# under set -e this silently terminates the function (#618)
57+
local actual
58+
actual=$(
59+
set -e
60+
bashunit::str::rpad "input" "1" 20
61+
)
62+
63+
assert_same "input 1" "$actual"
64+
}
65+
5466
function test_rpad_width_smaller_than_right_word() {
5567
local actual=$(bashunit::str::rpad "foo" "verylongword" 5)
5668

0 commit comments

Comments
 (0)