1 /*
2  * Copyright (C) 2024 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 kotlin.test.assertEquals
20 import kotlin.test.assertFalse
21 import kotlin.test.assertTrue
22 import org.junit.Rule
23 import org.junit.Test
24 import org.junit.rules.TemporaryFolder
25 
26 class MutableBaselineFileTest {
27 
28     @get:Rule val temporaryFolder = TemporaryFolder()
29 
30     @Test
Update filenull31     fun `Update file`() {
32         val projectDir = temporaryFolder.newFolder("project")
33         val file = projectDir.resolve(PROJECT_BASELINE_FILE)
34         val contents =
35             """
36                 Class1
37                   Method1
38 
39                 Class2
40                   Method1
41                   Method2
42 
43             """
44                 .trimIndent()
45 
46         file.parentFile.mkdirs()
47         file.writeText(contents)
48 
49         val baseline = BaselineFile.forProject(projectDir, RESOURCE_PATH)
50 
51         // Clear the file.
52         file.writeText("")
53         assertEquals("", file.readText(), message = "cleared file")
54 
55         // Write the baseline back unmodified, should be the same as
56         baseline.write()
57         assertEquals(contents, file.readText(), message = "round trip")
58 
59         // Modify the baseline
60         baseline.removeExpectedFailure("Class1", "Method1")
61         baseline.addExpectedFailure("Class2", "Method1")
62         baseline.addExpectedFailure("Class3", "Method3")
63         baseline.addExpectedFailure("Class3", "Method1")
64         baseline.write()
65         val expected =
66             """
67                 Class2
68                   Method1
69                   Method2
70 
71                 Class3
72                   Method1
73                   Method3
74 
75             """
76                 .trimIndent()
77 
78         assertEquals(expected, file.readText(), message = "updated")
79     }
80 
81     @Test
Write emptynull82     fun `Write empty`() {
83         val projectDir = temporaryFolder.newFolder("project")
84         val file = projectDir.resolve(PROJECT_BASELINE_FILE)
85         file.parentFile.mkdirs()
86         file.writeText("")
87 
88         val baseline = BaselineFile.forProject(projectDir, RESOURCE_PATH)
89         baseline.write()
90 
91         assertFalse(
92             file.exists(),
93             message = "baseline file has not been deleted even though it is empty"
94         )
95     }
96 
97     @Test
Write empty after removing last failurenull98     fun `Write empty after removing last failure`() {
99         val projectDir = temporaryFolder.newFolder("project")
100         val file = projectDir.resolve(PROJECT_BASELINE_FILE)
101         file.parentFile.mkdirs()
102         file.writeText("Class\n  Method\n")
103 
104         val baseline = BaselineFile.forProject(projectDir, RESOURCE_PATH)
105         baseline.removeExpectedFailure("Class", "Method")
106         baseline.write()
107 
108         assertFalse(
109             file.exists(),
110             message = "baseline file has not been deleted even though it is now empty"
111         )
112     }
113 
114     @Test
Read resource filenull115     fun `Read resource file`() {
116         val baseline = BaselineFile.fromResource(RESOURCE_PATH)
117 
118         assertTrue(baseline.isExpectedFailure("Class1", "Method1"), message = "Class1/Method1")
119         assertFalse(baseline.isExpectedFailure("Class1", "Method2"), message = "Class1/Method2")
120         assertTrue(baseline.isExpectedFailure("Class2", "Method1"), message = "Class2/Method1")
121         assertFalse(baseline.isExpectedFailure("Class3", "Method1"), message = "Class3/Method1")
122     }
123 
124     companion object {
125         private const val RESOURCE_PATH = "baseline-for-testing.txt"
126         private const val PROJECT_BASELINE_FILE = "src/test/resources/$RESOURCE_PATH"
127     }
128 }
129