xref: /aosp_15_r20/external/ksp/gradle-plugin/src/test/kotlin/com/google/devtools/ksp/gradle/testing/TestConfig.kt (revision af87fb4bb8e3042070d2a054e912924f599b22b7)
1 /*
<lambda>null2  * Copyright 2021 Google LLC
3  * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package com.google.devtools.ksp.gradle.testing
18 
19 import java.io.File
20 import java.util.*
21 
22 /**
23  * Test configuration passed down from the main KSP build.
24  * See the `prepareTestConfiguration` task in the build.gradle.kts file in the `gradle-plugin`.
25  */
26 data class TestConfig(
27     /**
28      * The root directory of the main KSP project
29      */
30     val kspProjectDir: File,
31     /**
32      * The classpath that can be used to load processors.
33      * The testing infra allows loading processors from the test classpath of the gradle-plugin.
34      * This classpath is the output of the test compilation in the main KSP project.
35      */
36     val processorClasspath: String,
37     /**
38      * The local maven repository that can be used while running tests
39      */
40     val mavenRepoDir: File,
41     /**
42      * The version of KSP.
43      */
44     val kspVersion: String
45 ) {
46     private val kspProjectProperties by lazy {
47         Properties().also { props ->
48             kspProjectDir.resolve("gradle.properties").inputStream().use {
49                 props.load(it)
50             }
51         }
52     }
53     val kotlinBaseVersion by lazy {
54         kspProjectProperties["kotlinBaseVersion"] as String
55     }
56 
57     val androidBaseVersion by lazy {
58         kspProjectProperties["agpBaseVersion"] as String
59     }
60 
61     val mavenRepoPath = mavenRepoDir.path.replace(File.separatorChar, '/')
62 
63     companion object {
64         /**
65          * Loads the test configuration from resources.
66          */
67         fun read(): TestConfig {
68             val props = Properties()
69             TestConfig::class.java.classLoader.getResourceAsStream("testprops.properties").use {
70                 props.load(it)
71             }
72             return TestConfig(
73                 kspProjectDir = File(props.get("kspProjectRootDir") as String),
74                 processorClasspath = props.get("processorClasspath") as String,
75                 mavenRepoDir = File(props.get("mavenRepoDir") as String),
76                 kspVersion = props.get("kspVersion") as String
77             )
78         }
79     }
80 }
81