1 /*
<lambda>null2  * Copyright 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 androidx.build.checkapi
18 
19 import androidx.build.Version
20 import java.io.File
21 import org.junit.Assert.assertEquals
22 import org.junit.Rule
23 import org.junit.Test
24 import org.junit.rules.TemporaryFolder
25 
26 class CheckApiTest {
27     @Rule
28     @JvmField
29     val tmpFolder = TemporaryFolder()
30 
31     @Test
32     fun getRequiredCompatibilityApiFileFromDirTest() {
33         val apiDir = createTempDir("api", CORE_API_FILES)
34 
35         // If we haven't committed the current version's API surface to a file yet, use the most
36         // recent version that came before it.
37         assertEquals(
38             "1.1.0-beta02.txt",
39             getRequiredCompatibilityApiFileFromDir(
40                 apiDir,
41                 Version("1.1.0-beta03"),
42                 ApiType.CLASSAPI
43             )?.name
44         )
45 
46         // If we already committed the current version's API surface to a file, then we should
47         // consider that the finalized API surface against which compatibility is checked.
48         assertEquals(
49             "1.1.0-beta02.txt",
50             getRequiredCompatibilityApiFileFromDir(
51                 apiDir,
52                 Version("1.1.0-beta02"),
53                 ApiType.CLASSAPI
54             )?.name
55         )
56 
57         assertEquals(
58             "1.3.0-beta01.txt",
59             getRequiredCompatibilityApiFileFromDir(
60                 apiDir,
61                 Version("1.4.0-alpha01"),
62                 ApiType.CLASSAPI
63             )?.name
64         )
65     }
66 
67     @Suppress("SameParameterValue")
68     private fun createTempDir(dirName: String, fileNames: List<String>): File =
69         tmpFolder.newFolder(dirName).also { apiDir ->
70             fileNames.forEach { fileName ->
71                 File(apiDir, fileName).createNewFile()
72             }
73         }
74 }
75 
76 /**
77  * List of API files representing `androidx.core:core:1.3.0-beta01`.
78  */
79 private val CORE_API_FILES = listOf(
80     "1.1.0-beta01.txt",
81     "1.1.0-beta02.txt",
82     "1.1.0-rc01.txt",
83     "1.2.0-beta01.txt",
84     "1.2.0-beta02.txt",
85     "1.3.0-beta01.txt",
86     "api_lint.ignore",
87     "current.txt",
88     "public_plus_experimental_1.0.0.txt",
89     "public_plus_experimental_1.1.0-beta01.txt",
90     "public_plus_experimental_1.1.0-rc01.txt",
91     "public_plus_experimental_1.2.0-beta01.txt",
92     "public_plus_experimental_1.2.0-beta02.txt",
93     "public_plus_experimental_1.3.0-beta01.txt",
94     "res-1.1.0-beta01.txt",
95     "res-1.1.0-beta02.txt",
96     "res-1.1.0-rc01.txt",
97     "res-1.2.0-beta01.txt",
98     "res-1.2.0-beta02.txt",
99     "res-1.3.0-beta01.txt",
100     "res-current.txt",
101     "restricted_1.0.0.txt",
102     "restricted_1.1.0-beta01.txt",
103     "restricted_1.1.0-beta02.txt",
104     "restricted_1.1.0-rc01.txt",
105     "restricted_1.2.0-beta01.txt",
106     "restricted_1.2.0-beta02.txt",
107     "restricted_1.3.0-beta01.txt",
108     "restricted_current.txt"
109 )
110