|
| 1 | +# Writing the Bible |
| 2 | + |
| 3 | +<!-- vim-markdown-toc GFM --> |
| 4 | + |
| 5 | +* [Adding Code to the Bible.](#adding-code-to-the-bible) |
| 6 | +* [Special meanings for code blocks.](#special-meanings-for-code-blocks) |
| 7 | +* [Writing tests](#writing-tests) |
| 8 | +* [Running the tests](#running-the-tests) |
| 9 | + |
| 10 | +<!-- vim-markdown-toc --> |
| 11 | + |
| 12 | +## Adding Code to the Bible. |
| 13 | + |
| 14 | +- The code must use only `bash` built-ins. |
| 15 | + - A fallback to an external program is allowed if the code doesn't |
| 16 | + always work. |
| 17 | + - Example Fallback: `${HOSTNAME:-$(hostname)}` |
| 18 | +- If possible, wrap the code in a function. |
| 19 | + - This allows tests to be written. |
| 20 | + - It also allows `shellcheck` to properly lint it. |
| 21 | + - An added bonus is showing a working use-case. |
| 22 | +- Write some examples. |
| 23 | + - Show some input and the modified output. |
| 24 | + |
| 25 | + |
| 26 | +## Special meanings for code blocks. |
| 27 | + |
| 28 | +Use `sh` for functions that should be linted and unit tested. |
| 29 | + |
| 30 | + ```sh |
| 31 | + # Shellcheck will lint this and the test script will source this. |
| 32 | + func() { |
| 33 | + # Usage: func "arg" |
| 34 | + : |
| 35 | + } |
| 36 | + ``` |
| 37 | + |
| 38 | +Use `shell` for code that should be ignored. |
| 39 | + |
| 40 | + ```shell |
| 41 | + # Shorter file creation syntax. |
| 42 | + :>file |
| 43 | + ``` |
| 44 | + |
| 45 | +## Writing tests |
| 46 | + |
| 47 | +The test file is viewable here: https://github.com/dylanaraps/pure-bash-bible/blob/master/test.sh |
| 48 | + |
| 49 | +Example test: |
| 50 | + |
| 51 | +```sh |
| 52 | +test_upper() { |
| 53 | + result="$(upper "HeLlO")" |
| 54 | + assert_equals "$result" "HELLO" |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +Steps: |
| 59 | + |
| 60 | +1. Write the test. |
| 61 | + - Naming is `test_func_name` |
| 62 | + - Store the function output in a variable. |
| 63 | + - Use `assert_equals` to test equality between the variable and the |
| 64 | + expected output. |
| 65 | +2. ??? |
| 66 | +3. The test script will automatically execute it. :+1: |
| 67 | + |
| 68 | + |
| 69 | +## Running the tests |
| 70 | + |
| 71 | +Running `test.sh` also runs `shellcheck` on the code. |
| 72 | + |
| 73 | +```sh |
| 74 | +cd pure-bash-bible |
| 75 | +./test.sh |
| 76 | +``` |
0 commit comments