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