Skip to content

Conversation

@brentwatson
Copy link

The internals of compileAnvil contain a KotlinCompilation instance which provides a parameter for the kotlin language version. This PR is simply surfacing access to set this param from compileAnvil(...) so that other versions of Kotlin can be targeted in tests other than the default. Eg: allows specifying Kotlin 1.9 when using Kotlin 2.0

Example:

    // Run with Kotlin 2.0.XX
    @Test
    fun `target kotlin 1.9`() {
        compileAnvil(
            """
                package com.example
                import com.mine.InjectWith
                import com.mine.InitializedScope
                @InjectWith(InitializedScope::class)
                class MyTestActivity : FakeBaseActivity()
            """.trimIndent(),
            kotlinLanguageVersion = KOTLIN_1_9, // <-- Target Kotlin 1.9 when using Kotlin 2.0
            mode = Embedded(codeGenerators = listOf(InjectWithCodeGenerator())),
            workingDir = workingDirectory.root
        ) {
            if (exitCode != ExitCode.OK) {
                fail(messages)
            }
            assertThat(classLoader.loadClass("com.example.MyTestActivityAnvilInjector").createInstance(
                MembersInjector<Any> { }
            )).isNotNull()
        }
    }

This also provides a workaround for languageVersion.set(KOTLIN_1_9) workarounds for Anvil when using Kotlin 2.0, in which case the above test fail and you need to use AnvilCompilation directly.

@RBusarow RBusarow force-pushed the anvil-language-version-param branch from 2cf6794 to b042abd Compare November 15, 2024 20:12
@RBusarow RBusarow enabled auto-merge (rebase) November 15, 2024 20:12
@RBusarow RBusarow merged commit 8edbf3b into square:main Nov 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants