AT_TESTED([rpm rpmbuild rpmquery])

m4_define([RPMTEST_SETUP],[[
if ! [ -d testing/ ]; then
    cp -aP "${RPMTEST}" .
    chmod -R u+w testing/
    mkdir -p testing/build
    ln -s ../data/SOURCES testing/build/
fi
export RPMTEST="${PWD}/testing"
export TOPDIR="${RPMTEST}/build"
export HOME="${RPMTEST}"
]])

m4_define([RPMDB_INIT],[
RPMTEST_SETUP
rm -rf "${RPMTEST}"`rpm --eval '%_dbpath'`/*
runroot rpm --initdb
])

m4_define([RPMPY_RUN],[[
cat << EOF > test.py
# coding=utf-8
import rpm, sys, os
dbpath=rpm.expandMacro('%_dbpath')
rpm.addMacro('_dbpath', '${RPMTEST}%s' % dbpath)
rpm.addMacro('_db_backend', os.getenv('DBFORMAT'))
def myprint(msg = ''):
    sys.stdout.write('%s\n' % msg)
$1
EOF
${PYTHON} test.py
]])

m4_define([RPMPY_CHECK],[
AT_SKIP_IF([$PYTHON_DISABLED])
RPMTEST_SETUP
AT_CHECK([RPMPY_RUN([$1])], [], [$2], [$3])
])

m4_define([RPMPY_TEST],[
AT_SETUP([$1])
AT_KEYWORDS([python])
RPMPY_CHECK([$2], [$3], [$4])
AT_CLEANUP
])

# Enable colored test output if available
m4_ifdef([AT_COLOR_TESTS], [AT_COLOR_TESTS])

AT_INIT
