summaryrefslogtreecommitdiff
path: root/compilationTests/src/test/java/androidx/databinding/compilationTest/DataBindingCompilationTestCase.kt
blob: 8258ef9c3aba2b683a1a24cdc126aeebbe99bc12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package androidx.databinding.compilationTest

import android.databinding.tool.processing.ScopedErrorReport
import android.databinding.tool.store.Location
import com.android.testutils.TestUtils
import com.android.tools.analytics.Environment
import com.android.tools.analytics.EnvironmentFakes
import com.android.tools.idea.gradle.project.build.invoker.GradleBuildInvoker
import com.android.tools.idea.testing.AndroidGradleTestCase
import com.android.tools.idea.testing.TestProjectPaths
import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskId
import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotificationListenerAdapter
import com.intellij.openapi.util.io.FileUtil.toSystemDependentName
import com.intellij.util.io.createDirectories
import com.intellij.util.io.readText
import org.junit.After
import org.junit.Assert
import org.junit.Before
import org.junit.Rule
import org.junit.rules.TemporaryFolder
import java.io.File
import java.io.IOException
import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.util.regex.Pattern

private const val TEST_DEPENDENCIES = "implementation 'androidx.fragment:fragment:+'"
private const val DEFAULT_SETTINGS_GRADLE = "include ':app'"

private const val TEST_DATA_PATH = "tools/data-binding/compilationTests/testData"

private val pattern: Pattern = Pattern.compile("!@\\{([A-Za-z0-9_-]*)}")

const val KEY_NAMESPACE = "NAMESPACE"
const val KEY_DEPENDENCIES = "DEPENDENCIES"
const val KEY_SETTINGS_INCLUDES = "SETTINGS_INCLUDES"
const val DEFAULT_APP_PACKAGE = "com.android.databinding.compilationTest.test"

abstract class DataBindingCompilationTestCase : AndroidGradleTestCase() {
    @get:Rule
    val temporaryFolder = TemporaryFolder()

    @Before
    fun setup() {
        EnvironmentFakes.setSingleProperty(
            Environment.EnvironmentVariable.ANDROID_PREFS_ROOT.key,
            temporaryFolder.newFolder().absolutePath
        )
    }

    @After
    fun restoreSystemProperty() {
        EnvironmentFakes.setSystemEnvironment()
    }

    protected fun loadApp() {
        loadApp(emptyMap())
    }

    protected fun loadApp(appReplacements: Map<String, String>) {
        loadProject(TestProjectPaths.DATA_BINDING_COMPILATION)
        projectRoot.toPath().resolve("app/src/main").createDirectories()
        val replacements = appendTestReplacements(appReplacements)
        copyTestDataWithReplacement(
            "AndroidManifest.xml",
            "app/src/main/AndroidManifest.xml",
            replacements
        )
        copyTestDataWithReplacement(
            "app_build.gradle",
            "app/build.gradle",
            replacements
        )
        copyTestDataWithReplacement(
            "settings.gradle",
             "settings.gradle",
            replacements
        )
    }

    protected fun loadModule(moduleName: String, moduleReplacements: Map<String, String>) {
        val replacements = appendTestReplacements(moduleReplacements)
        copyTestDataWithReplacement(
            "AndroidManifest.xml",
            "${moduleName}/src/main/AndroidManifest.xml",
            replacements
        )
        copyTestDataWithReplacement(
            "module_build.gradle",
            "${moduleName}/build.gradle",
            replacements
        )
    }

    protected fun assembleDebug() = invokeTasks(listOf("assembleDebug"))

    protected fun invokeTasks(
        tasks: List<String>,
        args: List<String> = emptyList()
    ): CompilationResult {
        val outBuilder = StringBuilder()
        val errBuilder = StringBuilder()
        val taskListener = object : ExternalSystemTaskNotificationListenerAdapter() {
            override fun onTaskOutput(id: ExternalSystemTaskId, text: String, stdOut: Boolean) {
                if (stdOut) {
                    outBuilder.append(text)
                } else {
                    errBuilder.append(text)
                }
            }
        }
        val request =
            GradleBuildInvoker.Request.builder(
                project,
                File(toSystemDependentName(project.basePath!!)),
                tasks
            )
                .setCommandLineArguments(listOf("--offline") + args)
                .setListener(taskListener)
                .build()
        val result = invokeGradle(project) { gradleInvoker ->
            gradleInvoker.executeTasks(request)
        }
        return CompilationResult(
            if (result.isBuildSuccessful) 0 else 1,
            outBuilder.toString(),
            errBuilder.toString()
        )
    }

