Skip to content

Commit e9be71b

Browse files
committed
fix(compat): address PR feedback for Bash 3.0 compatibility
- Remove duplicate local declaration in doc.sh - Fix uninitialized array variables in assert.sh by using safe initialization pattern - Clarify IFS usage in coverage.sh by moving to function start - Remove unnecessary count initialization in coverage.sh - Format all files with shfmt
1 parent 421fbee commit e9be71b

125 files changed

Lines changed: 2492 additions & 2549 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

bashunit

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ function _check_bash_version() {
1717
fi
1818

1919
local major
20-
IFS=. read -r major _ <<< "$current_version"
20+
IFS=. read -r major _ <<<"$current_version"
2121

22-
if (( major < 3 )); then
22+
if ((major < 3)); then
2323
printf 'Bashunit requires Bash >= %s. Current version: %s\n' "$BASHUNIT_MIN_BASH_VERSION" "$current_version" >&2
2424
exit 1
2525
fi
@@ -41,16 +41,16 @@ export BASHUNIT_WORKING_DIR
4141
# Early scan for flags that must be set before loading env.sh
4242
for arg in "$@"; do
4343
case "$arg" in
44-
--skip-env-file)
45-
export BASHUNIT_SKIP_ENV_FILE=true
46-
;;
47-
-l|--login)
48-
export BASHUNIT_LOGIN_SHELL=true
49-
;;
50-
--no-color)
51-
# shellcheck disable=SC2034
52-
BASHUNIT_NO_COLOR=true
53-
;;
44+
--skip-env-file)
45+
export BASHUNIT_SKIP_ENV_FILE=true
46+
;;
47+
-l | --login)
48+
export BASHUNIT_LOGIN_SHELL=true
49+
;;
50+
--no-color)
51+
# shellcheck disable=SC2034
52+
BASHUNIT_NO_COLOR=true
53+
;;
5454
esac
5555
done
5656

@@ -88,39 +88,39 @@ bashunit::clock::init
8888
_SUBCOMMAND=""
8989

9090
case "${1:-}" in
91-
test|bench|doc|init|learn|upgrade|assert)
92-
_SUBCOMMAND="$1"
93-
shift
94-
;;
95-
-v|--version)
96-
bashunit::console_header::print_version
97-
exit 0
98-
;;
99-
-h|--help)
100-
bashunit::console_header::print_help
101-
exit 0
102-
;;
103-
-*)
104-
# Flag without subcommand → assume "test"
105-
_SUBCOMMAND="test"
106-
;;
107-
"")
108-
# No arguments → assume "test" (uses BASHUNIT_DEFAULT_PATH)
109-
_SUBCOMMAND="test"
110-
;;
111-
*)
112-
# Path argument → assume "test"
113-
_SUBCOMMAND="test"
114-
;;
91+
test | bench | doc | init | learn | upgrade | assert)
92+
_SUBCOMMAND="$1"
93+
shift
94+
;;
95+
-v | --version)
96+
bashunit::console_header::print_version
97+
exit 0
98+
;;
99+
-h | --help)
100+
bashunit::console_header::print_help
101+
exit 0
102+
;;
103+
-*)
104+
# Flag without subcommand → assume "test"
105+
_SUBCOMMAND="test"
106+
;;
107+
"")
108+
# No arguments → assume "test" (uses BASHUNIT_DEFAULT_PATH)
109+
_SUBCOMMAND="test"
110+
;;
111+
*)
112+
# Path argument → assume "test"
113+
_SUBCOMMAND="test"
114+
;;
115115
esac
116116

