[pacman-dev] [PATCH] makepkg: Add check() function for running test suites

Allan McRae allan at archlinux.org
Wed Dec 15 09:12:56 EST 2010


A PKGBUILD can have an option check() function for running test suites
between the build() and package() stages.  This function is run by
default but can be disabled globally in with "!check" in BUILDENV in
makepkg.conf and on an individual package basis using the --nocheck flag.
Addition dependencies needed for running the test suite can be specified
in the checkdepends array and are only checked when running the check()
function.

Original-work-by: Jeff C <jeff at kcaccess.com>
Signed-off-by: Allan McRae <allan at archlinux.org>
---
 PKGBUILD-split.proto   |    6 ++++++
 PKGBUILD.proto         |    8 ++++++--
 doc/PKGBUILD.5.txt     |   16 +++++++++++++++-
 doc/makepkg.8.txt      |    3 +++
 doc/makepkg.conf.5.txt |    5 +++++
 etc/makepkg.conf.in    |    5 +++--
 scripts/makepkg.sh.in  |   27 +++++++++++++++++++++++++--
 7 files changed, 63 insertions(+), 7 deletions(-)

diff --git a/PKGBUILD-split.proto b/PKGBUILD-split.proto
index f5114ab..52aacc5 100644
--- a/PKGBUILD-split.proto
+++ b/PKGBUILD-split.proto
@@ -16,6 +16,7 @@ license=('GPL')
 groups=()
 depends=()
 makedepends=()
+checkdepends=()
 provides=()
 conflicts=()
 replaces=()
@@ -33,6 +34,11 @@ build() {
   make
 }
 
