shellkitshellkit
Guide
API reference
Examples
Changelog
Releases
llms.txt
GitHub
  • 简体中文
  • English
Guide
API reference
Examples
Changelog
Releases
llms.txt
GitHub
  • 简体中文
  • English
  • Getting started

    • Guide
    • Install and upgrade
    • Thirty seconds
  • Core concepts

    • Five hard rules
    • Bash 3.2 compatibility
    • Module map
    • POSIX subset
  • Common tasks

    • Cookbook
    • CLI framework
    • Machine-readable output
    • Security boundaries
    • Internationalisation
    • Testing and gates
    • Working with AI
  • Going further

    • Example projects
    • Man pages
    • Migration guide
    • Compatibility policy
    • Releasing
    • Contributing
    • Changelog
  • Troubleshooting

    • Troubleshooting
    • FAQ

Testing and gates

Testing here has three layers, all in service of the same thing: proving it actually ran, not that it "should" run.

LayerWhereWhat it covers
module teststests/test_<module>.sheach module's behaviour, failure branches included
scaffold teststests/test_scaffold.sh, tests/test_install.shshellkit new and the installed layout
example testsexamples/*/tests/whole projects, exercised through the real binary

Run

bash tests/run.sh

tests/run.sh is a per-file runner: it globs tests/test_*.sh, gives each file its own subprocess, connects stdin to /dev/null (a test must never wait for a keypress or CI hangs), and applies a per-file limit when timeout exists.

Output has exactly two shapes: ok or FAIL: <message>. Any FAIL line means failure -- and you have to look at it, not just at the exit code.

Machine-readable output

What CI actually needs is which case failed, not which file. The runner has three formats:

bash tests/run.sh                      # plain (default; behaviour is byte-for-byte the old one)
bash tests/run.sh --format tap         # TAP 13, straight into any test reporter
bash tests/run.sh --format junit        # JUnit XML, for GitHub Actions / Jenkins

Other options:

bash tests/run.sh --jobs 4             # run in parallel (each file gets its own temp root)
bash tests/run.sh --filter '*fs*'      # only files whose name matches
bash tests/run.sh --list               # list what would run, run nothing

--filter takes a shell glob (add your own *). When it matches nothing the runner exits 2 and lists the available test files -- an empty run is never reported as green.

Case granularity comes from # CASE: title comments inside the test file. A file without any CASE marker degrades to one file-level case, and the failure reason is the FAIL: line the assertion printed.

"A failure reported as all green" cannot happen: every format returns non-zero when something fails, and the failure shows up as not ok / <failure> / FAILED: in the report. tests/test_runner.sh holds that invariant down -- it copies the real runner into a fake repository and drives it with controlled failing cases.

Assertions

tests/test_helper.sh is a deliberately minimal assertion library. A failed assertion exits 1, and the runner marks the file red.

AssertionUsage
assert_eqassert_eq EXPECTED ACTUAL [MSG]
assert_neassert_ne UNEXPECTED ACTUAL [MSG]
assert_containsassert_contains HAYSTACK NEEDLE [MSG]
assert_not_containsassert_not_contains HAYSTACK NEEDLE [MSG]
assert_true / assert_falseassert_true CMD ARGS...
assert_exit_codeassert_exit_code WANT CMD ARGS...
assert_stdout_emptyassert_stdout_empty CMD ARGS...
assert_file_modeassert_file_mode FILE 600
tmpdirone isolated directory per case, path on stdout

One test file

#!/usr/bin/env bash
. "$(dirname "$0")/test_helper.sh"

. "$TEST_ROOT/lib/bootstrap.sh"

assert_eq 2 "$SK_USAGE" "usage errors exit 2"
assert_exit_code 2 require_cmd
assert_stdout_empty log_info "logs must not reach stdout"
assert_contains "$(log_info hi 2>&1)" "hi" "stderr must carry the message"

Note that the last two form a pair: assert_stdout_empty proves stdout is clean, assert_contains proves the message really went to stderr. Test only the first and "printed nothing at all" passes too.

stub: exercise failure branches without unplugging the network

stub shadows an external command with a shell function of the same name. Shell functions are visible inside $(...) subshells, so curl calls made from inside the library are intercepted:

workdir=$(tmpdir)

# make curl exit 7 (connection failure) and check net_download maps it to SK_NET(4)
stub curl --exit 7
assert_exit_code 4 net_download "https://example.com/x" "$workdir/x"
assert_stub_called curl "curl should have been called"
unstub curl

The same approach works for date (freeze time, assert on backup file names) and sleep (do not actually sleep, still assert on the retry delay window).

The gate: fifteen steps of make check

StepWhat it blocks
syntaxbash -n over every script and template
testtests/run.sh
lintshellcheck -S warning -x (templates are rendered before checking)
module-order-checka module calling a function from a module loaded later
check-exitcode-docsa public function that does not document its return code, or whose comment block is separated from it by a blank line
local-names-checka library local that is not __-prefixed, or a local on the out-parameter path that is missing from the reserved list (callers using that name silently keep the old value)
module-count-checkthe hand-written "N modules" in the docs does not match lib/modules.txt
pipe-safety-checkan early-exit consumer in a pipeline (grep -q / head): the producer is killed by SIGPIPE, so a caller with pipefail sees the whole pipeline fail
docs-checkpublic functions without a comment, generated artefacts out of sync
versions-checkVERSION against its five projections
check-docsevery bash block in markdown must pass bash -n, and every function name must exist
cookbook-checkthe hand-written "N scenarios" in the docs does not match the real cookbook
messages-checkpot versus sources (skipped when xgettext is absent)
exampleseach project's own test under examples/
fmtshfmt -d -i 2 -ci (checks, never rewrites)

check-exitcode-docs backs the promise "the return code is part of the interface". The README makes that promise, so a gate has to keep it. The sixteen core files written before the rule existed carried no @exitcode at all -- the interface was half missing.

check-docs is the easiest to overlook: it turns "documented but not implemented" into a build failure. Rename a function and the example in the README goes red immediately instead of silently becoming wrong -- and an AI that finds a wrong example will copy it, confidently.

Add a test

Create tests/test_<name>.sh. That is all: the glob picks it up, so no runner edit and no registration.

Two disciplines when writing tests:

  1. Assert on artefacts, not only on exit codes. Exit 0 while writing nothing is the most common false green.
  2. Verify in reverse once: run it against the old code and confirm it goes red, then against the new code and confirm it goes green. A test that has never been red proves nothing.
Last updated: 11/22/26, 7:50 PM
Prev
Internationalisation
Next
Working with AI
给 AI 直读:llms.txt 一个文件读完整个站点的地图