    protected val projectRoot: File
        get() = File(toSystemDependentName(project.basePath!!))

    /**
     * Copies the file in the testData directory to the target directory.
     *
     * [source] and [target] are relative to testData and projectRoot respectively.
     */
    protected fun copyTestData(source: String, target: String) {
        val sourcePath = TestUtils.resolveWorkspacePath(TEST_DATA_PATH).resolve(source)
        val targetPath = File(projectRoot, target).toPath()
        targetPath.parent.createDirectories()
        Files.copy(sourcePath, targetPath)
    }

    protected fun copyTestDataWithReplacement(
        source: String,
        target: String,
        replacements: Map<String, String> = emptyMap()
    ) {
        val sourcePath = TestUtils.resolveWorkspacePath(TEST_DATA_PATH).resolve(source)
        val targetPath = File(projectRoot, target)
        val contents = sourcePath.readText()
        val out = StringBuilder(contents.length)
        val matcher = pattern.matcher(contents)
        var location = 0
        while (matcher.find()) {
            val start = matcher.start()
            if (start > location) {
                out.append(contents, location, start)
            }
            val key = matcher.group(1)
            val replacement = replacements[key]
            if (replacement != null) {
                out.append(replacement)
            }
            location = matcher.end()
        }
        if (location < contents.length) {
            out.append(contents, location, contents.length)
        }
        targetPath.parentFile.mkdirs()
        targetPath.writeText(out.toString(), StandardCharsets.UTF_8)
    }

    /**
     * Custom logic that replaces or appends to the replacement values
     * depending on the key.
     *
     * If key is [KEY_DEPENDENCIES], then append the replacement.
     * If key is [KEY_NAMESPACE] or [KEY_SETTINGS_INCLUDES],
     *   then put if value doesn't already exist.
     */
    private fun appendTestReplacements(
        map: Map<String, String>
    ): Map<String, String> {
        val mutableMap = map.toMutableMap()
        if (mutableMap.containsKey(KEY_DEPENDENCIES)) {
            mutableMap[KEY_DEPENDENCIES] += "\n$TEST_DEPENDENCIES"
        } else {
            mutableMap[KEY_DEPENDENCIES] = TEST_DEPENDENCIES
        }
        if (!mutableMap.containsKey(KEY_NAMESPACE)) {
            mutableMap[KEY_NAMESPACE] = DEFAULT_APP_PACKAGE
        }
        if (!mutableMap.containsKey(KEY_SETTINGS_INCLUDES)) {
            mutableMap[KEY_SETTINGS_INCLUDES] = DEFAULT_SETTINGS_GRADLE
        }
        return mutableMap
    }

    /**
     * Finds the error file referenced in the given error report.
     * Handles possibly relative paths.
     *
     * Throws an assertion exception if the error file reported cannot be found.
     */
    protected fun requireErrorFile(report: ScopedErrorReport): File {
        val path = report.filePath
        Assert.assertNotNull(path)
        var file = File(path)
        if (file.exists()) {
            return file
        }
        // might be relative, try in test project folder
        file = File(projectRoot, path)
        Assert.assertTrue("required error file is missing in " + file.absolutePath, file.exists())
        return file
    }

    /**
     * Extracts the text in the given location from the file at the given application path.
     *
     * @param relativePath the relative path of the file to be extracted from
     * @param location  The location to extract
     * @return The string that is contained in the given location
     * @throws IOException If file is invalid.
     */
    protected fun extract(relativePath: String, location: Location): String {
        val file = File(projectRoot, relativePath)
        Assert.assertTrue(file.exists())
        val result = StringBuilder()
        val lines = file.readLines(StandardCharsets.UTF_8)
        for (i in location.startLine..location.endLine) {
            if (i > location.startLine) {
                result.append("\n")
            }
            val line = lines[i]
            var start = 0
            if (i == location.startLine) {
                start = location.startOffset
            }
            var end = line.length - 1 // inclusive
            if (i == location.endLine) {
                end = location.endOffset
            }
            result.append(line.substring(start, end + 1))
        }
        return result.toString()
    }

    protected fun writeFile(path: String, contents: String) {
        val targetFile = File(projectRoot, path)
        targetFile.parentFile.mkdirs()
        targetFile.writeText(contents, StandardCharsets.UTF_8)
    }
}