Skip to content

Commit

Permalink
tests: Factor out a libtest-core.sh
Browse files Browse the repository at this point in the history
This could be shared more easily with e.g. rpm-ostree, but what I'm currently
working on is installed, privileged (potentially destructive, i.e. VM) tests
that will source this separately from the current `libtest.sh`. That does work
installed, but in practice is oriented around unit (uninstalled, unprivileged)
tests.

Closes: #807
Approved by: jlebon
  • Loading branch information
cgwalters authored and rh-atomic-bot committed Apr 25, 2017
1 parent 511b31c commit 8b4196d
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 92 deletions.
111 changes: 111 additions & 0 deletions tests/libtest-core.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# Core source library for shell script tests
#
# Copyright (C) 2017 Colin Walters <[email protected]>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

fatal() {
echo $@ 1>&2; exit 1
}
# fatal() is shorter to type, but retain this alias
assert_not_reached () {
fatal "$@"
}

# Some tests look for specific English strings. Use a UTF-8 version
# of the C (POSIX) locale if we have one, or fall back to POSIX
# (https://sourceware.org/glibc/wiki/Proposals/C.UTF-8)
if locale -a | grep C.UTF-8 >/dev/null; then
export LC_ALL=C.UTF-8
else
export LC_ALL=C
fi

# This should really be the default IMO
export G_DEBUG=fatal-warnings
assert_streq () {
test "$1" = "$2" || fatal "$1 != $2"
}

assert_str_match () {
if ! echo "$1" | grep -E -q "$2"; then
fatal "$1 does not match regexp $2"
fi
}

assert_not_streq () {
(! test "$1" = "$2") || fatal "$1 == $2"
}

assert_has_file () {
test -f "$1" || fatal "Couldn't find '$1'"
}

assert_has_dir () {
test -d "$1" || fatal "Couldn't find '$1'"
}

assert_not_has_file () {
if test -f "$1"; then
sed -e 's/^/# /' < "$1" >&2
fatal "File '$1' exists"
fi
}

assert_not_file_has_content () {
if grep -q -e "$2" "$1"; then
sed -e 's/^/# /' < "$1" >&2
fatal "File '$1' incorrectly matches regexp '$2'"
fi
}

assert_not_has_dir () {
if test -d "$1"; then
fatal "Directory '$1' exists"
fi
}

assert_file_has_content () {
if ! grep -q -e "$2" "$1"; then
sed -e 's/^/# /' < "$1" >&2
fatal "File '$1' doesn't match regexp '$2'"
fi
}

assert_symlink_has_content () {
if ! test -L "$1"; then
echo 1>&2 "File '$1' is not a symbolic link"
exit 1
fi
if ! readlink "$1" | grep -q -e "$2"; then
sed -e 's/^/# /' < "$1" >&2
echo 1>&2 "Symbolic link '$1' doesn't match regexp '$2'"
exit 1
fi
}

assert_file_empty() {
if test -s "$1"; then
sed -e 's/^/# /' < "$1" >&2
fatal "File '$1' is not empty"
fi
}

# Use to skip all of these tests
skip() {
echo "1..0 # SKIP" "$@"
exit 0
}
95 changes: 3 additions & 92 deletions tests/libtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

dn=$(dirname $0)
. ${dn}/libtest-core.sh

if [ -n "${G_TEST_SRCDIR:-}" ]; then
test_srcdir="${G_TEST_SRCDIR}/tests"
else
Expand All @@ -29,25 +32,8 @@ else
test_builddir=$(dirname $0)
fi

fatal() {
echo $@ 1>&2; exit 1
}
# fatal() is shorter to type, but retain this alias
assert_not_reached () {
fatal "$@"
}

test_tmpdir=$(pwd)

# Some tests look for specific English strings. Use a UTF-8 version
# of the C (POSIX) locale if we have one, or fall back to POSIX
# (https://sourceware.org/glibc/wiki/Proposals/C.UTF-8)
if locale -a | grep C.UTF-8 >/dev/null; then
export LC_ALL=C.UTF-8
else
export LC_ALL=C
fi

# Sanity check that we're in a tmpdir that has
# just .testtmp (created by tap-driver for `make check`,
# or nothing at all (as ginstest-runner does)
Expand All @@ -62,8 +48,6 @@ if ! test -f .testtmp; then
touch .testtmp
fi

export G_DEBUG=fatal-warnings

# Also, unbreak `tar` inside `make check`...Automake will inject
# TAR_OPTIONS: --owner=0 --group=0 --numeric-owner presumably so that
# tarballs are predictable, except we don't want this in our tests.
Expand Down Expand Up @@ -124,74 +108,6 @@ else
OSTREE_HTTPD="${CMD_PREFIX} ostree trivial-httpd"
fi

assert_streq () {
test "$1" = "$2" || fatal "$1 != $2"
}

assert_str_match () {
if ! echo "$1" | grep -E -q "$2"; then
fatal "$1 does not match regexp $2"
fi
}

assert_not_streq () {
(! test "$1" = "$2") || fatal "$1 == $2"
}

assert_has_file () {
test -f "$1" || fatal "Couldn't find '$1'"
}

assert_has_dir () {
test -d "$1" || fatal "Couldn't find '$1'"
}

assert_not_has_file () {
if test -f "$1"; then
sed -e 's/^/# /' < "$1" >&2
fatal "File '$1' exists"
fi
}

assert_not_file_has_content () {
if grep -q -e "$2" "$1"; then
sed -e 's/^/# /' < "$1" >&2
fatal "File '$1' incorrectly matches regexp '$2'"
fi
}

assert_not_has_dir () {
if test -d "$1"; then
fatal "Directory '$1' exists"
fi
}

assert_file_has_content () {
if ! grep -q -e "$2" "$1"; then
sed -e 's/^/# /' < "$1" >&2
fatal "File '$1' doesn't match regexp '$2'"
fi
}

assert_symlink_has_content () {
if ! test -L "$1"; then
echo 1>&2 "File '$1' is not a symbolic link"
exit 1
fi
if ! readlink "$1" | grep -q -e "$2"; then
sed -e 's/^/# /' < "$1" >&2
echo 1>&2 "Symbolic link '$1' doesn't match regexp '$2'"
exit 1
fi
}

assert_file_empty() {
if test -s "$1"; then
sed -e 's/^/# /' < "$1" >&2
fatal "File '$1' is not empty"
fi
}

assert_files_hardlinked() {
f1=$(stat -c %i $1)
f2=$(stat -c %i $2)
Expand Down Expand Up @@ -541,11 +457,6 @@ os_repository_new_commit ()
cd ${test_tmpdir}
}

skip() {
echo "1..0 # SKIP" "$@"
exit 0
}

skip_without_user_xattrs () {
touch test-xattrs
setfattr -n user.testvalue -v somevalue test-xattrs || \
Expand Down

0 comments on commit 8b4196d

Please sign in to comment.