Skip to content

Commit

Permalink
Default to asserting that exitCode == OK in all internal tests
Browse files Browse the repository at this point in the history
The published `AnvilCompilation` logic gets a new
`expectExitCode: ExitCode? = null` parameter.
If a value is specified,
the assertion is done before executing the `block` lambda.

In our internal delegating `compile(...)`
functions, the parameter becomes non-nullable and defaults to `ExitCode.OK`.

This is done in follow-up to
[this](#934 (comment))
comment.
  • Loading branch information
RBusarow committed Apr 16, 2024
1 parent 41bc9d4 commit 6ba6161
Show file tree
Hide file tree
Showing 25 changed files with 301 additions and 241 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.squareup.anvil.compiler.internal.testing

import com.google.auto.value.processor.AutoAnnotationProcessor
import com.google.common.truth.Truth.assertWithMessage
import com.google.devtools.ksp.processing.SymbolProcessorProvider
import com.squareup.anvil.annotations.ExperimentalAnvilApi
import com.squareup.anvil.compiler.AnvilCommandLineProcessor
Expand Down Expand Up @@ -219,6 +220,7 @@ public class AnvilCompilation internal constructor(
*/
public fun compile(
@Language("kotlin") vararg sources: String,
expectExitCode: KotlinCompilation.ExitCode? = null,
block: JvmCompilationResult.() -> Unit = {},
): JvmCompilationResult {
checkNotCompiled()
Expand All @@ -229,7 +231,32 @@ public class AnvilCompilation internal constructor(
addSources(*sources)
isCompiled = true

return kotlinCompilation.compile().apply(block)
return kotlinCompilation.compile().apply {

if (exitCode != expectExitCode) {
when {
expectExitCode == null -> {
// No expected code, so no assertion to be made
}
expectExitCode == KotlinCompilation.ExitCode.OK -> {
assertWithMessage("Compilation failed unexpectedly\n\n$messages")
.that(exitCode)
.isEqualTo(KotlinCompilation.ExitCode.OK)
}
exitCode == KotlinCompilation.ExitCode.OK -> {
assertWithMessage("Compilation succeeded unexpectedly\n\n$messages")
.that(exitCode)
.isEqualTo(expectExitCode)
}
else -> {
assertWithMessage("Error code mismatch\n\n$messages")
.that(exitCode)
.isEqualTo(expectExitCode)
}
}
}
block()
}
}

public companion object {
Expand Down Expand Up @@ -269,6 +296,7 @@ public fun compileAnvil(
mode: AnvilCompilationMode = Embedded(emptyList()),
moduleName: String? = null,
jvmTarget: JvmTarget? = null,
expectExitCode: KotlinCompilation.ExitCode? = null,
block: JvmCompilationResult.() -> Unit = { },
): JvmCompilationResult {
return AnvilCompilation()
Expand Down Expand Up @@ -301,6 +329,9 @@ public fun compileAnvil(
trackSourceFiles = trackSourceFiles,
mode = mode,
)
.compile(*sources)
.compile(
*sources,
expectExitCode = expectExitCode,
)
.also(block)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.squareup.anvil.annotations.MergeComponent
import com.squareup.anvil.annotations.MergeSubcomponent
import com.squareup.anvil.annotations.compat.MergeInterfaces
import com.squareup.anvil.compiler.internal.testing.extends
import com.tschuchort.compiletesting.KotlinCompilation.ExitCode
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
Expand Down Expand Up @@ -44,8 +45,8 @@ class InterfaceMergerRepeatableTest(
$annotation(Any::class)
interface ComponentInterface
""",
expectExitCode = ExitCode.COMPILATION_ERROR,
) {
assertThat(exitCode).isError()
assertThat(messages).contains(
"com.squareup.test.ComponentInterface merges multiple times to the same scope: [Any]. " +
"Merging multiple times to the same scope is forbidden and all scopes must be distinct.",
Expand All @@ -67,8 +68,8 @@ class InterfaceMergerRepeatableTest(
@MergeSubcomponent(Unit::class)
interface ComponentInterface
""",
expectExitCode = ExitCode.COMPILATION_ERROR,
) {
assertThat(exitCode).isError()
assertThat(messages).contains(
"It's only allowed to have one single type of @Merge* annotation, however multiple " +
"instances of the same annotation are allowed. You mix " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.squareup.anvil.annotations.MergeSubcomponent
import com.squareup.anvil.annotations.compat.MergeInterfaces
import com.squareup.anvil.compiler.internal.testing.AnvilCompilation
import com.squareup.anvil.compiler.internal.testing.extends
import com.tschuchort.compiletesting.KotlinCompilation.ExitCode
import com.tschuchort.compiletesting.KotlinCompilation.ExitCode.OK
import org.junit.Test
import org.junit.runner.RunWith
Expand Down Expand Up @@ -147,8 +148,8 @@ class InterfaceMergerTest(
$annotation(Any::class)
abstract class MergingClass
""",
expectExitCode = ExitCode.COMPILATION_ERROR,
) {
assertThat(exitCode).isError()
// Position to the class.
assertThat(messages).contains("Source0.kt:6:16")
}
Expand Down Expand Up @@ -200,8 +201,8 @@ class InterfaceMergerTest(
$annotation(Any::class)
interface ComponentInterface
""",
expectExitCode = ExitCode.COMPILATION_ERROR,
) {
assertThat(exitCode).isError()
// Position to the class. Unfortunately, a different error is reported that the class is
// missing an @Module annotation.
assertThat(messages).contains("Source0.kt:7:7")
Expand Down Expand Up @@ -229,8 +230,8 @@ class InterfaceMergerTest(
$annotation(Any::class)
interface ComponentInterface
""",
expectExitCode = ExitCode.COMPILATION_ERROR,
) {
assertThat(exitCode).isError()
// Position to the class. Unfortunately, a different error is reported that the class is
// missing an @Module annotation.
assertThat(messages).contains("Source0.kt:14:11")
Expand Down Expand Up @@ -318,8 +319,8 @@ class InterfaceMergerTest(
)
interface ComponentInterface
""",
expectExitCode = ExitCode.COMPILATION_ERROR,
) {
assertThat(exitCode).isError()
// Position to the class.
assertThat(messages).contains("Source0.kt:18:11")
assertThat(messages).contains(
Expand Down Expand Up @@ -355,8 +356,8 @@ class InterfaceMergerTest(
)
interface ComponentInterface : ContributingInterface, OtherInterface
""",
expectExitCode = ExitCode.COMPILATION_ERROR,
) {
assertThat(exitCode).isError()
// Position to the class.
assertThat(messages).contains(
"ComponentInterface excludes types that it implements or extends. These types cannot " +
Expand Down Expand Up @@ -449,8 +450,8 @@ class InterfaceMergerTest(
$annotation(Any::class)
interface ComponentInterface
""",
expectExitCode = ExitCode.COMPILATION_ERROR,
) {
assertThat(exitCode).isError()
// Position to the class.
assertThat(messages).contains("Source0.kt:7")
}
Expand Down Expand Up @@ -721,8 +722,8 @@ class InterfaceMergerTest(
$annotation(Int::class)
interface SubcomponentInterface2
""",
expectExitCode = ExitCode.COMPILATION_ERROR,
) {
assertThat(exitCode).isError()
assertThat(messages).contains(
"com.squareup.test.SecondContributingInterface with scopes [kotlin.Int] wants to " +
"replace com.squareup.test.ContributingInterface, but the replaced class isn't " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.google.common.truth.Truth.assertThat
import com.squareup.anvil.annotations.compat.MergeModules
import com.squareup.anvil.compiler.internal.testing.daggerModule
import com.squareup.anvil.compiler.internal.testing.withoutAnvilModules
import com.tschuchort.compiletesting.KotlinCompilation.ExitCode
import org.junit.Test

class MergeModulesTest {
Expand Down Expand Up @@ -84,8 +85,8 @@ class MergeModulesTest {
@dagger.Module
class DaggerModule1
""",
expectExitCode = ExitCode.COMPILATION_ERROR,
) {
assertThat(exitCode).isError()
// Position to the class.
assertThat(messages).contains("Source0.kt:7:7")
}
Expand Down Expand Up @@ -153,8 +154,8 @@ class MergeModulesTest {
@MergeModules(Any::class)
class DaggerModule1
""",
expectExitCode = ExitCode.COMPILATION_ERROR,
) {
assertThat(exitCode).isError()
// Position to the class.
assertThat(messages).contains("Source0.kt:7:16")
}
Expand Down Expand Up @@ -284,8 +285,8 @@ class MergeModulesTest {
@MergeModules(Any::class)
class DaggerModule1
""",
expectExitCode = ExitCode.COMPILATION_ERROR,
) {
assertThat(exitCode).isError()
// Position to the class.
assertThat(messages).contains("Source0.kt:17:16")
assertThat(messages).contains(
Expand Down Expand Up @@ -320,8 +321,8 @@ class MergeModulesTest {
@MergeModules(Any::class)
class DaggerModule1
""",
expectExitCode = ExitCode.COMPILATION_ERROR,
) {
assertThat(exitCode).isError()
// Position to the class.
assertThat(messages).contains("Source0.kt:17:16")
assertThat(messages).contains(
Expand Down Expand Up @@ -424,8 +425,8 @@ class MergeModulesTest {
@MergeModules(Any::class)
class DaggerModule1
""",
expectExitCode = ExitCode.COMPILATION_ERROR,
) {
assertThat(exitCode).isError()
// Position to the class.
assertThat(messages).contains("Source0.kt:17:11")
assertThat(messages).contains(
Expand Down Expand Up @@ -460,8 +461,8 @@ class MergeModulesTest {
@MergeModules(Any::class)
class DaggerModule1
""",
expectExitCode = ExitCode.COMPILATION_ERROR,
) {
assertThat(exitCode).isError()
// Position to the class.
assertThat(messages).contains("Source0.kt:17:11")
assertThat(messages).contains(
Expand Down Expand Up @@ -492,8 +493,8 @@ class MergeModulesTest {
@MergeModules(Any::class)
class DaggerModule1
""",
expectExitCode = ExitCode.COMPILATION_ERROR,
) {
assertThat(exitCode).isError()
// Position to the class.
assertThat(messages).contains("Source0.kt:13:16")
}
Expand Down Expand Up @@ -521,8 +522,8 @@ class MergeModulesTest {
@MergeModules(Any::class)
class DaggerModule1
""",
expectExitCode = ExitCode.COMPILATION_ERROR,
) {
assertThat(exitCode).isError()
// Position to the class.
assertThat(messages).contains("Source0.kt:15:16")
assertThat(messages).contains(
Expand Down Expand Up @@ -616,8 +617,8 @@ class MergeModulesTest {
)
class DaggerModule1
""",
expectExitCode = ExitCode.COMPILATION_ERROR,
) {
assertThat(exitCode).isError()
// Position to the class.
assertThat(messages).contains("Source0.kt:16:7")
assertThat(messages).contains(
Expand Down Expand Up @@ -709,8 +710,8 @@ class MergeModulesTest {
)
interface ComponentInterface
""",
expectExitCode = ExitCode.COMPILATION_ERROR,
) {
assertThat(exitCode).isError()
// Position to the class.
assertThat(messages).contains("Source0.kt:17:11")
assertThat(messages).contains(
Expand Down Expand Up @@ -742,8 +743,8 @@ class MergeModulesTest {
)
interface ComponentInterface
""",
expectExitCode = ExitCode.COMPILATION_ERROR,
) {
assertThat(exitCode).isError()
// Position to the class.
assertThat(messages).contains("Source0.kt:17:11")
assertThat(messages).contains(
Expand Down Expand Up @@ -806,8 +807,8 @@ class MergeModulesTest {
@MergeModules(Any::class)
class DaggerModule1
""",
expectExitCode = ExitCode.COMPILATION_ERROR,
) {
assertThat(exitCode).isError()
// Position to the class.
assertThat(messages).contains("Source0.kt:8:")
}
Expand Down Expand Up @@ -907,8 +908,8 @@ class MergeModulesTest {
)
interface ComponentInterface
""",
expectExitCode = ExitCode.COMPILATION_ERROR,
) {
assertThat(exitCode).isError()
assertThat(messages).contains("Source0.kt:19:11")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.squareup.anvil.annotations.compat.MergeModules
import com.squareup.anvil.compiler.internal.testing.anyDaggerComponent
import com.squareup.anvil.compiler.internal.testing.daggerComponent
import com.squareup.anvil.compiler.internal.testing.withoutAnvilModules
import com.tschuchort.compiletesting.KotlinCompilation.ExitCode
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
Expand Down Expand Up @@ -46,8 +47,8 @@ class ModuleMergerRepeatableTest(
$annotation(Any::class)
interface ComponentInterface
""",
expectExitCode = ExitCode.COMPILATION_ERROR,
) {
assertThat(exitCode).isError()
assertThat(messages).contains(
"com.squareup.test.ComponentInterface merges multiple times to the same scope: [Any]. " +
"Merging multiple times to the same scope is forbidden and all scopes must be distinct.",
Expand All @@ -69,8 +70,8 @@ class ModuleMergerRepeatableTest(
@MergeSubcomponent(Unit::class)
interface ComponentInterface
""",
expectExitCode = ExitCode.COMPILATION_ERROR,
) {
assertThat(exitCode).isError()
assertThat(messages).contains(
"It's only allowed to have one single type of @Merge* annotation, however multiple " +
"instances of the same annotation are allowed. You mix " +
Expand Down
Loading

0 comments on commit 6ba6161

Please sign in to comment.