Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

### Changed
- `assert_contains_ignore_case` folds case with `shopt -s nocasematch` on Bash 3.1+ instead of two `tr` subprocesses, and falls back to `tr` only on Bash 3.0. Roughly 10x faster in a run dominated by that assertion (300 calls: 1419ms -> 131ms), with identical results including non-ASCII folding
- Internal: `src/runner.sh` and `src/coverage.sh` are split into `src/runner/` and `src/coverage/` modules of single-responsibility files, each behind a `source`-only `index.sh` aggregator. A pure relocation, no behavior change; see [ADR-010](adrs/adr-010-src-module-directories.md) (#924, #925)

### Fixed
Expand Down
47 changes: 45 additions & 2 deletions src/assert/core.sh
Original file line number Diff line number Diff line change
Expand Up @@ -325,15 +325,58 @@ function assert_contains() {
bashunit::state::add_assertions_passed
}

##
# Whether `shopt -s nocasematch` exists. Introduced in Bash 3.1; bashunit's
# floor is 3.0, so the caller keeps a tr-based fallback for that one version.
# Returns: 0 when nocasematch is available (Bash >= 3.1), 1 otherwise.
##
function bashunit::assert::_supports_nocasematch() {
if [ "${BASH_VERSINFO[0]:-0}" -gt 3 ]; then
return 0
fi
[ "${BASH_VERSINFO[0]:-0}" -eq 3 ] && [ "${BASH_VERSINFO[1]:-0}" -ge 1 ]
}

function assert_contains_ignore_case() {
bashunit::assert::should_skip && return 0

local expected="$1"
local actual="$2"
local label_override="${3:-}"

# Bash 3.0 compatible: use tr for case-insensitive comparison
# (shopt nocasematch was introduced in Bash 3.1)
# nocasematch (Bash 3.1+) folds case inside the `case` itself, which costs no
# fork at all; the two `tr` pipelines below cost two. Measured on Bash 3.2:
# 0.087ms per call versus 12.8ms. Both fold non-ASCII identically in a UTF-8
# locale -- `ñü` matches `ÑÜ` either way -- which rules out the tempting
# pure-bash A-Z loop, since that is ASCII-only and would silently stop
# matching accented text that matches today.
#
# Prior state is saved and restored rather than blindly unset: nocasematch is
# a global shell option and a user's test file may already have set it. `shopt
# -q` is a builtin, so the save costs nothing.
if bashunit::assert::_supports_nocasematch; then
local nocase_was_set=1
shopt -q nocasematch || nocase_was_set=0
shopt -s nocasematch

local matched=1
case "$actual" in
*"$expected"*) ;;
*) matched=0 ;;
esac

[ "$nocase_was_set" -eq 1 ] || shopt -u nocasematch

if [ "$matched" -eq 0 ]; then
bashunit::assert::fail_with "${label_override:-}" "${actual}" "to contain" "${expected}"
return
fi

bashunit::state::add_assertions_passed
return
fi

# Bash 3.0 only: nocasematch does not exist, so fold with tr.
local expected_lower
local actual_lower
expected_lower=$(printf '%s' "$expected" | tr '[:upper:]' '[:lower:]')
Expand Down
61 changes: 61 additions & 0 deletions tests/unit/assert/string_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,64 @@ function test_unsuccessful_assert_string_not_matches_format() {
"42 items" "to not match format" "%d items")" \
"$(assert_string_not_matches_format "%d items" "42 items")"
}

# Guarded on the same predicate the production code uses: `shopt -u nocasematch`
# is itself an "invalid shell option name" error on Bash 3.0, so these tests
# cannot even set up there -- and on 3.0 the tr path runs, where there is no
# option to leak.
#
# nocasematch is a global shell option, so the fast path in
# assert_contains_ignore_case has to leave it exactly as it found it -- a user's
# test file may have set it deliberately, and silently clearing it would change
# how their own `case` statements match.
function test_assert_contains_ignore_case_leaves_nocasematch_unset_when_it_was_unset() {
if ! bashunit::assert::_supports_nocasematch; then
bashunit::skip "nocasematch requires Bash 3.1+"
return
fi

shopt -u nocasematch

assert_contains_ignore_case "world" "Hello WORLD"

if shopt -q nocasematch; then
bashunit::fail "nocasematch leaked ON into the caller"
fi
}

function test_assert_contains_ignore_case_leaves_nocasematch_set_when_it_was_set() {
if ! bashunit::assert::_supports_nocasematch; then
bashunit::skip "nocasematch requires Bash 3.1+"
return
fi

shopt -s nocasematch

assert_contains_ignore_case "world" "Hello WORLD"

local still_set=1
shopt -q nocasematch || still_set=0
shopt -u nocasematch

assert_equals "1" "$still_set"
}

# Non-ASCII folding is why the tr fallback cannot be replaced with a pure-bash
# A-Z loop: where the locale folds accented text, both nocasematch and tr do it
# and an ASCII-only loop would silently stop matching.
#
# Whether it folds at all is a property of the platform's locale, not of
# bashunit: under LC_ALL=C neither path folds, which is correct and is what Git
# Bash on Windows does. So the capability is probed first and the assertion is
# skipped where it does not apply, rather than pinning a UTF-8-only outcome.
function test_assert_contains_ignore_case_folds_non_ascii() {
local folded
folded=$(printf '%s' "ÑÜ" | tr '[:upper:]' '[:lower:]')
if [ "$folded" != "ñü" ]; then
bashunit::skip "locale does not fold non-ASCII case"
return
fi

assert_contains_ignore_case "ñü" "Test ÑÜ string"
assert_contains_ignore_case "ÑÜ" "test ñü string"
}
Loading