Homebrew method shell_output is unclear

Hi,
i am currently trying to write a test for my homebrew formula, I noticed that a combination of assert_match and shell_output is the way to go. But cannot make this work… it is also not helpful that there is basically no documentation of shell_output other than that: https://www.rubydoc.info/github/Homebrew/brew/Homebrew%2FAssertions:shell_output

Basically a statement like this:

assert_match /DOSBox-X version #{version}/, shell_output("#{bin}/dosbox-x -version 2>&1", 1)

should run the binary with the version flag. In this case here, there is a large block of text, the first line in empty and the second line contains the string in the first argument of assert_match, plus some other words.

So I would guess that assert_match checks if it finds the pattern (first argument) in the second argument, but is does not have to be an exact match.
But what does the second argument of shell_output do? Most formulas do not use it at all…

In my case, when using anything else as 0 I get the error:

Test::Unit::AssertionFailedError: <64> expected but was <0>.

But when I use 0 I get this error:

Error: dosbox-staging: failed
An exception occurred within a child process:
Test::Unit::AssertionFailedError: </dosbox (dosbox-staging), version 0.75.0/> was expected to be =~
<“dosbox (dosbox-staging), version 0.75.0\n” +
“Copyright © 2020 The dosbox-staging team.\n” +
“License GPLv2+: GNU GPL version 2 or later https://www.gnu.org/licenses/gpl-2.0.html\n” +
“\n” +
“This is free software, and you are welcome to change and redistribute it\n” +
“under certain conditions; please read the COPYING file thoroughly before\n” +
“doing so. There is NO WARRANTY, to the extent permitted by law.\n” +
“\n” +
“This program (dosbox-staging) is modified version of DOSBox.\n” +
“Copyright © 2020 The DOSBox Team, published under GNU GPLv2+\n” +
“Read AUTHORS file for more details.\n”>.

I’m not sure why your assert_match is failing, but the second (optional) argument to shell_output is the expected return code. By default (when not specified), it is 0 (i.e. we expect a successful execution), but in some cases we expect the executed command to exit with a non-zero return code.

Thanks for your answer. I also found out what caused the failing of my assert_match statement. I had to escape the brackets