Build: Add test for reporting all gradle build cache misses

`kotlin.build.cache.check.enabled` will be used for cacheability testing
on teamcity
This commit is contained in:
Vyacheslav Gerasimov
2020-03-02 22:13:54 +03:00
parent b3f16cfc96
commit c77130d0f3
3 changed files with 62 additions and 0 deletions
+2
View File
@@ -2,6 +2,8 @@
<dictionary name="4u7">
<words>
<w>bintray</w>
<w>cacheability</w>
<w>cacheable</w>
<w>cidr</w>
<w>foldable</w>
<w>instrumentator</w>
+1
View File
@@ -149,6 +149,7 @@ rootProject.apply {
from(rootProject.file("gradle/javaInstrumentation.gradle.kts"))
from(rootProject.file("gradle/jps.gradle.kts"))
from(rootProject.file("gradle/checkArtifacts.gradle.kts"))
from(rootProject.file("gradle/checkCacheability.gradle.kts"))
}
IdeVersionConfigurator.setCurrentIde(project)
+59
View File
@@ -0,0 +1,59 @@
import org.gradle.api.internal.GeneratedSubclasses
import org.gradle.api.internal.TaskInternal
val checkCacheability = findProperty("kotlin.build.cache.check.enabled") as String? == "true"
val isTeamcityBuild = project.hasProperty("teamcity") || System.getenv("TEAMCITY_VERSION") != null
if (checkCacheability && buildCacheEnabled()) {
gradle.taskGraph.afterTask {
if (isCacheable()) {
if (isTeamcityBuild)
testStarted(path)
if (!state.skipped)
reportCacheMiss()
if (isTeamcityBuild)
testFinished(path)
}
}
}
fun Task.reportCacheMiss() {
if (isTeamcityBuild)
testFailed(path, "Build cache MISS", "$path task outputs expected to be taken from Gradle build cache")
else
println("BUILD CACHE MISS - $path")
}
fun Project.buildCacheEnabled() = gradle.startParameter.isBuildCacheEnabled
fun Task.isCacheable(): Boolean {
this as TaskInternal
return cachingEnabled() && !cachingDisabled()
}
fun TaskInternal.cachingEnabled(): Boolean {
return if (outputs.cacheIfSpecs.isEmpty())
GeneratedSubclasses.unpackType(this).isAnnotationPresent(CacheableTask::class.java)
else
outputs.cacheIfSpecs.all { it.invoke(this) }
}
fun TaskInternal.cachingDisabled(): Boolean = outputs.doNotCacheIfSpecs.any { it.invoke(this) }
fun escape(s: String): String {
return s.replace("[\\|'\\[\\]]".toRegex(), "\\|$0").replace("\n".toRegex(), "|n").replace("\r".toRegex(), "|r")
}
fun testStarted(testName: String) {
println("##teamcity[testStarted name='%s']".format(escape(testName)))
}
fun testFinished(testName: String) {
println("##teamcity[testFinished name='%s']".format(escape(testName)))
}
fun testFailed(name: String, message: String, details: String) {
println("##teamcity[testFailed name='%s' message='%s' details='%s']".format(escape(name), escape(message), escape(details)))
}