Diff
1 commit, 1 file changed
+78 -0

Add Test section to PHP guidelines
Otto Urpelainen • 2 years ago  
@@ -499,6 +499,84 @@

  %global php_version %(php-config --version 2>/dev/null || echo 0)

  ....

  

+ [#tests]

+ == Tests

+ 

+ [#tests-upstream]

+ === Upstream test suite

+ 

+ If a test suite exists upstream,

+ it should be run in the `+%check+` section.

+ 

+ Unfortunately, for PHP packages,

+ it is a common practise to exclude test suites from source tarballs

+ through use of `+.gitattributes+`.

+ In such cases, use of upstream tarballs should be avoided.

+ Instead, a checkout of the repository can be archived with `+tar+`.

+ As an exampe, the following script can be used,

+ assuming a GitHub repository and that the specfile sets the needed `+%global+`s:

+ 

+ ....

+ #!/bin/bash

+ 

+ NAME=$(basename $PWD)

+ OWNER=$(sed   -n '/^%global gh_vendor/{s/.* //;p}'  $NAME.spec)

+ PROJECT=$(sed -n '/^%global gh_project/{s/.* //;p}' $NAME.spec)

+ VERSION=$(sed -n '/^Version:/{s/.* //;p}'           $NAME.spec)

+ COMMIT=$(sed  -n '/^%global commit/{s/.* //;p}'     $NAME.spec)

+ SHORT=${COMMIT:0:7}

+ 

+ echo -e "\nCreate git snapshot\nName=$NAME, Owner=$OWNER, Project=$PROJECT, Version=$VERSION\n"

+ 

+ echo "Cloning..."

+ rm -rf $PROJECT-$COMMIT

+ git clone https://github.com/$OWNER/$PROJECT.git $PROJECT-$COMMIT

+ 

+ echo "Getting commit..."

+ pushd $PROJECT-$COMMIT

+    git checkout $COMMIT || exit 1

+    cp composer.json ../composer.json

+ popd

+ 

+ echo "Archiving..."

+ tar czf $NAME-$VERSION-$SHORT.tgz --exclude-vcs $PROJECT-$COMMIT

+ 

+ echo "Cleaning..."

+ rm -rf $PROJECT-$COMMIT

+ 

+ echo "Done."

+ ....

+ 

+ In order to standardize the archive creation method,

+ the script itself should be stored as an additional Source in the specfile

+ with usage instructions in a comment:

+ 

+ ....

+ # Since upstream tarballs lack the tests, a Git checkout is archived instead.

+ # Run ./makesrc.sh in the specfile directory to generate the archive.

+ Source0:        %{name}-%{version}-%{scommit}.tgz

+ Source1:        makesrc.sh

+ ....

+ 

+ [#tests-autoloader]

+ === Autoloader

+ 

+ If an autoloader is generated for the package,

+ also a test for it should be generated and run in `+%check+`.

+ For example:

+ 

+ ....

+ %check

+ : Check the autoloader

+ %{_bindir}/php -r "

+     require_once '%{buildroot}%{_datadir}/php/%{ns_dir}/autoload.php';

+     exit(

+         class_exists('%{ns_project}\SomeNamespace\SomeClass')

+         ? 0 : 1

+     );

+ "

+ ....

+ 

  [#hints]

  == Additional Hints for Packagers