Skip to content

Commit cb667aa

Browse files
authored
Merge pull request #522 from TypedDevs/feat/521-bashunit-subcommands
Bashunit subcommands
2 parents a9c122e + 59effa7 commit cb667aa

11 files changed

+656
-718
lines changed

CHANGELOG.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22

33
## Unreleased
44

5-
### Added
6-
- Add `--doc [search]` option
5+
### Changed
6+
- **BREAKING:** Introduce subcommand-based CLI architecture
7+
- `bashunit test [path]` - run tests (default, backwards compatible with `bashunit [path]`)
8+
- `bashunit bench [path]` - run benchmarks (replaces `--bench`)
9+
- `bashunit doc [filter]` - show assertion docs (replaces `--doc`)
10+
- `bashunit init [dir]` - initialize project (replaces `--init`)
11+
- `bashunit learn` - interactive tutorial (replaces `--learn`)
12+
- `bashunit upgrade` - upgrade to latest (replaces `--upgrade`)
713

814
### Fixed
915
- Stop executing remaining commands in `set_up`/`tear_down` after first failure

bashunit

Lines changed: 40 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -60,141 +60,46 @@ source "$BASHUNIT_ROOT_DIR/src/init.sh"
6060
source "$BASHUNIT_ROOT_DIR/src/learn.sh"
6161
source "$BASHUNIT_ROOT_DIR/src/main.sh"
6262

63-
_ASSERT_FN=""
64-
_FILTER=""
65-
_RAW_ARGS=()
66-
_ARGS=()
67-
_BENCH_MODE=false
68-
6963
check_os::init
7064
clock::init
7165

72-
# Argument parsing
73-
while [[ $# -gt 0 ]]; do
74-
case "$1" in
75-
-a|--assert)
76-
_ASSERT_FN="$2"
77-
shift
78-
;;
79-
-f|--filter)
80-
_FILTER="$2"
81-
shift
82-
;;
83-
-s|--simple)
84-
export BASHUNIT_SIMPLE_OUTPUT=true
85-
;;
86-
--detailed)
87-
export BASHUNIT_SIMPLE_OUTPUT=false
88-
;;
89-
--debug)
90-
OUTPUT_FILE="${2:-}"
91-
if [[ -n "$OUTPUT_FILE" ]]; then
92-
exec > "$OUTPUT_FILE" 2>&1
93-
fi
94-
set -x
95-
;;
96-
-b|--bench)
97-
_BENCH_MODE=true
98-
export BASHUNIT_BENCH_MODE=true
99-
source "$BASHUNIT_ROOT_DIR/src/benchmark.sh"
100-
;;
101-
-S|--stop-on-failure)
102-
export BASHUNIT_STOP_ON_FAILURE=true
103-
;;
104-
-p|--parallel)
105-
export BASHUNIT_PARALLEL_RUN=true
106-
;;
107-
--no-parallel)
108-
export BASHUNIT_PARALLEL_RUN=false
109-
;;
110-
-e|--env|--boot)
111-
# shellcheck disable=SC1090
112-
source "$2"
113-
shift
114-
;;
115-
-l|--log-junit)
116-
export BASHUNIT_LOG_JUNIT="$2"
117-
shift
118-
;;
119-
-r|--report-html)
120-
export BASHUNIT_REPORT_HTML="$2"
121-
shift
122-
;;
123-
--no-output)
124-
export BASHUNIT_NO_OUTPUT=true
125-
;;
126-
-vvv|--verbose)
127-
export BASHUNIT_VERBOSE=true
128-
;;
129-
-v|--version)
130-
console_header::print_version
131-
trap '' EXIT && exit 0
132-
;;
133-
--doc)
134-
if [[ -n ${2:-} && ${2:0:1} != "-" ]]; then
135-
doc::print_asserts "$2"
136-
shift
137-
else
138-
doc::print_asserts
139-
fi
140-
trap '' EXIT && exit 0
141-
;;
142-
--upgrade)
143-
upgrade::upgrade
144-
trap '' EXIT && exit 0
145-
;;
146-
--init)
147-
if [[ -n ${2:-} && ${2:0:1} != "-" ]]; then
148-
init::project "$2"
149-
shift
150-
else
151-
init::project
152-
fi
153-
trap '' EXIT && exit 0
154-
;;
155-
--learn)
156-
learn::start
157-
trap '' EXIT && exit 0
158-
;;
159-
-h|--help)
160-
console_header::print_help
161-
trap '' EXIT && exit 0
162-
;;
163-
*)
164-
_RAW_ARGS+=("$1")
165-
;;
166-
esac
167-
shift
168-
done
169-
170-
# Expand positional arguments after all options have been processed
171-
if [[ ${#_RAW_ARGS[@]} -gt 0 ]]; then
172-
pattern='*[tT]est.sh'
173-
[[ "$_BENCH_MODE" == true ]] && pattern='*[bB]ench.sh'
174-
for arg in "${_RAW_ARGS[@]}"; do
175-
while IFS= read -r file; do
176-
_ARGS+=("$file")
177-
done < <(helper::find_files_recursive "$arg" "$pattern")
178-
done
179-
fi
180-
181-
# Optional bootstrap
182-
# shellcheck disable=SC1090
183-
[[ -f "${BASHUNIT_BOOTSTRAP:-}" ]] && source "$BASHUNIT_BOOTSTRAP"
184-
185-
if [[ "${BASHUNIT_NO_OUTPUT:-false}" == true ]]; then
186-
exec >/dev/null 2>&1
187-
fi
188-
189-
set +eu
190-
191-
#################
192-
# Main execution
193-
#################
194-
if [[ -n "$_ASSERT_FN" ]]; then
195-
main::exec_assert "$_ASSERT_FN" "${_ARGS[@]}"
196-
elif [[ "$_BENCH_MODE" == true ]]; then
197-
main::exec_benchmarks "$_FILTER" "${_ARGS[@]}"
198-
else
199-
main::exec_tests "$_FILTER" "${_ARGS[@]}"
200-
fi
66+
# Subcommand detection
67+
_SUBCOMMAND=""
68+
69+
case "${1:-}" in
70+
test|bench|doc|init|learn|upgrade)
71+
_SUBCOMMAND="$1"
72+
shift
73+
;;
74+
-v|--version)
75+
console_header::print_version
76+
exit 0
77+
;;
78+
-h|--help)
79+
console_header::print_help
80+
exit 0
81+
;;
82+
-*)
83+
# Flag without subcommand → assume "test"
84+
_SUBCOMMAND="test"
85+
;;
86+
"")
87+
# No arguments → show help
88+
console_header::print_help
89+
exit 0
90+
;;
91+
*)
92+
# Path argument → assume "test"
93+
_SUBCOMMAND="test"
94+
;;
95+
esac
96+
97+
# Route to subcommand handler
98+
case "$_SUBCOMMAND" in
99+
test) main::cmd_test "$@" ;;
100+
bench) main::cmd_bench "$@" ;;
101+
doc) main::cmd_doc "$@" ;;
102+
init) main::cmd_init "$@" ;;
103+
learn) main::cmd_learn "$@" ;;
104+
upgrade) main::cmd_upgrade "$@" ;;
105+
esac

0 commit comments

Comments
 (0)