xref: /aosp_15_r20/external/nullaway/buildSrc/src/main/groovy/nullaway.java-test-conventions.gradle (revision f50c306653bc89b8210ce6c9e0b0b44fc134bc03)
1/*
2 * Copyright (C) 2023. Uber Technologies
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *    http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17// Mostly taken from official Gradle sample: https://docs.gradle.org/current/samples/sample_jvm_multi_project_with_code_coverage.html
18plugins {
19    id 'jacoco'
20}
21
22jacoco {
23    toolVersion = "0.8.10"
24}
25
26// Do not generate reports for individual projects
27tasks.named("jacocoTestReport") {
28    enabled = false
29}
30
31// Share sources folder with other projects for aggregated JaCoCo reports
32configurations.create('transitiveSourcesElements') {
33    visible = false
34    canBeResolved = false
35    canBeConsumed = true
36    extendsFrom(configurations.implementation)
37    attributes {
38        attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage, Usage.JAVA_RUNTIME))
39        attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category, Category.DOCUMENTATION))
40        attribute(DocsType.DOCS_TYPE_ATTRIBUTE, objects.named(DocsType, 'source-folders'))
41    }
42    sourceSets.main.java.srcDirs.forEach {
43        outgoing.artifact(it)
44    }
45}
46
47
48test {
49    maxHeapSize = "1024m"
50    // to expose necessary JDK types on JDK 16+; see https://errorprone.info/docs/installation#java-9-and-newer
51    jvmArgs += [
52        "--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
53        "--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
54        "--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED",
55        "--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED",
56        "--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED",
57        "--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED",
58        "--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED",
59        "--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
60        "--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED",
61        "--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED",
62        // Accessed by Lombok tests
63        "--add-opens=jdk.compiler/com.sun.tools.javac.jvm=ALL-UNNAMED",
64    ]
65}
66
67// Create a task to test on JDK 21
68def testJdk21 = tasks.register("testJdk21", Test) {
69    onlyIf {
70        // Only test on JDK 21 when using the latest Error Prone version
71        deps.versions.errorProneApi == deps.versions.errorProneLatest
72    }
73    javaLauncher = javaToolchains.launcherFor {
74        languageVersion = JavaLanguageVersion.of(21)
75    }
76
77    description = "Runs the test suite on JDK 21"
78    group = LifecycleBasePlugin.VERIFICATION_GROUP
79
80    // Copy inputs from normal Test task.
81    def testTask = tasks.getByName("test")
82    classpath = testTask.classpath
83    testClassesDirs = testTask.testClassesDirs
84    maxHeapSize = "1024m"
85    // to expose necessary JDK types on JDK 16+; see https://errorprone.info/docs/installation#java-9-and-newer
86    jvmArgs += [
87        "--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED",
88        "--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED",
89        "--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED",
90        "--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED",
91        "--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED",
92        "--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED",
93        "--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED",
94        "--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED",
95        "--add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED",
96        "--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED",
97        // Accessed by Lombok tests
98        "--add-opens=jdk.compiler/com.sun.tools.javac.jvm=ALL-UNNAMED",
99    ]
100}
101
102tasks.named('check').configure {
103    dependsOn testJdk21
104}
105
106
107// Share the coverage data to be aggregated for the whole product
108configurations.create('coverageDataElements') {
109    visible = false
110    canBeResolved = false
111    canBeConsumed = true
112    extendsFrom(configurations.implementation)
113    attributes {
114        attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage, Usage.JAVA_RUNTIME))
115        attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category, Category.DOCUMENTATION))
116        attribute(DocsType.DOCS_TYPE_ATTRIBUTE, objects.named(DocsType, 'jacoco-coverage-data'))
117    }
118    // This will cause the test tasks to run if the coverage data is requested by the aggregation task
119    tasks.withType(Test).forEach {task ->
120        // tasks.named(task.name) is a hack to introduce the right task dependence in Gradle.  We will fix this
121        // correctly when addressing https://github.com/uber/NullAway/issues/871
122        outgoing.artifact(tasks.named(task.name).map { t -> t.extensions.getByType(JacocoTaskExtension).destinationFile })
123    }
124}
125