Skip to content

Commit cd397b5

Browse files
committed
perf(state): cache base64 -w flag detection at source time
Detect base64 -w flag support once at load time instead of running base64 --help | grep on every test subshell exit. Also use the cached flag in encode_base64 to avoid redundant command -v and error fallback.
1 parent 907e1cc commit cd397b5

2 files changed

Lines changed: 12 additions & 3 deletions

File tree

src/helpers.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,10 @@ function bashunit::helper::encode_base64() {
120120
return
121121
fi
122122

123-
if command -v base64 >/dev/null; then
124-
printf '%s' "$value" | base64 -w 0 2>/dev/null || printf '%s' "$value" | base64 | tr -d '\n'
123+
if [[ "$_BASHUNIT_BASE64_WRAP_FLAG" == true ]]; then
124+
printf '%s' "$value" | base64 -w 0
125+
elif command -v base64 >/dev/null; then
126+
printf '%s' "$value" | base64 | tr -d '\n'
125127
else
126128
printf '%s' "$value" | openssl enc -base64 -A
127129
fi

src/state.sh

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
#!/usr/bin/env bash
22

3+
# Cache base64 -w flag support (Alpine needs -w 0, macOS does not support -w)
4+
if base64 --help 2>&1 | grep -q -- "-w"; then
5+
_BASHUNIT_BASE64_WRAP_FLAG=true
6+
else
7+
_BASHUNIT_BASE64_WRAP_FLAG=false
8+
fi
9+
310
_BASHUNIT_TESTS_PASSED=0
411
_BASHUNIT_TESTS_FAILED=0
512
_BASHUNIT_TESTS_SKIPPED=0
@@ -218,7 +225,7 @@ function bashunit::state::export_subshell_context() {
218225

219226
local encoded_test_hook_message
220227

221-
if base64 --help 2>&1 | grep -q -- "-w"; then
228+
if [[ "$_BASHUNIT_BASE64_WRAP_FLAG" == true ]]; then
222229
# Alpine requires the -w 0 option to avoid wrapping
223230
encoded_test_output=$(echo -n "$_BASHUNIT_TEST_OUTPUT" | base64 -w 0)
224231
encoded_test_title=$(echo -n "$_BASHUNIT_TEST_TITLE" | base64 -w 0)

0 commit comments

Comments
 (0)