#    rpmbuild.at: test rpmbuild
#
#    Copyright (C) 2007  Ralf Corsépius <corsepiu@fedoraproject.org>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program 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 General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

AT_BANNER([RPM build])

# ------------------------------
# Check if rpmbuild -ba *.spec works
AT_SETUP([rpmbuild -ba *.spec])
AT_KEYWORDS([build])
AT_CHECK([
rm -rf ${TOPDIR}
AS_MKDIR_P(${TOPDIR}/SOURCES)

cp "${abs_srcdir}"/data/SOURCES/hello-1.0.tar.gz "${abs_srcdir}"/data/SOURCES/hello-1.0-modernize.patch ${TOPDIR}/SOURCES

run rpmbuild \
  -ba "${abs_srcdir}"/data/SPECS/hello.spec
],
[0],
[ignore],
[ignore])
AT_CLEANUP

# ------------------------------
# Check if rpmbuild --rebuild *.src.rpm works
AT_SETUP([rpmbuild --rebuild])
AT_KEYWORDS([build])
AT_CHECK([
rm -rf ${TOPDIR}

run rpmbuild \
  --rebuild "${abs_srcdir}"/data/SRPMS/hello-1.0-1.src.rpm
],
[0],
[ignore],
[ignore])
AT_CLEANUP

# ------------------------------
# Check if tar unpacking works
AT_SETUP([rpmbuild -tb <tar with bad spec>])
AT_KEYWORDS([build])
AT_CHECK([
rm -rf ${TOPDIR}

run rpmbuild \
  -tb "${RPMDATA}/SOURCES/hello-1.0.tar.gz"
],
[1],
[ignore],
[error: line 5: Unknown tag: Serial:	1
])
AT_CLEANUP

# ------------------------------
# Check if tar build works
# TODO: test that the rpms are actually created...
AT_SETUP([rpmbuild -tb])
AT_KEYWORDS([build])
AT_CHECK([
rm -rf ${TOPDIR}

run rpmbuild \
  -ta "${RPMDATA}/SOURCES/hello-2.0.tar.gz"
],
[0],
[ignore],
[ignore])
AT_CLEANUP

# ------------------------------
# %attr/%defattr tests
AT_SETUP([rpmbuild %attr and %defattr])
AT_KEYWORDS([build])
AT_CHECK([[
rm -rf ${TOPDIR}

runroot rpmbuild \
  -bb --quiet /data/SPECS/attrtest.spec

runroot rpm -qp --qf \
  "\n[%{filemodes:perms} %-8{fileusername} %-8{filegroupname} %{filenames}\n]"\
  "${TOPDIR}"/RPMS/noarch/attrtest-1.0-1.noarch.rpm
]],
[0],
[
drwx------ root     root     /a/dir
-r-------- root     root     /a/file
drwx------ daemon   adm      /b/dir
-r-------- daemon   adm      /b/file
drwxr-x--- root     adm      /c/dir
-rw-r----- daemon   root     /c/file
drwxr-x--x daemon   bin      /d/dir
-rw-r--r-- bin      daemon   /d/file
drwx------ foo      bar      /e/dir
-r-------- foo      bar      /e/file
drwxrwx--- bar      foo      /f/dir
-rw-rw---- bar      foo      /f/file
drwx------ adm      foo      /g/dir
-r-------- bar      adm      /g/file
drwxr-xr-x foo      bar      /h/dir
-rw-r--r-- foo      bar      /h/file
drwxr-x--- adm      root     /i/dir
-rwsr-xr-x root     adm      /i/file
drwxrwxrwx zoot     zoot     /j/dir
--w--w--w- zoot     zoot     /j/file
],
[])
AT_CLEANUP

# ------------------------------
# hardlink tests
AT_SETUP([rpmbuild hardlink])
AT_KEYWORDS([build])
AT_CHECK([
RPMDB_CLEAR
RPMDB_INIT
rm -rf ${TOPDIR}

runroot rpmbuild \
  -bb --quiet /data/SPECS/hlinktest.spec

runroot rpm -i "${TOPDIR}"/RPMS/noarch/hlinktest-1.0-1.noarch.rpm

runroot rpm -q --qf "[[%{filenlinks} %{filenames}\n]]%{longsize}\n" hlinktest
runroot rpm -V --nouser --nogroup hlinktest
ls -i "${RPMTEST}"/foo/hello* | awk {'print $1'} | sort -u | wc -l

],
[0],
[2 /foo/aaaa
1 /foo/copyllo
4 /foo/hello
4 /foo/hello-bar
4 /foo/hello-foo
4 /foo/hello-world
2 /foo/zzzz
87
1
],
[])
AT_CLEANUP

