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
Refactor the `assert_output` function to improve clarity and
maintainability. Introduce a new helper function `__assert_stream`
to handle different stream types (output and stderr).
Introduce the `assert_stderr` function to validate that the
standard error output matches the expected value.
Refactor the `refute_output` function to improve stream handling by
introducing a new `__refute_stream` function. This change allows for
more flexible handling of both `output` and `stderr` streams. This
enhances the maintainability and usability of the code.
Introduce the `refute_stderr` function to allow verification that a command
or function does not produce unexpected stderr. This function complements
`assert_stderr` and supports matching via literal, partial, or regular
expression. Update documentation to reflect this addition and clarify
usage of `assert_stderr` with `--separate-stderr` for proper stderr
handling.
Add a new assert_stderr_line function for checking expected lines
in the stderr output. This improves the clarity and functionality
of the assertion process.
Refactor the `refute_line` function to handle both output and
stderr streams uniformly. Introduce a new helper function
`__refute_stream_line` to streamline the logic and reduce
code duplication.
Introduce the `refute_stderr_line` function to verify that an
unexpected line does not appear in the stderr output. Update
documentation to reflect usage and necessary conditions for correct
operation, including the requirement to use `--separate-stderr`
when running commands.
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
@@ -750,7 +752,7 @@ Fail if the value (first parameter) matches the pattern (second parameter).
750
752
751
753
On failure, the value, the pattern and the match are displayed.
752
754
753
-
```
755
+
```bash
754
756
@test 'refute_regex()' {
755
757
refute_regex 'WhatsApp''What.'
756
758
}
@@ -777,6 +779,245 @@ For description of the matching behavior, refer to the documentation of the
777
779
> Thus, it's good practice to avoid using `BASH_REMATCH` in conjunction with `refute_regex()`.
778
780
> The valuable information the array contains is the matching part of the value which is printed in the failing test log, as mentioned above._
779
781
782
+
### `assert_stderr`
783
+
784
+
> _**Note**:
785
+
> `run` has to be called with `--separate-stderr` to separate stdout and stderr into `$output` and `$stderr`.
786
+
> If not, `$stderr` will be empty, causing `assert_stderr` to always fail.
787
+
788
+
Similarly to `assert_output`, this function verifies that a command or function produces the expected stderr.
789
+
The stderr matching can be literal (the default), partial or by regular expression.
790
+
The expected stderr can be specified either by positional argument or read from STDIN by passing the `-`/`--stdin` flag.
791
+
792
+
#### Literal matching
793
+
794
+
By default, literal matching is performed.
795
+
The assertion fails if `$stderr` does not equal the expected stderr.
796
+
797
+
```bash
798
+
echo_err() {
799
+
echo "$@" >&2
800
+
}
801
+
802
+
@test 'assert_stderr()' {
803
+
run --separate-stderr echo_err 'have'
804
+
assert_stderr 'want'
805
+
}
806
+
807
+
@test 'assert_stderr() with pipe' {
808
+
run --separate-stderr echo_err 'hello'
809
+
echo_err 'hello' | assert_stderr -
810
+
}
811
+
812
+
@test 'assert_stderr() with herestring' {
813
+
run --separate-stderr echo_err 'hello'
814
+
assert_stderr - <<< hello
815
+
}
816
+
```
817
+
818
+
On failure, the expected and actual stderr are displayed.
819
+
820
+
```
821
+
-- stderr differs --
822
+
expected : want
823
+
actual : have
824
+
--
825
+
```
826
+
827
+
#### Existence
828
+
829
+
To assert that any stderr exists at all, omit the `expected` argument.
830
+
831
+
```bash
832
+
@test 'assert_stderr()' {
833
+
run --separate-stderr echo_err 'have'
834
+
assert_stderr
835
+
}
836
+
```
837
+
838
+
On failure, an error message is displayed.
839
+
840
+
```
841
+
-- no stderr --
842
+
expected non-empty stderr, but stderr was empty
843
+
--
844
+
```
845
+
846
+
#### Partial matching
847
+
848
+
Partial matching can be enabled with the `--partial` option (`-p` for short).
849
+
When used, the assertion fails if the expected _substring_ is not found in `$stderr`.
850
+
851
+
```bash
852
+
@test 'assert_stderr() partial matching' {
853
+
run --separate-stderr echo_err 'ERROR: no such file or directory'
854
+
assert_stderr --partial 'SUCCESS'
855
+
}
856
+
```
857
+
858
+
On failure, the substring and the stderr are displayed.
859
+
860
+
```
861
+
-- stderr does not contain substring --
862
+
substring : SUCCESS
863
+
stderr : ERROR: no such file or directory
864
+
--
865
+
```
866
+
867
+
#### Regular expression matching
868
+
869
+
Regular expression matching can be enabled with the `--regexp` option (`-e` for short).
870
+
When used, the assertion fails if the *extended regular expression* does not match `$stderr`.
871
+
872
+
*Note: The anchors `^` and `$` bind to the beginning and the end (respectively) of the entire stderr; not individual lines.*
0 commit comments