forked from goldy1992/Mp3Player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jacoco-with-test-support.gradle
146 lines (116 loc) · 5.71 KB
/
jacoco-with-test-support.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
apply plugin: 'jacoco'
jacoco {
toolVersion = jacoco_version
}
def fileFilter = [
'**/R.class',
'**/R$*.class',
'**/BuildConfig.*',
'**/Manifest*.*',
'**/*Test*.*',
'android/**/*.*',
'**/*$[0-9].*',
'**/dagger/**',
'**/databinding/**',
'**hilt_aggregated_deps**',
'**/Hilt_**'
]
def excludeUiCode = ['**/ui/**']
tasks.withType(Test) {
jacoco.includeNoLocationClasses = true
}
project.afterEvaluate {
def productFlavors = android.productFlavors.collect {
flavor -> flavor.name
}
def applicableProductFlavor = "full"
def unitTestBuildType ='debug'
def unitTestFlavor = getProductFlavor(applicableProductFlavor, productFlavors);
def (String unitTestBuildVariant, String unitTestBuildVariantCapitalised) = getBuildVariant(unitTestFlavor, unitTestBuildType)
def unitExecDir = "$buildDir/outputs/unit_test_code_coverage/${unitTestBuildVariant}UnitTest/test${unitTestBuildVariantCapitalised}UnitTest.exec"
def unitBuildFileTree = fileTree(unitExecDir)
/**
* Generates a JaCoCo Report for the test coverage for the unit tests.
*/
task jacocoUnitTestReport(type: JacocoReport, dependsOn: ["test${unitTestBuildVariantCapitalised}UnitTest"]) {
group = "Reporting"
description = "Generate Jacoco coverage reports on the ${unitTestBuildVariantCapitalised} build."
reports {
xml.required = true
html.required = true
}
def exclusions = fileFilter + excludeUiCode
def mainSrc = "$project.projectDir/src/main/java"
def mainClasses = fileTree(dir: "${buildDir}/tmp/kotlin-classes/${unitTestBuildVariant}", excludes: exclusions)
def javaClasses = fileTree(dir: "${buildDir}/intermediates/javac/${unitTestBuildVariant}", excludes: exclusions)
sourceDirectories.from = files([mainSrc])
classDirectories.from = files([mainClasses, javaClasses])
executionData.from = unitBuildFileTree
}
def uiTestBuildType = 'debug'
def uiTestFlavor = getProductFlavor(applicableProductFlavor, productFlavors);
def (String uiTestBuildVariant, String uiTestBuildVariantCapitalised) = getBuildVariant(uiTestFlavor, uiTestBuildType)
def uiBuildDir = "${buildDir}/outputs/code_coverage/${uiTestBuildVariant}AndroidTest/connected"
def uiBuildFileTree = fileTree(uiBuildDir){
include "**/*.ec"
}
/**
* Generates a JaCoCo Report for the test coverage for the UI tests.
*/
task jacocoUiTestReport(type: JacocoReport, dependsOn: ["connected${uiTestBuildVariantCapitalised}AndroidTest"]) {
group = "Reporting"
description = "Generate Jacoco coverage reports for both UI and Unit Tests on the ${uiTestBuildVariantCapitalised} build."
reports {
xml.required = true
html.required = true
}
def mainSrc = "$project.projectDir/src/main/java"
def mainClasses = fileTree(dir: "${buildDir}/tmp/kotlin-classes/", includes: ["${uiTestBuildVariant}/**"], excludes: fileFilter)
def javaClasses = fileTree(dir: "${buildDir}/intermediates/javac/", includes: ["${uiTestBuildVariant}/**"], excludes: fileFilter)
sourceDirectories.from = files([mainSrc])
classDirectories.from = files([mainClasses, javaClasses])
executionData.from = uiBuildFileTree
}
/**
* Generates a JaCoCo Report that aggregates the test coverage for the unit tests and
* instrumentation tests. This will not have any task dependencies as the unit tests and
* instrumentation tests should already have been run.
*
* We use the original source code but for the classes we use a combination of the debug and
* release variants.
*
* - For the release variant we use all classes excluding the ui directory.
* - For the debug variant we ONLY use the ui directory.
*/
task jacocoCombinedUnitTestAndroidTestReport(type: JacocoReport) {
group = "Reporting"
description = "Generate Jacoco coverage reports for both UI and Unit Tests on the ${uiTestBuildVariantCapitalised} build."
reports {
xml.required = true
html.required = true
}
def mainSrc = "$project.projectDir/src/main/java"
def releaseKotlinClasses = fileTree(dir: "${buildDir}/tmp/kotlin-classes/${unitTestBuildVariant}", excludes: fileFilter + ["com/github/goldy1992/mp3player/client/ui**"])
def debugKotlinClasses = fileTree(dir: "${buildDir}/tmp/kotlin-classes/${uiTestBuildVariant}/com/github/goldy1992/mp3player/client/ui/", excludes:fileFilter)
def releaseJavaClasses = fileTree(dir: "${buildDir}/intermediates/javac/${unitTestBuildVariant}", excludes: fileFilter + ["com/github/goldy1992/mp3player/client/ui**"])
def debugJavaClasses = fileTree(dir: "${buildDir}/intermediates/javac/${uiTestBuildVariant}/com/github/goldy1992/mp3player/client/ui/",excludes: fileFilter)
def mainClasses = releaseKotlinClasses + debugKotlinClasses
def javaClasses = releaseJavaClasses + debugJavaClasses
sourceDirectories.from = files([mainSrc])
classDirectories.from = files([mainClasses, javaClasses])
executionData.from = unitBuildFileTree.plus(uiBuildFileTree)
}
}
private static List getBuildVariant(flavor, buildType) {
def buildVariant =
flavor.isBlank() ?
buildType : "${flavor}${buildType.capitalize()}"
[buildVariant, buildVariant.capitalize()]
}
static def getProductFlavor(applicableProductFlavor, productFlavors) {
def flavor = ''
if (!productFlavors?.isEmpty() && productFlavors.contains(applicableProductFlavor) ) {
flavor = applicableProductFlavor
}
return flavor
}