+check() {
+  cd "$srcdir/$pkgname-$pkgver"
+  make -k check
+}
+
 package_pkg1() {
   # options and directives that can be overridden
   pkgver=
diff --git a/PKGBUILD.proto b/PKGBUILD.proto
index 71dbc14..98172d8 100644
--- a/PKGBUILD.proto
+++ b/PKGBUILD.proto
@@ -15,6 +15,7 @@ license=('GPL')
 groups=()
 depends=()
 makedepends=()
+checkdepends=()
 optdepends=()
 provides=()
 conflicts=()
@@ -29,14 +30,17 @@ md5sums=() #generate with 'makepkg -g'
 
 build() {
   cd "$srcdir/$pkgname-$pkgver"
-
   ./configure --prefix=/usr
   make
 }
 
-package() {
+check() {
   cd "$srcdir/$pkgname-$pkgver"
+  make -k check
+}
 
+package() {
+  cd "$srcdir/$pkgname-$pkgver"
   make DESTDIR="$pkgdir/" install
 }
 
diff --git a/doc/PKGBUILD.5.txt b/doc/PKGBUILD.5.txt
index 3d3909a..2ccf0b2 100644
--- a/doc/PKGBUILD.5.txt
+++ b/doc/PKGBUILD.5.txt
@@ -156,6 +156,12 @@ name. The syntax is: `source=('filename::url')`.
 	needed at runtime. Packages in this list follow the same format as
 	depends.
 
+*checkdepends (array)*::
+	An array of packages that this package depends on to run its test suite,
+	but are not needed at runtime. Packages in this list follow the same
+	format as depends. These dependencies are only considered when the
+	check() function is present and is to be run by makepkg.
+
 *optdepends (array)*::
 	An array of packages (and accompanying reasons) that are not essential for
 	base functionality, but may be necessary to make full use of the contents
@@ -263,10 +269,18 @@ If you create any variables of your own in the build function, it is
 recommended to use the bash `local` keyword to scope the variable to inside
 the build function.
 
+check() Function
+----------------
+An optional check() function can be specified in which a packages test-suite
+may be run. This function is run between the build() and package() functions.
+The function is run in `bash -e` mode, meaning any command that exits with a
+non-zero status will cause the function to exit. Be sure any exotic commands
+used are covered by `checkdepends`.
+
 package() Function
 ------------------
 An optional package() function can be specified in addition to the build()
-function. This function is run immediately after the build() function. The
+function. This function is run after the build() and check() functions. The
 function is run in `bash -e` mode, meaning any command that exits with a
 non-zero status will cause the function to exit. When specified in combination
 with the fakeroot BUILDENV option in linkman:makepkg.conf[5], fakeroot usage
diff --git a/doc/makepkg.8.txt b/doc/makepkg.8.txt
index ff0f2b6..0087ddd 100644
--- a/doc/makepkg.8.txt
+++ b/doc/makepkg.8.txt
@@ -153,6 +153,9 @@ Options
 	Only build listed packages from a split package. The use of quotes is
 	necessary when specifying multiple packages. e.g. `--pkg "pkg1 pkg3"`
 
+*\--nocheck*::
+	Do not run the check() function in the PKGBUILD or handle the checkdepends.
+
 *\--noconfirm*::
 	(Passed to pacman) Prevent pacman from waiting for user input before
 	proceeding with operations.
diff --git a/doc/makepkg.conf.5.txt b/doc/makepkg.conf.5.txt
index 86ea6a3..2293a8d 100644
--- a/doc/makepkg.conf.5.txt
+++ b/doc/makepkg.conf.5.txt
@@ -93,6 +93,11 @@ Options
 		be disabled for individual packages by placing `!ccache` in the
 		PKGBUILD options array.
 
+	*check*;;
+		Run the check() function if present in the PKGBUILD. This can be
+		disabled for individual packages through the use of makepkg's
+		`--nocheck` option.
+
 **DISTCC_HOSTS=**"host1 ..."::
 	If using DistCC, this is used to specify a space-delimited list of hosts
 	running in the DistCC cluster. In addition, you will want to modify your
diff --git a/etc/makepkg.conf.in b/etc/makepkg.conf.in
index c795432..81a11b1 100644
--- a/etc/makepkg.conf.in
+++ b/etc/makepkg.conf.in
@@ -39,15 +39,16 @@ CXXFLAGS="@CARCHFLAGS at -mtune=generic -O2 -pipe"
 # BUILD ENVIRONMENT
 #########################################################################
 #
-# Defaults: BUILDENV=(fakeroot !distcc color !ccache)
+# Defaults: BUILDENV=(fakeroot !distcc color !ccache check)
 #  A negated environment option will do the opposite of the comments below.
 #
 #-- fakeroot: Allow building packages as a non-root user
 #-- distcc:   Use the Distributed C/C++/ObjC compiler
 #-- color:    Colorize output messages
 #-- ccache:   Use ccache to cache compilation
+#-- check:    Run the check() function if present in the PKGBUILD
 #
-BUILDENV=(fakeroot !distcc color !ccache)
+BUILDENV=(fakeroot !distcc color !ccache check)
 #
 #-- If using DistCC, your MAKEFLAGS will also need modification. In addition,
 #-- specify a space-delimited list of hosts running in the DistCC cluster.
diff --git a/scripts/makepkg.sh.in b/scripts/makepkg.sh.in
index ac10ad3..6707336 100644
--- a/scripts/makepkg.sh.in
+++ b/scripts/makepkg.sh.in
@@ -71,6 +71,7 @@ SOURCEONLY=0
 IGNOREARCH=0
 HOLDVER=0
 BUILDFUNC=0
+CHECKFUNC=0
 PKGFUNC=0
 SPLITPKG=0
 PKGLIST=()
@@ -145,6 +146,9 @@ clean_up() {
 			if (( BUILDFUNC )); then
 				rm -f "${pkgbase}-${pkgver}-${pkgrel}-${CARCH}-build.log"*
 			fi
+			if (( CHECKFUNC )); then
+				rm -f "${pkgbase}-${pkgver}-${pkgrel}-${CARCH}-check.log"*
+			fi
 			if (( PKGFUNC )); then
 				rm -f "${pkgbase}-${pkgver}-${pkgrel}-${CARCH}-package.log"*
 			elif (( SPLITPKG )); then
@@ -805,6 +809,10 @@ run_build() {
 	run_function "build"
 }
 
+run_check() {
+	run_function "check"
+}
+
 run_package() {
 	local pkgfunc
 	if [[ -z $1 ]]; then
@@ -1547,6 +1555,7 @@ usage() {
 	echo "$(gettext "  --asroot         Allow makepkg to run as root user")"
 	printf "$(gettext "  --config <file>  Use an alternate config file (instead of '%s')")\n" "$confdir/makepkg.conf"
 	echo "$(gettext "  --holdver        Prevent automatic version bumping for development PKGBUILDs")"
+	echo "$(gettext "  --nocheck        Do not run the check() function in the '%s'")" "$BUILDSCRIPT"
 	echo "$(gettext "  --pkg <list>     Only build listed packages from a split package")"
 	echo "$(gettext "  --skipinteg      Do not fail when integrity checks are missing")"
 	echo "$(gettext "  --source         Generate a source-only tarball without downloaded sources")"
@@ -1584,8 +1593,8 @@ ARGLIST=("$@")
 OPT_SHORT="AcCdefFghiLmop:rRsV"
 OPT_LONG="allsource,asroot,ignorearch,clean,cleancache,nodeps"
 OPT_LONG+=",noextract,force,forcever:,geninteg,help,holdver"
-OPT_LONG+=",install,log,nocolor,nobuild,pkg:,rmdeps,repackage,skipinteg"
-OPT_LONG+=",source,syncdeps,version,config:"
+OPT_LONG+=",install,log,nocolor,nobuild,nocheck,pkg:,rmdeps"
+OPT_LONG+=",repackage,skipinteg,source,syncdeps,version,config:"
 # Pacman Options
 OPT_LONG+=",noconfirm,noprogressbar"
 OPT_TEMP="$(parse_options $OPT_SHORT $OPT_LONG "$@" || echo 'PARSE_OPTIONS FAILED')"
@@ -1620,6 +1629,7 @@ while true; do
 		-i|--install)     INSTALL=1 ;;
 		-L|--log)         LOGGING=1 ;;
 		-m|--nocolor)     USE_COLOR='n' ;;
+		--nocheck)        RUN_CHECK='n' ;;
 		-o|--nobuild)     NOBUILD=1 ;;
 		-p)               shift; BUILDFILE=$1 ;;
 		--pkg)            shift; PKGLIST=($1) ;;
