tests/functional: Also keep plain grep calls safe from newlines

This commit is contained in:
Robert Hensing 2024-07-16 01:54:12 +02:00
parent 644b97ce25
commit 41a03738d6
2 changed files with 21 additions and 3 deletions

View File

@ -351,9 +351,12 @@ checkGrepArgs() {
#
# `!` normally doesn't work well with `set -e`, but when we wrap in a
# function it *does*.
#
# `command grep` lets us avoid re-checking the args by going directly to the
# executable.
grepInverse() {
checkGrepArgs "$@" && \
! grep "$@"
! command grep "$@"
}
# A shorthand, `> /dev/null` is a bit noisy.
@ -367,15 +370,26 @@ grepInverse() {
# the closing of the pipe, the buffering of the pipe, and the speed of
# the producer into the pipe. But rest assured we've seen it happen in
# CI reliably.
#
# `command grep` lets us avoid re-checking the args by going directly to the
# executable.
grepQuiet() {
checkGrepArgs "$@" && \
grep "$@" > /dev/null
command grep "$@" > /dev/null
}
# The previous two, combined
grepQuietInverse() {
checkGrepArgs "$@" && \
! grep "$@" > /dev/null
! command grep "$@" > /dev/null
}
# Wrap grep to remove its newline footgun; see checkGrepArgs.
# Note that we keep the checkGrepArgs calls in the other helpers, because some
# of them are negated and that would defeat this check.
grep() {
checkGrepArgs "$@" && \
command grep "$@"
}
# Return the number of arguments

View File

@ -113,3 +113,7 @@ unset res
# treats them as multiple queries.
( echo foo; echo bar; ) | expectStderr -101 grepQuiet $'foo\nbar' \
| grepQuiet -E 'test-infra\.sh:[0-9]+: in call to grepQuiet: newline not allowed in arguments; grep would try each line individually as if connected by an OR operator'
# We took the blue pill and woke up in a world where `grep` is moderately safe.
expectStderr -101 grep $'foo\nbar' \
| grepQuiet -E 'test-infra\.sh:[0-9]+: in call to grep: newline not allowed in arguments; grep would try each line individually as if connected by an OR operator'