1 /*
<lambda>null2 * Copyright (C) 2023 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.text
18
19 import com.android.tools.metalava.model.api.surface.ApiSurfaces
20 import com.android.tools.metalava.model.api.surface.ApiVariantType
21 import java.io.File
22 import org.junit.Assert.assertEquals
23
24 /** Verify that two signature files match. */
25 fun assertSignatureFilesMatch(
26 expected: String,
27 actual: String,
28 expectedFormat: FileFormat = FileFormat.LATEST,
29 message: String? = null
30 ) {
31 val expectedPrepared = prepareSignatureFileForTest(expected, expectedFormat)
32 val actualStripped = actual.stripBlankLines()
33 assertEquals(message, expectedPrepared, actualStripped)
34 }
35
<lambda>null36 fun String.stripBlankLines() = lines().filter { it.isNotBlank() }.joinToString("\n")
37
38 /** Strip comments, trim indent, and add a signature format version header if one is missing */
prepareSignatureFileForTestnull39 fun prepareSignatureFileForTest(expectedApi: String, format: FileFormat): String {
40 val header = format.header()
41
42 return expectedApi
43 .trimIndent()
44 .let { if (!it.startsWith(FileFormat.SIGNATURE_FORMAT_PREFIX)) header + it else it }
45 .trim()
46 }
47
48 /**
49 * Get the [ApiVariantType] for a test signature file with [name].
50 *
51 * If it contains "removed" then it will be [ApiVariantType.REMOVED] else it will be
52 * [ApiVariantType.CORE].
53 */
apiVariantTypeForTestSignatureFilenull54 fun apiVariantTypeForTestSignatureFile(name: String) =
55 when {
56 name.contains("removed") -> ApiVariantType.REMOVED
57 else -> ApiVariantType.CORE
58 }
59
60 /**
61 * Check if the test signature file with [name] is for the main API surface.
62 *
63 * If it contains "base" then it will return `false` as it is for the base API surface, otherwise it
64 * will return `true` as it is assumed it is for the main API surface.
65 */
isTestSignatureFileForMainApiSurfacenull66 private fun isTestSignatureFileForMainApiSurface(name: String) =
67 when {
68 name.contains("base") -> false
69 else -> true
70 }
71
72 /**
73 * Create a list of [SignatureFile]s from [files] suitable for testing.
74 *
75 * This extracts information from the file name as to the purpose of each [SignatureFile] as
76 * follows:
77 * * If the name contains `base` then it is assumed to be for the [ApiSurfaces.base], otherwise it
78 * is assumed to be for [ApiSurfaces.main].
79 * * If the name contains `removed` then it is assumed to be for the [ApiVariantType.REMOVED],
80 * otherwise it is assumed to be for [ApiVariantType.CORE].
81 */
SignatureFilenull82 fun SignatureFile.Companion.forTest(files: List<File>) =
83 fromFiles(
84 files,
85 apiVariantTypeChooser = { file -> apiVariantTypeForTestSignatureFile(file.name) },
filenull86 forMainApiSurfacePredicate = { _, file -> isTestSignatureFileForMainApiSurface(file.name) }
87 )
88