You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aurora.apache.org by wf...@apache.org on 2014/10/20 20:40:39 UTC

git commit: Add a message when coverage exceeds minimum.

Repository: incubator-aurora
Updated Branches:
  refs/heads/master 233e75b76 -> c6d0d78f1


Add a message when coverage exceeds minimum.

This way we'll be sure to continually raise the minimum threshold as we exceed it.

Reviewed at https://reviews.apache.org/r/26902/


Project: http://git-wip-us.apache.org/repos/asf/incubator-aurora/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-aurora/commit/c6d0d78f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-aurora/tree/c6d0d78f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-aurora/diff/c6d0d78f

Branch: refs/heads/master
Commit: c6d0d78f1c4760f6ee4542c8e6a138d700d1e246
Parents: 233e75b
Author: Joshua Cohen <jc...@twopensource.com>
Authored: Mon Oct 20 11:37:07 2014 -0700
Committer: Bill Farner <wf...@apache.org>
Committed: Mon Oct 20 11:37:07 2014 -0700

----------------------------------------------------------------------
 .../apache/aurora/CoverageReportCheck.groovy    | 82 ++++++++++++--------
 1 file changed, 51 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/c6d0d78f/buildSrc/src/main/groovy/org/apache/aurora/CoverageReportCheck.groovy
----------------------------------------------------------------------
diff --git a/buildSrc/src/main/groovy/org/apache/aurora/CoverageReportCheck.groovy b/buildSrc/src/main/groovy/org/apache/aurora/CoverageReportCheck.groovy
index 390acf7..16f648b 100644
--- a/buildSrc/src/main/groovy/org/apache/aurora/CoverageReportCheck.groovy
+++ b/buildSrc/src/main/groovy/org/apache/aurora/CoverageReportCheck.groovy
@@ -40,49 +40,69 @@ class CoverageReportCheck extends DefaultTask {
     return ((double) covered) / (missed + covered)
   }
 
-  @TaskAction
-  def analyze() {
-    def parser = new XmlSlurper()
-    parser.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false);
-    // Avoid trying to load the DTD for the XML document, which does not exist.
-    parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false)
-
-    def coverageReport = parser.parse(coverageReportFile)
-
-    // Check global thresholds.
-    if (computeCoverage(coverageReport.counter, 'INSTRUCTION') < minInstructionCoverage) {
-      throw new GradleException(
-          'Instruction coverage must be greater than ' + minInstructionCoverage)
+  def checkThresholds(coverage, minCoverage, type) {
+    if (coverage < minCoverage) {
+      return "$type coverage must be greater than $minCoverage"
+    } else {
+      def floored = Math.floor(coverage * 100) / 100
+      if (floored > minCoverage) {
+        return("$type coverage of $floored exceeds min instruction coverage of $minCoverage,"
+            + " please raise the threshold!")
+      }
     }
+  }
 
-    if (computeCoverage(coverageReport.counter, 'BRANCH') < minBranchCoverage) {
-      throw new GradleException('Branch coverage must be greater than ' + minBranchCoverage)
+  def checkGlobalCoverage(coverageCounts) {
+    def coverageErrors = [
+        [computeCoverage(coverageCounts, 'INSTRUCTION'), minInstructionCoverage, 'Instruction'],
+        [computeCoverage(coverageCounts, 'BRANCH'), minBranchCoverage, 'Branch']
+    ].collect() {
+      return checkThresholds(*it)
+    }.findAll()
+
+    if (!coverageErrors.isEmpty()) {
+      throw new GradleException(coverageErrors.join('\n'))
     }
+  }
 
+  def checkClassCoverage(coverageReport) {
     def coverageErrors = coverageReport.package.class.collect { cls ->
-        // javac inserts a synthetic constructor for anonymous classes, and jacoco tends to mark
-        // these as covered in some cases, leading to flaky behavior.  We work around that by
-        // collecting the coverage count for each method in each class, omitting the default
-        // constructor for anonymous classes.  Anonymous classes are identified by their name,
-        // which we expect to be of the form 'Something$1'.  Jacoco names default constructors
-        // '<init>', so we filter based on that.
-        def methodFilter = cls.@name ==~ /.*\$\d+/ ? { m -> m.@name != '<init>' } : { true }
+      // javac inserts a synthetic constructor for anonymous classes, and jacoco tends to mark
+      // these as covered in some cases, leading to flaky behavior.  We work around that by
+      // collecting the coverage count for each method in each class, omitting the default
+      // constructor for anonymous classes.  Anonymous classes are identified by their name,
+      // which we expect to be of the form 'Something$1'.  Jacoco names default constructors
+      // '<init>', so we filter based on that.
+      def methodFilter = cls.@name ==~ /.*\$\d+/ ? { m -> m.@name != '<init>' } : { true }
 
-        def covered = cls.method.findAll(methodFilter).collect { m ->
-          m.counter.find({ c -> c.@type == 'INSTRUCTION' }).@covered.toInteger()}.sum(0)
+      def covered = cls.method.findAll(methodFilter).collect { m ->
+        m.counter.find({ c -> c.@type == 'INSTRUCTION' }).@covered.toInteger()}.sum(0)
 
-        if (cls.@name in legacyClassesWithoutCoverage) {
-          if (covered != 0) {
-            return 'Thanks for adding the first test coverage to: ' + cls.@name \
+      if (cls.@name in legacyClassesWithoutCoverage) {
+        if (covered != 0) {
+          return 'Thanks for adding the first test coverage to: ' + cls.@name \
               + ' please remove it from the legacyClassesWithoutCoverage list'
-          }
-        } else if (covered == 0) {
-          return 'Test coverage missing for ' + cls.@name
         }
-        return null
+      } else if (covered == 0) {
+        return 'Test coverage missing for ' + cls.@name
+      }
+      return null
     }.findAll()  // Filter nulls.
     if (!coverageErrors.isEmpty()) {
       throw new GradleException(coverageErrors.join('\n'))
     }
   }
+
+  @TaskAction
+  def analyze() {
+    def parser = new XmlSlurper()
+    parser.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false);
+    // Avoid trying to load the DTD for the XML document, which does not exist.
+    parser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false)
+
+    def coverageReport = parser.parse(coverageReportFile)
+
+    checkGlobalCoverage(coverageReport.counter)
+    checkClassCoverage(coverageReport)
+  }
 }