Skip to content

Commit

Permalink
Diagnostic info for when tests hang.
Browse files Browse the repository at this point in the history
  • Loading branch information
TWiStErRob committed Aug 9, 2023
1 parent c78ffbc commit ccb1232
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
8 changes: 7 additions & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
id("net.twisterrob.inventory.android.app")
id("net.twisterrob.inventory.mapping")
id("net.twisterrob.inventory.upgradeTest")
id("net.twisterrob.inventory.build.tests.instrumentation")
}

dependencies {
Expand Down Expand Up @@ -149,8 +150,13 @@ tasks.withType(Test).configureEach {

// CONSIDER a rewrite of plugin "android-reporting" (see Color Filters repo)
def copyAndroidTestResults = tasks.register("copyAndroidTestResults", Sync) {
// No SingleArtifact/InternalArtifactType in AGP 8.1 for androidTest results/reports, so hardcode.
from(layout.buildDirectory.dir("reports/androidTests/connected/debug"))
from(layout.buildDirectory.dir("outputs/androidTest-results/connected/debug"))
into(rootProject.layout.buildDirectory.dir("androidTest-results"))
}
tasks.named("connectedDebugAndroidTest").configure { finalizedBy(copyAndroidTestResults) }
afterEvaluate {
tasks.named("connectedDebugAndroidTest").configure {
finalizedBy(copyAndroidTestResults)
}
}
4 changes: 4 additions & 0 deletions gradle/plugins-inventory/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ dependencies {
compileOnly(libs.plugin.android.tools.ddmlib)
// UpgradeTestTask: FileUtils, StdLogger, ILogger
compileOnly(libs.plugin.android.tools.common)
// instrumentation.gradle.kts
compileOnly("com.android.tools.utp:android-test-plugin-result-listener-gradle-proto:31.1.0")
compileOnly("com.google.testing.platform:core-proto:0.0.8-alpha08")
compileOnly("com.google.protobuf:protobuf-java:3.19.3")
// endregion

// TODEL https://github.com/gradle/gradle/issues/15383
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.twisterrob.inventory.build.tests

import com.android.build.gradle.internal.tasks.DeviceProviderInstrumentTestTask
import net.twisterrob.inventory.build.tests.instrumentation.LoggingUtpTestResultListener

// TODEL https://issuetracker.google.com/issues/37056080
tasks.withType<DeviceProviderInstrumentTestTask>().configureEach {
setUtpTestResultListener(LoggingUtpTestResultListener(logger))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package net.twisterrob.inventory.build.tests.instrumentation

import com.android.build.gradle.internal.testing.utp.UtpTestResultListener
import com.android.tools.utp.plugins.result.listener.gradle.proto.GradleAndroidTestResultListenerProto.TestResultEvent
import com.android.tools.utp.plugins.result.listener.gradle.proto.GradleAndroidTestResultListenerProto.TestResultEvent.StateCase
import com.google.testing.platform.proto.api.core.TestCaseProto.TestCase
import com.google.testing.platform.proto.api.core.TestResultProto.TestResult
import com.google.testing.platform.proto.api.core.TestStatusProto.TestStatus
import org.gradle.api.logging.Logger

/**
* See https://issuetracker.google.com/issues/37056080#comment12.
*/
class LoggingUtpTestResultListener(
private val logger: Logger
) : UtpTestResultListener {
// See com.android.build.gradle.internal.testing.utp.DdmlibTestResultAdapter.onTestResultEvent
// See com.android.build.gradle.internal.testing.CustomTestRunListener.testEnded
override fun onTestResultEvent(testResultEvent: TestResultEvent) {
//logger.lifecycle("UTP: {}", testResultEvent)
when (testResultEvent.stateCase) {
StateCase.TEST_CASE_STARTED -> {
val tc = testResultEvent.testCaseStarted.testCase.unpack(TestCase::class.java)
logger.lifecycle("${tc.testPackage}.${tc.testClass} > ${tc.testMethod} \u001b[34mSTARTED\u001b[0m")
}
StateCase.TEST_CASE_FINISHED -> {
val result = testResultEvent.testCaseFinished.testCaseResult.unpack(TestResult::class.java)
val status = when (result.testStatus) {
TestStatus.PASSED -> "\u001b[32mSUCCESS\u001b[0m"
// Already handled by AGP.
TestStatus.FAILED, TestStatus.ERROR, TestStatus.IGNORED -> null
else -> "\u001b[31${result.testStatus.name}\u001b[0m"
}
if (status != null) {
val tc = result.testCase
logger.lifecycle("${tc.testPackage}.${tc.testClass} > ${tc.testMethod} ${status}")
}
}
else -> {}
}
}
}

0 comments on commit ccb1232

Please sign in to comment.