|
1 | 1 | #!/usr/bin/env bash |
2 | 2 | # shellcheck disable=SC2317 |
| 3 | +# shellcheck disable=SC1003 # intentional literal trailing backslashes in test inputs |
3 | 4 |
|
4 | 5 | # Save original coverage state to restore after tests |
5 | 6 | _ORIG_COVERAGE_DATA_FILE="" |
@@ -283,3 +284,97 @@ function test_coverage_is_executable_line_returns_false_for_done_with_append_red |
283 | 284 | result=$(bashunit::coverage::is_executable_line ' done >> /tmp/out.log' 2 && echo "yes" || echo "no") |
284 | 285 | assert_equals "no" "$result" |
285 | 286 | } |
| 287 | + |
| 288 | +# --- _ends_with_continuation (#722) ----------------------------------------- |
| 289 | + |
| 290 | +function test_coverage_ends_with_continuation_true_for_single_trailing_backslash() { |
| 291 | + local result |
| 292 | + result=$(bashunit::coverage::_ends_with_continuation 'echo foo \' && echo "yes" || echo "no") |
| 293 | + assert_equals "yes" "$result" |
| 294 | +} |
| 295 | + |
| 296 | +function test_coverage_ends_with_continuation_true_for_indented_continuation() { |
| 297 | + local result |
| 298 | + result=$(bashunit::coverage::_ends_with_continuation ' some_command --flag \' && echo "yes" || echo "no") |
| 299 | + assert_equals "yes" "$result" |
| 300 | +} |
| 301 | + |
| 302 | +function test_coverage_ends_with_continuation_false_for_no_backslash() { |
| 303 | + local result |
| 304 | + result=$(bashunit::coverage::_ends_with_continuation 'echo foo' && echo "yes" || echo "no") |
| 305 | + assert_equals "no" "$result" |
| 306 | +} |
| 307 | + |
| 308 | +function test_coverage_ends_with_continuation_false_for_escaped_backslash() { |
| 309 | + local result |
| 310 | + result=$(bashunit::coverage::_ends_with_continuation 'echo foo \\' && echo "yes" || echo "no") |
| 311 | + assert_equals "no" "$result" |
| 312 | +} |
| 313 | + |
| 314 | +function test_coverage_ends_with_continuation_true_for_three_backslashes() { |
| 315 | + local result |
| 316 | + result=$(bashunit::coverage::_ends_with_continuation 'echo foo \\\' && echo "yes" || echo "no") |
| 317 | + assert_equals "yes" "$result" |
| 318 | +} |
| 319 | + |
| 320 | +function test_coverage_ends_with_continuation_false_for_trailing_whitespace_after_backslash() { |
| 321 | + local result |
| 322 | + result=$(bashunit::coverage::_ends_with_continuation 'echo foo \ ' && echo "yes" || echo "no") |
| 323 | + assert_equals "no" "$result" |
| 324 | +} |
| 325 | + |
| 326 | +function test_coverage_ends_with_continuation_false_for_comment_line() { |
| 327 | + local result |
| 328 | + result=$(bashunit::coverage::_ends_with_continuation '# a trailing slash in a comment \' && echo "yes" || echo "no") |
| 329 | + assert_equals "no" "$result" |
| 330 | +} |
| 331 | + |
| 332 | +function test_coverage_ends_with_continuation_false_for_empty_line() { |
| 333 | + local result |
| 334 | + result=$(bashunit::coverage::_ends_with_continuation '' && echo "yes" || echo "no") |
| 335 | + assert_equals "no" "$result" |
| 336 | +} |
| 337 | + |
| 338 | +# --- get_all_line_hits continuation propagation (#722) ---------------------- |
| 339 | + |
| 340 | +function test_coverage_get_all_line_hits_propagates_across_continuation_chain() { |
| 341 | + local dir |
| 342 | + dir=$(mktemp -d) |
| 343 | + _BASHUNIT_COVERAGE_DATA_FILE="${dir}/coverage.data" |
| 344 | + |
| 345 | + local src="${dir}/script.sh" |
| 346 | + printf '%s\n' 'echo start \' ' middle \' ' end' 'echo other' >"$src" |
| 347 | + |
| 348 | + # Start line (1) hit twice, standalone line (4) hit once. |
| 349 | + printf '%s\n' "${src}:1" "${src}:1" "${src}:4" >"$_BASHUNIT_COVERAGE_DATA_FILE" |
| 350 | + |
| 351 | + local result |
| 352 | + result=$(bashunit::coverage::get_all_line_hits "$src") |
| 353 | + |
| 354 | + local expected |
| 355 | + expected=$(printf '%s\n' "1:2" "2:2" "3:2" "4:1") |
| 356 | + |
| 357 | + rm -rf "$dir" 2>/dev/null || true |
| 358 | + _BASHUNIT_COVERAGE_DATA_FILE="" |
| 359 | + |
| 360 | + assert_equals "$expected" "$result" |
| 361 | +} |
| 362 | + |
| 363 | +function test_coverage_get_all_line_hits_does_not_propagate_without_continuation() { |
| 364 | + local dir |
| 365 | + dir=$(mktemp -d) |
| 366 | + _BASHUNIT_COVERAGE_DATA_FILE="${dir}/coverage.data" |
| 367 | + |
| 368 | + local src="${dir}/script.sh" |
| 369 | + printf '%s\n' 'echo one' 'echo two' >"$src" |
| 370 | + |
| 371 | + printf '%s\n' "${src}:1" >"$_BASHUNIT_COVERAGE_DATA_FILE" |
| 372 | + |
| 373 | + local result |
| 374 | + result=$(bashunit::coverage::get_all_line_hits "$src") |
| 375 | + |
| 376 | + rm -rf "$dir" 2>/dev/null || true |
| 377 | + _BASHUNIT_COVERAGE_DATA_FILE="" |
| 378 | + |
| 379 | + assert_equals "1:1" "$result" |
| 380 | +} |
0 commit comments