Skip to content

Commit a9c122e

Browse files
authored
Merge pull request #445 from TypedDevs/feat/doc-option
Add --doc option
2 parents 269e358 + 001b126 commit a9c122e

12 files changed

+399
-35
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Unreleased
44

5+
### Added
6+
- Add `--doc [search]` option
7+
58
### Fixed
69
- Stop executing remaining commands in `set_up`/`tear_down` after first failure
710
- Count all tests as failed when `set_up_before_script` fails

bashunit

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ source "$BASHUNIT_ROOT_DIR/src/helpers.sh"
5252
source "$BASHUNIT_ROOT_DIR/src/test_title.sh"
5353
source "$BASHUNIT_ROOT_DIR/src/upgrade.sh"
5454
source "$BASHUNIT_ROOT_DIR/src/assertions.sh"
55+
source "$BASHUNIT_ROOT_DIR/src/doc.sh"
5556
source "$BASHUNIT_ROOT_DIR/src/reports.sh"
5657
source "$BASHUNIT_ROOT_DIR/src/runner.sh"
5758
source "$BASHUNIT_ROOT_DIR/src/bashunit.sh"
@@ -129,6 +130,15 @@ while [[ $# -gt 0 ]]; do
129130
console_header::print_version
130131
trap '' EXIT && exit 0
131132
;;
133+
--doc)
134+
if [[ -n ${2:-} && ${2:0:1} != "-" ]]; then
135+
doc::print_asserts "$2"
136+
shift
137+
else
138+
doc::print_asserts
139+
fi
140+
trap '' EXIT && exit 0
141+
;;
132142
--upgrade)
133143
upgrade::upgrade
134144
trap '' EXIT && exit 0

build.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ function build::generate_bin() {
4848
grep -v '^source' "$temp" > "$out"
4949
rm "$temp"
5050
chmod u+x "$out"
51+
52+
# Embed the assertions.md docs into the binary
53+
build::embed_docs "$out"
5154
}
5255

5356
# Recursive function to process each file and any files it sources
@@ -105,13 +108,39 @@ function build::dependencies() {
105108
"src/runner.sh"
106109
"src/init.sh"
107110
"src/learn.sh"
111+
"src/doc.sh"
108112
"src/bashunit.sh"
109113
"src/main.sh"
110114
)
111115

112116
echo "${deps[@]}"
113117
}
114118

