Skip to content

Commit bc90a84

Browse files
committed
run shellcheck in the test script
1 parent a20e36b commit bc90a84

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

.travis.yml

-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ os:
44
- linux
55

66
script:
7-
- shellcheck -s bash --exclude=SC2034,SC2154 <(awk '/```sh$/{f=1;next}/```/{f=0}f' README.md)
87
- ./test.sh

CONTRIBUTING.md

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
```

test.sh

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env bash
2+
# shellcheck source=/dev/null
23
#
34
# Tests for the Pure Bash Bible.
45

@@ -134,8 +135,12 @@ assert_equals() {
134135
}
135136

136137
main() {
138+
# Run shellcheck on the code.
139+
awk '/```sh$/{f=1;next}/```/{f=0}f' README.md > readme_code
140+
shellcheck -s bash --exclude=SC2034,SC2154 readme_code || exit 1
141+
137142
# Get the code blocks from README.md
138-
source <(awk '/```sh$/{f=1;next}/```/{f=0}f' README.md) 2>/dev/null
143+
. readme_code
139144

140145
head="-> Running tests on the Pure Bash Bible.."
141146
printf '\n%s\n%s\n' "$head" "${head//?/-}"
@@ -146,6 +151,7 @@ main() {
146151

147152
comp="Completed ${#funcs[@]} tests. ${pass:-0} passed, ${err:-0} errored."
148153
printf '%s\n%s\n\n' "${comp//?/-}" "$comp"
154+
rm readme_code
149155

150156
# If a test failed, exit with '1'.
151157
[[ -f /tmp/err ]] || exit 0 && { rm /tmp/err; exit 1; }

0 commit comments

Comments
 (0)