Skip to content

bashunit assert with a missing argument leaks a bash error and exits 0 #877

Description

@Chemaclass

🤔 Background

The standalone assert subcommand does not check that it received enough arguments before indexing the argument array.

local last_index=$((args_count - 1))
local last_arg="${args[$last_index]}"

With no arguments after the assertion name, args_count is 0, so last_index is -1. Bash 3.x has no negative array subscripts, so the expansion fails — and the run continues and exits 0:

$ ./bashunit assert equals
./src/main.sh: line 1182: args: bad array subscript
$ echo $?
0

A malformed standalone assertion reports success. docs/standalone.md presents this subcommand for integration and end-to-end checks, so a mistyped invocation in a deploy script passes silently.

Working cases, for contrast

The rest of the subcommand handles errors well — this is the one gap:

Invocation Result
assert equals foo foo exit 0
assert equals foo bar exit 1, ✗ Failed: assert equals
assert no_such_assertion foo foo exit 127, Function no_such_assertion does not exist.
assert equals exit 0, bad array subscript

Suggested fix

Reject an argument count below what the assertion needs with a clear message and a non-zero exit, in the same style as the existing "Function ... does not exist." path. Note the negative-subscript expansion is also a Bash 3.0 compatibility violation per .claude/rules/bash-style.md.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

Status
No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions