# bash-utest **Repository Path**: blh-gitee/bash-utest ## Basic Information - **Project Name**: bash-utest - **Description**: A lightweight unit testing framework for bash scripts. - **Primary Language**: Shell - **License**: Apache-2.0 - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-05-25 - **Last Updated**: 2026-05-25 ## Categories & Tags **Categories**: Uncategorized **Tags**: unit-test, Bash ## README # bash-utest A lightweight unit testing framework for Bash scripts. It isolates tests under a per-process sandbox directory, records mocked commands and environment changes, and provides small assertion helpers. ## Quick start Example layout in this repo: | File | Role | |------|------| | `examples/foo.sh` | Source under test — **do not** `source utest.sh` here. | | `examples/ut-foo.sh` | Test driver — `source utest.sh`, then `source foo.sh`, define `test_*`, then `@run_tests`. | ```bash bash examples/ut-foo.sh ``` Minimal `ut-foo.sh` shape: ```bash #!/usr/bin/bash -e source /path/to/utest.sh source /path/to/foo.sh # after utest, so mkdir/export/… are tracked function test_feature() { your_function_from_foo @check_exist /expected/path } @run_tests ``` Optional: set `DEBUG=on` to trace execution with `set -x`. ## How `utest.sh` works - **Sandbox directory**: Each run uses `./rootdir_` so parallel `make -j` or RPM builds do not clash. Paths you pass as absolute (starting with `/`) to helpers and mocked commands are rewritten to live under that directory (`utest::chroot`). - **Tracker backend** (`utest__trackers_backend`): default **`memory`** keeps command / env / output lines in Bash arrays (fast). **`@launch`** temporarily switches to **`file`**: it flushes arrays to `./rootdir_/{.outputs,.commands,.env}`, **exports** `utest__trackers_backend=file` so nested subshells append to the same files, then reloads files into arrays and restores **`memory`**. That way scripts run via `@launch` still contribute to the same trackers. You can also call **`@use_file_trackers`** / **`@use_memory_trackers`** manually for long-lived file-backed sections. - **Checks** read from the active backend (arrays or on-disk files). Exact matches use a Bash loop or `grep -cxF`; substring / regex use `grep -cF` / `grep -cE` with `LC_ALL=C`. Mocked `export` appends one `NAME=value` per assignment to the env tracker. - **Default mocked builtins/commands**: `source`, `mkdir`, `rm`, `touch`, `ls` use aliases. **`export`** is implemented as a **shell function** (not only an alias) so it still records env lines inside **nested** subshells such as `@launch`'s inner `( … )`; flags like `export -p` / `export -n` are forwarded to the real builtin. - **Test discovery**: `@run_tests` finds all defined functions whose names start with `test_`, runs each in a fresh sandbox (after optional `setup` / `teardown`), and exits with the number of failed cases. ## Assertions | Helper | Meaning | |--------|---------| | `@check_eq A B` | Strings must be equal. | | `@check_ne A B` | Strings must differ. | | `@check_exist PATH` | File or directory must exist (under chroot). | | `@check_not_exist PATH` | Must not exist (under chroot). | | `@check_output LINE N` | A full line equal to `LINE` must appear exactly `N` times among recorded output lines (fixed string, not regex). | | `@check_output_contains SUB N` | Exactly `N` recorded output lines contain substring `SUB` (fixed string). | | `@check_output_re ERE N` | Exactly `N` recorded output lines match extended regex `ERE` (same rules as `grep -E`). | | `@check_command CMD N` | A full line equal to `CMD` must appear exactly `N` times among recorded command lines (fixed string, whole line). | | `@check_command_contains SUB N` | Exactly `N` recorded command lines contain substring `SUB`. | | `@check_command_re ERE N` | Exactly `N` recorded command lines match extended regex `ERE` (`grep -E`). | | `@check_env NAME VALUE` | At least one recorded env line is exactly `NAME=VALUE` (VALUE may be empty). | | `@check_env_line LINE N` | Full line `LINE` (e.g. `FOO=bar`) appears exactly `N` times among recorded env lines. | | `@check_env_contains SUB N` | Exactly `N` recorded env lines contain substring `SUB`. | | `@check_env_re ERE N` | Exactly `N` recorded env lines match extended regex `ERE` (`grep -E`). | ## Mocking - **`@mock cmd`** — Alias `cmd` so invocations are appended to `.commands` only (no real `cmd` run). Use when the unit under test should not execute the real binary. - **`@@mock cmd [replacement]`** — Generic alias: optional `replacement` command, or `utest::mock_` if defined, else wrap with `utest::mock_wrap`. - **`@unmock cmd`** — Remove the alias. Other helpers: `@echo`, `@export`, `@launch`, `@reset_test`, `@dump_outputs`, `@dump_commands`, `@dump_env` (dumps are useful when a check fails). **`@launch`**: first argument is either a **script file** (sourced with `.`) or a **shell function** already defined in the current shell (run as `( name "$@" )` in a subshell); trackers use temporary file backing for that call, then merge back into memory. ## Examples - **`examples/foo.sh`** — sample module (no framework). - **`examples/ut-foo.sh`** — tests for `foo.sh`. **`@launch`** can run a script path (dot-sourced) or, after `source foo.sh`, a **function name** in a subshell so trackers stay consistent (file-backed during the call). Optional `__utest_invoke_foo__` in `foo.sh` is only for dot-sourcing `foo.sh` without an earlier `source`. ## Requirements Bash (uses `[[ ]]`, `declare -F`, `declare -n` for tracker arrays, and aliases) and `grep` (substring/regex line counts on buffered lines). Typical environments: Linux, WSL, or Git Bash on Windows.