[pacman-dev] [PATCH] [RFC] makepkg: add a test suite
Allan McRae
allan at archlinux.org
Wed Jun 9 03:59:06 EDT 2010
Signed-off-by: Allan McRae <allan at archlinux.org>
---
I am getting annoyed at constantly missing bugs in makepkg patches! So,
this is a really, really, really crappy attempt at getting a test suite up
and going for makepkg. Essentially, stick a PKGBUILD in test/makepkg/tests
and run test/makepkg/mptest.
The test suite is controlled by an 'optional' mptest() function in the PKGBUILD.
In that function you can define the following variables:
files (array):
A list of file necessary for the test to run. First entry is always the
PKGBUILD. The syntax 'test001::PKGBUILD' can be used, tell the test suite
to copy file 'test001' to 'PKGBUILD'.
flags (string):
Flags to pass to makepkg
tests (array):
Lists of tests to run
files and tests have defaults (file to just copying the test file to 'PKGBUILD'
and tests to running 'pkg_exists')
The only tests available so far are:
pkg_exists - checks all packages exist and have a .PKGINFO file
pkg_has_file - checks a package has a file (not tested...)
Needed tests:
pkginfo - checks pkginfo file is correct
Of course this needs a lot more structure and tests, but comments on the
direction this is heading would be appreciated. All work on this is on
my mptest branch.
test/makepkg/mptest | 140 ++++++++++++++++++++++++++++++++++++++++++++
test/makepkg/tests/test001 | 7 ++
2 files changed, 147 insertions(+), 0 deletions(-)
create mode 100755 test/makepkg/mptest
create mode 100644 test/makepkg/tests/test001
diff --git a/test/makepkg/mptest b/test/makepkg/mptest
new file mode 100755
index 0000000..01e4b4a
--- /dev/null
+++ b/test/makepkg/mptest
@@ -0,0 +1,140 @@
+#!/bin/bash
+
+splitpkg_overrides=('pkgver' 'pkgrel' 'pkgdesc' 'arch' 'license' 'groups' \
+ 'depends' 'optdepends' 'provides' 'conflicts' 'replaces' \
+ 'backup' 'options' 'install' 'changelog')
+readonly -a splitpkg_overrides
+
+backup_package_variables() {
+ for var in ${splitpkg_overrides[@]}; do
+ indirect="${var}_backup"
+ eval "${indirect}=(\"\${$var[@]}\")"
+ done
+}
+
+restore_package_variables() {
+ for var in ${splitpkg_overrides[@]}; do
+ indirect="${var}_backup"
+ if [[ -n ${!indirect} ]]; then
+ eval "${var}=(\"\${$indirect[@]}\")"
+ else
+ unset ${var}
+ fi
+ done
+}
+
+##
+# Performs necessary PKGBUILD variable overrides for a given split-package
+#
+# Usage: pasre_splitpkg_overrides <pkgname>
+##
+parse_splitpkg_overrides() {
+ if grep -q "package_$1()" ${files[0]#*::}; then
+ backup_package_variables
+ pkgfunc=$(sed -n "/^package_$1\(\)/,/^}/p" ${files[0]#*::})
+ for var in ${splitpkg_overrides[@]}; do
+ eval $(echo $pkgfunc | grep -Eo "$var=(\"[^)]*\"|\([^)]*\)|[^ ]*)")
+ done
+ fi
+}
+
+##
+# Prepares for testing (including the running of makepkg)
+##
+prepare_test() {
+ # set default values
+ files=("$test::PKGBUILD")
+ flags=""
+ tests=('pkg_exists')
+
+ # override defaults
+ if [[ $(type -t mptest) = "function" ]]; then
+ mptest
+ fi
+
+ for file in ${files[@]}; do
+ cp ${file%%::*} ${testdir}/${file#*::}
+ done
+
+ cd $testdir
+ ../../../../scripts/makepkg --config ../../../../etc/makepkg.conf $flags &> /dev/null
+ cd ..
+}
+
+##
+# Runs the requested tests
+##
+run_test() {
+ FAIL=0
+
+ cd $testdir
+ for i in ${tests[@]}; do
+ eval $i || FAIL=1
+ done
+ cd ..
+
+ return $FAIL
+}
+
+##
+# Checks if all package are created including PKGINFO presence
+##
+pkg_exists() {
+ for pkg in ${pkgname[@]}; do
+ parse_splitpkg_overrides $pkg
+ if [[ $arch != "any" ]]; then
+ arch=$CARCH
+ fi
+ if [[ ! -e $pkg-$pkgver-$pkgrel-$arch$PKGEXT ]] ;then
+ return 1
+ fi
+ if ! bsdtar -tf $pkg-$pkgver-$pkgrel-$arch$PKGEXT -q .PKGINFO &> /dev/null; then
+ return 1
+ fi
+ restore_package_variables
+ done
+
+ return 0
+}
+
+##
+# Checks for a file in a package
+#
+# Usage: pkg_has_file <pkgname> <file>
+##
+pkg_has_file() {
+ parse_splitpkg_overrides $1
+
+ if ! bsdtar -tf $pkg-$pkgver-$pkgrel-$arch$PKGEXT $2 &> /dev/null; then
+ return 1
+ fi
+ restore_package_variables
+
+ return 0
+}
+
+
+##
+# RUN THE TESTS!
+##
+cd tests
+
+for test in test*; do
+ source ../../../etc/makepkg.conf
+ source $test
+
+ testdir=$(mktemp -d $test.XXXXXXXX)
+ if ! prepare_test; then
+ echo "FAIL: $test"
+ else
+ if ! run_test; then
+ echo "FAIL: $test"
+ else
+ echo "PASS: $test"
+ fi
+ fi
+
+ # cleanup
+ unset mptest files flags tests
+ rm -rf $testdir
+done
\ No newline at end of file
diff --git a/test/makepkg/tests/test001 b/test/makepkg/tests/test001
new file mode 100644
index 0000000..dfe6ec6
--- /dev/null
+++ b/test/makepkg/tests/test001
@@ -0,0 +1,7 @@
+# test001: Minimal working PKGBUILD
+
+pkgname=NAME
+pkgver=1
+pkgrel=1
+arch=('any')
+
--
1.7.1
More information about the pacman-dev
mailing list