You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+242-1Lines changed: 242 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -39,6 +39,8 @@ This project provides the following functions:
39
39
-[assert_output](#assert_output) / [refute_output](#refute_output) Assert output does (or does not) contain given content.
40
40
-[assert_line](#assert_line) / [refute_line](#refute_line) Assert a specific line of output does (or does not) contain given content.
41
41
-[assert_regex](#assert_regex) / [refute_regex](#refute_regex) Assert a parameter does (or does not) match given pattern.
42
+
-[assert_stderr](#assert_stderr) / [refute_stderr](#refute_stderr) Assert stderr does (or does not) contain given content.
43
+
-[assert_stderr_line](#assert_stderr_line) / [refute_stderr_line](#refute_stderr_line) Assert a specific line of stderr does (or does not) contain given content.
42
44
43
45
These commands are described in more detail below.
44
46
@@ -759,7 +761,7 @@ Fail if the value (first parameter) matches the pattern (second parameter).
759
761
760
762
On failure, the value, the pattern and the match are displayed.
761
763
762
-
```
764
+
```bash
763
765
@test 'refute_regex()' {
764
766
refute_regex 'WhatsApp''What.'
765
767
}
@@ -786,6 +788,245 @@ For description of the matching behavior, refer to the documentation of the
786
788
> Thus, it's good practice to avoid using `BASH_REMATCH` in conjunction with `refute_regex()`.
787
789
> The valuable information the array contains is the matching part of the value which is printed in the failing test log, as mentioned above._
788
790
791
+
### `assert_stderr`
792
+
793
+
> _**Note**:
794
+
> `run` has to be called with `--separate-stderr` to separate stdout and stderr into `$output` and `$stderr`.
795
+
> If not, `$stderr` will be empty, causing `assert_stderr` to always fail.
796
+
797
+
Similarly to `assert_output`, this function verifies that a command or function produces the expected stderr.
798
+
The stderr matching can be literal (the default), partial or by regular expression.
799
+
The expected stderr can be specified either by positional argument or read from STDIN by passing the `-`/`--stdin` flag.
800
+
801
+
#### Literal matching
802
+
803
+
By default, literal matching is performed.
804
+
The assertion fails if `$stderr` does not equal the expected stderr.
805
+
806
+
```bash
807
+
echo_err() {
808
+
echo "$@" >&2
809
+
}
810
+
811
+
@test 'assert_stderr()' {
812
+
run --separate-stderr echo_err 'have'
813
+
assert_stderr 'want'
814
+
}
815
+
816
+
@test 'assert_stderr() with pipe' {
817
+
run --separate-stderr echo_err 'hello'
818
+
echo_err 'hello' | assert_stderr -
819
+
}
820
+
821
+
@test 'assert_stderr() with herestring' {
822
+
run --separate-stderr echo_err 'hello'
823
+
assert_stderr - <<< hello
824
+
}
825
+
```
826
+
827
+
On failure, the expected and actual stderr are displayed.
828
+
829
+
```
830
+
-- stderr differs --
831
+
expected : want
832
+
actual : have
833
+
--
834
+
```
835
+
836
+
#### Existence
837
+
838
+
To assert that any stderr exists at all, omit the `expected` argument.
839
+
840
+
```bash
841
+
@test 'assert_stderr()' {
842
+
run --separate-stderr echo_err 'have'
843
+
assert_stderr
844
+
}
845
+
```
846
+
847
+
On failure, an error message is displayed.
848
+
849
+
```
850
+
-- no stderr --
851
+
expected non-empty stderr, but stderr was empty
852
+
--
853
+
```
854
+
855
+
#### Partial matching
856
+
857
+
Partial matching can be enabled with the `--partial` option (`-p` for short).
858
+
When used, the assertion fails if the expected _substring_ is not found in `$stderr`.
859
+
860
+
```bash
861
+
@test 'assert_stderr() partial matching' {
862
+
run --separate-stderr echo_err 'ERROR: no such file or directory'
863
+
assert_stderr --partial 'SUCCESS'
864
+
}
865
+
```
866
+
867
+
On failure, the substring and the stderr are displayed.
868
+
869
+
```
870
+
-- stderr does not contain substring --
871
+
substring : SUCCESS
872
+
stderr : ERROR: no such file or directory
873
+
--
874
+
```
875
+
876
+
#### Regular expression matching
877
+
878
+
Regular expression matching can be enabled with the `--regexp` option (`-e` for short).
879
+
When used, the assertion fails if the *extended regular expression* does not match `$stderr`.
880
+
881
+
*Note: The anchors `^` and `$` bind to the beginning and the end (respectively) of the entire stderr; not individual lines.*
0 commit comments