DEVOURED
Shell Tool Testing
The `prove` command and Test Anything Protocol (TAP) offer a lightweight, language-agnostic way to test Unix shell tools without heavy frameworks, simplifying test suite management.
What: Stefan `st` Kottwitz advocates for `prove` (from the Perl ecosystem) and TAP for testing shell tools, similar to Python's `unittest`. TAP is a plain-text format for test results, and `prove` discovers, runs, and summarizes tests. A small shared `common.sh` harness can manage temporary directories (`mktemp -d`), provide `report_ok`/`report_not_ok` functions for automated test numbering, and handle cleanup (`rm -rf`), making shell test suites easy to grow while keeping failures readable.
Why it matters: This approach democratizes automated testing for shell scripts, removing the barrier of complex frameworks and dependencies. By using widely available tools and a simple, human-readable protocol, it encourages more robust development practices for command-line utilities and system automation.
Takeaway: When developing shell scripts or Unix tools, consider using `prove` with TAP for lightweight, readable, and easy-to-manage automated testing.
Deep dive
- Automated testing is crucial for shell tools and Kakoune plugins.
- Many shell script test harnesses exist, but often require third-party libraries.
provecommand (from Perl ecosystem) is a language-agnostic test runner.proveis similar to Python'sunittestbut works with any language outputting TAP.- Test Anything Protocol (TAP) is a plain-text, human-readable format for test results (e.g.,
ok N - description,not ok N - description). - TAP includes
TAP version Xand1..N(total tests expected). - By default,
provelooks fort/*.tfiles; can be configured via.provercto use--ext testfort/*.testfiles. - Tests should be isolated using temporary directories created with
mktemp -dand cleaned up withrm -rf. - A shared
common.shscript can provide helper functions likereport_okandreport_not_okto automate test numbering and TAP output. common.shcan also handle bailouts (stopping on first failure), strict mode checks, and capturing stdout/stderr.
Decoder
prove: A command-line utility from the Perl ecosystem used to discover, run, and summarize test results in various formats, notably TAP.- Test Anything Protocol (TAP): A simple, human-readable, line-based text format for reporting test results, commonly used for automated testing.
mktemp -d: A Unix command that securely creates a unique temporary directory.common.sh: A common practice in shell scripting for a shared script to include reusable functions and setup for a test suite.
Original article
Full article content is not available for inline reading.
Explore all 57 articles in today's digest
Fresh dev news, AI-digested daily. Never miss a trend at devoured.fyi