AT_SETUP([rpmbuild glob])
AT_KEYWORDS([build])
AT_CHECK([
RPMDB_CLEAR
RPMDB_INIT
rm -rf ${TOPDIR}

runroot rpmbuild -bb --quiet /data/SPECS/globtest.spec
runroot rpm -qp \
	--qf "[[%{filemodes:perms} %{filenames}\n]]" \
	"${TOPDIR}"/RPMS/noarch/globtest-1.0-1.noarch.rpm
],
[0],
[-rw-r--r-- /opt/globtest/baf
drwxr-xr-x /opt/globtest/bang
-rw-r--r-- /opt/globtest/bif
drwxr-xr-x /opt/globtest/bing
drwxr-xr-x /opt/globtest/bong
drwxr-xr-x /opt/globtest/foo
-rw-r--r-- /opt/globtest/foo/one
-rw-r--r-- /opt/globtest/foo/three
-rw-r--r-- /opt/globtest/foo/two
lrwxrwxrwx /opt/globtest/linkbad
lrwxrwxrwx /opt/globtest/linkgood
-rw-r--r-- /opt/globtest/weird%name
-rw-r--r-- /opt/globtest/zab
-rw-r--r-- /opt/globtest/zeb
-rw-r--r-- /opt/globtest/zib
],
[])
AT_CLEANUP

# ------------------------------
# Check if weak and reverse requires can be built
AT_SETUP([Weak and reverse requires])
AT_KEYWORDS([build])
AT_CHECK([

runroot rpmbuild -bb --quiet \
	--define "pkg weakdeps" \
	--define "recs foo > 1.2.3" \
	--define "sugs bar >= 0.1.2" \
	--define "sups baz" \
	--define "ens zap = 3" \
	  /data/SPECS/deptest.spec

runroot rpm -qp --recommends /build/RPMS/noarch/deptest-weakdeps-1.0-1.noarch.rpm
runroot rpm -qp --suggests /build/RPMS/noarch/deptest-weakdeps-1.0-1.noarch.rpm
runroot rpm -qp --supplements /build/RPMS/noarch/deptest-weakdeps-1.0-1.noarch.rpm
runroot rpm -qp --enhances /build/RPMS/noarch/deptest-weakdeps-1.0-1.noarch.rpm
],
[0],
[foo > 1.2.3
bar >= 0.1.2
baz
zap = 3
],
[ignore])
AT_CLEANUP

# ------------------------------
# Test BuildRequire functionality
AT_SETUP([Build requires])
AT_KEYWORDS([build])
AT_CHECK([

runroot rpmbuild -bb --quiet \
		--define "pkg buildreq" \
		--define "buildreqs foo > 1.2.3" \
		/data/SPECS/deptest.spec
],
[1],
[],
[error: Failed build dependencies:
	foo > 1.2.3 is needed by deptest-buildreq-1.0-1.noarch
])
AT_CLEANUP

# ------------------------------
# Test spec query functionality
AT_SETUP([rpmspec query 1])
AT_KEYWORDS([build])
AT_CHECK([

runroot rpmspec -q \
		--define "pkg dep" \
		--define "reqs foo > 1.2.3 bar <= 2.3" \
		--requires \
		/data/SPECS/deptest.spec
],
[0],
[bar <= 2.3
foo > 1.2.3
],
[])

AT_CLEANUP

# ------------------------------
# archive sanity check
AT_SETUP([rpmbuild archive sanity])
AT_KEYWORDS([build])
AT_CHECK([
rm -rf ${TOPDIR}

runroot rpmbuild \
  -bb --quiet /data/SPECS/attrtest.spec

runroot rpm2cpio \
  "${TOPDIR}"/RPMS/noarch/attrtest-1.0-1.noarch.rpm > /tmp/attrtest.cpio
],
[0],
[],
[])
AT_CLEANUP
