tests: Hide unnecessary trace output from the test logs

We use some BASH_XTRACEFD hackery to hide the trace output from
the internals of the assert (and ok) functions, so that the
log output can focus on what is important, i.e. what asserts are
checked and what errors do they print.
This commit is contained in:
Alexander Larsson
2020-03-20 09:34:13 +01:00
parent b1fdf6c4ef
commit 241c5c7f40

View File

@@ -30,13 +30,23 @@ else
test_builddir=$(dirname $0)
fi
# All the asserts and ok functions below are wrapped such that they
# don't output any set -x traces of their internals (but still echo
# errors to stderr). This way the log output focuses on tracing what
# is essential to the test (the asserts being run and errors from them)
assert_not_reached () {
{ { local BASH_XTRACEFD=3; } 2> /dev/null
echo $@ 1>&2; exit 1
} 3> /dev/null
}
ok () {
echo "ok $@"
echo "============================================================"
# Wrap this to avoid set -x showing the echo commands
{
echo "ok $@";
echo "========================================================================";
} 2> /dev/null
}
test_tmpdir=$(pwd)
@@ -114,64 +124,85 @@ fi
export FLATPAK="${CMD_PREFIX} flatpak"
assert_streq () {
{ { local BASH_XTRACEFD=3; } 2> /dev/null
test "$1" = "$2" || (echo 1>&2 "$1 != $2"; exit 1)
} 3> /dev/null
}
assert_not_streq () {
{ { local BASH_XTRACEFD=3; } 2> /dev/null
(! test "$1" = "$2") || (echo 1>&2 "$1 == $2"; exit 1)
} 3> /dev/null
}
assert_has_file () {
{ { local BASH_XTRACEFD=3; } 2> /dev/null
test -f "$1" || (echo 1>&2 "Couldn't find '$1'"; exit 1)
} 3> /dev/null
}
assert_has_symlink () {
{ { local BASH_XTRACEFD=3; } 2> /dev/null
test -L "$1" || (echo 1>&2 "Couldn't find '$1'"; exit 1)
} 3> /dev/null
}
assert_has_dir () {
{ { local BASH_XTRACEFD=3; } 2> /dev/null
test -d "$1" || (echo 1>&2 "Couldn't find '$1'"; exit 1)
} 3> /dev/null
}
assert_not_has_file () {
{ { local BASH_XTRACEFD=3; } 2> /dev/null
if test -f "$1"; then
sed -e 's/^/# /' < "$1" >&2
echo 1>&2 "File '$1' exists"
exit 1
fi
} 3> /dev/null
}
assert_not_file_has_content () {
{ { local BASH_XTRACEFD=3; } 2> /dev/null
if grep -q -e "$2" "$1"; then
sed -e 's/^/# /' < "$1" >&2
echo 1>&2 "File '$1' incorrectly matches regexp '$2'"
exit 1
fi
} 3> /dev/null
}
assert_file_has_mode () {
{ { local BASH_XTRACEFD=3; } 2> /dev/null
mode=$(stat -c '%a' $1)
if [ "$mode" != "$2" ]; then
echo 1>&2 "File '$1' has wrong mode: expected $2, but got $mode"
exit 1
fi
} 3> /dev/null
}
assert_not_has_dir () {
{ { local BASH_XTRACEFD=3; } 2> /dev/null
if test -d "$1"; then
echo 1>&2 "Directory '$1' exists"; exit 1
fi
} 3> /dev/null
}
assert_file_has_content () {
{ { local BASH_XTRACEFD=3; } 2> /dev/null
if ! grep -q -e "$2" "$1"; then
sed -e 's/^/# /' < "$1" >&2
echo 1>&2 "File '$1' doesn't match regexp '$2'"
exit 1
fi
} 3> /dev/null
}
assert_log_has_gpg_signature_error () {
{ { local BASH_XTRACEFD=3; } 2> /dev/null
if ! grep -q -e "GPG signatures found, but none are in trusted keyring" "$1"; then
if ! grep -q -e "Can't check signature: public key not found" "$1"; then
sed -e 's/^/# /' < "$1" >&2
@@ -179,34 +210,43 @@ assert_log_has_gpg_signature_error () {
exit 1
fi
fi
} 3> /dev/null
}
assert_symlink_has_content () {
{ { local BASH_XTRACEFD=3; } 2> /dev/null
if ! readlink "$1" | grep -q -e "$2"; then
readlink "$1" |sed -e 's/^/# /' >&2
echo 1>&2 "Symlink '$1' doesn't match regexp '$2'"
exit 1
fi
} 3> /dev/null
}
assert_file_empty() {
{ { local BASH_XTRACEFD=3; } 2> /dev/null
if test -s "$1"; then
sed -e 's/^/# /' < "$1" >&2
echo 1>&2 "File '$1' is not empty"
exit 1
fi
} 3> /dev/null
}
assert_remote_has_config () {
{ { local BASH_XTRACEFD=3; } 2> /dev/null
ostree config --repo=$FL_DIR/repo get --group 'remote "'"$1"'"' "$2" > key-output
assert_file_has_content key-output "$3"
} 3> /dev/null
}
assert_remote_has_no_config () {
{ { local BASH_XTRACEFD=3; } 2> /dev/null
if ostree config --repo=$FL_DIR/repo get --group 'remote "'"$1"'"' "$2" > /dev/null; then
echo 1>&2 "Remote '$1' unexpectedly has key '$2'"
exit 1
fi
} 3> /dev/null
}
export FL_GPG_HOMEDIR=${TEST_DATA_DIR}/gpghome