Unverified Commit 4c768473 authored by david-perez's avatar david-perez Committed by GitHub
Browse files

Enclose missing Rust template parameters in backticks in the exception message (#1492)

* Enclose missing Rust template parameters in backticks in the exception message

* appease ktlint

* ./gradlew ktlintFormat
parent e23790e1
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -167,9 +167,10 @@ private fun transformTemplate(template: String, scope: Array<out Pair<String, An
        val templateType = matchResult.groupValues[2].ifEmpty { ":T" }
        if (!scope.toMap().keys.contains(keyName)) {
            throw CodegenException(
                "Rust block template expected `$keyName` but was not present in template.\n  hint: Template contains: ${
                    scope.map { it.first }
                }",
                """
                Rust block template expected `$keyName` but was not present in template.
                Hint: Template contains: ${scope.map { "`${it.first}`" }}
                """.trimIndent(),
            )
        }
        "#{${keyName.lowercase()}$templateType}"
+19 −0
Original line number Diff line number Diff line
@@ -10,6 +10,8 @@ import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldContain
import io.kotest.matchers.string.shouldContainOnlyOnce
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import software.amazon.smithy.codegen.core.CodegenException
import software.amazon.smithy.model.Model
import software.amazon.smithy.model.shapes.SetShape
import software.amazon.smithy.model.shapes.StringShape
@@ -161,6 +163,23 @@ class RustWriterTest {
        sut.toString().shouldContain("inner: hello, regular: http::foo")
    }

    @Test
    fun `missing template parameters are enclosed in backticks in the exception message`() {
        val sut = RustWriter.forModule("lib")
        val exception = assertThrows<CodegenException> {
            sut.rustTemplate(
                "#{Foo} #{Bar}",
                "Foo Bar" to CargoDependency.Http.toType().resolve("foo"),
                "Baz" to CargoDependency.Http.toType().resolve("foo"),
            )
        }
        exception.message shouldBe
            """
            Rust block template expected `Foo` but was not present in template.
            Hint: Template contains: [`Foo Bar`, `Baz`]
            """.trimIndent()
    }

    @Test
    fun `can handle file paths properly when determining module`() {
        val sut = RustWriter.forModule("src/module_name")