Skip to content

Commit

Permalink
WIP: use kspLoggingLevels
Browse files Browse the repository at this point in the history
  • Loading branch information
eygraber committed Jun 17, 2024
1 parent 151415e commit 009a7e7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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)
}
fun output(): String = result.messages
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ import javax.tools.Diagnostic

fun Assert<Throwable>.output(): Assert<String> = message().isNotNull()

fun Assert<TestCompilationResult>.warnings(): Assert<String> =
prop("warnings") { it.output(Diagnostic.Kind.WARNING) }
fun Assert<TestCompilationResult>.output(): Assert<String> =
prop("output") { it.output() }
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.")
Expand Down Expand Up @@ -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.")
}
Expand Down

0 comments on commit 009a7e7

Please sign in to comment.