Skip to content

Commit

Permalink
test: ensure non-zero coverage for androidTest reports
Browse files Browse the repository at this point in the history
there are many toolchain incompatibilities which can result in a
false-positive androidTest test run, but with zero actual coverage
tallied

this adds a finalization task that scans the coverage report looking
for an aggregate coverage count that is non-zero
  • Loading branch information
mikehardy committed Jun 24, 2024
1 parent 7437b73 commit cd2cc0b
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions AnkiDroid/jacoco.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import groovy.transform.Memoized
import groovy.xml.XmlParser

apply plugin: 'jacoco'

Expand Down Expand Up @@ -167,3 +168,39 @@ def androidTestReport = tasks.register('jacocoAndroidTestReport', JacocoReport)
])
}
androidTestReport.configure { dependsOn('connectedPlayDebugAndroidTest') }

// Issue 16640 - some emulators run, but register zero coverage
tasks.register('assertNonzeroAndroidTestCoverage') {
doLast {
File jacocoReport = layout.buildDirectory.dir("reports/jacoco/jacocoAndroidTestReport/jacocoAndroidTestReport.xml").get().asFile

if (!jacocoReport.exists())
throw new FileNotFoundException("jacocoAndroidTestReport.xml was not found after running jacocoAndroidTestReport")

XmlParser xmlParser = new XmlParser()
xmlParser.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false)
xmlParser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false)

Node reportRoot = xmlParser.parse(jacocoReport)
boolean hasCovered = false

// https://github.com/jacoco/jacoco/blob/5aabb2eb60bbcd05df968005f1746ba19dcd5361/org.jacoco.report/src/org/jacoco/report/internal/xml/ReportElement.java#L190
for (Node child : reportRoot.children()) {
if (child.name() != "counter")
continue;

if (child.attribute("covered") == "0") {
logger.warn("jacoco registered zero code coverage for counter type %s", child.attribute("type"))
} else {
hasCovered = true
}
}

if (!hasCovered)
throw new GradleScriptException("androidTest registered zero code coverage in jacocoAndroidTestReport.xml. Probably some incompatibilities in the toolchain.", null)
}
}
afterEvaluate {
tasks.named('jacocoAndroidTestReport').configure { finalizedBy('assertNonzeroAndroidTestCoverage') }
tasks.named('jacocoTestReport').configure { finalizedBy('assertNonzeroAndroidTestCoverage') }
}

0 comments on commit cd2cc0b

Please sign in to comment.