Skip to content

Commit

Permalink
Decouple robolectric and unit tests for sentry-android-core
Browse files Browse the repository at this point in the history
  • Loading branch information
romtsn committed Dec 30, 2023
1 parent 78a44b1 commit 65887b4
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 19 deletions.
30 changes: 16 additions & 14 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,22 @@ allprojects {
version = properties[Config.Sentry.versionNameProp].toString()
description = Config.Sentry.description
tasks {
withType<Test> {
testLogging.showStandardStreams = true
testLogging.exceptionFormat = TestExceptionFormat.FULL
testLogging.events = setOf(
TestLogEvent.SKIPPED,
TestLogEvent.PASSED,
TestLogEvent.FAILED
)
maxParallelForks = Runtime.getRuntime().availableProcessors() / 2

// Cap JVM args per test
minHeapSize = "128m"
maxHeapSize = "1g"
dependsOn("cleanTest")
if (this@allprojects.name != "sentry-android-core") {
withType<Test> {
testLogging.showStandardStreams = true
testLogging.exceptionFormat = TestExceptionFormat.FULL
testLogging.events = setOf(
TestLogEvent.SKIPPED,
TestLogEvent.PASSED,
TestLogEvent.FAILED
)
maxParallelForks = Runtime.getRuntime().availableProcessors() / 2

// Cap JVM args per test
minHeapSize = "128m"
maxHeapSize = "1g"
dependsOn("cleanTest")
}
}
withType<JavaCompile> {
options.compilerArgs.addAll(arrayOf("-Xlint:all", "-Werror", "-Xlint:-classfile", "-Xlint:-processing"))
Expand Down
70 changes: 65 additions & 5 deletions sentry-android-core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import net.ltgt.gradle.errorprone.errorprone
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent
import org.gradle.configurationcache.extensions.capitalized
import org.jetbrains.kotlin.config.KotlinCompilerVersion

plugins {
Expand All @@ -20,7 +23,11 @@ android {

testInstrumentationRunner = Config.TestLibs.androidJUnitRunner

buildConfigField("String", "SENTRY_ANDROID_SDK_NAME", "\"${Config.Sentry.SENTRY_ANDROID_SDK_NAME}\"")
buildConfigField(
"String",
"SENTRY_ANDROID_SDK_NAME",
"\"${Config.Sentry.SENTRY_ANDROID_SDK_NAME}\""
)

// for AGP 4.1
buildConfigField("String", "VERSION_NAME", "\"${project.version}\"")
Expand Down Expand Up @@ -65,10 +72,63 @@ android {
}
}

tasks.withType<JavaCompile>().configureEach {
options.errorprone {
check("NullAway", net.ltgt.gradle.errorprone.CheckSeverity.ERROR)
option("NullAway:AnnotatedPackages", "io.sentry")
tasks {
withType<JavaCompile>().configureEach {
options.errorprone {
check("NullAway", net.ltgt.gradle.errorprone.CheckSeverity.ERROR)
option("NullAway:AnnotatedPackages", "io.sentry")
}
}

withType<Test>().configureEach {
testLogging.showStandardStreams = true
testLogging.exceptionFormat = TestExceptionFormat.FULL
testLogging.events = setOf(
TestLogEvent.SKIPPED,
TestLogEvent.PASSED,
TestLogEvent.FAILED
)
maxParallelForks = Runtime.getRuntime().availableProcessors() / 2

// Cap JVM args per test
minHeapSize = "128m"
maxHeapSize = "1g"
if (!this.name.contains("robolectric", ignoreCase = true)) {
filter {
exclude { element ->
if (element.isDirectory || !element.file.exists()) {
return@exclude false
}
return@exclude element.file
.readText()
.contains("Landroidx/test/ext/junit/runners/AndroidJUnit4;")
}
}
}
dependsOn("cleanTest")
}
}

afterEvaluate {
setOf("debug", "release").forEach { variant ->
task<Test>("${variant}RobolectricTest") {
group = "verification"
description = "Runs the Robolectric tests"

val testTask = tasks.findByName("test${variant.capitalized()}UnitTest") as Test
classpath = testTask.classpath
testClassesDirs = testTask.testClassesDirs
filter {
include { element ->
if (element.isDirectory || !element.file.exists()) {
return@include true
}
return@include element.file
.readText()
.contains("Landroidx/test/ext/junit/runners/AndroidJUnit4;")
}
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ import androidx.test.espresso.IdlingRegistry
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.ext.junit.runners.AndroidJUnit4
import io.sentry.NoOpLogger
import io.sentry.ProfilingTraceData
import io.sentry.Sentry
import io.sentry.SentryEvent
import io.sentry.android.core.AndroidLogger
import io.sentry.android.core.BuildInfoProvider
import io.sentry.android.core.SentryAndroidOptions
import io.sentry.assertEnvelopeProfile
import io.sentry.assertEnvelopeTransaction
import io.sentry.profilemeasurements.ProfileMeasurement
import io.sentry.protocol.SentryTransaction
import org.junit.Assume.assumeFalse
import org.junit.Assume.assumeNotNull
import org.junit.runner.RunWith
import java.util.concurrent.TimeUnit
Expand Down Expand Up @@ -176,6 +179,12 @@ class EnvelopeTests : BaseUiTest() {

@Test
fun checkTimedOutProfile() {
assumeFalse(
"Sometimes traceFile does not exist for profiles on emulators and it fails." +
" Although, Android Runtime is the one that manages the file, so something" +
" must be wrong with Debug.startMethodTracing",
BuildInfoProvider(NoOpLogger.getInstance()).isEmulator ?: true
)
// We increase the IdlingResources timeout to exceed the profiling timeout
IdlingPolicies.setIdlingResourceTimeout(1, TimeUnit.MINUTES)
initSentry(true) { options: SentryAndroidOptions ->
Expand Down

0 comments on commit 65887b4

Please sign in to comment.