tests/libtest-core: Copy rpm-ostree changes, clean up
I want to keep this a "pure copy-able" file into various projects like rpm-ostree, bwrap, and flatpak. Pull in changes from rpm-ostree to prep for that. While we have the patient open, dedup the code for file matching a bit. Closes: #877 Approved by: jlebon
This commit is contained in:
parent
add88c3a23
commit
a2be46114a
|
|
@ -1,4 +1,8 @@
|
||||||
# Core source library for shell script tests
|
# Core source library for shell script tests; this
|
||||||
|
# file is intended to be the canonical source, which at
|
||||||
|
# is copied at least into:
|
||||||
|
#
|
||||||
|
# - https://github.com/projectatomic/rpm-ostree
|
||||||
#
|
#
|
||||||
# Copyright (C) 2017 Colin Walters <walters@verbum.org>
|
# Copyright (C) 2017 Colin Walters <walters@verbum.org>
|
||||||
#
|
#
|
||||||
|
|
@ -36,6 +40,7 @@ fi
|
||||||
|
|
||||||
# This should really be the default IMO
|
# This should really be the default IMO
|
||||||
export G_DEBUG=fatal-warnings
|
export G_DEBUG=fatal-warnings
|
||||||
|
|
||||||
assert_streq () {
|
assert_streq () {
|
||||||
test "$1" = "$2" || fatal "$1 != $2"
|
test "$1" = "$2" || fatal "$1 != $2"
|
||||||
}
|
}
|
||||||
|
|
@ -58,18 +63,29 @@ assert_has_dir () {
|
||||||
test -d "$1" || fatal "Couldn't find '$1'"
|
test -d "$1" || fatal "Couldn't find '$1'"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Dump ls -al + file contents to stderr, then fatal()
|
||||||
|
_fatal_print_file() {
|
||||||
|
file="$1"
|
||||||
|
shift
|
||||||
|
ls -al "$file" >&2
|
||||||
|
sed -e 's/^/# /' < "$file" >&2
|
||||||
|
fatal "$@"
|
||||||
|
}
|
||||||
|
|
||||||
assert_not_has_file () {
|
assert_not_has_file () {
|
||||||
if test -f "$1"; then
|
if test -f "$1"; then
|
||||||
sed -e 's/^/# /' < "$1" >&2
|
_fatal_print_file "$1" "File '$1' exists"
|
||||||
fatal "File '$1' exists"
|
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_not_file_has_content () {
|
assert_not_file_has_content () {
|
||||||
if grep -q -e "$2" "$1"; then
|
fpath=$1
|
||||||
sed -e 's/^/# /' < "$1" >&2
|
shift
|
||||||
fatal "File '$1' incorrectly matches regexp '$2'"
|
for re in "$@"; do
|
||||||
fi
|
if grep -q -e "$re" "$fpath"; then
|
||||||
|
_fatal_print_file "$fpath" "File '$fpath' matches regexp '$re'"
|
||||||
|
fi
|
||||||
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_not_has_dir () {
|
assert_not_has_dir () {
|
||||||
|
|
@ -79,35 +95,33 @@ assert_not_has_dir () {
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_file_has_content () {
|
assert_file_has_content () {
|
||||||
if ! grep -q -e "$2" "$1"; then
|
fpath=$1
|
||||||
sed -e 's/^/# /' < "$1" >&2
|
shift
|
||||||
fatal "File '$1' doesn't match regexp '$2'"
|
for re in "$@"; do
|
||||||
fi
|
if ! grep -q -e "$re" "$fpath"; then
|
||||||
|
_fatal_print_file "$fpath" "File '$fpath' doesn't match regexp '$re'"
|
||||||
|
fi
|
||||||
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_file_has_content_literal () {
|
assert_file_has_content_literal () {
|
||||||
if ! grep -q -F -e "$2" "$1"; then
|
if ! grep -q -F -e "$2" "$1"; then
|
||||||
sed -e 's/^/# /' < "$1" >&2
|
_fatal_print_file "$1" "File '$1' doesn't match fixed string list '$2'"
|
||||||
fatal "File '$1' doesn't match fixed string list '$2'"
|
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_symlink_has_content () {
|
assert_symlink_has_content () {
|
||||||
if ! test -L "$1"; then
|
if ! test -L "$1"; then
|
||||||
echo 1>&2 "File '$1' is not a symbolic link"
|
fatal "File '$1' is not a symbolic link"
|
||||||
exit 1
|
|
||||||
fi
|
fi
|
||||||
if ! readlink "$1" | grep -q -e "$2"; then
|
if ! readlink "$1" | grep -q -e "$2"; then
|
||||||
sed -e 's/^/# /' < "$1" >&2
|
_fatal_print_file "$1" "Symbolic link '$1' doesn't match regexp '$2'"
|
||||||
echo 1>&2 "Symbolic link '$1' doesn't match regexp '$2'"
|
|
||||||
exit 1
|
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_file_empty() {
|
assert_file_empty() {
|
||||||
if test -s "$1"; then
|
if test -s "$1"; then
|
||||||
sed -e 's/^/# /' < "$1" >&2
|
_fatal_print_file "$1" "File '$1' is not empty"
|
||||||
fatal "File '$1' is not empty"
|
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue