diff options
author | Chris Warrington <cmw@google.com> | 2022-02-04 17:12:23 +0000 |
---|---|---|
committer | Chris Warrington <cmw@google.com> | 2022-02-07 13:31:22 +0000 |
commit | 413cb696aaad255d0ac529946ea26c87cbe00e82 (patch) | |
tree | 5c26e849eebcc627ada3a86715df49a33ce87072 | |
parent | 0fcbfb83e54a1bc68d087f7d81d6e5a27a2b5f10 (diff) | |
download | data-binding-413cb696aaad255d0ac529946ea26c87cbe00e82.tar.gz |
Non-transitive R classes: Allow empty dependencies
It is possible to have a library with no dependencies, and in the
process of serialization of compiler arguments the difference between
empty string (serializing an empty list) and null is lost.
Just relax the assertion here, a reference to a resource that doesn't
exist will still be caught by the compiler.
Bug: 216667498
Test: Added unit test
Change-Id: Iaeb8494de85e9356ea68134804f9e92f2ba754c8
-rw-r--r-- | BUILD.bazel | 1 | ||||
-rw-r--r-- | compiler/build.gradle | 1 | ||||
-rw-r--r-- | compiler/src/main/java/android/databinding/tool/util/SymbolTableUtil.kt | 16 | ||||
-rw-r--r-- | compiler/src/test/java/android/databinding/tool/util/SymbolTableUtilTest.kt | 26 |
4 files changed, 32 insertions, 12 deletions
diff --git a/BUILD.bazel b/BUILD.bazel index 6670d54a..b4022c82 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -314,6 +314,7 @@ kotlin_test( "@maven//:com.google.truth.truth", "@maven//:commons-io.commons-io", "@maven//:junit.junit", + "@maven//:org.jetbrains.kotlin.kotlin-test", "@maven//:org.mockito.mockito-core", ], ) diff --git a/compiler/build.gradle b/compiler/build.gradle index e744f865..be805379 100644 --- a/compiler/build.gradle +++ b/compiler/build.gradle @@ -48,6 +48,7 @@ dependencies { implementation libs.juniversalchardet implementation libs.jaxb_runtime testImplementation libs.junit + testImplementation libs.kotlin_test testImplementation libs.mockito_core testImplementation libs.compile_testing testImplementation libs.truth diff --git a/compiler/src/main/java/android/databinding/tool/util/SymbolTableUtil.kt b/compiler/src/main/java/android/databinding/tool/util/SymbolTableUtil.kt index ab57d62e..1b6a4481 100644 --- a/compiler/src/main/java/android/databinding/tool/util/SymbolTableUtil.kt +++ b/compiler/src/main/java/android/databinding/tool/util/SymbolTableUtil.kt @@ -84,18 +84,10 @@ fun parseRTxtFiles( val symbolTables = ImmutableList.builder<SymbolTable>() // local resources at the front of the list symbolTables.add(parseLocalRTxt(localRFile)) - when { - dependenciesRFiles != null -> { - // then add the rest of the dependencies, in order - dependenciesRFiles.forEach { symbolTables.add(parsePackageAwareRTxt(it)) } - } - mergedDependenciesRFile != null -> { - parseMergedPackageAwareRTxt(mergedDependenciesRFile, symbolTables) - } - else -> { - error("Unexpected error: Missing dependency resources") - } - } + + // then add the rest of the dependencies, in order + dependenciesRFiles?.forEach { symbolTables.add(parsePackageAwareRTxt(it)) } + mergedDependenciesRFile?.let { parseMergedPackageAwareRTxt(it, symbolTables) } return Resources(symbolTables.build()) } diff --git a/compiler/src/test/java/android/databinding/tool/util/SymbolTableUtilTest.kt b/compiler/src/test/java/android/databinding/tool/util/SymbolTableUtilTest.kt index f33738c6..2c23649c 100644 --- a/compiler/src/test/java/android/databinding/tool/util/SymbolTableUtilTest.kt +++ b/compiler/src/test/java/android/databinding/tool/util/SymbolTableUtilTest.kt @@ -17,11 +17,13 @@ package android.databinding.tool.util import com.google.common.collect.ImmutableList +import com.google.common.truth.Truth.assertThat import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue import org.junit.Test import java.lang.IllegalStateException import java.nio.file.Files +import kotlin.test.assertFailsWith class SymbolTableUtilTest { @@ -118,6 +120,30 @@ class SymbolTableUtilTest { } @Test + fun testParsingWithEmptyDependencies() { + val localFile = Files.createTempFile("local", "R.txt") + Files.write( + localFile, + """ + // This is a comment expected in package-aware R.txt + local + string first + """.trimIndent().toByteArray()) + val result = + parseRTxtFiles(localFile.toFile(), null, null) + + assertEquals(result.symbolTables!!.size, 1) + assertEquals(result.symbolTables!![0].rPackage, "") + + assertEquals(result.getRPackagePrefix(null, "string", "first"), "") + val failure = assertFailsWith<Exception> { + result.getRPackagePrefix(null, "string", "second") + } + assertThat(failure).hasMessageThat().contains("not found") + assertEquals(result.getRPackagePrefix("android", "string", "not_found"), "android.") + } + + @Test fun testParsingMergedDependencies() { val mergedR = Files.createTempFile("merged", "R.txt") Files.write( |