Skip to content

Commit 8d919a1

Browse files
committed
feat: add refute_stderr_line
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.
1 parent 448b1bc commit 8d919a1

File tree

3 files changed

+400
-1
lines changed

3 files changed

+400
-1
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ This project provides the following functions:
4040
- [assert_line](#assert_line) / [refute_line](#refute_line) Assert a specific line of output does (or does not) contain given content.
4141
- [assert_regex](#assert_regex) / [refute_regex](#refute_regex) Assert a parameter does (or does not) match given pattern.
4242
- [assert_stderr](#assert_stderr) / [refute_stderr](#refute_stderr) Assert stderr does (or does not) contain given content.
43-
- [assert_stderr_line](#assert_stderr_line) Assert a specific line of stderr does 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.
4444

4545
These commands are described in more detail below.
4646

@@ -1007,6 +1007,17 @@ On failure, the same details are displayed as for literal matching, except that
10071007
--
10081008
```
10091009
1010+
### `refute_stderr_line`
1011+
1012+
> _**Note**:
1013+
> `run` has to be called with `--separate-stderr` to separate stdout and stderr into `$output` and `$stderr`.
1014+
> If not, `$stderr` will be empty, causing `refute_stderr_line` to always pass.
1015+
1016+
Similarly to `refute_stderr`, this function helps to verify that a command or function produces the correct stderr.
1017+
It checks that the unexpected line does not appear in the stderr (default) or in a specific line of it.
1018+
Matching can be literal (default), partial or regular expression.
1019+
This function is the logical complement of `assert_stderr_line`.
1020+
10101021
<!-- REFERENCES -->
10111022
10121023
[bats]: https://github.com/bats-core/bats-core

src/refute_line.bash

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,38 @@ refute_line() {
128128
__refute_stream_line "$@"
129129
}
130130

131+
# refute_stderr_line
132+
# ==================
133+
#
134+
# Summary: Fail if the unexpected line is found in the stderr (default) or at a specific line number.
135+
#
136+
# Usage: refute_stderr_line [-n index] [-p | -e] [--] <unexpected>
137+
#
138+
# Options:
139+
# -n, --index <idx> Match the <idx>th line
140+
# -p, --partial Match if `unexpected` is a substring of `$stderr` or line <idx>
141+
# -e, --regexp Treat `unexpected` as an extended regular expression
142+
# <unexpected> The unexpected line string, substring, or regular expression.
143+
#
144+
# IO:
145+
# STDERR - details, on failure
146+
# error message, on error
147+
# Globals:
148+
# stderr
149+
# stderr_lines
150+
# Returns:
151+
# 0 - if match not found
152+
# 1 - otherwise
153+
#
154+
# Similarly to `refute_stderr`, this function verifies that a command or function does not produce the unexpected stderr.
155+
# (It is the logical complement of `assert_stderr_line`.)
156+
# It checks that the unexpected line does not appear in the stderr (default) or at a specific line number.
157+
# Matching can be literal (default), partial or regular expression.
158+
#
159+
refute_stderr_line() {
160+
__refute_stream_line "$@"
161+
}
162+
131163
__refute_stream_line() {
132164
local -r caller=${FUNCNAME[1]}
133165
local -i is_match_line=0

0 commit comments

Comments
 (0)