From a6b9548c43325269d861f170cdb2c31b07046b56 Mon Sep 17 00:00:00 2001
From: Stanley Cheung <stanleycheung@google.com>
Date: Wed, 13 Jan 2016 16:10:48 -0800
Subject: [PATCH] Add lcov php code coverage report

---
 src/php/ext/grpc/config.m4             | 66 ++++++++++++++++++++++++++
 src/php/ext/grpc/package.xml           | 11 +++--
 src/php/ext/grpc/tests/grpc-basic.phpt | 10 ++++
 tools/run_tests/build_php.sh           |  6 ++-
 tools/run_tests/post_tests_php.sh      | 46 ++++++++++++++++++
 tools/run_tests/run_tests.py           |  3 +-
 6 files changed, 134 insertions(+), 8 deletions(-)
 create mode 100644 src/php/ext/grpc/tests/grpc-basic.phpt
 create mode 100755 tools/run_tests/post_tests_php.sh

diff --git a/src/php/ext/grpc/config.m4 b/src/php/ext/grpc/config.m4
index 7928687943..0fb843d51f 100755
--- a/src/php/ext/grpc/config.m4
+++ b/src/php/ext/grpc/config.m4
@@ -1,6 +1,9 @@
 PHP_ARG_ENABLE(grpc, whether to enable grpc support,
 [  --enable-grpc           Enable grpc support])
 
+PHP_ARG_ENABLE(coverage, whether to include code coverage symbols,
+[  --enable-coverage       Enable coverage support], no, no)
+
 if test "$PHP_GRPC" != "no"; then
   dnl Write more examples of tests here...
 
@@ -75,3 +78,66 @@ if test "$PHP_GRPC" != "no"; then
     channel_credentials.c completion_queue.c timeval.c server.c \
     server_credentials.c php_grpc.c, $ext_shared, , -Wall -Werror -std=c11)
 fi
+
+if test "$PHP_COVERAGE" = "yes"; then
+
+  if test "$GCC" != "yes"; then
+    AC_MSG_ERROR([GCC is required for --enable-coverage])
+  fi
+  
+  dnl Check if ccache is being used
+  case `$php_shtool path $CC` in
+    *ccache*[)] gcc_ccache=yes;;
+    *[)] gcc_ccache=no;;
+  esac
+
+  if test "$gcc_ccache" = "yes" && (test -z "$CCACHE_DISABLE" || test "$CCACHE_DISABLE" != "1"); then
+    AC_MSG_ERROR([ccache must be disabled when --enable-coverage option is used. You can disable ccache by setting environment variable CCACHE_DISABLE=1.])
+  fi
+  
+  lcov_version_list="1.5 1.6 1.7 1.9 1.10 1.11"
+
+  AC_CHECK_PROG(LCOV, lcov, lcov)
+  AC_CHECK_PROG(GENHTML, genhtml, genhtml)
+  PHP_SUBST(LCOV)
+  PHP_SUBST(GENHTML)
+
+  if test "$LCOV"; then
+    AC_CACHE_CHECK([for lcov version], php_cv_lcov_version, [
+      php_cv_lcov_version=invalid
+      lcov_version=`$LCOV -v 2>/dev/null | $SED -e 's/^.* //'` #'
+      for lcov_check_version in $lcov_version_list; do
+        if test "$lcov_version" = "$lcov_check_version"; then
+          php_cv_lcov_version="$lcov_check_version (ok)"
+        fi
+      done
+    ])
+  else
+    lcov_msg="To enable code coverage reporting you must have one of the following LCOV versions installed: $lcov_version_list"      
+    AC_MSG_ERROR([$lcov_msg])
+  fi
+
+  case $php_cv_lcov_version in
+    ""|invalid[)]
+      lcov_msg="You must have one of the following versions of LCOV: $lcov_version_list (found: $lcov_version)."
+      AC_MSG_ERROR([$lcov_msg])
+      LCOV="exit 0;"
+      ;;
+  esac
+
+  if test -z "$GENHTML"; then
+    AC_MSG_ERROR([Could not find genhtml from the LCOV package])
+  fi
+
+  PHP_ADD_MAKEFILE_FRAGMENT
+
+  dnl Remove all optimization flags from CFLAGS
+  changequote({,})
+  CFLAGS=`echo "$CFLAGS" | $SED -e 's/-O[0-9s]*//g'`
+  CXXFLAGS=`echo "$CXXFLAGS" | $SED -e 's/-O[0-9s]*//g'`
+  changequote([,])
+
+  dnl Add the special gcc flags
+  CFLAGS="$CFLAGS -O0 -ggdb -fprofile-arcs -ftest-coverage"
+  CXXFLAGS="$CXXFLAGS -ggdb -O0 -fprofile-arcs -ftest-coverage"
+fi
diff --git a/src/php/ext/grpc/package.xml b/src/php/ext/grpc/package.xml
index 9c98f82540..daf2ee5a53 100644
--- a/src/php/ext/grpc/package.xml
+++ b/src/php/ext/grpc/package.xml
@@ -10,8 +10,8 @@
   <email>grpc-packages@google.com</email>
   <active>yes</active>
  </lead>
- <date>2015-12-16</date>
- <time>12:56:11</time>
+ <date>2016-01-13</date>
+ <time>16:06:07</time>
  <version>
   <release>0.7.0</release>
   <api>0.7.0</api>
@@ -29,6 +29,7 @@
  </notes>
  <contents>
   <dir baseinstalldir="/" name="/">