119+
function build::embed_docs() {
120+
local file=$1
121+
local docs_file="docs/assertions.md"
122+
local temp_file="${file}.tmp"
123+
124+
# Build the replacement content
125+
{
126+
# Print everything before the start marker (excluding the marker line)
127+
local line_num
128+
line_num=$(grep -n "# __BASHUNIT_EMBEDDED_DOCS_START__" "$file" | cut -d: -f1)
129+
head -n "$((line_num - 1))" "$file"
130+
131+
# Print the heredoc with embedded docs
132+
echo " cat <<'__BASHUNIT_DOCS_EOF__'"
133+
cat "$docs_file"
134+
echo "__BASHUNIT_DOCS_EOF__"
135+
136+
# Print everything after the end marker
137+
sed -n '/# __BASHUNIT_EMBEDDED_DOCS_END__/,$p' "$file" | tail -n +2
138+
} > "$temp_file"
139+
140+
mv "$temp_file" "$file"
141+
chmod u+x "$file"
142+
}
143+
115144
function build::generate_checksum() {
116145
local out=$1
117146

docs/assertions.md

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ function test_failure() {
113113

114114
Reports an error if `needle` is not a substring of `haystack`.
115115

116-
[assert_not_contains](#assert-not-contains) is the inverse of this assertion and takes the same arguments.
116+
- [assert_not_contains](#assert-not-contains) is the inverse of this assertion and takes the same arguments.
117117

118118
::: code-group
119119
```bash [Example]
@@ -150,7 +150,7 @@ function test_failure() {
150150

151151
Reports an error if `actual` is not empty.
152152

153-
[assert_not_empty](#assert-not-empty) is the inverse of this assertion and takes the same arguments.
153+
- [assert_not_empty](#assert-not-empty) is the inverse of this assertion and takes the same arguments.
154154

155155
::: code-group
156156
```bash [Example]
@@ -169,7 +169,7 @@ function test_failure() {
169169

170170
Reports an error if `value` does not match the regular expression `pattern`.
171171

172-
[assert_not_matches](#assert-not-matches) is the inverse of this assertion and takes the same arguments.
172+
- [assert_not_matches](#assert-not-matches) is the inverse of this assertion and takes the same arguments.
173173

174174
::: code-group
175175
```bash [Example]
@@ -188,7 +188,7 @@ function test_failure() {
188188

189189
Reports an error if `haystack` does not starts with `needle`.
190190

191-
[assert_string_not_starts_with](#assert-string-not-starts-with) is the inverse of this assertion and takes the same arguments.
191+
- [assert_string_not_starts_with](#assert-string-not-starts-with) is the inverse of this assertion and takes the same arguments.
192192

193193
::: code-group
194194
```bash [Example]
@@ -207,7 +207,7 @@ function test_failure() {
207207

208208
Reports an error if `haystack` does not ends with `needle`.
209209

210-
[assert_string_not_ends_with](#assert-string-not-ends-with) is the inverse of this assertion and takes the same arguments.
210+
- [assert_string_not_ends_with](#assert-string-not-ends-with) is the inverse of this assertion and takes the same arguments.
211211

212212
::: code-group
213213
```bash [Example]
@@ -247,7 +247,7 @@ function test_failure() {
247247

248248
Reports an error if `actual` is not less than `expected`.
249249

250-
[assert_greater_than](#assert-greater-than) is the inverse of this assertion and takes the same arguments.
250+
- [assert_greater_than](#assert-greater-than) is the inverse of this assertion and takes the same arguments.
251251

252252
::: code-group
253253
```bash [Example]
@@ -266,7 +266,7 @@ function test_failure() {
266266

267267
Reports an error if `actual` is not less than or equal to `expected`.
268268

269-
[assert_greater_than](#assert-greater-or-equal-than) is the inverse of this assertion and takes the same arguments.
269+
- [assert_greater_than](#assert-greater-or-equal-than) is the inverse of this assertion and takes the same arguments.
270270

271271
::: code-group
272272
```bash [Example]
@@ -289,7 +289,7 @@ function test_failure() {
289289

290290
Reports an error if `actual` is not greater than `expected`.
291291

292-
[assert_less_than](#assert-less-than) is the inverse of this assertion and takes the same arguments.
292+
- [assert_less_than](#assert-less-than) is the inverse of this assertion and takes the same arguments.
293293

294294
::: code-group
295295
```bash [Example]
@@ -308,7 +308,7 @@ function test_failure() {
308308

309309
Reports an error if `actual` is not greater than or equal to `expected`.
310310

311-
[assert_less_or_equal_than](#assert-less-or-equal-than) is the inverse of this assertion and takes the same arguments.
311+
- [assert_less_or_equal_than](#assert-less-or-equal-than) is the inverse of this assertion and takes the same arguments.
312312

313313
::: code-group
314314
```bash [Example]
@@ -333,7 +333,7 @@ Reports an error if the exit code of `callable` is not equal to `expected`.
333333

334334
If `callable` is not provided, it takes the last executed command or function instead.
335335

336-
[assert_successful_code](#assert-successful-code), [assert_unsuccessful_code](#assert-unsuccessful-code), [assert_general_error](#assert-general-error) and [assert_command_not_found](#assert-command-not-found)
336+
- [assert_successful_code](#assert-successful-code), [assert_unsuccessful_code](#assert-unsuccessful-code), [assert_general_error](#assert-general-error) and [assert_command_not_found](#assert-command-not-found)
337337
are more semantic versions of this assertion, for which you don't need to specify an exit code.
338338
339339
::: code-group
@@ -396,7 +396,7 @@ function test_failure() {
396396
397397
Reports an error if `needle` is not an element of `haystack`.
398398
399-
[assert_array_not_contains](#assert-array-not-contains) is the inverse of this assertion and takes the same arguments.
399+
- [assert_array_not_contains](#assert-array-not-contains) is the inverse of this assertion and takes the same arguments.
400400
401401
::: code-group
402402
```bash [Example]
@@ -421,7 +421,7 @@ Reports an error if the exit code of `callable` is not successful (`0`).
421421
422422
If `callable` is not provided, it takes the last executed command or function instead.
423423
424-
[assert_exit_code](#assert-exit-code) is the full version of this assertion where you can specify the expected exit code.
424+
- [assert_exit_code](#assert-exit-code) is the full version of this assertion where you can specify the expected exit code.
425425
426426
::: code-group
427427
```bash [Example]
@@ -499,7 +499,7 @@ Reports an error if the exit code of `callable` is not a general error (`1`).
499499
500500
If `callable` is not provided, it takes the last executed command or function instead.
501501
502-
[assert_exit_code](#assert-exit-code) is the full version of this assertion where you can specify the expected exit code.
502+
- [assert_exit_code](#assert-exit-code) is the full version of this assertion where you can specify the expected exit code.
503503
504504
::: code-group
505505
```bash [Example]
@@ -539,7 +539,7 @@ In other words, if executing `callable` does not return a command not found exit
539539
540540
If `callable` is not provided, it takes the last executed command or function instead.
541541
542-
[assert_exit_code](#assert-exit-code) is the full version of this assertion where you can specify the expected exit code.
542+
- [assert_exit_code](#assert-exit-code) is the full version of this assertion where you can specify the expected exit code.
543543
544544
::: code-group
545545
```bash [Example]
@@ -564,7 +564,7 @@ function test_failure() {
564564
565565
Reports an error if `file` does not exists, or it is a directory.
566566
567-
[assert_file_not_exists](#assert-file-not-exists) is the inverse of this assertion and takes the same arguments.
567+
- [assert_file_not_exists](#assert-file-not-exists) is the inverse of this assertion and takes the same arguments.
568568
569569
::: code-group
570570
```bash [Example]
@@ -590,7 +590,7 @@ function test_failure() {
590590
591591
Reports an error if `file` does not contains the search string.
592592
593-
[assert_file_not_contains](#assert-file-not-contains) is the inverse of this assertion and takes the same arguments.
593+
- [assert_file_not_contains](#assert-file-not-contains) is the inverse of this assertion and takes the same arguments.
594594
595595
::: code-group
596596
```bash [Example]
@@ -665,7 +665,7 @@ function test_failure() {
665665
666666
Reports an error if `directory` does not exist.
667667
668-
[assert_directory_not_exists](#assert-directory-not-exists) is the inverse of this assertion and takes the same arguments.
668+
- [assert_directory_not_exists](#assert-directory-not-exists) is the inverse of this assertion and takes the same arguments.
669669
670670
::: code-group
671671
```bash [Example]
@@ -709,7 +709,7 @@ function test_failure() {
709709
710710
Reports an error if `directory` is not an empty directory.
711711
712-
[assert_is_directory_not_empty](#assert-is-directory-not-empty) is the inverse of this assertion and takes the same arguments.
712+
- [assert_is_directory_not_empty](#assert-is-directory-not-empty) is the inverse of this assertion and takes the same arguments.
713713
714714
::: code-group
715715
```bash [Example]
@@ -733,7 +733,7 @@ function test_failure() {
733733
734734
Reports an error if `directory` is not a readable directory.
735735
736-
[assert_is_directory_not_readable](#assert-is-directory-not-readable) is the inverse of this assertion and takes the same arguments.
736+
- [assert_is_directory_not_readable](#assert-is-directory-not-readable) is the inverse of this assertion and takes the same arguments.
737737
738738
::: code-group
739739
```bash [Example]
@@ -757,7 +757,7 @@ function test_failure() {
757757
758758
Reports an error if `directory` is not a writable directory.
759759
760-
[assert_is_directory_not_writable](#assert-is-directory-not-writable) is the inverse of this assertion and takes the same arguments.
760+
- [assert_is_directory_not_writable](#assert-is-directory-not-writable) is the inverse of this assertion and takes the same arguments.
761761
762762
::: code-group
763763
```bash [Example]
@@ -781,7 +781,7 @@ function test_failure() {
781781
782782
Reports an error if `expected` and `actual` are not equals.
783783
784-
[assert_files_not_equals](#assert-files-not-equals) is the inverse of this assertion and takes the same arguments.
784+
- [assert_files_not_equals](#assert-files-not-equals) is the inverse of this assertion and takes the same arguments.
785785
786786
::: code-group
787787
```bash [Example]
@@ -821,7 +821,7 @@ function test_failure() {
821821
822822
Reports an error if the two variables `expected` and `actual` are the same value.
823823
824-
[assert_same](#assert-same) is the inverse of this assertion and takes the same arguments.
824+
- [assert_same](#assert-same) is the inverse of this assertion and takes the same arguments.
825825
826826
::: code-group
827827
```bash [Example]
@@ -840,7 +840,7 @@ function test_failure() {
840840
841841
Reports an error if `needle` is a substring of `haystack`.
842842
843-
[assert_contains](#assert-contains) is the inverse of this assertion and takes the same arguments.
843+
- [assert_contains](#assert-contains) is the inverse of this assertion and takes the same arguments.
844844
845845
::: code-group
846846
```bash [Example]
@@ -859,7 +859,7 @@ function test_failure() {
859859
860860
Reports an error if `haystack` does starts with `needle`.
861861
862-
[assert_string_starts_with](#assert-string-starts-with) is the inverse of this assertion and takes the same arguments.
862+
- [assert_string_starts_with](#assert-string-starts-with) is the inverse of this assertion and takes the same arguments.
863863
864864
::: code-group
865865
```bash [Example]
@@ -878,7 +878,7 @@ function test_failure() {
878878
879879
Reports an error if `haystack` does ends with `needle`.
880880
881-
[assert_string_ends_with](#assert-string-ends-with) is the inverse of this assertion and takes the same arguments.
881+
- [assert_string_ends_with](#assert-string-ends-with) is the inverse of this assertion and takes the same arguments.
882882
883883
::: code-group
884884
```bash [Example]
@@ -897,7 +897,7 @@ function test_failure() {
897897
898898
Reports an error if `actual` is empty.
899899
900-
[assert_empty](#assert-empty) is the inverse of this assertion and takes the same arguments.
900+
- [assert_empty](#assert-empty) is the inverse of this assertion and takes the same arguments.
901901
902902
::: code-group
903903
```bash [Example]
@@ -916,7 +916,7 @@ function test_failure() {
916916
917917
Reports an error if `value` matches the regular expression `pattern`.
918918
919-
[assert_matches](#assert-matches) is the inverse of this assertion and takes the same arguments.
919+
- [assert_matches](#assert-matches) is the inverse of this assertion and takes the same arguments.
920920
921921
::: code-group
922922
```bash [Example]
@@ -935,7 +935,7 @@ function test_failure() {
935935
936936
Reports an error if `needle` is an element of `haystack`.
937937
938-
[assert_array_contains](#assert-array-contains) is the inverse of this assertion and takes the same arguments.
938+
- [assert_array_contains](#assert-array-contains) is the inverse of this assertion and takes the same arguments.
939939
940940
::: code-group
941941
```bash [Example]
@@ -958,7 +958,7 @@ function test_failure() {
958958
959959
Reports an error if `file` does exists.
960960
961-
[assert_file_exists](#assert-file-exists) is the inverse of this assertion and takes the same arguments.
961+
- [assert_file_exists](#assert-file-exists) is the inverse of this assertion and takes the same arguments.
962962
963963
::: code-group
964964
```bash [Example]
@@ -985,7 +985,7 @@ function test_failed() {
985985
986986
Reports an error if `file` contains the search string.
987987
988-
[assert_file_contains](#assert-file-contains) is the inverse of this assertion and takes the same arguments.
988+
- [assert_file_contains](#assert-file-contains) is the inverse of this assertion and takes the same arguments.
989989
990990
::: code-group
991991
```bash [Example]
@@ -1010,7 +1010,7 @@ function test_failure() {
10101010
10111011
Reports an error if `directory` exists.
10121012
1013-
[assert_directory_exists](#assert-directory-exists) is the inverse of this assertion and takes the same arguments.
1013+
- [assert_directory_exists](#assert-directory-exists) is the inverse of this assertion and takes the same arguments.
10141014
10151015
::: code-group
10161016
```bash [Example]
@@ -1033,7 +1033,7 @@ function test_failure() {
10331033
10341034
Reports an error if `directory` is empty.
10351035
1036-
[assert_is_directory_empty](#assert-is-directory-empty) is the inverse of this assertion and takes the same arguments.
1036+
- [assert_is_directory_empty](#assert-is-directory-empty) is the inverse of this assertion and takes the same arguments.
10371037
10381038
::: code-group
10391039
```bash [Example]
@@ -1057,7 +1057,7 @@ function test_failure() {
10571057
10581058
Reports an error if `directory` is readable.
10591059
1060-
[assert_is_directory_readable](#assert-is-directory-readable) is the inverse of this assertion and takes the same arguments.
1060+
- [assert_is_directory_readable](#assert-is-directory-readable) is the inverse of this assertion and takes the same arguments.
10611061
10621062
::: code-group
10631063
```bash [Example]
@@ -1081,7 +1081,7 @@ function test_failure() {
10811081
10821082
Reports an error if `directory` is writable.
10831083
1084-
[assert_is_directory_writable](#assert-is-directory-writable) is the inverse of this assertion and takes the same arguments.
1084+
- [assert_is_directory_writable](#assert-is-directory-writable) is the inverse of this assertion and takes the same arguments.
10851085
10861086
::: code-group
10871087
```bash [Example]
@@ -1106,7 +1106,7 @@ function test_failure() {
11061106
11071107
Reports an error if `expected` and `actual` are not equals.
11081108
1109-
[assert_files_equals](#assert-files-equals) is the inverse of this assertion and takes the same arguments.
1109+
- [assert_files_equals](#assert-files-equals) is the inverse of this assertion and takes the same arguments.
11101110
11111111
::: code-group
11121112
```bash [Example]

0 commit comments

Comments
 (0)