Skip to content

Commit db47208

Browse files
authored
ref: codebase cleanup sweep — dedup, constant, dead-code (#847)
1 parent e3adc9b commit db47208

13 files changed

Lines changed: 115 additions & 180 deletions

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Changed
66
- Core string assertions (`assert_contains`/`assert_not_contains`, `assert_matches`/`assert_not_matches`, `assert_string_starts_with`/`assert_string_ends_with` and their negations) no longer fork a subshell per call to join their arguments; a fork-free join with identical behaviour replaces it (#844)
7+
- The array, date, duration, json, files and folders assertions now resolve their failure label through the fork-free slot helper instead of a per-call command substitution — same labels, fewer forks
78

89
## [0.42.0](https://github.com/TypedDevs/bashunit/compare/0.41.0...0.42.0) - 2026-07-20
910

‎src/assert.sh‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ function bashunit::assert::label_to_slot() {
3131
_BASHUNIT_ASSERT_JOINED_OUT=""
3232

3333
# Join positional args into _BASHUNIT_ASSERT_JOINED_OUT with no fork.
34-
# Mirrors $(printf '%s\n' "$@"): joins with newlines and strips trailing
35-
# newlines, so callers that match on the result behave exactly as the previous
36-
# command-substitution did.
34+
# Output matches $(printf '%s\n' "$@") exactly: newline-joined, trailing
35+
# newlines stripped (as command substitution strips them).
3736
function bashunit::assert::join_to_slot() {
3837
local IFS=$'\n'
3938
local joined="$*"

‎src/assert_arrays.sh‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,8 @@ function assert_array_contains() {
5656
bashunit::assert::should_skip && return 0
5757

5858
local expected="$1"
59-
local test_fn
60-
test_fn="$(bashunit::helper::find_test_function_name)"
61-
local label
62-
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
59+
bashunit::assert::label_to_slot
60+
local label=$_BASHUNIT_ASSERT_LABEL_OUT
6361
shift
6462

6563
local -a actual
@@ -82,10 +80,8 @@ function assert_array_length() {
8280
bashunit::assert::should_skip && return 0
8381

8482
local expected="$1"
85-
local test_fn
86-
test_fn="$(bashunit::helper::find_test_function_name)"
87-
local label
88-
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
83+
bashunit::assert::label_to_slot
84+
local label=$_BASHUNIT_ASSERT_LABEL_OUT
8985
shift
9086

9187
# Use $# / $* rather than building an array: on Bash 3.0 under `set -u`,
@@ -106,10 +102,8 @@ function assert_array_not_contains() {
106102
bashunit::assert::should_skip && return 0
107103

108104
local expected="$1"
109-
local test_fn
110-
test_fn="$(bashunit::helper::find_test_function_name)"
111-
local label
112-
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
105+
bashunit::assert::label_to_slot
106+
local label=$_BASHUNIT_ASSERT_LABEL_OUT
113107
shift
114108
local -a actual
115109
actual=("$@")

‎src/assert_dates.sh‎

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,8 @@ function assert_date_equals() {
105105
actual="$(bashunit::date::to_epoch "$2")"
106106

107107
if [ "$actual" -ne "$expected" ]; then
108-
local test_fn
109-
test_fn="$(bashunit::helper::find_test_function_name)"
110-
local label
111-
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
108+
bashunit::assert::label_to_slot
109+
local label=$_BASHUNIT_ASSERT_LABEL_OUT
112110
bashunit::assert::mark_failed
113111
bashunit::console_results::print_failed_test "${label}" "${actual}" "to be equal to" "${expected}"
114112
return
@@ -126,10 +124,8 @@ function assert_date_before() {
126124
actual="$(bashunit::date::to_epoch "$2")"
127125

128126
if [ "$actual" -ge "$expected" ]; then
129-
local test_fn
130-
test_fn="$(bashunit::helper::find_test_function_name)"
131-
local label
132-
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
127+
bashunit::assert::label_to_slot
128+
local label=$_BASHUNIT_ASSERT_LABEL_OUT
133129
bashunit::assert::mark_failed
134130
bashunit::console_results::print_failed_test "${label}" "${actual}" "to be before" "${expected}"
135131
return
@@ -147,10 +143,8 @@ function assert_date_after() {
147143
actual="$(bashunit::date::to_epoch "$2")"
148144

149145
if [ "$actual" -le "$expected" ]; then
150-
local test_fn
151-
test_fn="$(bashunit::helper::find_test_function_name)"
152-
local label
153-
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
146+
bashunit::assert::label_to_slot
147+
local label=$_BASHUNIT_ASSERT_LABEL_OUT
154148
bashunit::assert::mark_failed
155149
bashunit::console_results::print_failed_test "${label}" "${actual}" "to be after" "${expected}"
156150
return
@@ -170,10 +164,8 @@ function assert_date_within_range() {
170164
actual="$(bashunit::date::to_epoch "$3")"
171165

172166
if [ "$actual" -lt "$from" ] || [ "$actual" -gt "$to" ]; then
173-
local test_fn
174-
test_fn="$(bashunit::helper::find_test_function_name)"
175-
local label
176-
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
167+
bashunit::assert::label_to_slot
168+
local label=$_BASHUNIT_ASSERT_LABEL_OUT
177169
bashunit::assert::mark_failed
178170
bashunit::console_results::print_failed_test "${label}" "${actual}" "to be between" "${from} and ${to}"
179171
return
@@ -197,10 +189,8 @@ function assert_date_within_delta() {
197189
fi
198190

199191
if [ "$diff" -gt "$delta" ]; then
200-
local test_fn
201-
test_fn="$(bashunit::helper::find_test_function_name)"
202-
local label
203-
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
192+
bashunit::assert::label_to_slot
193+
local label=$_BASHUNIT_ASSERT_LABEL_OUT
204194
bashunit::assert::mark_failed
205195
bashunit::console_results::print_failed_test "${label}" "${actual}" "to be within" "${delta} seconds of ${expected}"
206196
return

‎src/assert_duration.sh‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@ function assert_duration() {
2727
elapsed_ms=$(bashunit::duration::measure_ms "$command")
2828

2929
if [ "$elapsed_ms" -gt "$threshold_ms" ]; then
30-
local test_fn
31-
test_fn="$(bashunit::helper::find_test_function_name)"
32-
local label
33-
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
30+
bashunit::assert::label_to_slot
31+
local label=$_BASHUNIT_ASSERT_LABEL_OUT
3432
bashunit::assert::mark_failed
3533
bashunit::console_results::print_failed_test "${label}" "${threshold_ms}" "to complete within (ms)" "${command}"
3634
return
@@ -49,10 +47,8 @@ function assert_duration_less_than() {
4947
elapsed_ms=$(bashunit::duration::measure_ms "$command")
5048

5149
if [ "$elapsed_ms" -ge "$threshold_ms" ]; then
52-
local test_fn
53-
test_fn="$(bashunit::helper::find_test_function_name)"
54-
local label
55-
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
50+
bashunit::assert::label_to_slot
51+
local label=$_BASHUNIT_ASSERT_LABEL_OUT
5652
bashunit::assert::mark_failed
5753
bashunit::console_results::print_failed_test "${label}" "${threshold_ms}" "to complete within (ms)" "${command}"
5854
return
@@ -71,10 +67,8 @@ function assert_duration_greater_than() {
7167
elapsed_ms=$(bashunit::duration::measure_ms "$command")
7268

7369
if [ "$elapsed_ms" -le "$threshold_ms" ]; then
74-
local test_fn
75-
test_fn="$(bashunit::helper::find_test_function_name)"
76-
local label
77-
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
70+
bashunit::assert::label_to_slot
71+
local label=$_BASHUNIT_ASSERT_LABEL_OUT
7872
bashunit::assert::mark_failed
7973
bashunit::console_results::print_failed_test "${label}" "${threshold_ms}" "to take at least (ms)" "${command}"
8074
return

‎src/assert_files.sh‎

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ function assert_file_exists() {
44
bashunit::assert::should_skip && return 0
55

66
local expected="$1"
7-
local test_fn
8-
test_fn="$(bashunit::helper::find_test_function_name)"
9-
local label="${3:-$(bashunit::helper::normalize_test_function_name "$test_fn")}"
107

118
if [ ! -f "$expected" ]; then
9+
bashunit::assert::label_to_slot "${3:-}"
10+
local label=$_BASHUNIT_ASSERT_LABEL_OUT
1211
bashunit::assert::mark_failed
1312
bashunit::console_results::print_failed_test "${label}" "${expected}" "to exist but" "do not exist"
1413
return
@@ -21,11 +20,10 @@ function assert_file_not_exists() {
2120
bashunit::assert::should_skip && return 0
2221

2322
local expected="$1"
24-
local test_fn
25-
test_fn="$(bashunit::helper::find_test_function_name)"
26-
local label="${3:-$(bashunit::helper::normalize_test_function_name "$test_fn")}"
2723

2824
if [ -f "$expected" ]; then
25+
bashunit::assert::label_to_slot "${3:-}"
26+
local label=$_BASHUNIT_ASSERT_LABEL_OUT
2927
bashunit::assert::mark_failed
3028
bashunit::console_results::print_failed_test "${label}" "${expected}" "to not exist but" "the file exists"
3129
return
@@ -38,11 +36,10 @@ function assert_is_file() {
3836
bashunit::assert::should_skip && return 0
3937

4038
local expected="$1"
41-
local test_fn
42-
test_fn="$(bashunit::helper::find_test_function_name)"
43-
local label="${3:-$(bashunit::helper::normalize_test_function_name "$test_fn")}"
4439

4540
if [ ! -f "$expected" ]; then
41+
bashunit::assert::label_to_slot "${3:-}"
42+
local label=$_BASHUNIT_ASSERT_LABEL_OUT
4643
bashunit::assert::mark_failed
4744
bashunit::console_results::print_failed_test "${label}" "${expected}" "to be a file" "but is not a file"
4845
return
@@ -55,11 +52,10 @@ function assert_is_file_empty() {
5552
bashunit::assert::should_skip && return 0
5653

5754
local expected="$1"
58-
local test_fn
59-
test_fn="$(bashunit::helper::find_test_function_name)"
60-
local label="${3:-$(bashunit::helper::normalize_test_function_name "$test_fn")}"
6155

6256
if [ -s "$expected" ]; then
57+
bashunit::assert::label_to_slot "${3:-}"
58+
local label=$_BASHUNIT_ASSERT_LABEL_OUT
6359
bashunit::assert::mark_failed
6460
bashunit::console_results::print_failed_test "${label}" "${expected}" "to be empty" "but is not empty"
6561
return
@@ -75,10 +71,8 @@ function assert_files_equals() {
7571
local actual="$2"
7672

7773
if [ "$(diff -u "$expected" "$actual")" != '' ]; then
78-
local test_fn
79-
test_fn="$(bashunit::helper::find_test_function_name)"
80-
local label
81-
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
74+
bashunit::assert::label_to_slot
75+
local label=$_BASHUNIT_ASSERT_LABEL_OUT
8276
bashunit::assert::mark_failed
8377

8478
bashunit::console_results::print_failed_test "${label}" "${expected}" "Compared" "${actual}" \
@@ -96,10 +90,8 @@ function assert_files_not_equals() {
9690
local actual="$2"
9791

9892
if [ "$(diff -u "$expected" "$actual")" = '' ]; then
99-
local test_fn
100-
test_fn="$(bashunit::helper::find_test_function_name)"
101-
local label
102-
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
93+
bashunit::assert::label_to_slot
94+
local label=$_BASHUNIT_ASSERT_LABEL_OUT
10395
bashunit::assert::mark_failed
10496

10597
bashunit::console_results::print_failed_test "${label}" "${expected}" "Compared" "${actual}" \
@@ -117,10 +109,8 @@ function assert_file_contains() {
117109
local string="$2"
118110

119111
if ! grep -F -q "$string" "$file"; then
120-
local test_fn
121-
test_fn="$(bashunit::helper::find_test_function_name)"
122-
local label
123-
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
112+
bashunit::assert::label_to_slot
113+
local label=$_BASHUNIT_ASSERT_LABEL_OUT
124114
bashunit::assert::mark_failed
125115

126116
bashunit::console_results::print_failed_test "${label}" "${file}" "to contain" "${string}"
@@ -137,10 +127,8 @@ function assert_file_not_contains() {
137127
local string="$2"
138128

139129
if grep -q "$string" "$file"; then
140-
local test_fn
141-
test_fn="$(bashunit::helper::find_test_function_name)"
142-
local label
143-
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
130+
bashunit::assert::label_to_slot
131+
local label=$_BASHUNIT_ASSERT_LABEL_OUT
144132
bashunit::assert::mark_failed
145133

146134
bashunit::console_results::print_failed_test "${label}" "${file}" "to not contain" "${string}"
@@ -172,10 +160,8 @@ function assert_file_permissions() {
172160

173161
local expected="$1"
174162
local file="$2"
175-
local test_fn
176-
test_fn="$(bashunit::helper::find_test_function_name)"
177-
local label
178-
label="$(bashunit::helper::normalize_test_function_name "$test_fn")"
163+
bashunit::assert::label_to_slot
164+
local label=$_BASHUNIT_ASSERT_LABEL_OUT
179165

180166
if [ ! -e "$file" ]; then
181167
bashunit::assert::mark_failed

0 commit comments

Comments
 (0)