mirror of
https://github.com/NixOS/nix.git
synced 2024-11-25 08:12:29 +00:00
tests/functional: Make our grep* helpers reject newlines in the query
Newlines behave like *OR*; not "and then".
This commit is contained in:
parent
f2df3f0c6c
commit
644b97ce25
@ -336,12 +336,23 @@ callerPrefix() {
|
||||
done
|
||||
}
|
||||
|
||||
checkGrepArgs() {
|
||||
local arg
|
||||
for arg in "$@"; do
|
||||
if [[ "$arg" != "${arg//$'\n'/_}" ]]; then
|
||||
echo "$(callerPrefix)newline not allowed in arguments; grep would try each line individually as if connected by an OR operator" >&2
|
||||
return -101
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
# `grep -v` doesn't work well for exit codes. We want `!(exist line l. l
|
||||
# matches)`. It gives us `exist line l. !(l matches)`.
|
||||
#
|
||||
# `!` normally doesn't work well with `set -e`, but when we wrap in a
|
||||
# function it *does*.
|
||||
grepInverse() {
|
||||
checkGrepArgs "$@" && \
|
||||
! grep "$@"
|
||||
}
|
||||
|
||||
@ -357,11 +368,13 @@ grepInverse() {
|
||||
# the producer into the pipe. But rest assured we've seen it happen in
|
||||
# CI reliably.
|
||||
grepQuiet() {
|
||||
checkGrepArgs "$@" && \
|
||||
grep "$@" > /dev/null
|
||||
}
|
||||
|
||||
# The previous two, combined
|
||||
grepQuietInverse() {
|
||||
checkGrepArgs "$@" && \
|
||||
! grep "$@" > /dev/null
|
||||
}
|
||||
|
||||
|
@ -108,3 +108,8 @@ unset res
|
||||
res=$(set -eu -o pipefail; echo foo | expect 1 grepQuietInverse foo | wc -c)
|
||||
(( res == 0 ))
|
||||
unset res
|
||||
|
||||
# `grepQuiet` does not allow newlines in its arguments, because grep quietly
|
||||
# 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'
|
||||
|
Loading…
Reference in New Issue
Block a user