+   <file baseinstalldir="/" md5sum="f201d644fdbd8228ffd1d4a69cc44f1f" name="tests/grpc-basic.phpt" role="test" />
    <file baseinstalldir="/" md5sum="6f19828fb869b7b8a590cbb76b4f996d" name="byte_buffer.c" role="src" />
    <file baseinstalldir="/" md5sum="c8de0f819499c48adfc8d7f472c0196b" name="byte_buffer.h" role="src" />
    <file baseinstalldir="/" md5sum="ee7eb7757f9e6f0e36f8f616b6bd0af5" name="call.c" role="src" />
@@ -41,9 +42,9 @@
    <file baseinstalldir="/" md5sum="a86250e03f610ce6c2c7595a84e08821" name="channel_credentials.h" role="src" />
    <file baseinstalldir="/" md5sum="55ab7a42f9dd9bfc7e28a61cfc5fca63" name="completion_queue.c" role="src" />
    <file baseinstalldir="/" md5sum="f10b5bb232d74a6878e829e2e76cdaa2" name="completion_queue.h" role="src" />
-   <file baseinstalldir="/" md5sum="c7bba7f0f00d1b1483de457d55311382" name="config.m4" role="src" />
+   <file baseinstalldir="/" md5sum="cafed254127007ff2271dad7d56a06c8" name="config.m4" role="src" />
    <file baseinstalldir="/" md5sum="38a1bc979d810c36ebc2a52d4b7b5319" name="CREDITS" role="doc" />
-   <file baseinstalldir="/" md5sum="3f35b472bbdef5a788cd90617d7d0847" name="LICENSE" role="doc" />
+   <file baseinstalldir="/" md5sum="8847cf67b1b54c981d47ecbb0d139a0c" name="LICENSE" role="doc" />
    <file baseinstalldir="/" md5sum="3131a8af38fe5918e5409016b89d6cdb" name="php_grpc.c" role="src" />
    <file baseinstalldir="/" md5sum="673b07859d9f69232f8a754c56780686" name="php_grpc.h" role="src" />
    <file baseinstalldir="/" md5sum="7533a6d3ea02c78cad23a9651de0825d" name="README.md" role="doc" />
@@ -142,7 +143,7 @@ Update to wrap gRPC C Core version 0.10.0
     <release>beta</release>
     <api>beta</api>
    </stability>
-   <date>2015-12-16</date>
+   <date>2016-01-13</date>
    <license>BSD</license>
    <notes>
 - Breaking change to Credentials class (removed) #3765
diff --git a/src/php/ext/grpc/tests/grpc-basic.phpt b/src/php/ext/grpc/tests/grpc-basic.phpt
new file mode 100644
index 0000000000..523cc4a59f
--- /dev/null
+++ b/src/php/ext/grpc/tests/grpc-basic.phpt
@@ -0,0 +1,10 @@
+--TEST--
+Check for grpc presence
+--SKIPIF--
+<?php if (!extension_loaded("grpc")) print "skip"; ?>
+--FILE--
+<?php 
+echo "grpc extension is available";
+?>
+--EXPECT--
+grpc extension is available
\ No newline at end of file
diff --git a/tools/run_tests/build_php.sh b/tools/run_tests/build_php.sh
index 1d81779b6a..77a8abcfe7 100755
--- a/tools/run_tests/build_php.sh
+++ b/tools/run_tests/build_php.sh
@@ -44,5 +44,9 @@ cd src/php
 
 cd ext/grpc
 phpize
-./configure --enable-grpc=$root
+if [ "$CONFIG" != "gcov" ] ; then
+  ./configure --enable-grpc=$root
+else
+  ./configure --enable-grpc=$root --enable-coverage
+fi
 make
diff --git a/tools/run_tests/post_tests_php.sh b/tools/run_tests/post_tests_php.sh
new file mode 100755
index 0000000000..b4098066ea
--- /dev/null
+++ b/tools/run_tests/post_tests_php.sh
@@ -0,0 +1,46 @@
+#!/bin/bash
+# Copyright 2015, Google Inc.
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#
+#     * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+#     * Redistributions in binary form must reproduce the above
+# copyright notice, this list of conditions and the following disclaimer
+# in the documentation and/or other materials provided with the
+# distribution.
+#     * Neither the name of Google Inc. nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+set -ex
+
+if [ "$CONFIG" != "gcov" ] ; then exit ; fi
+
+root=$(readlink -f $(dirname $0)/../..)
+out=$root/reports/php_ext_coverage
+tmp1=$(mktemp)
+tmp2=$(mktemp)
+cd $root
+lcov --capture --directory . --output-file $tmp1
+lcov --extract $tmp1 "$root/src/php/ext/grpc/*" --output-file $tmp2
+genhtml $tmp2 --output-directory $out
+rm $tmp2
+rm $tmp1
+
+cp -rv $root/src/php/coverage $root/reports/php
diff --git a/tools/run_tests/run_tests.py b/tools/run_tests/run_tests.py
index 0de20a634a..127377fcf6 100755
--- a/tools/run_tests/run_tests.py
+++ b/tools/run_tests/run_tests.py
@@ -245,7 +245,7 @@ class PhpLanguage(object):
     return [['tools/run_tests/build_php.sh']]
 
   def post_tests_steps(self):
-    return []
+    return [['tools/run_tests/post_tests_php.sh']]
 
   def makefile_name(self):
     return 'Makefile'
@@ -1092,4 +1092,3 @@ else:
   if BuildAndRunError.POST_TEST in errors:
     exit_code |= 4
   sys.exit(exit_code)
-
-- 
GitLab