Skip to content

Commit

Permalink
Use messagesWithSeverity for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eygraber committed Jul 11, 2024
1 parent 863bf51 commit f52527a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 37 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ detekt-formatting = { module = "io.gitlab.arturbosch.detekt:detekt-formatting",
ksp = { module = "com.google.devtools.ksp:symbol-processing-api", version.ref = "ksp" }
kotlin-reflect = { module = "org.jetbrains.kotlin:kotlin-reflect", version.ref = "kotlin" }
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
kotlin-compile-testing = { module = "dev.zacsweers.kctfork:ksp", version = "0.5.0" }
kotlin-compile-testing = { module = "dev.zacsweers.kctfork:ksp", version = "0.5.1" }
kotlin-metadata-jvm = { module = "org.jetbrains.kotlin:kotlin-metadata-jvm", version.ref = "kotlin" }
kotlinx-coroutines = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version = "1.8.0" }
kotlinx-atomicfu = { module = "org.jetbrains.kotlinx:atomicfu", version = "0.23.2" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package me.tatarka.inject

import com.google.devtools.ksp.processing.SymbolProcessorProvider
import com.tschuchort.compiletesting.CompilationResult
import com.tschuchort.compiletesting.DiagnosticSeverity
import com.tschuchort.compiletesting.KotlinCompilation
import com.tschuchort.compiletesting.SourceFile
import com.tschuchort.compiletesting.kspProcessorOptions
Expand All @@ -14,7 +15,6 @@ import me.tatarka.inject.compiler.ksp.InjectProcessorProvider
import org.intellij.lang.annotations.Language
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
import java.io.File
import javax.tools.Diagnostic

class ProjectCompiler(
private val target: Target,
Expand Down Expand Up @@ -63,42 +63,12 @@ class ProjectCompiler(

if (!result.success) {
@Suppress("TooGenericExceptionThrown")
throw Exception(result.output(Diagnostic.Kind.ERROR))
throw Exception(result.output(DiagnosticSeverity.ERROR))
}
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 +78,8 @@ 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(vararg severities: DiagnosticSeverity): String = when {
severities.isEmpty() -> result.messages
else -> result.messagesWithSeverity(*severities)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import assertk.Assert
import assertk.assertions.isNotNull
import assertk.assertions.message
import assertk.assertions.prop
import com.tschuchort.compiletesting.DiagnosticSeverity
import me.tatarka.inject.TestCompilationResult
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) }
prop("warnings") { it.output(DiagnosticSeverity.WARNING) }

0 comments on commit f52527a

Please sign in to comment.