diff --git a/kotlin-inject-compiler/test/src/main/kotlin/me/tatarka/inject/ProjectCompiler.kt b/kotlin-inject-compiler/test/src/main/kotlin/me/tatarka/inject/ProjectCompiler.kt index a136ea11..48a317f2 100644 --- a/kotlin-inject-compiler/test/src/main/kotlin/me/tatarka/inject/ProjectCompiler.kt +++ b/kotlin-inject-compiler/test/src/main/kotlin/me/tatarka/inject/ProjectCompiler.kt @@ -6,15 +6,16 @@ import com.google.devtools.ksp.processing.SymbolProcessorProvider import com.tschuchort.compiletesting.CompilationResult import com.tschuchort.compiletesting.KotlinCompilation import com.tschuchort.compiletesting.SourceFile +import com.tschuchort.compiletesting.kspLoggingLevels import com.tschuchort.compiletesting.kspProcessorOptions import com.tschuchort.compiletesting.kspWithCompilation import com.tschuchort.compiletesting.symbolProcessorProviders import me.tatarka.inject.compiler.Options import me.tatarka.inject.compiler.ksp.InjectProcessorProvider import org.intellij.lang.annotations.Language +import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi import java.io.File -import javax.tools.Diagnostic class ProjectCompiler( private val target: Target, @@ -40,7 +41,9 @@ class ProjectCompiler( return this } - fun compile(): TestCompilationResult { + fun compile( + vararg loggingLevels: CompilerMessageSeverity + ): TestCompilationResult { val result = TestCompilationResult( KotlinCompilation().apply { workingDir = this@ProjectCompiler.workingDir @@ -58,47 +61,23 @@ class ProjectCompiler( // work-around for https://github.com/ZacSweers/kotlin-compile-testing/issues/197 kspWithCompilation = true messageOutputStream = System.out + + if(loggingLevels.isNotEmpty()) { + kspLoggingLevels = loggingLevels.toSet() + } }.compile() ) if (!result.success) { @Suppress("TooGenericExceptionThrown") - throw Exception(result.output(Diagnostic.Kind.ERROR)) + // this will include everything that was specified by kspLoggingLevels + // which might include more than just errors + throw Exception(result.output()) } return result } } -private fun String.filterByKind(kind: Diagnostic.Kind): String = buildString { - var currentKind: Diagnostic.Kind? = null - for (line in this@filterByKind.lineSequence()) { - val lineKind = line.matchLine() - if (lineKind != null) { - currentKind = lineKind - } - if (currentKind == kind) { - append(line) - append('\n') - } - } -} - -private fun String.matchLine(): Diagnostic.Kind? { - if (length < 2) return null - val matchedKind = when (get(0)) { - 'e' -> Diagnostic.Kind.ERROR - 'w' -> Diagnostic.Kind.WARNING - 'v' -> Diagnostic.Kind.NOTE - else -> null - } ?: return null - - return if (get(1) == ':') { - matchedKind - } else { - null - } -} - enum class Target { KSP } @@ -108,5 +87,5 @@ class TestCompilationResult(private val result: CompilationResult) { get() = result.exitCode == KotlinCompilation.ExitCode.OK @OptIn(ExperimentalCompilerApi::class) - fun output(kind: Diagnostic.Kind): String = result.messages.filterByKind(kind) -} \ No newline at end of file + fun output(): String = result.messages +} diff --git a/kotlin-inject-compiler/test/src/test/kotlin/me/tatarka/inject/test/Utils.kt b/kotlin-inject-compiler/test/src/test/kotlin/me/tatarka/inject/test/Utils.kt index 7ed4abe5..ce5745de 100644 --- a/kotlin-inject-compiler/test/src/test/kotlin/me/tatarka/inject/test/Utils.kt +++ b/kotlin-inject-compiler/test/src/test/kotlin/me/tatarka/inject/test/Utils.kt @@ -9,5 +9,5 @@ import javax.tools.Diagnostic fun Assert.output(): Assert = message().isNotNull() -fun Assert.warnings(): Assert = - prop("warnings") { it.output(Diagnostic.Kind.WARNING) } \ No newline at end of file +fun Assert.output(): Assert = + prop("output") { it.output() } diff --git a/kotlin-inject-compiler/test/src/test/kotlin/me/tatarka/inject/test/WarningTest.kt b/kotlin-inject-compiler/test/src/test/kotlin/me/tatarka/inject/test/WarningTest.kt index 92b7969f..2bba5535 100644 --- a/kotlin-inject-compiler/test/src/test/kotlin/me/tatarka/inject/test/WarningTest.kt +++ b/kotlin-inject-compiler/test/src/test/kotlin/me/tatarka/inject/test/WarningTest.kt @@ -5,6 +5,7 @@ import assertk.assertThat import assertk.assertions.contains import me.tatarka.inject.ProjectCompiler import me.tatarka.inject.Target +import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity import org.junit.jupiter.api.io.TempDir import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.EnumSource @@ -51,8 +52,8 @@ class WarningTest { @Provides fun str(): String = "" } """.trimIndent() - ).compile() - ).warnings().all { + ).compile(CompilerMessageSeverity.WARNING) + ).output().all { contains("Scope: @MyScope1 has no effect. Place on @Provides function or @Inject constructor instead.") contains("Scope: @MyScope2 has no effect. Place on @Provides function or @Inject constructor instead.") contains("Scope: @MyScope3 has no effect. Place on @Provides function or @Inject constructor instead.") @@ -88,8 +89,8 @@ class WarningTest { @Provides fun str(): String = "" } """.trimIndent() - ).compile() - ).warnings().all { + ).compile(CompilerMessageSeverity.WARNING) + ).output().all { contains("Scope: @MyScope1 has no effect. Place on @Provides function or @Inject constructor instead.") contains("Scope: @MyScope2 has no effect. Place on @Provides function or @Inject constructor instead.") }