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 f766d11 commit 0f2273e
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions AnkiDroid/jacoco.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,28 @@ def androidTestReport = tasks.register('jacocoAndroidTestReport', JacocoReport)
])
}
androidTestReport.configure { dependsOn('connectedPlayDebugAndroidTest') }

// Issue 16640 - some emulators run, but register zero coverage
tasks.register('assertNonzeroAndroidTestCoverage') {
doLast {
// androidTestReport currently creates one .xml file results in this dir
File folder = file("./build/reports/jacoco/jacocoAndroidTestReport/")
File[] listOfFiles = folder.listFiles({ d, f -> f ==~ /.*.xml/ } as FilenameFilter)
for (File file : listOfFiles) {
// The aggregate results file currently ends with a final aggregate coverage amount
// it is non-zero in any sane execution of our tests
// the last characters in the report look like ' covered="393"/></report>'
String[] matches = file.readLines().findAll { it.contains('"/></report>') }
if (matches.length != 1) {
throw new GradleScriptException("Unable to determine coverage for tests executed for " + file.name + ". Regex pattern out of date?", null)
}
if (!(matches[0] =~ /covered="\d+"\/><\/report>/) || matches[0].contains('covered="0"/></report>')) {
throw new GradleScriptException("androidTest registered zero coverage for " + file.name + " - Probably some incompatibilities in the toolchain.", null)
}
}
}
}
afterEvaluate {
tasks.named('jacocoAndroidTestReport').configure { finalizedBy('assertNonzeroAndroidTestCoverage') }
tasks.named('jacocoTestReport').configure { finalizedBy('assertNonzeroAndroidTestCoverage') }
}

0 comments on commit 0f2273e

Please sign in to comment.