• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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.testing
18 
19 import com.android.tools.lint.checks.infrastructure.TestFile
20 import java.io.File
21 import java.nio.file.Files
22 
23 object KnownJarFiles {
24     /**
25      * The jar produced by the `:stub-annotations` project.
26      *
27      * This must only be access from a project that depends on `:stub-annotations` and sets the
28      * `METALAVA_STUB_ANNOTATIONS_JAR` environment variable to the jar produced by the project.
29      */
<lambda>null30     val stubAnnotationsJar by lazy {
31         val envValue =
32             System.getenv("METALAVA_STUB_ANNOTATIONS_JAR")
33                 ?: error("Environment variable METALAVA_STUB_ANNOTATIONS_JAR was not set")
34         require(envValue.isNotBlank()) {
35             "Invalid environment variable METALAVA_STUB_ANNOTATIONS_JAR: '$envValue'"
36         }
37         val jar = File(envValue)
38         require(jar.isFile) { "stub-annotations jar not found: $jar" }
39         jar
40     }
41 
42     /** The jar produced by the `:stub-annotations` project, exposed as a [TestFile]. */
<lambda>null43     val stubAnnotationsTestFile: TestFile by lazy {
44         ExistingFile(stubAnnotationsJar).to("known-jar-files/stub-annotations.jar")
45     }
46 }
47 
48 /** A simple [TestFile] that just uses an existing file by creating a symbolic link to it. */
49 private class ExistingFile(private val file: File) : TestFile() {
createFilenull50     override fun createFile(targetDir: File): File {
51         val link = targetDir.resolve(targetPath)
52         link.parentFile.mkdirs()
53         Files.createSymbolicLink(link.toPath(), file.toPath())
54         return link
55     }
56 }
57 
58 /**
59  * Wrap an existing file in a [TestFile].
60  *
61  * By default, this will create a symbolic link in the `targetDir` passed to [TestFile.createFile]
62  * called [File.getName].
63  */
toTestFilenull64 fun File.toTestFile(): TestFile = ExistingFile(this).to(name)
65