@@ -1835,6 +1845,12 @@ fi
 if declare -f build >/dev/null; then
 	BUILDFUNC=1
 fi
+if declare -f check >/dev/null; then
+	# "Hide" check() function if not going to be run
+	if [[ ! $(check_buildenv check) = "n" &&  ! $RUN_CHECK = "n" ]]; then
+		CHECKFUNC=1
+	fi
+fi
 if declare -f package >/dev/null; then
 	PKGFUNC=1
 elif [[ $SPLITPKG -eq 0 ]] && declare -f package_${pkgname} >/dev/null; then
@@ -1898,6 +1914,7 @@ if (( INFAKEROOT )); then
 			if (( ! REPKG )); then
 				if (( BUILDFUNC )); then
 					run_build
+					(( CHECKFUNC )) && run_check
 					tidy_install
 				fi
 			else
@@ -1948,6 +1965,10 @@ elif type -p "${PACMAN%% *}" >/dev/null; then
 	msg "$(gettext "Checking buildtime dependencies...")"
 	resolve_deps ${makedepends[@]} || deperr=1
 
+	if (( CHECKFUNC )); then
+		resolve_deps ${checkdepends[@]} || deperr=1
+	fi
+
 	if (( RMDEPS )); then
 		current_pkglist=($(run_pacman -Qq))    # required by remove_deps
 	fi
@@ -2013,6 +2034,7 @@ else
 		if (( ! REPKG )); then
 			devel_update
 			(( BUILDFUNC )) && run_build
+			(( CHECKFUNC )) && run_check
 		fi
 		if (( ! SPLITPKG )); then
 			if (( PKGFUNC )); then
@@ -2034,6 +2056,7 @@ else
 		if (( ! REPKG && ( PKGFUNC || SPLITPKG ) )); then
 			devel_update
 			(( BUILDFUNC )) && run_build
+			(( CHECKFUNC )) && run_check
 			cd "$startdir"
 		fi
 
-- 
1.7.3.3



More information about the pacman-dev mailing list