mirror of
https://github.com/dylanaraps/pure-bash-bible.git
synced 2025-12-17 07:54:19 +01:00
1.7 KiB
1.7 KiB
Writing the Bible
Adding Code to the Bible.
- The code must use only
bashbuilt-ins.- A fallback to an external program is allowed if the code doesn't always work.
- Example Fallback:
${HOSTNAME:-$(hostname)}
- If possible, wrap the code in a function.
- This allows tests to be written.
- It also allows
shellcheckto properly lint it. - An added bonus is showing a working use-case.
- Write some examples.
- Show some input and the modified output.
Special meanings for code blocks.
Use sh for functions that should be linted and unit tested.
```sh
# Shellcheck will lint this and the test script will source this.
func() {
# Usage: func "arg"
:
}
```
Use shell for code that should be ignored.
```shell
# Shorter file creation syntax.
:>file
```
Writing tests
The test file is viewable here: https://github.com/dylanaraps/pure-bash-bible/blob/master/test.sh
Example test:
test_upper() {
result="$(upper "HeLlO")"
assert_equals "$result" "HELLO"
}
Steps:
- Write the test.
- Naming is
test_func_name - Store the function output in a variable (
$resultor${result[@]}). - Use
assert_equalsto test equality between the variable and the expected output.
- Naming is
- The test script will automatically execute it. 👍
Running tests
Running test.sh also runs shellcheck on the code.
cd pure-bash-bible
./test.sh