mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-10 08:16:15 +02:00
tests/vars-and-functions: Add callerPrefix helper
This commit is contained in:
parent
783a8341ee
commit
f2df3f0c6c
2 changed files with 43 additions and 0 deletions
|
@ -297,6 +297,45 @@ onError() {
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Prints an error message prefix referring to the last call into this file.
|
||||||
|
# Ignores `expect` and `expectStderr` calls.
|
||||||
|
# Set a special exit code when test suite functions are misused, so that
|
||||||
|
# functions like expectStderr won't mistake them for expected Nix CLI errors.
|
||||||
|
# Suggestion: -101 (negative to indicate very abnormal, and beyond the normal
|
||||||
|
# range of signals)
|
||||||
|
# Example (showns as string): 'repl.sh:123: in call to grepQuiet: '
|
||||||
|
# This function is inefficient, so it should only be used in error messages.
|
||||||
|
callerPrefix() {
|
||||||
|
# Find the closes caller that's not from this file
|
||||||
|
local i file line fn savedFn
|
||||||
|
# Use `caller`
|
||||||
|
for i in $(seq 0 100); do
|
||||||
|
caller $i > /dev/null || {
|
||||||
|
if [[ -n "${file:-}" ]]; then
|
||||||
|
echo "$file:$line: ${savedFn+in call to $savedFn: }"
|
||||||
|
fi
|
||||||
|
break
|
||||||
|
}
|
||||||
|
line="$(caller $i | cut -d' ' -f1)"
|
||||||
|
fn="$(caller $i | cut -d' ' -f2)"
|
||||||
|
file="$(caller $i | cut -d' ' -f3)"
|
||||||
|
if [[ $file != "${BASH_SOURCE[0]}" ]]; then
|
||||||
|
echo "$file:$line: ${savedFn+in call to $savedFn: }"
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
case "$fn" in
|
||||||
|
# Ignore higher order functions that don't report any misuse of themselves
|
||||||
|
# This way a misuse of a foo in `expectStderr 1 foo` will be reported as
|
||||||
|
# calling foo, not expectStderr.
|
||||||
|
expect|expectStderr|callerPrefix)
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
savedFn="$fn"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
# `grep -v` doesn't work well for exit codes. We want `!(exist line l. l
|
# `grep -v` doesn't work well for exit codes. We want `!(exist line l. l
|
||||||
# matches)`. It gives us `exist line l. !(l matches)`.
|
# matches)`. It gives us `exist line l. !(l matches)`.
|
||||||
#
|
#
|
||||||
|
|
|
@ -88,6 +88,10 @@ funBang () {
|
||||||
expect 1 funBang
|
expect 1 funBang
|
||||||
unset funBang
|
unset funBang
|
||||||
|
|
||||||
|
# callerPrefix can be used by the test framework to improve error messages
|
||||||
|
# it reports about our call site here
|
||||||
|
echo "<[$(callerPrefix)]>" | grepQuiet -F "<[test-infra.sh:$LINENO: ]>"
|
||||||
|
|
||||||
# `grep -v -q` is not what we want for exit codes, but `grepInverse` is
|
# `grep -v -q` is not what we want for exit codes, but `grepInverse` is
|
||||||
# Avoid `grep -v -q`. The following line proves the point, and if it fails,
|
# Avoid `grep -v -q`. The following line proves the point, and if it fails,
|
||||||
# we'll know that `grep` had a breaking change or `-v -q` may not be portable.
|
# we'll know that `grep` had a breaking change or `-v -q` may not be portable.
|
||||||
|
|
Loading…
Reference in a new issue