1 /*
<lambda>null2 * Copyright (C) 2021 The Android Open Source Project
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 package com.android.tools.metalava.model.psi
18
19 import com.android.SdkConstants
20 import com.android.tools.lint.UastEnvironment
21 import com.android.tools.lint.checks.infrastructure.TestFile
22 import com.android.tools.metalava.ARG_CLASS_PATH
23 import com.android.tools.metalava.DriverTest
24 import com.android.tools.metalava.ENV_VAR_METALAVA_TESTS_RUNNING
25 import com.android.tools.metalava.Options
26 import com.android.tools.metalava.findKotlinStdlibPathArgs
27 import com.android.tools.metalava.options
28 import com.android.tools.metalava.parseSources
29 import com.android.tools.metalava.tempDirectory
30 import com.intellij.openapi.util.Disposer
31 import java.io.File
32 import kotlin.test.assertNotNull
33
34 inline fun testCodebase(
35 vararg sources: TestFile,
36 action: (PsiBasedCodebase) -> Unit
37 ) {
38 tempDirectory { tempDirectory ->
39 val codebase = createTestCodebase(tempDirectory, *sources)
40 try {
41 action(codebase)
42 } finally {
43 destroyTestCodebase(codebase)
44 }
45 }
46 }
47
createTestCodebasenull48 fun createTestCodebase(
49 directory: File,
50 vararg sources: TestFile
51 ): PsiBasedCodebase {
52 System.setProperty(ENV_VAR_METALAVA_TESTS_RUNNING, SdkConstants.VALUE_TRUE)
53 Disposer.setDebugMode(true)
54
55 val sourcePaths = sources.map { it.targetPath }.toTypedArray()
56 val args = findKotlinStdlibPathArgs(sourcePaths) + arrayOf(
57 ARG_CLASS_PATH,
58 DriverTest.getAndroidJar().path
59 )
60 options = Options(args)
61
62 return parseSources(
63 sources = sources.map { it.createFile(directory) },
64 description = "Test Codebase",
65 )
66 }
67
destroyTestCodebasenull68 fun destroyTestCodebase(codebase: PsiBasedCodebase) {
69 codebase.dispose()
70
71 UastEnvironment.disposeApplicationEnvironment()
72 Disposer.assertIsEmpty(true)
73 }
74
PsiBasedCodebasenull75 fun PsiBasedCodebase.assertClass(qualifiedName: String): PsiClassItem {
76 val classItem = this.findClass(qualifiedName)
77 assertNotNull(classItem) { "Expected $qualifiedName to be defined" }
78 return classItem
79 }
80