Skip to content

Commit e114552

Browse files
committed
Fix: Coverage report not picked-up using Gradle Managed Devices and flavors
Fixes: #102
1 parent 7f6d0db commit e114552

File tree

18 files changed

+179
-17
lines changed

18 files changed

+179
-17
lines changed

plugin/src/main/kotlin/org/neotech/plugin/rootcoverage/JaCoCoConfiguration.kt

+2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ internal fun Project.getExecutionDataFileTree(includeUnitTestResults: Boolean, i
7474

7575
// Gradle Managed Devices 8.3+
7676
buildFolderPatterns.add("outputs/managed_device_code_coverage/*/*/coverage.ec")
77+
// In case of flavors coverage is nested an additional 2 folder deeper
78+
buildFolderPatterns.add("outputs/managed_device_code_coverage/*/flavors/*/*/coverage.ec")
7779
}
7880
return if(buildFolderPatterns.isEmpty()) {
7981
null

plugin/src/test/kotlin/org/neotech/plugin/rootcoverage/IntegrationTest.kt

+11-10
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,16 @@ class IntegrationTest(
8787
})
8888
})
8989

90-
val executeAndroidTests = configuration.pluginConfiguration.getPropertyValue("executeAndroidTests", "true").toBoolean()
90+
val executeAndroidTests = configuration.pluginConfiguration.getPropertyValue("executeAndroidTests", true)
9191

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

