Skip to content

Commit 00ff87e

Browse files
committed
Introduce skip_if
On can now skip some tests in the file if some condition is true.
1 parent c15f49a commit 00ff87e

File tree

4 files changed

+63
-21
lines changed

4 files changed

+63
-21
lines changed

README.adoc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ _(by the way, the documentation you are reading is itself tested with bash-unit)
4141
You can specify several patterns by repeating this option
4242
for each pattern.
4343
Tests will appear in *bash_unit* output as _pending_.
44+
(see also _skip_if_)
4445

4546
*-r*::
4647
executes test cases in random order.
@@ -618,6 +619,33 @@ out> > bar
618619
doc:2:test_obvious_notmatching_with_assert_no_diff()
619620
```
620621

622+
== *skip_if* function
623+
624+
skip_if <condition> <pattern>
625+
626+
If _condition_ is true, will skip all the tests in the current file which match the given _pattern_.
627+
628+
This can be useful when one has tests that are dependent on system environment, for instance:
629+
630+
```test
631+
skip_if "uname | grep Darwin" linux
632+
skip_if "uname | grep Linux" darwin
633+
634+
test_linux_proc_exists() {
635+
assert "ls /proc/" "there should exist /proc on Linux"
636+
}
637+
test_darwin_proc_does_not_exist() {
638+
assert_fail "ls /proc/" "there should not exist /proc on Darwin"
639+
}
640+
```
641+
642+
will output, on a Linux system:
643+
644+
```output
645+
Running test_darwin_proc_does_not_exist ... PENDING
646+
Running test_linux_proc_exists ... SUCCESS
647+
```
648+
621649
== *fake* function
622650

623651
fake <command> [replacement code]

bash_unit

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,16 @@ tap_format() {
437437
}
438438
}
439439

440+
skip_if() {
441+
local condition="$1"
442+
local pattern="$2"
443+
if eval $condition >/dev/null 2>&1
444+
then
445+
skip_pattern="${skip_pattern}${skip_pattern_separator}${pattern}"
446+
skip_pattern_separator="|"
447+
fi
448+
}
449+
440450
output_format=text
441451
test_pattern=""
442452
test_pattern_separator=""
Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
11
# This test demonstrates some test skipping logic that
22
# one can implement with bash_unit
33

4-
todo_will_always_be_siped() {
4+
todo_will_always_be_skipped() {
55
fail "This test is always skipped"
66
}
77

8-
if uname | grep Linux
9-
then
10-
test_proc_exists() {
11-
assert "ls /proc/" "there should exist /proc on Linux"
12-
}
13-
test_another_test_to_run_only_on_linux() {
14-
assert "mkdir /tmp/foo/bar -p"
15-
}
16-
fi
8+
skip_if "uname | grep Darwin" linux
9+
skip_if "uname | grep Linux" darwin
10+
11+
test_linux_proc_exists() {
12+
assert "ls /proc/" "there should exist /proc on Linux"
13+
}
14+
test_another_test_to_run_only_on_linux() {
15+
assert "mkdir /tmp/foo/bar -p"
16+
}
1717

1818
test_that_always_run() {
1919
assert true
2020
}
2121

22-
if uname | grep Darwin
23-
then
24-
test_proc_does_not_exist() {
25-
assert_fail "ls /proc/" "there should not exist /proc on Darwin"
26-
}
27-
test_another_test_to_run_only_on_darwin() {
28-
assert "mkdir -p /tmp/foo/bar"
29-
}
30-
fi
22+
test_darwin_proc_does_not_exist() {
23+
assert_fail "ls /proc/" "there should not exist /proc on Darwin"
24+
}
25+
test_another_test_to_run_only_on_darwin() {
26+
assert "mkdir -p /tmp/foo/bar"
27+
}

tests/test_cli.sh

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,26 @@ Overall result: SUCCESS" \
8888
test_do_not_run_skipped_tests() {
8989
assert "$BASH_UNIT -s two \
9090
<(echo 'test_one() { echo -n ; }
91-
test_two() { fail ; }') \
91+
test_two() { fail ; }
92+
test_three() { fail ; }
93+
skip_if true three
94+
') \
9295
"
9396
}
9497

9598
test_skipped_tests_appear_in_output() {
9699
bash_unit_output=$($BASH_UNIT -s two \
97100
<(echo 'test_one() { echo -n ; }
98-
test_two() { fail ; }') \
101+
test_two() { fail ; }
102+
test_three() { fail ; }
103+
skip_if true three
104+
') \
99105
| "$SED" -e 's:/dev/fd/[0-9]*:test_file:' \
100106
)
101107

102108
assert_equals "\
103109
Running tests in test_file
110+
Running test_three ... PENDING
104111
Running test_two ... PENDING
105112
Running test_one ... SUCCESS
106113
Overall result: SUCCESS" \

0 commit comments

Comments
 (0)