Repository: square/anvil
Pull request #447 (2021-12-14 17:58:18)

Upgrade the compile testing library 
And remove the workaround that was necessary until now.

This PR requires Kotlin 1.6 first, though. I wanted to put it up nonetheless.


Patch:
diff --git a/dependencies.gradle b/dependencies.gradle
index 6ee8dc84..5292c2bb 100644
--- a/dependencies.gradle
+++ b/dependencies.gradle
@@ -68,7 +68,7 @@ ext {
 
       kotlin: [
           annotation_processing_embeddable: "org.jetbrains.kotlin:kotlin-annotation-processing-embeddable:$kotlinVersion",
-          compile_testing: "com.github.tschuchortdev:kotlin-compile-testing:1.4.5",
+          compile_testing: "com.github.tschuchortdev:kotlin-compile-testing:1.4.7",
           compiler: "org.jetbrains.kotlin:kotlin-compiler-embeddable:$kotlinVersion",
           dokka: "org.jetbrains.dokka:dokka-gradle-plugin:1.6.10",
           gradle_plugin: "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion",


Test patch:
diff --git a/compiler-utils/src/testFixtures/java/com/squareup/anvil/compiler/internal/testing/AnvilCompilation.kt b/compiler-utils/src/testFixtures/java/com/squareup/anvil/compiler/internal/testing/AnvilCompilation.kt
index 4e40eb2e..d6fbe5cf 100644
--- a/compiler-utils/src/testFixtures/java/com/squareup/anvil/compiler/internal/testing/AnvilCompilation.kt
+++ b/compiler-utils/src/testFixtures/java/com/squareup/anvil/compiler/internal/testing/AnvilCompilation.kt
@@ -8,12 +8,12 @@ import com.tschuchort.compiletesting.KotlinCompilation
 import com.tschuchort.compiletesting.KotlinCompilation.Result
 import com.tschuchort.compiletesting.PluginOption
 import com.tschuchort.compiletesting.SourceFile
+import com.tschuchort.compiletesting.addPreviousResultToClasspath
 import dagger.internal.codegen.ComponentProcessor
 import org.intellij.lang.annotations.Language
 import org.jetbrains.kotlin.config.JvmTarget
 import java.io.File
 import java.io.OutputStream
-import java.net.URLClassLoader
 import java.nio.file.Files
 
 /**
@@ -26,7 +26,6 @@ public class AnvilCompilation internal constructor(
 
   private var isCompiled = false
   private var anvilConfigured = false
-  private var swapClassLoader = false
 
   /** Configures this the Anvil behavior of this compilation. */
   @Suppress("SuspiciousCollectionReassignment")
@@ -129,8 +128,7 @@ public class AnvilCompilation internal constructor(
 
   public fun addPreviousCompilationResult(result: Result) = apply {
     checkNotCompiled()
-    kotlinCompilation.classpaths += result.outputDirectory
-    swapClassLoader = true
+    kotlinCompilation.addPreviousResultToClasspath(result)
   }
 
   /**
@@ -176,24 +174,8 @@ public class AnvilCompilation internal constructor(
     }
     addSources(*sources)
     isCompiled = true
-    return kotlinCompilation.compile()
-      .apply(block)
-      .apply { swapClassLoaderIfNecessary() }
-  }
-
-  // This workaround is needed until this issue is resolved:
-  // https://github.com/tschuchortdev/kotlin-compile-testing/issues/216
-  private fun Result.swapClassLoaderIfNecessary() {
-    if (!swapClassLoader) return
-
-    val newClassLoader = URLClassLoader(
-      kotlinCompilation.classpaths.plus(outputDirectory).map { it.toURI().toURL() }.toTypedArray(),
-      this::class.java.classLoader
-    )
 
-    Result::class.java.declaredFields
-      .single { it.name == "classLoader" }
-      .use { it.set(this, newClassLoader) }
+    return kotlinCompilation.compile().apply(block)
   }
 
   companion object {
@@ -214,7 +196,8 @@ public class AnvilCompilation internal constructor(
  * Helpful for testing code generators in unit tests end to end.
  *
  * This covers common cases, but is built upon reusable logic in [AnvilCompilation] and
- * [configureForAnvil]. Consider using those APIs if more advanced configuration is needed.
+ * [AnvilCompilation.configureAnvil]. Consider using those APIs if more advanced configuration
+ * is needed.
  */
 @ExperimentalAnvilApi
 public fun compileAnvil(