Skip to content

Commit 56052d9

Browse files
committed
Introduces skip_if and can now be used through pre-commit
1 parent 3bda81e commit 56052d9

File tree

2 files changed

+148
-6
lines changed

2 files changed

+148
-6
lines changed

bash_unit

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# shellcheck disable=2317 # Ignore unreachable - most function are not called.
2020
# shellcheck disable=2155 # Ignore Declare and assign separately
2121

22-
VERSION=v2.1.0
22+
VERSION=v2.2.0
2323

2424
ESCAPE=$(printf "\033")
2525
NOCOLOR="${ESCAPE}[0m"

docs/man/man1/bash_unit.1

+147-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
'\" t
22
.\" Title: bash_unit
33
.\" Author: [see the "AUTHOR(S)" section]
4-
.\" Generator: Asciidoctor 2.0.18
5-
.\" Date: 2023-03-03
4+
.\" Generator: Asciidoctor 2.0.21
5+
.\" Date: 2024-02-22
66
.\" Manual: \ \&
77
.\" Source: \ \&
88
.\" Language: English
99
.\"
10-
.TH "BASH_UNIT" "1" "2023-03-03" "\ \&" "\ \&"
10+
.TH "BASH_UNIT" "1" "2024-02-22" "\ \&" "\ \&"
1111
.ie \n(.g .ds Aq \(aq
1212
.el .ds Aq '
1313
.ss \n[.ss] 0
@@ -31,7 +31,7 @@
3131
bash_unit \- bash unit testing enterprise edition framework for professionals!
3232
.SH "SYNOPSIS"
3333
.sp
34-
\fBbash_unit\fP [\-f tap] [\-p <pattern>] [\-r] [test_file]
34+
\fBbash_unit\fP [\-f tap] [\-p <pattern>] [\-s <pattern>] [\-r] [test_file]
3535
.SH "DESCRIPTION"
3636
.sp
3737
\fBbash_unit\fP allows you to write unit tests (functions starting with \fBtest\fP),
@@ -55,6 +55,15 @@ You can specify several patterns by repeating this option
5555
for each pattern.
5656
.RE
5757
.sp
58+
\fB\-s\fP \fIpattern\fP
59+
.RS 4
60+
skip tests which name matches the given pattern.
61+
You can specify several patterns by repeating this option
62+
for each pattern.
63+
Tests will appear in \fBbash_unit\fP output as \fIskipped\fP.
64+
(see also \fIskip_if\fP)
65+
.RE
66+
.sp
5867
\fB\-r\fP
5968
.RS 4
6069
executes test cases in random order.
@@ -68,6 +77,58 @@ command line).
6877
specify an alternative output format.
6978
The only supported value is \fBtap\fP.
7079
.RE
80+
.SS "\c .URL "https://pre\-commit.com" "pre\-commit" hook"
81+
.sp
82+
You can run \f(CRbash_unit\fP as a \c
83+
.URL "https://pre\-commit.com" "pre\-commit" ""
84+
hook.
85+
.sp
86+
Add the following to your \c
87+
.URL "https://pre\-commit.com" "pre\-commit" ""
88+
configuration. By default it will run scripts that are identified as shell scripts that match the path \f(CR^tests/(.*/)?test_.*\(rs.sh$\fP.
89+
.sp
90+
.if n .RS 4
91+
.nf
92+
.fam C
93+
repos:
94+
\- repo: https://github.com/pgrange/bash_unit
95+
rev: v1.7.0
96+
hooks:
97+
\- id: bash\-unit
98+
.fam
99+
.fi
100+
.if n .RE
101+
.SH "RUNNING \f(CRbash_unit\fP IN THE TEST SCRIPT"
102+
.sp
103+
In some cases you want to run \f(CRbash_unit\fP from inside the test script.
104+
.sp
105+
One example is this project’s \f(CRtest_doc.sh\fP test script.
106+
.sp
107+
One method to define a \f(CRBASH_UNIT\fP variable is shown below:
108+
.sp
109+
.if n .RS 4
110+
.nf
111+
.fam C
112+
# Function to recursively search upwards for file
113+
_find_file() {
114+
local dir="$1"
115+
local file="$2"
116+
while [ "${dir}" != "/" ]; do
117+
if [ \-f "${dir}/${file}" ]; then
118+
echo "${dir}/${file}"
119+
return 0
120+
fi
121+
dir=$(dirname "${dir}")
122+
done
123+
return 1
124+
}
125+
126+
B_U=$(_find_file "$(dirname "$(realpath "$0")")" bash_unit)
127+
# shellcheck disable=2089
128+
BASH_UNIT="eval FORCE_COLOR=false \(rs"$B_U\(rs""
129+
.fam
130+
.fi
131+
.if n .RE
71132
.SH "HOW TO RUN TESTS"
72133
.sp
73134
To run tests, simply call \fBbash_unit\fP with all your tests files as parameter. For instance to run some \fBbash_unit\fP tests, from \fBbash_unit\fP directory:
@@ -161,6 +222,47 @@ Overall result: SUCCESS
161222
.fi
162223
.if n .RE
163224
.sp
225+
You can combine the \fI\-p\fP option with \fI\-s\fP to skip some of the tests. This option accepts a pattern
226+
as parameter and mark as skipped any test function which matches this pattern.
227+
.sp
228+
.if n .RS 4
229+
.nf
230+
.fam C
231+
\&./bash_unit \-p fail_fails \-p assert \-s no \-s status tests/test_core.sh
232+
.fam
233+
.fi
234+
.if n .RE
235+
.sp
236+
.if n .RS 4
237+
.nf
238+
.fam C
239+
Running tests in tests/test_core.sh
240+
Running test_assert_equals_fails_when_not_equal ... SKIPPED
241+
Running test_assert_matches_fails_when_not_matching ... SKIPPED
242+
Running test_assert_no_diff_fails_when_diff ... SKIPPED
243+
Running test_assert_no_diff_succeeds_when_no_diff ... SKIPPED
244+
Running test_assert_not_equals_fails_when_equal ... SKIPPED
245+
Running test_assert_not_equals_succeeds_when_not_equal ... SKIPPED
246+
Running test_assert_not_matches_fails_when_matching ... SKIPPED
247+
Running test_assert_not_matches_succeed_when_not_matching ... SKIPPED
248+
Running test_assert_status_code_fails ... SKIPPED
249+
Running test_assert_status_code_succeeds ... SKIPPED
250+
Running test_assert_equals_succeed_when_equal ... SUCCESS
251+
Running test_assert_fails ... SUCCESS
252+
Running test_assert_fails_fails ... SUCCESS
253+
Running test_assert_fails_succeeds ... SUCCESS
254+
Running test_assert_matches_succeed_when_matching ... SUCCESS
255+
Running test_assert_shows_stderr_on_failure ... SUCCESS
256+
Running test_assert_shows_stdout_on_failure ... SUCCESS
257+
Running test_assert_succeeds ... SUCCESS
258+
Running test_assert_within_delta_fails ... SUCCESS
259+
Running test_assert_within_delta_succeeds ... SUCCESS
260+
Running test_fail_fails ... SUCCESS
261+
Overall result: SUCCESS
262+
.fam
263+
.fi
264+
.if n .RE
265+
.sp
164266
\fBbash_unit\fP supports the \c
165267
.URL "http://testanything.org/" "Test Anything Protocol" ""
166268
so you can ask for a tap formatted
@@ -690,6 +792,46 @@ doc:2:test_obvious_notmatching_with_assert_no_diff()
690792
.fam
691793
.fi
692794
.if n .RE
795+
.SH "\fBSKIP_IF\fP FUNCTION"
796+
.sp
797+
.if n .RS 4
798+
.nf
799+
.fam C
800+
skip_if <condition> <pattern>
801+
.fam
802+
.fi
803+
.if n .RE
804+
.sp
805+
If \fIcondition\fP is true, will skip all the tests in the current file which match the given \fIpattern\fP.
806+
.sp
807+
This can be useful when one has tests that are dependent on system environment, for instance:
808+
.sp
809+
.if n .RS 4
810+
.nf
811+
.fam C
812+
skip_if "uname | grep Darwin" linux
813+
skip_if "uname | grep Linux" darwin
814+
815+
test_linux_proc_exists() {
816+
assert "ls /proc/" "there should exist /proc on Linux"
817+
}
818+
test_darwin_proc_does_not_exist() {
819+
assert_fail "ls /proc/" "there should not exist /proc on Darwin"
820+
}
821+
.fam
822+
.fi
823+
.if n .RE
824+
.sp
825+
will output, on a Linux system:
826+
.sp
827+
.if n .RS 4
828+
.nf
829+
.fam C
830+
Running test_darwin_proc_does_not_exist ... SKIPPED
831+
Running test_linux_proc_exists ... SUCCESS
832+
.fam
833+
.fi
834+
.if n .RE
693835
.SH "\fBFAKE\fP FUNCTION"
694836
.sp
695837
.if n .RS 4
@@ -1094,4 +1236,4 @@ test_get_data_from_fake() {
10941236
doc:13:test_get_data_from_fake()
10951237
.fam
10961238
.fi
1097-
.if n .RE
1239+
.if n .RE

0 commit comments

Comments
 (0)