Skip to content

Fix: Coverage data not picked-up using Gradle Managed Devices and flavors #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ internal fun Project.getExecutionDataFileTree(includeUnitTestResults: Boolean, i

// Gradle Managed Devices 8.3+
buildFolderPatterns.add("outputs/managed_device_code_coverage/*/*/coverage.ec")
// In case of flavors coverage is nested an additional 2 folder deeper
buildFolderPatterns.add("outputs/managed_device_code_coverage/*/flavors/*/*/coverage.ec")
}
return if(buildFolderPatterns.isEmpty()) {
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,19 +87,19 @@ class IntegrationTest(
})
})

val executeAndroidTests = configuration.pluginConfiguration.getPropertyValue("executeAndroidTests", "true").toBoolean()
val executeAndroidTests = configuration.pluginConfiguration.getPropertyValue("executeAndroidTests", true)

// Note: rootCodeCoverageReport is the old and deprecated name of the rootCoverageReport task, it is
// used to check whether the old name properly aliases to the new task name.
val gradleCommands = if (!executeAndroidTests) {
val runOnGradleManagedDevices = configuration.pluginConfiguration.getPropertyValue("runOnGradleManagedDevices") ?: "false"
val runOnGradleManagedDevices = configuration.pluginConfiguration.getPropertyValue("runOnGradleManagedDevices", false)

// Execute Android tests completely separately (as if run on some external service,
// after which the resulting files have been imported)
if (runOnGradleManagedDevices == "false") {
executeGradleTasks(listOf("clean", "connectedDebugAndroidTest"))
if (!runOnGradleManagedDevices) {
executeGradleTasks(listOf("clean", "connectedDebugAndroidTest", "connectedDemoDebugAndroidTest"))
} else {
executeGradleTasks(listOf("clean", "nexusoneapi30DebugAndroidTest"))
executeGradleTasks(listOf("clean", "nexusoneapi30DebugAndroidTest", "nexusoneapi30DemoDebugAndroidTest"))
}

listOf("coverageReport", "rootCodeCoverageReport", "--stacktrace")
Expand Down Expand Up @@ -129,7 +129,7 @@ class IntegrationTest(
}

private fun BuildResult.assertCorrectAndroidTestTasksAreExecuted() {
if (configuration.pluginConfiguration.getPropertyValue("runOnGradleManagedDevices", "false").toBoolean()) {
if (configuration.pluginConfiguration.getPropertyValue("runOnGradleManagedDevices", false)) {
val device = configuration.pluginConfiguration.getPropertyValue("gradleManagedDeviceName", "allDevices")
assertTaskSuccess(":app:${device}DebugAndroidTest")
assertTaskSuccess(":library_android:${device}DebugAndroidTest")
Expand All @@ -141,14 +141,15 @@ class IntegrationTest(
}

private fun BuildResult.assertCorrectAndroidTestTasksAreNotExecuted() {
if (configuration.pluginConfiguration.getPropertyValue("runOnGradleManagedDevices", "false").toBoolean()) {
if (configuration.pluginConfiguration.getPropertyValue("runOnGradleManagedDevices", false)) {
val device = configuration.pluginConfiguration.getPropertyValue("gradleManagedDeviceName", "allDevices")
assertTaskNotExecuted(":app:${device}DebugAndroidTest")
assertTaskNotExecuted(":library_android:${device}DebugAndroidTest")

assertTaskNotExecuted(":library_android_flavors:${device}DemoDebugAndroidTest")
} else {
assertTaskNotExecuted(":app:connectedDebugAndroidTest")
assertTaskNotExecuted(":library_android:connectedDebugAndroidTest")
assertTaskNotExecuted(":library_android_flavors:connectedDemoDebugAndroidTest")
}
}

Expand All @@ -162,6 +163,7 @@ class IntegrationTest(

report.assertCoverage("org.neotech.library.android", "LibraryAndroidJava")
report.assertCoverage("org.neotech.library.android", "LibraryAndroidKotlin")
report.assertCoverage("org.neotech.library.android.flavors", "LibraryAndroidFlavorsKotlin")
report.assertCoverage("org.neotech.app", "AppJava")
report.assertCoverage("org.neotech.app", "AppKotlin")
report.assertCoverage("org.neotech.app", "RobolectricTestedActivity")
Expand Down Expand Up @@ -253,12 +255,11 @@ class IntegrationTest(
) {
data class PluginConfiguration(val properties: List<Property> = emptyList()) {

fun getPropertyValue(name: String, defaultValue: String): String = getPropertyValue(name) ?: defaultValue

fun getPropertyValue(name: String): String? = properties.find { it.name == name }?.value
fun <T> getPropertyValue(name: String, defaultValue: T): T = getPropertyValue(name) ?: defaultValue

fun <T> getPropertyValue(name: String): T? = properties.find { it.name == name }?.value as T?

data class Property(val name: String, val value: String)
data class Property(val name: String, val value: Any)
}

data class ProjectConfiguration(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ inline fun <reified T> File.readYaml(): T {
return mapper.readValue(this, T::class.java)
}

internal fun List<IntegrationTest.TestConfiguration.PluginConfiguration.Property>.toGroovyString(): String = map {
val stringValue = it.value
if (stringValue.toBooleanStrictOrNull() != null) {
"${it.name} ${it.value}"
} else {
"${it.name} \"${it.value}\""
internal fun List<IntegrationTest.TestConfiguration.PluginConfiguration.Property>.toGroovyString(): String = map { property ->
when(property.value) {
is Boolean -> "${property.name} ${property.value}"
is String -> "${property.name} \"${property.value}\""
is Map<*, *> -> {
val values = property.value.map { "\"${it.key}\": \"${it.value}\"" }.joinToString(separator = ", ")
"${property.name} $values"
}
else -> error("Unknown value type: ${property.value}")
}
}.joinToString(separator = System.lineSeparator())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ android {
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
missingDimensionStrategy 'version', 'demo', 'full'

{{defaultConfig.clearPackageData}}

Expand All @@ -29,8 +30,10 @@ android {
debug {
enableUnitTestCoverage true
enableAndroidTestCoverage true
matchingFallbacks = ['demo']
}
release {
matchingFallbacks = ['full']
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
Expand Down Expand Up @@ -58,6 +61,7 @@ android {

dependencies {
implementation project(":library_android")
implementation project(":library_android_flavors")

implementation libs.appCompat

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ pluginConfiguration:

- name: buildVariant
value: debug
- name: buildVariantOverrides
value:
":library_android_flavors": "demoDebug"

- name: executeUnitTests
value: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ pluginConfiguration:

- name: buildVariant
value: debug
- name: buildVariantOverrides
value:
":library_android_flavors": "demoDebug"

- name: executeUnitTests
value: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ pluginConfiguration:

- name: buildVariant
value: debug
- name: buildVariantOverrides
value:
":library_android_flavors": "demoDebug"

- name: executeTests
value: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ pluginConfiguration:

- name: buildVariant
value: debug
- name: buildVariantOverrides
value:
":library_android_flavors": "demoDebug"

- name: executeUnitTests
value: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ pluginConfiguration:

- name: buildVariant
value: debug
- name: buildVariantOverrides
value:
":library_android_flavors": "demoDebug"

- name: executeTests
value: true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
plugins {
id "com.android.library"
alias(libs.plugins.kotlinAndroid)
}

android {

namespace "org.neotech.library.flavors"
compileSdkVersion libs.versions.androidCompileSdk.get().toInteger()

defaultConfig {
minSdkVersion libs.versions.androidMinSdk.get().toInteger()
targetSdkVersion libs.versions.androidTargetSdk.get().toInteger()
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildFeatures {
buildConfig false
}

buildTypes {
debug {

testCoverageEnabled true
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

flavorDimensions = ["version"]
productFlavors {
demo {
dimension "version"
}
full {
dimension "version"
}
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

testOptions {
managedDevices {
devices {
nexusoneapi30 (com.android.build.api.dsl.ManagedVirtualDevice) {
device = "Nexus One"
apiLevel = 30
systemImageSource = "aosp-atd"
}
}
}
}

kotlinOptions {
jvmTarget = "17"
}
}

dependencies {
implementation libs.appCompat

testImplementation libs.bundles.androidTest
androidTestImplementation libs.bundles.androidInstrumentedTest
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.neotech.library.android.flavors

import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class LibraryAndroidFlavorsKotlinInstrumentedTest {

@Test
fun touch() {
LibraryAndroidFlavorsKotlin.touchedByInstrumentedTest()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<manifest />
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.neotech.library.android.flavors

object LibraryAndroidFlavorsKotlin {

fun touchedByUnitTest(): String {
return "touchedByUnitTest"
}

fun touchedByInstrumentedTest(): String {
return "touchedByInstrumentedTest"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="app_name">Library Android Flavors</string>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.neotech.library.android.flavors

import org.junit.Assert
import org.junit.Test

class LibraryAndroidFlavorsKotlinUnitTest {

@Test
fun touch() {
LibraryAndroidFlavorsKotlin.touchedByUnitTest()
}
}
2 changes: 1 addition & 1 deletion plugin/src/test/test-fixtures/multi-module/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ dependencyResolutionManagement {
}
}

include ':app', ':library_android', ':library_java', ':library_nested:java'
include ':app', ':library_android', ':library_android_flavors', ':library_java', ':library_nested:java'