117117
# Route to subcommand handler
118118
case "$_SUBCOMMAND" in
119-
test) bashunit::main::cmd_test "$@" ;;
120-
bench) bashunit::main::cmd_bench "$@" ;;
121-
doc) bashunit::main::cmd_doc "$@" ;;
122-
init) bashunit::main::cmd_init "$@" ;;
123-
learn) bashunit::main::cmd_learn "$@" ;;
124-
upgrade) bashunit::main::cmd_upgrade "$@" ;;
125-
assert) bashunit::main::cmd_assert "$@" ;;
119+
test) bashunit::main::cmd_test "$@" ;;
120+
bench) bashunit::main::cmd_bench "$@" ;;
121+
doc) bashunit::main::cmd_doc "$@" ;;
122+
init) bashunit::main::cmd_init "$@" ;;
123+
learn) bashunit::main::cmd_learn "$@" ;;
124+
upgrade) bashunit::main::cmd_upgrade "$@" ;;
125+
assert) bashunit::main::cmd_assert "$@" ;;
126126
esac

bin/create-pr

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function console_header::print_version() {
2121
}
2222

2323
function console_header::print_help() {
24-
cat <<EOF
24+
cat <<EOF
2525
create-pr [arguments] [options]
2626
2727
Arguments:
@@ -59,8 +59,8 @@ set -o allexport
5959
[[ -f ".env" ]] && source .env set
6060
set +o allexport
6161

62-
APP_CREATE_PR_ROOT_DIR=${APP_CREATE_PR_ROOT_DIR:-"$(git rev-parse --show-toplevel)"} \
63-
|| error_and_exit "This directory is not a git repository"
62+
APP_CREATE_PR_ROOT_DIR=${APP_CREATE_PR_ROOT_DIR:-"$(git rev-parse --show-toplevel)"} ||
63+
error_and_exit "This directory is not a git repository"
6464
PR_LINK_PREFIX_TEXT=${PR_LINK_PREFIX_TEXT:-""}
6565
PR_TICKET_LINK_PREFIX=${PR_TICKET_LINK_PREFIX:-""}
6666
PR_TEMPLATE_PATH=${PR_TEMPLATE_PATH:-".github/PULL_REQUEST_TEMPLATE.md"}
@@ -71,8 +71,8 @@ PR_ASSIGNEE=${PR_ASSIGNEE:-${ASSIGNEE:-"@me"}}
7171
PR_REVIEWER=${PR_REVIEWER:-${REVIEWER:-""}}
7272
PR_RUN_AFTER_CREATION=${PR_RUN_AFTER_CREATION:-""}
7373
TARGET_BRANCH=${TARGET_BRANCH:-"main"}
74-
CURRENT_BRANCH=${CURRENT_BRANCH:-"$(git rev-parse --abbrev-ref HEAD 2>/dev/null)"} \
75-
|| error_and_exit "Failed to get the current branch name."
74+
CURRENT_BRANCH=${CURRENT_BRANCH:-"$(git rev-parse --abbrev-ref HEAD 2>/dev/null)"} ||
75+
error_and_exit "Failed to get the current branch name."
7676

7777
REMOTE_URL=${REMOTE_URL:-"$(git config --get remote.origin.url)"}
7878
if [[ "$REMOTE_URL" == *"github.com"* ]]; then
@@ -121,8 +121,8 @@ function main::create_pr() {
121121

122122
# Push the current branch
123123
if ! git push -u origin "$CURRENT_BRANCH"; then
124-
error_and_exit "Failed to push the current branch to the remote repository."\
125-
"Please check your git remote settings."
124+
error_and_exit "Failed to push the current branch to the remote repository." \
125+
"Please check your git remote settings."
126126
fi
127127

128128
if [[ "$PR_USING_CLIENT" == "gitlab" ]]; then
@@ -137,13 +137,13 @@ function main::create_pr_gitlab() {
137137

138138
local glab_command=(
139139
glab mr create
140-
--title "$PR_TITLE"
141-
--target-branch "$TARGET_BRANCH"
142-
--source-branch "$CURRENT_BRANCH"
143-
--assignee "$PR_ASSIGNEE"
144-
--reviewer "$PR_REVIEWER"
145-
--label "$PR_LABEL"
146-
--description "$PR_BODY"
140+
--title "$PR_TITLE"
141+
--target-branch "$TARGET_BRANCH"
142+
--source-branch "$CURRENT_BRANCH"
143+
--assignee "$PR_ASSIGNEE"
144+
--reviewer "$PR_REVIEWER"
145+
--label "$PR_LABEL"
146+
--description "$PR_BODY"
147147
)
148148

149149
if [[ ${#EXTRA_ARGS[@]} -gt 0 ]]; then
@@ -163,22 +163,22 @@ function main::create_pr_github() {
163163

164164
local gh_command=(
165165
gh pr create
166-
--title "$PR_TITLE"
167-
--base "$TARGET_BRANCH"
168-
--head "$CURRENT_BRANCH"
169-
--assignee "$PR_ASSIGNEE"
170-
--reviewer "$PR_REVIEWER"
171-
--label "$PR_LABEL"
172-
--body "$PR_BODY"
166+
--title "$PR_TITLE"
167+
--base "$TARGET_BRANCH"
168+
--head "$CURRENT_BRANCH"
169+
--assignee "$PR_ASSIGNEE"
170+
--reviewer "$PR_REVIEWER"
171+
--label "$PR_LABEL"
172+
--body "$PR_BODY"
173173
)
174174

175175
if [[ ${#EXTRA_ARGS[@]} -gt 0 ]]; then
176176
gh_command+=("${EXTRA_ARGS[@]}")
177177
fi
178178

179179
if ! "${gh_command[@]}"; then
180-
error_and_exit "Failed to create the Pull Request." \
181-
"Ensure you have the correct permissions and the repository is properly configured."
180+
error_and_exit "Failed to create the Pull Request." \
181+
"Ensure you have the correct permissions and the repository is properly configured."
182182
fi
183183

184184
main::run_after_creation_script
@@ -287,7 +287,7 @@ function pr_label() {
287287
IFS=';' # Split mapping entries by semicolon
288288
for entry in $mapping; do
289289
# Split each entry into keys and value
290-
IFS=':' read -r keys value <<< "$entry"
290+
IFS=':' read -r keys value <<<"$entry"
291291

292292
# Check if the prefix matches any of the keys
293293
IFS='|' # Split keys by pipe symbol
@@ -388,7 +388,7 @@ function pr_title() {
388388
fi
389389

390390
local title
391-
title=$(echo "$branch_name" | cut -d'-' -f3- | tr '-' ' '| tr '_' ' ')
391+
title=$(echo "$branch_name" | cut -d'-' -f3- | tr '-' ' ' | tr '_' ' ')
392392
title="$(echo "${title:0:1}" | tr '[:lower:]' '[:upper:]')${title:1}"
393393

394394
# Normalize the template by removing spaces around placeholders
@@ -404,17 +404,17 @@ function pr_title() {
404404

405405
if [[ -n "$PR_TITLE_REMOVE_PREFIX" ]]; then
406406
# Split PR_TITLE_REMOVE_PREFIX into an array
407-
IFS=',' read -ra prefixes <<< "$PR_TITLE_REMOVE_PREFIX"
407+
IFS=',' read -ra prefixes <<<"$PR_TITLE_REMOVE_PREFIX"
408408
# Loop through each prefix and remove it from the start if it matches
409409
for prefix in "${prefixes[@]}"; do
410410
# shellcheck disable=SC2001
411411
new_title="$(echo "$new_title" | sed -e "s/^${prefix}//I")"
412412
done
413413

414414
# Trim leading whitespace and capitalize the first letter
415-
new_title=$(echo "$new_title" \
416-
| sed 's/^ *//' \
417-
| awk '{ print toupper(substr($0,1,1)) tolower(substr($0,2)) }')
415+
new_title=$(echo "$new_title" |
416+
sed 's/^ *//' |
417+
awk '{ print toupper(substr($0,1,1)) tolower(substr($0,2)) }')
418418
fi
419419

420420
formatted="${formatted//\{\{PR_TITLE\}\}/$new_title}"
@@ -450,8 +450,8 @@ GH_CLI_INSTALLATION_URL="https://cli.github.com/"
450450
GLAB_CLI_INSTALLATION_URL="https://gitlab.com/gitlab-org/cli/"
451451

452452
function error_and_exit() {
453-
echo "Error: $1" >&2
454-
exit 1
453+
echo "Error: $1" >&2
454+
exit 1
455455
}
456456

457457
function validate::target_branch_exists() {
@@ -473,13 +473,13 @@ function validate::current_branch_is_not_target() {
473473
}
474474

475475
function validate::gh_cli_is_installed() {
476-
if ! command -v gh &> /dev/null; then
476+
if ! command -v gh &>/dev/null; then
477477
error_and_exit "gh CLI is not installed. Please install it from $GH_CLI_INSTALLATION_URL and try again."
478478
fi
479479
}
480480

481481
function validate::glab_cli_is_installed() {
482-
if ! command -v glab &> /dev/null; then
482+
if ! command -v glab &>/dev/null; then
483483
error_and_exit "glab CLI is not installed. Please install it from $GLAB_CLI_INSTALLATION_URL and try again."
484484
fi
485485
}
@@ -493,38 +493,38 @@ declare -r CREATE_PR_VERSION="0.10.0"
493493
CREATE_PR_ROOT_DIR="$(dirname "${BASH_SOURCE[0]}")"
494494
export CREATE_PR_ROOT_DIR
495495

496-
497496
DRY_RUN=${DRY_RUN:-false}
498497
EXTRA_ARGS=()
499498

500499
while [[ $# -gt 0 ]]; do
501500
argument="$1"
502501
case $argument in
503-
--debug)
504-
set -x
505-
;;
506-
--dry-run)
507-
DRY_RUN=true
508-
;;
509-
-e|--env)
510-
# shellcheck disable=SC1090
511-
source "$2"
512-
shift
513-
;;
514-
-t|--title)
515-
helpers::generate_branch_name "$2" "${3:-}"
516-
trap '' EXIT && exit 0
517-
;;
518-
-h|--help)
519-
console_header::print_help
520-
trap '' EXIT && exit 0
521-
;;
522-
-v|--version)
523-
console_header::print_version
524-
trap '' EXIT && exit 0
525-
;;
526-
*)
527-
EXTRA_ARGS+=("$argument")
502+
--debug)
503+
set -x
504+
;;
505+
--dry-run)
506+
DRY_RUN=true
507+
;;
508+
-e | --env)
509+
# shellcheck disable=SC1090
510+
source "$2"
511+
shift
512+
;;
513+
-t | --title)
514+
helpers::generate_branch_name "$2" "${3:-}"
515+
trap '' EXIT && exit 0
516+
;;
517+
-h | --help)
518+
console_header::print_help
519+
trap '' EXIT && exit 0
520+
;;
521+
-v | --version)
522+
console_header::print_version
523+
trap '' EXIT && exit 0
524+
;;
525+
*)
526+
EXTRA_ARGS+=("$argument")
527+
;;
528528
esac
529529
shift
530530
done

bin/pre-commit

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ make pre_commit/run
55
EXIT_CODE=$?
66

77
if [[ ${EXIT_CODE} -ne 0 ]]; then
8-
echo "Pre Commit checks failed. Please fix the above issues before committing"
9-
exit ${EXIT_CODE}
8+
echo "Pre Commit checks failed. Please fix the above issues before committing"
9+
exit ${EXIT_CODE}
1010
else
11-
echo "Pre Commit checks passed, no problems found"
12-
exit 0
11+
echo "Pre Commit checks passed, no problems found"
12+
exit 0
1313
fi

0 commit comments

Comments
 (0)