9797
// Execute Android tests completely separately (as if run on some external service,
9898
// after which the resulting files have been imported)
99-
if (runOnGradleManagedDevices == "false") {
99+
if (!runOnGradleManagedDevices) {
100100
executeGradleTasks(listOf("clean", "connectedDebugAndroidTest"))
101101
} else {
102102
executeGradleTasks(listOf("clean", "nexusoneapi30DebugAndroidTest"))
@@ -129,7 +129,7 @@ class IntegrationTest(
129129
}
130130

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

143143
private fun BuildResult.assertCorrectAndroidTestTasksAreNotExecuted() {
144-
if (configuration.pluginConfiguration.getPropertyValue("runOnGradleManagedDevices", "false").toBoolean()) {
144+
if (configuration.pluginConfiguration.getPropertyValue("runOnGradleManagedDevices", false)) {
145145
val device = configuration.pluginConfiguration.getPropertyValue("gradleManagedDeviceName", "allDevices")
146146
assertTaskNotExecuted(":app:${device}DebugAndroidTest")
147147
assertTaskNotExecuted(":library_android:${device}DebugAndroidTest")
148-
148+
assertTaskNotExecuted(":library_android_flavors:${device}DemoDebugAndroidTest")
149149
} else {
150150
assertTaskNotExecuted(":app:connectedDebugAndroidTest")
151151
assertTaskNotExecuted(":library_android:connectedDebugAndroidTest")
152+
assertTaskNotExecuted(":library_android_flavors:connectedDemoDebugAndroidTest")
152153
}
153154
}
154155

@@ -162,6 +163,7 @@ class IntegrationTest(
162163

163164
report.assertCoverage("org.neotech.library.android", "LibraryAndroidJava")
164165
report.assertCoverage("org.neotech.library.android", "LibraryAndroidKotlin")
166+
report.assertCoverage("org.neotech.library.android.flavors", "LibraryAndroidFlavorsKotlin")
165167
report.assertCoverage("org.neotech.app", "AppJava")
166168
report.assertCoverage("org.neotech.app", "AppKotlin")
167169
report.assertCoverage("org.neotech.app", "RobolectricTestedActivity")
@@ -253,12 +255,11 @@ class IntegrationTest(
253255
) {
254256
data class PluginConfiguration(val properties: List<Property> = emptyList()) {
255257

256-
fun getPropertyValue(name: String, defaultValue: String): String = getPropertyValue(name) ?: defaultValue
257-
258-
fun getPropertyValue(name: String): String? = properties.find { it.name == name }?.value
258+
fun <T> getPropertyValue(name: String, defaultValue: T): T = getPropertyValue(name) ?: defaultValue
259259

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

261-
data class Property(val name: String, val value: String)
262+
data class Property(val name: String, val value: Any)
262263
}
263264

264265
data class ProjectConfiguration(

plugin/src/test/kotlin/org/neotech/plugin/rootcoverage/util/ProjectGeneration.kt

+9-6
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,15 @@ inline fun <reified T> File.readYaml(): T {
3535
return mapper.readValue(this, T::class.java)
3636
}
3737

38-
internal fun List<IntegrationTest.TestConfiguration.PluginConfiguration.Property>.toGroovyString(): String = map {
39-
val stringValue = it.value
40-
if (stringValue.toBooleanStrictOrNull() != null) {
41-
"${it.name} ${it.value}"
42-
} else {
43-
"${it.name} \"${it.value}\""
38+
internal fun List<IntegrationTest.TestConfiguration.PluginConfiguration.Property>.toGroovyString(): String = map { property ->
39+
when(property.value) {
40+
is Boolean -> "${property.name} ${property.value}"
41+
is String -> "${property.name} \"${property.value}\""
42+
is Map<*, *> -> {
43+
val values = property.value.map { "\"${it.key}\": \"${it.value}\"" }.joinToString(separator = ", ")
44+
"${property.name} $values"
45+
}
46+
else -> error("Unknown value type: ${property.value}")
4447
}
4548
}.joinToString(separator = System.lineSeparator())
4649

plugin/src/test/test-fixtures/multi-module/app/build.gradle.tmp

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ android {
1515
versionCode 1
1616
versionName "1.0"
1717
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
18+
missingDimensionStrategy 'version', 'demo', 'full'
1819

1920
{{defaultConfig.clearPackageData}}
2021

@@ -29,8 +30,10 @@ android {
2930
debug {
3031
enableUnitTestCoverage true
3132
enableAndroidTestCoverage true
33+
matchingFallbacks = ['demo']
3234
}
3335
release {
36+
matchingFallbacks = ['full']
3437
minifyEnabled false
3538
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
3639
}
@@ -58,6 +61,7 @@ android {
5861

5962
dependencies {
6063
implementation project(":library_android")
64+
implementation project(":library_android_flavors")
6165

6266
implementation libs.appCompat
6367

plugin/src/test/test-fixtures/multi-module/configurations/connected-device-clear-package-data.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ pluginConfiguration:
2323

2424
- name: buildVariant
2525
value: debug
26+
- name: buildVariantOverrides
27+
value:
28+
":library_android_flavors": "demoDebug"
2629

2730
- name: executeUnitTests
2831
value: true

plugin/src/test/test-fixtures/multi-module/configurations/connected-device-dont-execute-android-tests.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ pluginConfiguration:
2020

2121
- name: buildVariant
2222
value: debug
23+
- name: buildVariantOverrides
24+
value:
25+
":library_android_flavors": "demoDebug"
2326

2427
- name: executeUnitTests
2528
value: true

plugin/src/test/test-fixtures/multi-module/configurations/connected-device.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ pluginConfiguration:
1111

1212
- name: buildVariant
1313
value: debug
14+
- name: buildVariantOverrides
15+
value:
16+
":library_android_flavors": "demoDebug"
1417

1518
- name: executeTests
1619
value: true

plugin/src/test/test-fixtures/multi-module/configurations/gradle-managed-device-dont-execute-android-tests.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ pluginConfiguration:
1111

1212
- name: buildVariant
1313
value: debug
14+
- name: buildVariantOverrides
15+
value:
16+
":library_android_flavors": "demoDebug"
1417

1518
- name: executeUnitTests
1619
value: true

plugin/src/test/test-fixtures/multi-module/configurations/gradle-managed-device.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ pluginConfiguration:
1111

1212
- name: buildVariant
1313
value: debug
14+
- name: buildVariantOverrides
15+
value:
16+
":library_android_flavors": "demoDebug"
1417

1518
- name: executeTests
1619
value: true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
plugins {
2+
id "com.android.library"
3+
alias(libs.plugins.kotlinAndroid)
4+
}
5+
6+
android {
7+
8+
namespace "org.neotech.library.flavors"
9+
compileSdkVersion libs.versions.androidCompileSdk.get().toInteger()
10+
11+
defaultConfig {
12+
minSdkVersion libs.versions.androidMinSdk.get().toInteger()
13+
targetSdkVersion libs.versions.androidTargetSdk.get().toInteger()
14+
versionCode 1
15+
versionName "1.0"
16+
17+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
18+
}
19+
20+
buildFeatures {
21+
buildConfig false
22+
}
23+
24+
buildTypes {
25+
debug {
26+
27+
testCoverageEnabled true
28+
}
29+
release {
30+
minifyEnabled false
31+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
32+
}
33+
}
34+
35+
flavorDimensions = ["version"]
36+
productFlavors {
37+
demo {
38+
dimension "version"
39+
}
40+
full {
41+
dimension "version"
42+
}
43+
}
44+
45+
compileOptions {
46+
sourceCompatibility = JavaVersion.VERSION_17
47+
targetCompatibility = JavaVersion.VERSION_17
48+
}
49+
50+
testOptions {
51+
managedDevices {
52+
devices {
53+
nexusoneapi30 (com.android.build.api.dsl.ManagedVirtualDevice) {
54+
device = "Nexus One"
55+
apiLevel = 30
56+
systemImageSource = "aosp-atd"
57+
}
58+
}
59+
}
60+
}
61+
62+
kotlinOptions {
63+
jvmTarget = "17"
64+
}
65+
}
66+
67+
dependencies {
68+
implementation libs.appCompat
69+
70+
testImplementation libs.bundles.androidTest
71+
androidTestImplementation libs.bundles.androidInstrumentedTest
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.neotech.library.android.flavors
2+
3+
import androidx.test.ext.junit.runners.AndroidJUnit4
4+
import org.junit.Assert
5+
import org.junit.Test
6+
import org.junit.runner.RunWith
7+
8+
@RunWith(AndroidJUnit4::class)
9+
class LibraryAndroidFlavorsKotlinInstrumentedTest {
10+
11+
@Test
12+
fun touch() {
13+
LibraryAndroidFlavorsKotlin.touchedByInstrumentedTest()
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<manifest />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.neotech.library.android.flavors
2+
3+
object LibraryAndroidFlavorsKotlin {
4+
5+
fun touchedByUnitTest(): String {
6+
return "touchedByUnitTest"
7+
}
8+
9+
fun touchedByInstrumentedTest(): String {
10+
return "touchedByInstrumentedTest"
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<resources>
2+
<string name="app_name">Library Android Flavors</string>
3+
</resources>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.neotech.library.android.flavors
2+
3+
import org.junit.Assert
4+
import org.junit.Test
5+
6+
class LibraryAndroidFlavorsKotlinUnitTest {
7+
8+
@Test
9+
fun touch() {
10+
LibraryAndroidFlavorsKotlin.touchedByUnitTest()
11+
}
12+
}

plugin/src/test/test-fixtures/multi-module/settings.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ dependencyResolutionManagement {
1818
}
1919
}
2020

21-
include ':app', ':library_android', ':library_java', ':library_nested:java'
21+
include ':app', ':library_android', ':library_android_flavors', ':library_java', ':library_nested:java'

0 commit comments

Comments
 (0)