Fix that const definitions referencing imported consts cannot be loaded (#551)

* We should check the expectedPath against the thrift file name, instead of the full path, which might contain directories.

* Addressing the comment for the test case.

* Use an existing API of the Location class.

* Remove unintentional format change.
This commit is contained in:
CoolTomatos 2024-06-11 20:49:46 +02:00 коммит произвёл GitHub
Родитель 3c6d80877a
Коммит ef9b63c8af
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
2 изменённых файлов: 43 добавлений и 3 удалений

Просмотреть файл

@ -443,10 +443,9 @@ internal class Linker(
if (ix != -1) {
val includeName = symbol.substring(0, ix)
val qualifiedName = symbol.substring(ix + 1)
val expectedPath = "$includeName.thrift"
constant = program.includes
.asSequence()
.filter { p -> p.location.path == expectedPath } // TODO: Should this be ==, or endsWith?
.filter { p -> p.location.programName == includeName }
.mapNotNull { p -> p.constantMap[qualifiedName] }
.firstOrNull()
}

Просмотреть файл

@ -264,6 +264,47 @@ class LoaderTest {
enum.location.path shouldBe listOf("nested", "a.thrift").joinToString(File.separator)
}
@Test
fun crazyIncludeReferencedConst() {
val nestedDir = File(tempDir, "nested").apply { mkdir() }
val fileNestedAThrift = File(nestedDir, "a.thrift")
val fileAnotherAThrift = File(tempDir, "another_a.thrift")
val fileBThrift = File(tempDir, "b.thrift")
fileNestedAThrift.writeText(
"""
namespace java com.microsoft.thrifty.test.crazyIncludeReferencedConst
const string HELLO = "hello"
"""
)
fileAnotherAThrift.writeText(
"""
namespace java com.microsoft.thrifty.test.crazyIncludeReferencedConst
const string HELLO = "actually goodbye"
"""
)
fileBThrift.writeText(
"""
include 'another_a.thrift'
include 'nested/a.thrift'
namespace java com.microsoft.thrifty.test.crazyIncludeReferencedConst
const string HELLO_AGAIN = a.HELLO
"""
)
val loader = Loader()
loader.addIncludePath(tempDir.toPath())
val schema = loader.load()
val helloAgain = schema.constants.single { const -> const.name == "HELLO_AGAIN" }
val referencedConstant = helloAgain.referencedConstants.single()
referencedConstant.location.path shouldBe listOf("nested", "a.thrift").joinToString(File.separator)
}
@Test
fun relativeIncludesConsiderIncludingFileLocation() {
val thriftDir = File(tempDir, "thrift").apply { mkdir() }
@ -1370,4 +1411,4 @@ class LoaderTest {
name
}
}
}
}