Skip to content

feat(builtins): add assert command (non-standard) #562

@chaliy

Description

@chaliy

Summary

A new, non-standard builtin for first-class test assertions with clear error messages. Useful for self-verifying scripts, CI pipelines, and inline correctness checks.

Note: This is a bashkit-specific extension, not a standard Bash command.

Proposed Syntax

assert <test-expression> [message]

Behavior

  • Evaluates the expression using the same logic as test/[
  • If true: silent, exit 0
  • If false: prints assertion failure message to stderr, exits with code 1
  • If set -e is active, assertion failure stops the script (desired behavior)

Use Cases

# Basic value check
result=$(compute_something)
assert [ "$result" = "expected" ] "compute_something returned '$result', expected 'expected'"

# File existence
assert [ -f "/app/config.json" ] "Config file missing"

# Non-empty output
output=$(curl -s https://api.example.com/health)
assert [ -n "$output" ] "Health check returned empty response"

# Numeric comparison
count=$(wc -l < data.csv)
assert [ "$count" -gt 0 ] "data.csv is empty"

# In CI scripts
./build.sh
assert [ -f "dist/app.js" ] "Build did not produce dist/app.js"

# Multiple assertions as lightweight tests
assert [ "$(echo hello | tr a-z A-Z)" = "HELLO" ] "tr uppercase failed"
assert [ "$(expr 2 + 2)" = "4" ] "arithmetic broken"

Error Output Format

assertion failed: Config file missing
  expression: [ -f /app/config.json ]
  at: script.sh:15

Implementation Notes

  • Expression parsing reuses test/[ builtin logic
  • Message is optional — without it, prints the expression itself
  • $LINENO and ${BASH_SOURCE} provide location info
  • Could support assert_eq val1 val2 [message] as a shorthand
  • Exit code 1 on failure (not 2 like test syntax errors)
  • Works naturally with set -e — script stops on first failed assertion

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions